Search in sources :

Example 21 with EzyChannel

use of com.tvd12.ezyfoxserver.socket.EzyChannel in project ezyfox-server by youngmonkeys.

the class EzyHandlerGroupManagerImplTest method test.

@Test
public void test() {
    EzyNioSessionManager sessionManager = (EzyNioSessionManager) EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
    ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
    EzyCodecFactory codecFactory = mock(EzyCodecFactory.class);
    when(codecFactory.newDecoder(any())).thenReturn(decoder);
    ExecutorService statsThreadPool = EzyExecutors.newSingleThreadExecutor("stats");
    EzySocketStreamQueue streamQueue = new EzyBlockingSocketStreamQueue();
    EzySocketDisconnectionQueue disconnectionQueue = new EzyBlockingSocketDisconnectionQueue();
    EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
    EzySimpleSettings settings = new EzySimpleSettings();
    EzySimpleStreamingSetting streaming = settings.getStreaming();
    streaming.setEnable(true);
    EzySimpleServer server = new EzySimpleServer();
    server.setSettings(settings);
    server.setSessionManager(sessionManager);
    EzySimpleServerContext serverContext = new EzySimpleServerContext();
    serverContext.setServer(server);
    serverContext.init();
    EzySessionTicketsQueue socketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzySessionTicketsQueue webSocketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzyStatistics statistics = new EzySimpleStatistics();
    EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = EzyHandlerGroupBuilderFactoryImpl.builder().statistics(statistics).statsThreadPool(statsThreadPool).streamQueue(streamQueue).disconnectionQueue(disconnectionQueue).codecFactory(codecFactory).serverContext(serverContext).socketSessionTicketsQueue(socketSessionTicketsQueue).webSocketSessionTicketsQueue(webSocketSessionTicketsQueue).socketSessionTicketsQueue(webSocketSessionTicketsQueue).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
    EzyHandlerGroupManager handlerGroupManager = EzyHandlerGroupManagerImpl.builder().handlerGroupBuilderFactory(handlerGroupBuilderFactory).build();
    handlerGroupManager.removeHandlerGroup(null);
    EzySession session1 = mock(EzyAbstractSession.class);
    handlerGroupManager.removeHandlerGroup(session1);
    EzySession session2 = mock(EzyAbstractSession.class);
    EzyChannel channel2 = mock(EzyChannel.class);
    when(session2.getChannel()).thenReturn(channel2);
    handlerGroupManager.removeHandlerGroup(session2);
    EzyChannel channel3 = mock(EzyChannel.class);
    Session connection3 = mock(Session.class);
    when(channel3.getConnection()).thenReturn(connection3);
    EzyHandlerGroup handlerGroup3 = handlerGroupManager.newHandlerGroup(channel3, EzyConnectionType.WEBSOCKET);
    EzySession session3 = mock(EzyAbstractSession.class);
    when(session3.getChannel()).thenReturn(channel3);
    assert handlerGroupManager.getDataHandlerGroup(null) == null;
    assert handlerGroupManager.getDataHandlerGroup(session1) == null;
    assert handlerGroupManager.getDataHandlerGroup(session2) == null;
    assert handlerGroupManager.getWriterGroup(session3) == handlerGroup3;
    InetSocketAddress udpAddress = new InetSocketAddress("127.0.0.1", 12345);
    handlerGroupManager.mapSocketChannel(udpAddress, session3);
    assert handlerGroupManager.getSocketChannel(udpAddress) != null;
    handlerGroupManager.unmapHandlerGroup(udpAddress);
    handlerGroupManager.removeHandlerGroup(session3);
    handlerGroupManager.destroy();
}
Also used : EzyHandlerGroupBuilderFactory(com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory) EzySimpleServerContext(com.tvd12.ezyfoxserver.context.EzySimpleServerContext) InetSocketAddress(java.net.InetSocketAddress) EzyStatistics(com.tvd12.ezyfoxserver.statistics.EzyStatistics) EzyHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzyHandlerGroup) EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzySimpleStreamingSetting(com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting) EzyNioSessionManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyNioSessionManager) EzyCodecFactory(com.tvd12.ezyfoxserver.codec.EzyCodecFactory) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) ExecutorService(java.util.concurrent.ExecutorService) EzySimpleStatistics(com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics) EzySimpleSessionTokenGenerator(com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Session(org.eclipse.jetty.websocket.api.Session) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 22 with EzyChannel

use of com.tvd12.ezyfoxserver.socket.EzyChannel in project ezyfox-server by youngmonkeys.

the class EzyCloseSessionImplTest method test.

@Test
public void test() {
    EzyServerContext serverContext = mock(EzyServerContext.class);
    EzyCloseSessionImpl cmd = new EzyCloseSessionImpl(serverContext);
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    EzyChannel channel = mock(EzyChannel.class);
    session.setChannel(channel);
    cmd.close(session, EzyDisconnectReason.ADMIN_BAN);
}
Also used : EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyCloseSessionImpl(com.tvd12.ezyfoxserver.command.impl.EzyCloseSessionImpl) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 23 with EzyChannel

use of com.tvd12.ezyfoxserver.socket.EzyChannel in project ezyfox-server by youngmonkeys.

the class EzyCloseSessionImplTest method noSendToClient.

@Test
public void noSendToClient() {
    EzyServerContext serverContext = mock(EzyServerContext.class);
    EzyCloseSessionImpl cmd = new EzyCloseSessionImpl(serverContext);
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    EzyChannel channel = mock(EzyChannel.class);
    session.setChannel(channel);
    cmd.close(session, EzyDisconnectReason.UNKNOWN);
}
Also used : EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyCloseSessionImpl(com.tvd12.ezyfoxserver.command.impl.EzyCloseSessionImpl) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 24 with EzyChannel

use of com.tvd12.ezyfoxserver.socket.EzyChannel in project ezyfox-server by youngmonkeys.

the class EzySimpleSessionManager method unmapSession.

protected void unmapSession(S session) {
    providedObjects.remove(session);
    sessionsById.remove(session.getId());
    loggedInSession.remove(session.getId());
    EzyChannel channel = session.getChannel();
    Object connection = channel.getConnection();
    sessionsByConnection.remove(connection);
}
Also used : EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel)

Example 25 with EzyChannel

use of com.tvd12.ezyfoxserver.socket.EzyChannel 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)

Aggregations

Test (org.testng.annotations.Test)26 EzyChannel (com.tvd12.ezyfoxserver.socket.EzyChannel)21 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)19 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)16 BaseTest (com.tvd12.test.base.BaseTest)14 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)12 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)12 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)12 EzySimpleSessionTokenGenerator (com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)12 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)12 EzyServerControllers (com.tvd12.ezyfoxserver.wrapper.EzyServerControllers)12 EzyController (com.tvd12.ezyfoxserver.controller.EzyController)11 EzyStreamingController (com.tvd12.ezyfoxserver.controller.EzyStreamingController)11 EzyInterceptor (com.tvd12.ezyfoxserver.interceptor.EzyInterceptor)11 EzySimpleStreamingSetting (com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting)11 EzySimpleStatistics (com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics)11 EzyStatistics (com.tvd12.ezyfoxserver.statistics.EzyStatistics)11 ExecutorService (java.util.concurrent.ExecutorService)11 EzyServer (com.tvd12.ezyfoxserver.EzyServer)10 EzyZone (com.tvd12.ezyfoxserver.EzyZone)10