Skip to content

Commit 3844240

Browse files
committed
Issue menacher#46 Added test case to validate multi-disconnect scenario
1 parent 950d888 commit 3844240

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

jetserver/src/test/java/org/menacheri/jetserver/JetlangEventDispatcherTest.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ public void sessionDisconnectValidation() throws InterruptedException {
166166
Event event = Events.event(null, Events.SESSION_MESSAGE);
167167
playerSession.onEvent(event);
168168
assertFalse(latch.await(500, TimeUnit.MILLISECONDS));
169-
169+
170170
// Connect to another game room
171171
sessionBuilder.gameRoomName("Zombie_ROOM_2");
172-
172+
173173
Session gameRoomSession2 = new TestGameRoom(sessionBuilder, counter,
174174
latch);
175175
GameRoom gameRoom2 = (GameRoom) gameRoomSession2;
@@ -180,6 +180,49 @@ public void sessionDisconnectValidation() throws InterruptedException {
180180
assertTrue(latch.await(500, TimeUnit.MILLISECONDS));
181181
}
182182

183+
@Test
184+
public void multiSessionDisconnectValidation() throws InterruptedException {
185+
// create necessary setup objects.
186+
Game game = new SimpleGame(1, "Test");
187+
Protocol dummyProtocol = new DummyProtocol();
188+
GameRoomSessionBuilder sessionBuilder = new GameRoomSessionBuilder();
189+
sessionBuilder.parentGame(game).gameRoomName("Zombie_ROOM_1")
190+
.protocol(dummyProtocol);
191+
CountDownLatch latch1 = new CountDownLatch(2);
192+
CountDownLatch latch2 = new CountDownLatch(2);
193+
AtomicLong counter = new AtomicLong(0l);
194+
Session gameRoomSession = new TestGameRoom(sessionBuilder, counter,
195+
latch1);
196+
GameRoom gameRoom = (GameRoom) gameRoomSession;
197+
PlayerSession playerSession = gameRoom.createPlayerSession(null);
198+
PlayerSession playerSession2 = gameRoom.createPlayerSession(null);
199+
PlayerSession playerSession3 = gameRoom.createPlayerSession(null);
200+
gameRoom.connectSession(playerSession);
201+
gameRoom.connectSession(playerSession2);
202+
gameRoom.connectSession(playerSession3);
203+
playerSession.addHandler(new SessionHandlerLatchCounter(playerSession,
204+
counter, latch1));
205+
playerSession2.addHandler(new SessionHandlerLatchCounter(playerSession,
206+
counter, latch2));
207+
playerSession3.addHandler(new SessionHandlerLatchCounter(playerSession,
208+
counter, latch2));
209+
// start test
210+
Event event1 = Events.event(null, Events.DISCONNECT);
211+
playerSession.onEvent(event1);// disconnect session 1.
212+
assertFalse(latch1.await(1000, TimeUnit.MILLISECONDS));// This is just a wait
213+
Event message = Events.event(null, Events.SESSION_MESSAGE);
214+
playerSession.onEvent(message);
215+
assertFalse(latch1.await(500, TimeUnit.MILLISECONDS));// Ensure that the message is not sent.
216+
Event event2 = Events.event(null, Events.DISCONNECT);
217+
Event event3 = Events.event(null, Events.DISCONNECT);
218+
playerSession2.onEvent(event2);
219+
playerSession3.onEvent(event3);
220+
221+
assertTrue(latch2.await(500, TimeUnit.MILLISECONDS));
222+
// 1 ondisconnect(session1) + 0 onnetwork(session1) + 2 ondisconnect(session2 and 3)
223+
assertTrue(counter.get() == 3);
224+
}
225+
183226
private void assertNoListeners(JetlangEventDispatcher dispatcher) {
184227
Map<Integer, List<EventHandler>> listeners = dispatcher
185228
.getListenersByEventType();

0 commit comments

Comments
 (0)