Search in sources :

Example 1 with EzySessionStats

use of com.tvd12.ezyfoxserver.statistics.EzySessionStats in project ezyfox-server by youngmonkeys.

the class EzyAbstractHandlerGroupTest method test.

@SuppressWarnings("rawtypes")
@Test
public void test() throws Exception {
    EzyStatistics statistics = new EzySimpleStatistics();
    EzySimpleSettings settings = new EzySimpleSettings();
    EzySimpleStreamingSetting streaming = settings.getStreaming();
    streaming.setEnable(true);
    EzySimpleServer server = new EzySimpleServer();
    server.setSettings(settings);
    EzySimpleServerContext serverContext = new EzySimpleServerContext();
    serverContext.setServer(server);
    serverContext.init();
    EzySessionManager sessionManager = EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
    EzyChannel channel = mock(EzyChannel.class);
    when(channel.isConnected()).thenReturn(true);
    when(channel.getConnection()).thenReturn(SocketChannel.open());
    when(channel.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
    EzyNioSession session = (EzyNioSession) sessionManager.provideSession(channel);
    ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
    ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
    EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
    when(channel.write(any(ByteBuffer.class), anyBoolean())).thenReturn(123456);
    ExHandlerGroup group = (ExHandlerGroup) new ExHandlerGroup.Builder().session(session).decoder(decoder).sessionCount(new AtomicInteger()).networkStats((EzyNetworkStats) statistics.getSocketStats().getNetworkStats()).sessionStats((EzySessionStats) statistics.getSocketStats().getSessionStats()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
    EzyChannel channelX = mock(EzyChannel.class);
    MethodInvoker.create().object(group).method("canWriteBytes").param(EzyChannel.class, null).invoke();
    MethodInvoker.create().object(group).method("canWriteBytes").param(EzyChannel.class, channelX).invoke();
    sessionTicketsRequestQueues = mock(EzySessionTicketsRequestQueues.class);
    when(sessionTicketsRequestQueues.addRequest(any())).thenReturn(false);
    group = (ExHandlerGroup) new ExHandlerGroup.Builder().session(session).decoder(decoder).sessionCount(new AtomicInteger()).networkStats((EzyNetworkStats) statistics.getSocketStats().getNetworkStats()).sessionStats((EzySessionStats) statistics.getSocketStats().getSessionStats()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
    MethodInvoker.create().object(group).method("handleReceivedData").param(Object.class, EzyEntityFactory.newArrayBuilder().append(EzyCommand.PING.getId()).append(EzyEntityFactory.EMPTY_OBJECT).build()).param(int.class, 100).invoke();
    ((EzyDatagramChannelPoolAware) session).setDatagramChannelPool(new EzyDatagramChannelPool(1));
    ((EzyUdpClientAddressAware) session).setUdpClientAddress(new InetSocketAddress("127.0.0.1", 12348));
    EzyPacket packet = mock(EzyPacket.class);
    when(packet.getData()).thenReturn(new byte[] { 1, 2, 3 });
    ByteBuffer buffer = ByteBuffer.allocate(100);
    MethodInvoker.create().object(group).method("executeSendingPacket").param(EzyPacket.class, packet).param(Object.class, buffer).invoke();
    MethodInvoker.create().object(group).method("executeSendingPacket").param(EzyPacket.class, packet).param(Object.class, new Object()).invoke();
    Asserts.assertNotNull(group.getSession());
}
Also used : EzySimpleSessionManagementSetting(com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting) EzySimpleServerContext(com.tvd12.ezyfoxserver.context.EzySimpleServerContext) InetSocketAddress(java.net.InetSocketAddress) EzyStatistics(com.tvd12.ezyfoxserver.statistics.EzyStatistics) EzyNetworkStats(com.tvd12.ezyfoxserver.statistics.EzyNetworkStats) EzySimpleMaxRequestPerSecond(com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond) EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzySimpleStreamingSetting(com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) ByteBuffer(java.nio.ByteBuffer) EzySessionStats(com.tvd12.ezyfoxserver.statistics.EzySessionStats) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) EzySimpleStatistics(com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics) EzySimpleSessionTokenGenerator(com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator) ExEzyByteToObjectDecoder(com.tvd12.ezyfoxserver.nio.testing.handler.EzySimpleNioHandlerGroupTest.ExEzyByteToObjectDecoder) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 2 with EzySessionStats

use of com.tvd12.ezyfoxserver.statistics.EzySessionStats in project ezyfox-server by youngmonkeys.

the class EzyAbstractHandlerGroupTest method newHandlerGroup.

@SuppressWarnings("rawtypes")
private ExHandlerGroup newHandlerGroup() throws Exception {
    EzyStatistics statistics = new EzySimpleStatistics();
    EzySimpleSettings settings = new EzySimpleSettings();
    EzySimpleStreamingSetting streaming = settings.getStreaming();
    streaming.setEnable(true);
    EzySimpleServer server = new EzySimpleServer();
    server.setSettings(settings);
    EzySimpleServerContext serverContext = new EzySimpleServerContext();
    serverContext.setServer(server);
    serverContext.init();
    EzyChannel channel = mock(EzyChannel.class);
    when(channel.isConnected()).thenReturn(true);
    when(channel.getConnection()).thenReturn(SocketChannel.open());
    when(channel.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
    EzySimpleSession session = mock(EzySimpleSession.class);
    EzySessionManager sessionManager = mock(EzySessionManager.class);
    server.setSessionManager(sessionManager);
    EzyResponseApi responseApi = mock(EzyResponseApi.class);
    server.setResponseApi(responseApi);
    ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
    ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
    EzySessionTicketsRequestQueues sessionTicketsRequestQueues = mock(EzySessionTicketsRequestQueues.class);
    when(channel.write(any(ByteBuffer.class), anyBoolean())).thenReturn(123456);
    return (ExHandlerGroup) new ExHandlerGroup.Builder().session(session).decoder(decoder).sessionCount(new AtomicInteger()).networkStats((EzyNetworkStats) statistics.getSocketStats().getNetworkStats()).sessionStats((EzySessionStats) statistics.getSocketStats().getSessionStats()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
}
Also used : EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzySimpleServerContext(com.tvd12.ezyfoxserver.context.EzySimpleServerContext) EzyStatistics(com.tvd12.ezyfoxserver.statistics.EzyStatistics) EzySimpleStreamingSetting(com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) EzySimpleSession(com.tvd12.ezyfoxserver.nio.entity.EzySimpleSession) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) ByteBuffer(java.nio.ByteBuffer) EzySessionStats(com.tvd12.ezyfoxserver.statistics.EzySessionStats) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) EzySimpleStatistics(com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics) ExEzyByteToObjectDecoder(com.tvd12.ezyfoxserver.nio.testing.handler.EzySimpleNioHandlerGroupTest.ExEzyByteToObjectDecoder)

Example 3 with EzySessionStats

use of com.tvd12.ezyfoxserver.statistics.EzySessionStats 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)

Aggregations

EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)3 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)3 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)3 EzySessionStats (com.tvd12.ezyfoxserver.statistics.EzySessionStats)3 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 EzyNioSession (com.tvd12.ezyfoxserver.nio.entity.EzyNioSession)2 ExEzyByteToObjectDecoder (com.tvd12.ezyfoxserver.nio.testing.handler.EzySimpleNioHandlerGroupTest.ExEzyByteToObjectDecoder)2 EzySimpleSessionTokenGenerator (com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)2 EzySimpleSessionManagementSetting (com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting)2 EzySimpleStreamingSetting (com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting)2 EzySimpleStatistics (com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics)2 EzyStatistics (com.tvd12.ezyfoxserver.statistics.EzyStatistics)2 BaseTest (com.tvd12.test.base.BaseTest)2 ByteBuffer (java.nio.ByteBuffer)2 ExecutorService (java.util.concurrent.ExecutorService)2 Test (org.testng.annotations.Test)2 EzyResponseApi (com.tvd12.ezyfoxserver.api.EzyResponseApi)1 EzySimpleSession (com.tvd12.ezyfoxserver.nio.entity.EzySimpleSession)1 EzySimpleNioHandlerGroup (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup)1