Search in sources :

Example 11 with EzySocketDisconnectionQueue

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();
}
Also used : EzySocketDisconnection(com.tvd12.ezyfoxserver.socket.EzySocketDisconnection) EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel) EzySimpleSessionManager(com.tvd12.ezyfoxserver.wrapper.EzySimpleSessionManager) EzyBlockingSocketDisconnectionQueue(com.tvd12.ezyfoxserver.socket.EzyBlockingSocketDisconnectionQueue) EzySimpleSessionTokenGenerator(com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator) EzySocketDisconnectionQueue(com.tvd12.ezyfoxserver.socket.EzySocketDisconnectionQueue) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) Test(org.testng.annotations.Test)

Example 12 with EzySocketDisconnectionQueue

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();
}
Also used : EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test)

Example 13 with EzySocketDisconnectionQueue

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);
}
Also used : EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzySimpleSessionManagementSetting(com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting) EzySimpleServerContext(com.tvd12.ezyfoxserver.context.EzySimpleServerContext) EzySimpleNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) EzySessionStats(com.tvd12.ezyfoxserver.statistics.EzySessionStats) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EzySimpleSessionTokenGenerator(com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 14 with EzySocketDisconnectionQueue

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;
}
Also used : EzyHandlerGroupBuilderFactory(com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) ExecutorService(java.util.concurrent.ExecutorService) EzyStreamingApi(com.tvd12.ezyfoxserver.api.EzyStreamingApi) EzyCodecFactory(com.tvd12.ezyfoxserver.codec.EzyCodecFactory) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) EzyNioServerBootstrap(com.tvd12.ezyfoxserver.nio.EzyNioServerBootstrap)

Aggregations

Test (org.testng.annotations.Test)10 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)7 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)7 EzySimpleSessionTokenGenerator (com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)7 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)7 EzyCodecFactory (com.tvd12.ezyfoxserver.codec.EzyCodecFactory)6 EzyHandlerGroupBuilderFactory (com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory)6 EzySimpleStreamingSetting (com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting)6 ExecutorService (java.util.concurrent.ExecutorService)6 EzyNioSessionManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyNioSessionManager)5 EzySimpleStatistics (com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics)5 EzyStatistics (com.tvd12.ezyfoxserver.statistics.EzyStatistics)5 BaseTest (com.tvd12.test.base.BaseTest)5 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)4 EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)4 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)3 EzyResponseApi (com.tvd12.ezyfoxserver.api.EzyResponseApi)2 EzyStreamingApi (com.tvd12.ezyfoxserver.api.EzyStreamingApi)2 EzyImmediateDeliver (com.tvd12.ezyfoxserver.entity.EzyImmediateDeliver)2 EzyNioServerBootstrap (com.tvd12.ezyfoxserver.nio.EzyNioServerBootstrap)2