-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
TcpIpConnection_AbstractBasicTest.java
259 lines (200 loc) · 8.53 KB
/
TcpIpConnection_AbstractBasicTest.java
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
*
* 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.hazelcast.nio.tcp;
import com.hazelcast.internal.networking.Channel;
import com.hazelcast.nio.ConnectionLifecycleListener;
import com.hazelcast.nio.Packet;
import com.hazelcast.test.AssertTask;
import com.hazelcast.util.function.Consumer;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static java.lang.System.currentTimeMillis;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
public abstract class TcpIpConnection_AbstractBasicTest extends TcpIpConnection_AbstractTest {
// sleep time for lastWrite and lastRead tests
private static final int LAST_READ_WRITE_SLEEP_SECONDS = 5;
// if we make this MARGIN_OF_ERROR_MS very small, there is a high chance of spurious failures
private static final int MARGIN_OF_ERROR_MS = 3000;
private List<Packet> packetsB;
@Mock
private ConnectionLifecycleListener<TcpIpConnection> mockedListener;
@Before
public void setup() throws Exception {
super.setup();
packetsB = Collections.synchronizedList(new ArrayList<Packet>());
startAllNetworkingServices();
ioServiceB.packetConsumer = new Consumer<Packet>() {
@Override
public void accept(Packet packet) {
packetsB.add(packet);
}
};
}
@Test
public void write_whenNonUrgent() {
TcpIpConnection c = connect(networkingServiceA, addressB);
Packet packet = new Packet(serializationService.toBytes("foo"));
boolean result = c.write(packet);
assertTrue(result);
assertTrueEventually(new AssertTask() {
@Override
public void run() {
assertEquals(1, packetsB.size());
}
});
Packet found = packetsB.get(0);
assertEquals(packet, found);
}
@Test
public void write_whenUrgent() {
TcpIpConnection c = connect(networkingServiceA, addressB);
Packet packet = new Packet(serializationService.toBytes("foo"));
packet.raiseFlags(Packet.FLAG_URGENT);
boolean result = c.write(packet);
assertTrue(result);
assertTrueEventually(new AssertTask() {
@Override
public void run() {
assertEquals(1, packetsB.size());
}
});
Packet found = packetsB.get(0);
assertEquals(packet, found);
}
@Test
public void lastWriteTimeMillis_whenPacketWritten() {
TcpIpConnection connAB = connect(networkingServiceA, addressB);
// we need to sleep some so that the lastWriteTime of the connection gets nice and old.
// we need this so we can determine the lastWriteTime got updated
sleepSeconds(LAST_READ_WRITE_SLEEP_SECONDS);
Packet packet = new Packet(serializationService.toBytes("foo"));
connAB.write(packet);
// wait for the packet to get written
assertTrueEventually(new AssertTask() {
@Override
public void run() {
assertEquals(1, packetsB.size());
}
});
long lastWriteTimeMs = connAB.lastWriteTimeMillis();
long nowMs = currentTimeMillis();
// make sure that the lastWrite time is within the given MARGIN_OF_ERROR_MS
// last write time should be equal or smaller than now
assertTrue("nowMs = " + nowMs + ", lastWriteTimeMs = " + lastWriteTimeMs, lastWriteTimeMs <= nowMs);
// last write time should be larger or equal than the now - MARGIN_OF_ERROR_MS
assertTrue("nowMs = " + nowMs + ", lastWriteTimeMs = " + lastWriteTimeMs,
lastWriteTimeMs >= nowMs - MARGIN_OF_ERROR_MS);
}
@Test
public void lastWriteTime_whenNothingWritten() {
TcpIpConnection c = connect(networkingServiceA, addressB);
long result1 = c.lastWriteTimeMillis();
long result2 = c.lastWriteTimeMillis();
assertEquals(result1, result2);
}
// we check the lastReadTimeMillis by sending a packet on the local connection, and
// on the remote side we check the if the lastReadTime is updated
@Test
public void lastReadTimeMillis() {
TcpIpConnection connAB = connect(networkingServiceA, addressB);
TcpIpConnection connBA = connect(networkingServiceB, addressA);
// we need to sleep some so that the lastReadTime of the connection gets nice and old.
// we need this so we can determine the lastReadTime got updated
sleepSeconds(LAST_READ_WRITE_SLEEP_SECONDS);
Packet packet = new Packet(serializationService.toBytes("foo"));
connAB.write(packet);
// wait for the packet to get read
assertTrueEventually(new AssertTask() {
@Override
public void run() {
assertEquals(1, packetsB.size());
System.out.println("Packet processed");
}
});
long lastReadTimeMs = connBA.lastReadTimeMillis();
long nowMs = currentTimeMillis();
// make sure that the lastRead time is within the given MARGIN_OF_ERROR_MS
// last read time should be equal or smaller than now
assertTrue("nowMs = " + nowMs + ", lastReadTimeMs = " + lastReadTimeMs, lastReadTimeMs <= nowMs);
// last read time should be larger or equal than the now - MARGIN_OF_ERROR_MS
assertTrue("nowMs = " + nowMs + ", lastReadTimeMs = " + lastReadTimeMs, lastReadTimeMs >= nowMs - MARGIN_OF_ERROR_MS);
}
@Test
public void lastReadTime_whenNothingWritten() {
TcpIpConnection c = connect(networkingServiceA, addressB);
long result1 = c.lastReadTimeMillis();
long result2 = c.lastReadTimeMillis();
assertEquals(result1, result2);
}
@Test
public void write_whenNotAlive() {
TcpIpConnection c = connect(networkingServiceA, addressB);
c.close(null, null);
Packet packet = new Packet(serializationService.toBytes("foo"));
boolean result = c.write(packet);
assertFalse(result);
}
@Test
public void getInetAddress() {
TcpIpConnection c = connect(networkingServiceA, addressB);
InetAddress result = c.getInetAddress();
assertEquals(c.getChannel().socket().getInetAddress(), result);
}
@Test
public void getRemoteSocketAddress() {
TcpIpConnection c = connect(networkingServiceA, addressB);
InetSocketAddress result = c.getRemoteSocketAddress();
assertEquals(new InetSocketAddress(addressB.getHost(), addressB.getPort()), result);
}
@Test
public void getPort() {
TcpIpConnection c = connect(networkingServiceA, addressB);
int result = c.getPort();
assertEquals(c.getChannel().socket().getPort(), result);
}
@Test
public void test_equals() {
TcpIpConnection connAB = connect(networkingServiceA, addressB);
TcpIpConnection connAC = connect(networkingServiceA, addressC);
assertEquals(connAB, connAB);
assertEquals(connAC, connAC);
assertNotEquals(connAB, null);
assertNotEquals(connAB, connAC);
assertNotEquals(connAC, connAB);
assertNotEquals(connAB, "foo");
// don't mock if you don't need to
TcpIpEndpointManager em = connAB.getEndpointManager();
Channel channel = connAB.getChannel();
TcpIpConnection conn1 = new TcpIpConnection(em, mockedListener, 0, channel);
TcpIpConnection conn2 = new TcpIpConnection(em, mockedListener, 0, channel);
assertEquals(conn1, conn2);
conn1.setEndPoint(addressA);
assertNotEquals(conn1, conn2);
conn2.setEndPoint(addressB);
assertNotEquals(conn1, conn2);
conn2.setEndPoint(addressA);
assertEquals(conn1, conn2);
}
}