use of com.tvd12.ezyfoxserver.socket.EzySocketDisconnectionQueue in project ezyfox-server by youngmonkeys.
the class V121SessionManagerTest method inspectTest.
@Test
public void inspectTest() throws Exception {
// given
EzySocketDisconnectionQueue disconnectionQueue = new EzyBlockingSocketDisconnectionQueue();
SessionManager sut = (SessionManager) new SessionManager.Builder().validationInterval(100).validationDelay(100).objectFactory(() -> new Session(disconnectionQueue)).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
EzyChannel channel1 = mock(EzyChannel.class);
when(channel1.getConnection()).thenReturn(new Object());
EzyChannel channel2 = mock(EzyChannel.class);
when(channel2.getConnection()).thenReturn(new Object());
EzyChannel channel3 = mock(EzyChannel.class);
when(channel3.getConnection()).thenReturn(new Object());
Session session1 = sut.provideSession(channel1);
session1.setLoggedIn(true);
session1.setLastReadTime(System.currentTimeMillis());
Session session2 = sut.provideSession(channel2);
session2.setLoggedIn(true);
session2.setLastReadTime(System.currentTimeMillis());
Session session3 = sut.provideSession(channel3);
session3.setLoggedIn(true);
session3.setLastReadTime(System.currentTimeMillis());
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.length; ++i) {
threads[i] = new Thread(() -> {
long start = System.currentTimeMillis();
long elapsedTime = 0;
while (elapsedTime < 100) {
EzyChannel channel = mock(EzyChannel.class);
when(channel.getConnection()).thenReturn(new Object());
sut.provideSession(channel);
elapsedTime = System.currentTimeMillis() - start;
}
});
}
Thread disconnectionThread = new Thread(() -> {
while (true) {
try {
EzySocketDisconnection item = disconnectionQueue.take();
sut.clearSession((Session) item.getSession());
} catch (Exception e) {
e.printStackTrace();
}
}
});
disconnectionThread.start();
// when
sut.start();
for (Thread thread : threads) {
thread.start();
}
Thread.sleep(1000);
disconnectionThread.interrupt();
// then
Asserts.assertEquals(3, sut.getAllSessionCount());
sut.destroy();
}
use of com.tvd12.ezyfoxserver.socket.EzySocketDisconnectionQueue in project ezyfox-server by youngmonkeys.
the class EzySocketDisconnectionHandlerTest method hasNoHandlerGroupCaseTest.
@Test
public void hasNoHandlerGroupCaseTest() {
EzySocketDisconnectionHandler handler = new EzySocketDisconnectionHandler();
EzySocketDisconnectionQueue disconnectionQueue = new EzyBlockingSocketDisconnectionQueue();
EzySocketDataHandlerGroupRemover dataHandlerGroupRemover = mock(EzySocketDataHandlerGroupRemover.class);
when(dataHandlerGroupRemover.removeHandlerGroup(any(EzySession.class))).thenReturn(null);
EzySession session = spy(EzyAbstractSession.class);
EzySocketDisconnection disconnection = new EzySimpleSocketDisconnection(session);
disconnectionQueue.add(disconnection);
handler.setDisconnectionQueue(disconnectionQueue);
handler.setDataHandlerGroupRemover(dataHandlerGroupRemover);
handler.handleEvent();
}
use of com.tvd12.ezyfoxserver.socket.EzySocketDisconnectionQueue in project ezyfox-server by youngmonkeys.
the class EzySimpleNioHandlerGroupTest method builderTest.
@Test
public void builderTest() {
// given
EzySimpleSettings settings = new EzySimpleSettings();
EzySimpleServer server = new EzySimpleServer();
server.setSettings(settings);
@SuppressWarnings("rawtypes") EzySessionManager sessionManager = EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
server.setSessionManager(sessionManager);
EzySimpleServerContext serverContext = new EzySimpleServerContext();
serverContext.setServer(server);
serverContext.init();
EzySessionTicketsQueue sessionTicketsQueue = mock(EzySessionTicketsQueue.class);
EzySocketDisconnectionQueue disconnectionQueue = mock(EzySocketDisconnectionQueue.class);
Object connection = new Object();
EzyChannel channel = mock(EzyChannel.class);
when(channel.getConnection()).thenReturn(connection);
EzyNioSession session = (EzyNioSession) sessionManager.provideSession(channel);
EzySessionStats sessionStats = mock(EzySessionStats.class);
doNothing().when(sessionStats).setCurrentSessions(anyInt());
// when
EzySimpleNioHandlerGroup sut = (EzySimpleNioHandlerGroup) EzySimpleNioHandlerGroup.builder().session(session).serverContext(serverContext).sessionCount(new AtomicInteger()).sessionStats(sessionStats).sessionTicketsQueue(sessionTicketsQueue).disconnectionQueue(disconnectionQueue).build();
// then
Asserts.assertEquals(FieldUtil.getFieldValue(sut, "disconnectionQueue"), disconnectionQueue);
Asserts.assertEquals(FieldUtil.getFieldValue(sut, "sessionTicketsQueue"), sessionTicketsQueue);
}
use of com.tvd12.ezyfoxserver.socket.EzySocketDisconnectionQueue in project ezyfox-server by youngmonkeys.
the class EzyNioServerBootstrapBuilderImpl method newServerBootstrap.
@Override
protected EzyServerBootstrap newServerBootstrap() {
ExecutorService statsThreadPool = newStatsThreadPool();
EzyCodecFactory codecFactory = newCodecFactory();
EzyStreamingApi streamingApi = newStreamingApi();
EzyResponseApi responseApi = newResponseApi(codecFactory);
EzySocketStreamQueue streamQueue = newStreamQueue();
EzySessionTicketsQueue socketSessionTicketsQueue = newSocketSessionTicketsQueue();
EzySessionTicketsQueue websocketSessionTicketsQueue = newWebSocketSessionTicketsQueue();
EzySocketDisconnectionQueue socketDisconnectionQueue = newSocketDisconnectionQueue();
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = newSessionTicketsRequestQueues();
EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = newHandlerGroupBuilderFactory(statsThreadPool, codecFactory, streamQueue, socketDisconnectionQueue, socketSessionTicketsQueue, websocketSessionTicketsQueue, sessionTicketsRequestQueues);
EzyHandlerGroupManager handlerGroupManager = newHandlerGroupManager(handlerGroupBuilderFactory);
EzySocketDataReceiver socketDataReceiver = newSocketDataReceiver(handlerGroupManager);
EzyNioServerBootstrap bootstrap = new EzyNioServerBootstrap();
bootstrap.setResponseApi(responseApi);
bootstrap.setStreamingApi(streamingApi);
bootstrap.setStreamQueue(streamQueue);
bootstrap.setSocketDataReceiver(socketDataReceiver);
bootstrap.setHandlerGroupManager(handlerGroupManager);
bootstrap.setSocketDisconnectionQueue(socketDisconnectionQueue);
bootstrap.setSocketSessionTicketsQueue(socketSessionTicketsQueue);
bootstrap.setWebsocketSessionTicketsQueue(websocketSessionTicketsQueue);
bootstrap.setSocketSessionTicketsRequestQueues(sessionTicketsRequestQueues);
bootstrap.setSslContext(newSslContext(getWebsocketSetting().getSslConfig()));
return bootstrap;
}
Aggregations