Search in sources :

Example 46 with EzyAbstractSession

use of com.tvd12.ezyfoxserver.entity.EzyAbstractSession in project ezyfox-server by youngmonkeys.

the class EzySimpleSocketStreamTest method test.

@Test
public void test() {
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    EzySimpleSocketStream stream = new EzySimpleSocketStream(session, new byte[0]);
    System.out.println(stream.getTimestamp());
}
Also used : EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzySimpleSocketStream(com.tvd12.ezyfoxserver.socket.EzySimpleSocketStream) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 47 with EzyAbstractSession

use of com.tvd12.ezyfoxserver.entity.EzyAbstractSession 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

EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)46 Test (org.testng.annotations.Test)35 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)25 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)23 BaseTest (com.tvd12.test.base.BaseTest)20 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)19 EzySimpleUser (com.tvd12.ezyfoxserver.entity.EzySimpleUser)17 EzyZoneUserManager (com.tvd12.ezyfoxserver.wrapper.EzyZoneUserManager)16 EzyArray (com.tvd12.ezyfox.entity.EzyArray)14 EzyChannel (com.tvd12.ezyfoxserver.socket.EzyChannel)12 EzyController (com.tvd12.ezyfoxserver.controller.EzyController)11 EzyStreamingController (com.tvd12.ezyfoxserver.controller.EzyStreamingController)11 EzyInterceptor (com.tvd12.ezyfoxserver.interceptor.EzyInterceptor)11 EzyServerControllers (com.tvd12.ezyfoxserver.wrapper.EzyServerControllers)11 EzyServer (com.tvd12.ezyfoxserver.EzyServer)10 EzyZone (com.tvd12.ezyfoxserver.EzyZone)10 EzyCloseSession (com.tvd12.ezyfoxserver.command.EzyCloseSession)10 EzyLoggerSetting (com.tvd12.ezyfoxserver.setting.EzyLoggerSetting)10 EzySessionManagementSetting (com.tvd12.ezyfoxserver.setting.EzySessionManagementSetting)10 EzySettings (com.tvd12.ezyfoxserver.setting.EzySettings)10