Repository navigation
Expand file tree
/
Copy pathConnectionTest.java
More file actions
200 lines (165 loc) · 7.27 KB
/
Copy pathConnectionTest.java
File metadata and controls
200 lines (165 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.database.connection;
import com.google.common.collect.ImmutableMap;
import com.google.firebase.database.connection.Connection.Delegate;
import com.google.firebase.database.connection.Connection.DisconnectReason;
import com.google.firebase.database.connection.Connection.WebsocketConnectionFactory;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import org.junit.AfterClass;
import org.junit.Test;
import org.mockito.Mockito;
public class ConnectionTest {
private static final ScheduledExecutorService executor =
Executors.newSingleThreadScheduledExecutor();
@AfterClass
public static void tearDownClass() {
executor.shutdownNow();
}
@Test
public void testOpen() {
MockConnectionFactory connFactory = new MockConnectionFactory();
connFactory.connection.open();
Mockito.verify(connFactory.wsConn, Mockito.times(1)).open();
}
@Test
public void testClose() {
MockConnectionFactory connFactory = new MockConnectionFactory();
connFactory.connection.open();
connFactory.connection.close();
Mockito.verify(connFactory.wsConn, Mockito.times(1)).close();
Mockito.verify(connFactory.delegate, Mockito.times(1)).onDisconnect(DisconnectReason.OTHER);
}
@Test
public void testCloseWithReason() {
MockConnectionFactory connFactory = new MockConnectionFactory();
connFactory.connection.open();
connFactory.connection.close(DisconnectReason.SERVER_RESET);
Mockito.verify(connFactory.wsConn, Mockito.times(1)).close();
Mockito.verify(connFactory.delegate, Mockito.times(1)).onDisconnect(
DisconnectReason.SERVER_RESET);
}
@Test
public void testSendRequest() {
MockConnectionFactory connFactory = new MockConnectionFactory();
connFactory.connection.open();
// should not be sent, since not connected
ImmutableMap<String, Object> data = ImmutableMap.<String, Object>of("key", "value");
connFactory.connection.sendRequest(data, true);
Mockito.verify(connFactory.wsConn, Mockito.never()).send(Mockito.<String, Object>anyMap());
Map<String, Object> handshake = ImmutableMap.<String, Object>of(
"t", "c",
"d", ImmutableMap.of("t", "h", "d", ImmutableMap.of(
"ts", System.currentTimeMillis())));
connFactory.connection.onMessage(handshake);
Map<String, Object> expected = ImmutableMap.<String, Object>of("t", "d", "d", data);
connFactory.connection.sendRequest(data, true);
connFactory.connection.sendRequest(data, false);
Mockito.verify(connFactory.wsConn, Mockito.times(2)).send(expected);
}
@Test
public void testOnDataMessage() {
MockConnectionFactory connFactory = new MockConnectionFactory();
connFactory.connection.open();
ImmutableMap<String, Object> data = ImmutableMap.<String, Object>of("key", "value");
Map<String, Object> incoming = ImmutableMap.<String, Object>of(
"t", "d",
"d", data);
connFactory.connection.onMessage(incoming);
Mockito.verify(connFactory.delegate, Mockito.times(1)).onDataMessage(data);
}
@Test
public void testOnDataMessageParseError() {
MockConnectionFactory connFactory = new MockConnectionFactory();
connFactory.connection.open();
ImmutableMap<String, Object> data = ImmutableMap.<String, Object>of("key", "value");
Mockito.doThrow(ClassCastException.class).when(connFactory.delegate).onDataMessage(data);
Map<String, Object> incoming = ImmutableMap.<String, Object>of(
"t", "d",
"d", data);
connFactory.connection.onMessage(incoming);
Mockito.verify(connFactory.delegate, Mockito.times(1)).onDisconnect(DisconnectReason.OTHER);
Mockito.verify(connFactory.wsConn, Mockito.times(1)).close();
}
@Test
public void testOnInvalidMessage() {
MockConnectionFactory connFactory = new MockConnectionFactory();
connFactory.connection.open();
ImmutableMap<String, Object> data = ImmutableMap.<String, Object>of("key", "value");
Map<String, Object> incoming = ImmutableMap.<String, Object>of("d", data);
connFactory.connection.onMessage(incoming);
Mockito.verify(connFactory.delegate, Mockito.times(1)).onDisconnect(DisconnectReason.OTHER);
Mockito.verify(connFactory.wsConn, Mockito.times(1)).close();
}
@Test
public void testOnShutdown() {
MockConnectionFactory connFactory = new MockConnectionFactory();
connFactory.connection.open();
Map<String, Object> command = ImmutableMap.<String, Object>of(
"t", "c",
"d", ImmutableMap.of("t", "s", "d", "shutdown_reason"));
connFactory.connection.onMessage(command);
Mockito.verify(connFactory.delegate, Mockito.times(1)).onKill("shutdown_reason");
Mockito.verify(connFactory.delegate, Mockito.times(1)).onDisconnect(DisconnectReason.OTHER);
Mockito.verify(connFactory.wsConn, Mockito.times(1)).close();
}
@Test
public void testOnReset() {
MockConnectionFactory connFactory = new MockConnectionFactory();
connFactory.connection.open();
Map<String, Object> command = ImmutableMap.<String, Object>of(
"t", "c",
"d", ImmutableMap.of("t", "r", "d", "reset_host"));
connFactory.connection.onMessage(command);
Mockito.verify(connFactory.delegate, Mockito.times(1)).onCacheHost("reset_host");
Mockito.verify(connFactory.delegate, Mockito.times(1)).onDisconnect(
DisconnectReason.SERVER_RESET);
Mockito.verify(connFactory.wsConn, Mockito.times(1)).close();
}
@Test
public void testOnDisconnect() {
MockConnectionFactory connFactory = new MockConnectionFactory();
connFactory.connection.open();
connFactory.connection.onDisconnect(true);
Mockito.verify(connFactory.delegate, Mockito.times(1)).onDisconnect(DisconnectReason.OTHER);
Mockito.verify(connFactory.wsConn, Mockito.never()).close();
}
@Test
public void testOnDisconnectNeverConnected() {
MockConnectionFactory connFactory = new MockConnectionFactory();
connFactory.connection.open();
connFactory.connection.onDisconnect(false);
Mockito.verify(connFactory.delegate, Mockito.times(1)).onDisconnect(DisconnectReason.OTHER);
Mockito.verify(connFactory.wsConn, Mockito.never()).close();
}
private static class MockConnectionFactory implements WebsocketConnectionFactory {
private final Delegate delegate = Mockito.mock(Delegate.class);
private final WebsocketConnection wsConn = Mockito.mock(WebsocketConnection.class);
private final Connection connection;
MockConnectionFactory() {
this.connection = new Connection(
Mockito.mock(HostInfo.class),
delegate,
this);
}
@Override
public WebsocketConnection newConnection(WebsocketConnection.Delegate delegate) {
return wsConn;
}
}
}