Search in sources :

Example 31 with EzySessionManager

use of com.tvd12.ezyfoxserver.wrapper.EzySessionManager 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 32 with EzySessionManager

use of com.tvd12.ezyfoxserver.wrapper.EzySessionManager 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 33 with EzySessionManager

use of com.tvd12.ezyfoxserver.wrapper.EzySessionManager in project ezyfox-server by youngmonkeys.

the class EzyAbstractHandlerGroupTest method fireChannelInactiveException.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void fireChannelInactiveException() throws Exception {
    // given
    ExHandlerGroup sut = newHandlerGroup();
    EzySession session = FieldUtil.getFieldValue(sut, "session");
    EzyNioDataHandler handler = FieldUtil.getFieldValue(sut, "handler");
    EzySessionManager sessionManager = FieldUtil.getFieldValue(handler, "sessionManager");
    doThrow(new RuntimeException()).when(sessionManager).clearSession(session);
    // when
    sut.fireChannelInactive(EzyDisconnectReason.ADMIN_BAN);
    // then
    verify(sessionManager, times(1)).clearSession(session);
}
Also used : EzyNioDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzyNioDataHandler) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 34 with EzySessionManager

use of com.tvd12.ezyfoxserver.wrapper.EzySessionManager in project ezyfox-server by youngmonkeys.

the class EzyAbstractHandlerGroupTest method hasMaxRequestPerSecondTest.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void hasMaxRequestPerSecondTest() throws Exception {
    // given
    ExHandlerGroup sut = newHandlerGroup();
    EzySession session = FieldUtil.getFieldValue(sut, "session");
    when(session.addReceivedRequests(1)).thenReturn(true);
    EzyNioDataHandler handler = FieldUtil.getFieldValue(sut, "handler");
    EzySessionManager sessionManager = FieldUtil.getFieldValue(handler, "sessionManager");
    EzySimpleMaxRequestPerSecond maxRequestPerSecond = FieldUtil.getFieldValue(handler, "maxRequestPerSecond");
    maxRequestPerSecond.setAction(EzyMaxRequestPerSecondAction.DISCONNECT_SESSION);
    EzyArray data = EzyEntityFactory.newArray();
    // when
    MethodInvoker.create().object(sut).method("handleReceivedData").param(Object.class, data).param(int.class, 100).call();
    // then
    verify(sessionManager, times(1)).removeSession(session, MAX_REQUEST_PER_SECOND);
}
Also used : EzySimpleMaxRequestPerSecond(com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond) EzyNioDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzyNioDataHandler) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 35 with EzySessionManager

use of com.tvd12.ezyfoxserver.wrapper.EzySessionManager in project ezyfox-server by youngmonkeys.

the class EzySimpleNioHandlerGroupTest method test.

@SuppressWarnings("rawtypes")
@Test
public void test() throws Exception {
    EzySessionTicketsQueue socketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzySessionTicketsQueue webSocketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzySessionManager sessionManager = EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
    EzyStatistics statistics = new EzySimpleStatistics();
    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();
    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);
    ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
    EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
    EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = EzyHandlerGroupBuilderFactoryImpl.builder().statistics(statistics).serverContext(serverContext).statsThreadPool(statsThreadPool).codecFactory(new ExCodecFactory()).sessionTicketsRequestQueues(sessionTicketsRequestQueues).socketSessionTicketsQueue(socketSessionTicketsQueue).webSocketSessionTicketsQueue(webSocketSessionTicketsQueue).build();
    EzySimpleNioHandlerGroup group = (EzySimpleNioHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.SOCKET).build();
    group.fireBytesReceived("hello".getBytes());
    EzySimplePacket packet = new EzySimplePacket();
    packet.setBinary(true);
    packet.setData("world".getBytes());
    packet.setTransportType(EzyTransportType.TCP);
    ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
    group.firePacketSend(packet, writeBuffer);
    group.sendPacketNow(packet);
    group.fireChannelRead(EzyCommand.PING, EzyEntityArrays.newArray(EzyCommand.PING.getId(), EzyEntityFactory.EMPTY_ARRAY));
    group.fireStreamBytesReceived(new byte[] { 0, 1, 2 });
    EzyPacket droppedPacket = mock(EzyPacket.class);
    when(droppedPacket.getSize()).thenReturn(12);
    group.addDroppedPacket(droppedPacket);
    EzyPacket failedPacket = mock(EzyPacket.class);
    when(failedPacket.getData()).thenReturn(new byte[] { 1, 2, 3 });
    when(failedPacket.isBinary()).thenReturn(true);
    when(channel.write(any(ByteBuffer.class), anyBoolean())).thenThrow(new IllegalStateException("maintain"));
    group.firePacketSend(failedPacket, writeBuffer);
    group.fireChannelInactive();
    Thread.sleep(2000);
    group.destroy();
    group.destroy();
    EzySocketStreamQueue streamQueue = new EzyBlockingSocketStreamQueue();
    group = (EzySimpleNioHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.SOCKET).session(session).streamQueue(streamQueue).decoder(new ExStreamEzyByteToObjectDecoder()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
    ((EzyAbstractSession) session).setStreamingEnable(false);
    group.fireBytesReceived("hello".getBytes());
    Thread.sleep(500);
    ((EzyAbstractSession) session).setStreamingEnable(true);
    group.fireBytesReceived("hello".getBytes());
    Thread.sleep(1000);
    streamQueue = mock(EzySocketStreamQueue.class);
    when(streamQueue.add(any())).thenThrow(new IllegalStateException("maintain"));
    group = (EzySimpleNioHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.SOCKET).session(session).streamQueue(streamQueue).decoder(new ExStreamEzyByteToObjectDecoder()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
    group.fireBytesReceived("hello".getBytes());
    Thread.sleep(1000);
    group = (EzySimpleNioHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.SOCKET).session(session).decoder(new ErrorEzyByteToObjectDecoder()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
    group.fireBytesReceived("hello".getBytes());
    group.fireMessageReceived(mock(EzyMessage.class));
    Thread.sleep(300);
}
Also used : EzySimpleSessionManagementSetting(com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting) EzyHandlerGroupBuilderFactory(com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory) EzySimpleServerContext(com.tvd12.ezyfoxserver.context.EzySimpleServerContext) EzySimpleNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup) EzyStatistics(com.tvd12.ezyfoxserver.statistics.EzyStatistics) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) 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) ExecutorService(java.util.concurrent.ExecutorService) EzySimpleStatistics(com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics) EzySimpleSessionTokenGenerator(com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Aggregations

EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)39 Test (org.testng.annotations.Test)33 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)22 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)20 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)19 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)19 EzyZoneUserManager (com.tvd12.ezyfoxserver.wrapper.EzyZoneUserManager)19 EzyStatistics (com.tvd12.ezyfoxserver.statistics.EzyStatistics)17 EzyServer (com.tvd12.ezyfoxserver.EzyServer)12 EzyServerControllers (com.tvd12.ezyfoxserver.wrapper.EzyServerControllers)12 EzyZone (com.tvd12.ezyfoxserver.EzyZone)11 EzyController (com.tvd12.ezyfoxserver.controller.EzyController)11 EzyStreamingController (com.tvd12.ezyfoxserver.controller.EzyStreamingController)11 EzyInterceptor (com.tvd12.ezyfoxserver.interceptor.EzyInterceptor)11 EzyLoggerSetting (com.tvd12.ezyfoxserver.setting.EzyLoggerSetting)11 EzySessionManagementSetting (com.tvd12.ezyfoxserver.setting.EzySessionManagementSetting)11 EzySettings (com.tvd12.ezyfoxserver.setting.EzySettings)11 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)11 BaseTest (com.tvd12.test.base.BaseTest)11 EzyCloseSession (com.tvd12.ezyfoxserver.command.EzyCloseSession)10