EventsResource { @OnMessage public void onMessage(ChatAlyticsEvent event) { LOG.debug("Got realtime event: {}", event); event.setClazz(null); Set<Session> closedSessions = Sets.newHashSet(); for (Session clientSession : sessions) { if (!clientSession.isOpen()) { closedSessions.add(clientSession); continue; } clientSession.getAsyncRemote().sendObject(event); } sessions.removeAll(closedSessions); } EventsResource(); @OnOpen void onOpen(Session session); @OnClose void close(Session session, CloseReason reason); @OnMessage void onMessage(ChatAlyticsEvent event); @OnError void onError(Throwable t); static final String RT_EVENT_ENDPOINT; }
@Test public void testOnMessage() { Session computeSession = mock(Session.class); URI computeURI = URI.create("http: when(computeSession.getRequestURI()).thenReturn(computeURI); underTest.onOpen(computeSession); assertEquals(0, underTest.getSessions().size()); verify(computeSession).getRequestURI(); verify(computeSession).setMaxIdleTimeout(0); verifyNoMoreInteractions(computeSession); assertTrue(underTest.isConnectedToCompute()); Async asyncRemote = mock(Async.class); Session firstClientSession = mock(Session.class); URI resourceURI = URI.create("http: when(firstClientSession.getRequestURI()).thenReturn(resourceURI); when(firstClientSession.isOpen()).thenReturn(true); when(firstClientSession.getAsyncRemote()).thenReturn(asyncRemote); underTest.onOpen(firstClientSession); verify(firstClientSession).getRequestURI(); verify(firstClientSession).getId(); verify(firstClientSession).setMaxIdleTimeout(0); verifyNoMoreInteractions(firstClientSession); assertEquals(1, underTest.getSessions().size()); Session secondClientSession = mock(Session.class); when(secondClientSession.getRequestURI()).thenReturn(resourceURI); when(secondClientSession.isOpen()).thenReturn(true); when(secondClientSession.getAsyncRemote()).thenReturn(asyncRemote); underTest.onOpen(secondClientSession); verify(secondClientSession).getRequestURI(); verify(secondClientSession).getId(); verify(secondClientSession).setMaxIdleTimeout(0); verifyNoMoreInteractions(secondClientSession); assertEquals(2, underTest.getSessions().size()); when(firstClientSession.isOpen()).thenReturn(false); ChatAlyticsEvent event = mock(ChatAlyticsEvent.class); underTest.onMessage(event); verify(event).setClazz(null); verify(firstClientSession, never()).getAsyncRemote(); verify(secondClientSession).getAsyncRemote(); verify(asyncRemote).sendObject(event); assertEquals(1, underTest.getSessions().size()); }