Search in sources :

Example 6 with EzySimplePacket

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

the class EzySimpleWsHandlerGroupTest 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);
    EzyServerControllers controllers = mock(EzyServerControllers.class);
    server.setControllers(controllers);
    EzyInterceptor interceptor = mock(EzyInterceptor.class);
    when(controllers.getInterceptor(EzyCommand.PING)).thenReturn(interceptor);
    EzyController controller = mock(EzyController.class);
    when(controllers.getController(EzyCommand.PING)).thenReturn(controller);
    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.WEBSOCKET);
    EzyNioSession session = (EzyNioSession) sessionManager.provideSession(channel);
    ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
    ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
    EzySocketStreamQueue streamQueue = new EzyBlockingSocketStreamQueue();
    EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
    EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = EzyHandlerGroupBuilderFactoryImpl.builder().statistics(statistics).serverContext(serverContext).statsThreadPool(statsThreadPool).streamQueue(streamQueue).codecFactory(new ExCodecFactory()).sessionTicketsRequestQueues(sessionTicketsRequestQueues).socketSessionTicketsQueue(socketSessionTicketsQueue).webSocketSessionTicketsQueue(webSocketSessionTicketsQueue).build();
    EzySimpleWsHandlerGroup group = (EzySimpleWsHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.WEBSOCKET).build();
    group.fireBytesReceived("hello");
    group.fireBytesReceived(new byte[] { 127, 2, 3, 4, 5, 6 }, 0, 5);
    ((EzyAbstractSession) session).setStreamingEnable(true);
    group.fireBytesReceived(new byte[] { 127, 2, 3, 4, 5, 6 }, 0, 5);
    EzySimplePacket packet = new EzySimplePacket();
    packet.setBinary(false);
    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));
    EzyInterceptor streamInterceptor = mock(EzyInterceptor.class);
    when(controllers.getStreamingInterceptor()).thenReturn(streamInterceptor);
    EzyStreamingController streamController = mock(EzyStreamingController.class);
    when(controllers.getStreamingController()).thenReturn(streamController);
    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(false);
    when(channel.write(any(ByteBuffer.class), anyBoolean())).thenThrow(new IllegalStateException("maintain"));
    group.firePacketSend(failedPacket, writeBuffer);
    MethodInvoker.create().object(group).method("executeHandleReceivedBytes").param("hello").invoke();
    MethodInvoker.create().object(group).method("executeHandleReceivedBytes").param("hello".getBytes()).invoke();
    group.fireChannelInactive();
    Thread.sleep(2000);
    group.destroy();
    group.destroy();
    EzySocketStreamQueue streamQueue1 = mock(EzySocketStreamQueue.class);
    when(streamQueue1.add(any())).thenThrow(new IllegalStateException("queue full"));
    group = (EzySimpleWsHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.WEBSOCKET).session(session).decoder(decoder).serverContext(serverContext).statsThreadPool(statsThreadPool).streamQueue(streamQueue1).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
    group.fireBytesReceived(new byte[] { 127, 2, 3, 4, 5, 6 }, 0, 5);
}
Also used : EzySimpleSessionManagementSetting(com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting) EzyHandlerGroupBuilderFactory(com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory) EzySimpleServerContext(com.tvd12.ezyfoxserver.context.EzySimpleServerContext) EzyStatistics(com.tvd12.ezyfoxserver.statistics.EzyStatistics) EzyInterceptor(com.tvd12.ezyfoxserver.interceptor.EzyInterceptor) EzyStreamingController(com.tvd12.ezyfoxserver.controller.EzyStreamingController) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzySimpleWsHandlerGroup(com.tvd12.ezyfoxserver.nio.websocket.EzySimpleWsHandlerGroup) EzyController(com.tvd12.ezyfoxserver.controller.EzyController) EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzySimpleStreamingSetting(com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzyServerControllers(com.tvd12.ezyfoxserver.wrapper.EzyServerControllers) 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)

Example 7 with EzySimplePacket

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

the class EzySocketWriterTest method processSessionTicketsQueue0ExceptionCase.

@Test
public void processSessionTicketsQueue0ExceptionCase() {
    EzySessionTicketsQueue sessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzySocketWriterGroupFetcher writerGroupFetcher = mock(EzySocketWriterGroupFetcher.class);
    when(writerGroupFetcher.getWriterGroup(any(EzySession.class))).thenThrow(new IllegalArgumentException());
    EzySocketWriter socketWriter = new EzySocketWriter();
    socketWriter.setSessionTicketsQueue(sessionTicketsQueue);
    socketWriter.setWriterGroupFetcher(writerGroupFetcher);
    EzyPacketQueue packetQueue = new EzyNonBlockingPacketQueue();
    EzyPacket packet = new EzySimplePacket();
    packetQueue.add(packet);
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    session.setActivated(true);
    session.setSessionTicketsQueue(sessionTicketsQueue);
    session.setPacketQueue(packetQueue);
    sessionTicketsQueue.add(session);
    socketWriter.handleEvent();
}
Also used : EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 8 with EzySimplePacket

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

the class EzySimplePacketTest method test.

@Test
public void test() {
    EzySimplePacket packet = new EzySimplePacket();
    packet.setData("hello");
    packet.setFragment("hello");
    packet.setBinary(false);
    packet.setTransportType(EzyTransportType.TCP);
    assert packet.getData().equals("hello");
    assert packet.isFragmented();
    assert !packet.isBinary();
    assert packet.getTransportType() == EzyTransportType.TCP;
    assert packet.getSize() == "hello".length();
    System.out.println(packet);
}
Also used : EzySimplePacket(com.tvd12.ezyfoxserver.socket.EzySimplePacket) Test(org.testng.annotations.Test)

Example 9 with EzySimplePacket

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

Example 10 with EzySimplePacket

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

the class EzySimpleNioHandlerGroupTest method writeSuccessTest.

@SuppressWarnings("rawtypes")
@Test
public void writeSuccessTest() 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);
    ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
    ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
    EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
    when(channel.write(any(ByteBuffer.class), anyBoolean())).thenReturn(123456);
    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).session(session).decoder(decoder).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
    EzySimplePacket packet = new EzySimplePacket();
    packet.setBinary(true);
    packet.setData("world".getBytes());
    packet.setTransportType(EzyTransportType.TCP);
    ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
    group.firePacketSend(packet, writeBuffer);
    Thread.sleep(1000);
}
Also used : EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) 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) 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

Test (org.testng.annotations.Test)8 BaseTest (com.tvd12.test.base.BaseTest)7 EzySimplePacket (com.tvd12.ezyfoxserver.socket.EzySimplePacket)5 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)4 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)4 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)4 EzyNioSession (com.tvd12.ezyfoxserver.nio.entity.EzyNioSession)4 EzyHandlerGroupBuilderFactory (com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory)4 EzySimpleSessionTokenGenerator (com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)4 EzySimpleSessionManagementSetting (com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting)4 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)4 EzySimpleStreamingSetting (com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting)4 EzySimpleStatistics (com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics)4 EzyStatistics (com.tvd12.ezyfoxserver.statistics.EzyStatistics)4 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)4 ByteBuffer (java.nio.ByteBuffer)4 ExecutorService (java.util.concurrent.ExecutorService)4 EzySimpleNioHandlerGroup (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup)3 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)2 EzyController (com.tvd12.ezyfoxserver.controller.EzyController)1