Search in sources :

Example 16 with EzyChannel

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

the class EzySimpleDataHandlerTest method notifyAppsSessionRemoved0Case.

@SuppressWarnings({ "rawtypes" })
@Test
public void notifyAppsSessionRemoved0Case() {
    int zoneId = 1;
    EzyServerContext serverContext = mock(EzyServerContext.class);
    EzyZoneContext zoneContext = mock(EzyZoneContext.class);
    EzyZone zone = mock(EzyZone.class);
    when(zoneContext.getZone()).thenReturn(zone);
    EzyZoneUserManager zoneUserManager = mock(EzyZoneUserManager.class);
    when(zone.getUserManager()).thenReturn(zoneUserManager);
    when(serverContext.getZoneContext(zoneId)).thenReturn(zoneContext);
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    EzyChannel channel = mock(EzyChannel.class);
    when(session.getChannel()).thenReturn(channel);
    EzyServer server = mock(EzyServer.class);
    when(serverContext.getServer()).thenReturn(server);
    EzyServerControllers controllers = mock(EzyServerControllers.class);
    EzyInterceptor streamingInterceptor = mock(EzyInterceptor.class);
    when(controllers.getStreamingInterceptor()).thenReturn(streamingInterceptor);
    EzyStreamingController streamingController = mock(EzyStreamingController.class);
    when(controllers.getStreamingController()).thenReturn(streamingController);
    EzyInterceptor loginInterceptor = mock(EzyInterceptor.class);
    when(controllers.getInterceptor(EzyCommand.LOGIN)).thenReturn(loginInterceptor);
    EzyController loginController = mock(EzyController.class);
    when(controllers.getController(EzyCommand.LOGIN)).thenReturn(loginController);
    when(server.getControllers()).thenReturn(controllers);
    EzySessionManager sessionManager = mock(EzySessionManager.class);
    when(server.getSessionManager()).thenReturn(sessionManager);
    EzyCloseSession closeSession = mock(EzyCloseSession.class);
    when(serverContext.get(EzyCloseSession.class)).thenReturn(closeSession);
    EzySettings settings = mock(EzySettings.class);
    when(settings.isDebug()).thenReturn(true);
    when(server.getSettings()).thenReturn(settings);
    EzySessionManagementSetting sessionManagementSetting = mock(EzySessionManagementSetting.class);
    when(settings.getSessionManagement()).thenReturn(sessionManagementSetting);
    EzyLoggerSetting loggerSetting = mock(EzyLoggerSetting.class);
    when(settings.getLogger()).thenReturn(loggerSetting);
    EzyLoggerSetting.EzyIgnoredCommandsSetting ignoredCommandsSetting = mock(EzyLoggerSetting.EzyIgnoredCommandsSetting.class);
    when(loggerSetting.getIgnoredCommands()).thenReturn(ignoredCommandsSetting);
    when(ignoredCommandsSetting.getCommands()).thenReturn(new HashSet<>());
    EzySessionManagementSetting.EzyMaxRequestPerSecond maxRequestPerSecond = mock(EzySessionManagementSetting.EzyMaxRequestPerSecond.class);
    when(maxRequestPerSecond.getValue()).thenReturn(3);
    when(maxRequestPerSecond.getAction()).thenReturn(EzyMaxRequestPerSecondAction.DISCONNECT_SESSION);
    when(sessionManagementSetting.getSessionMaxRequestPerSecond()).thenReturn(maxRequestPerSecond);
    MyTestDataHandler handler = new MyTestDataHandler(serverContext, session);
    when(session.getDelegate()).thenReturn(handler);
    when(session.isActivated()).thenReturn(true);
    FieldUtil.setFieldValue(handler, "zoneContext", zoneContext);
    doThrow(new IllegalArgumentException("notifyAppsSessionRemoved0Case")).when(zoneContext).broadcastApps(any(EzyConstant.class), any(EzyEvent.class), any(EzyUser.class), anyBoolean());
    MethodInvoker.create().object(handler).method("doNotifyAppsSessionRemoved").param(EzyEvent.class, mock(EzyEvent.class)).invoke();
}
Also used : EzyZone(com.tvd12.ezyfoxserver.EzyZone) EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel) EzyInterceptor(com.tvd12.ezyfoxserver.interceptor.EzyInterceptor) EzyStreamingController(com.tvd12.ezyfoxserver.controller.EzyStreamingController) EzyCloseSession(com.tvd12.ezyfoxserver.command.EzyCloseSession) EzyEvent(com.tvd12.ezyfoxserver.event.EzyEvent) EzySessionManagementSetting(com.tvd12.ezyfoxserver.setting.EzySessionManagementSetting) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzyServer(com.tvd12.ezyfoxserver.EzyServer) EzySettings(com.tvd12.ezyfoxserver.setting.EzySettings) EzyUser(com.tvd12.ezyfoxserver.entity.EzyUser) EzyController(com.tvd12.ezyfoxserver.controller.EzyController) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzyServerControllers(com.tvd12.ezyfoxserver.wrapper.EzyServerControllers) EzyLoggerSetting(com.tvd12.ezyfoxserver.setting.EzyLoggerSetting) EzyZoneUserManager(com.tvd12.ezyfoxserver.wrapper.EzyZoneUserManager) EzyConstant(com.tvd12.ezyfox.constant.EzyConstant) BaseCoreTest(com.tvd12.ezyfoxserver.testing.BaseCoreTest) Test(org.testng.annotations.Test)

Example 17 with EzyChannel

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

the class EzyNioSocketReaderTest method testExceptionCase.

@Test
public void testExceptionCase() throws Exception {
    EzyHandlerGroupManager handlerGroupManager = newHandlerGroupManager();
    EzySocketDataReceiver socketDataReceiver = EzySocketDataReceiver.builder().threadPoolSize(1).handlerGroupManager(handlerGroupManager).build();
    Selector ownSelector = spy(ExSelector.class);
    when(ownSelector.selectNow()).thenReturn(1);
    SelectionKey selectionKey1 = spy(ExSelectionKey.class);
    when(ownSelector.selectedKeys()).thenReturn(Sets.newHashSet(selectionKey1));
    when(selectionKey1.isValid()).thenReturn(true);
    when(selectionKey1.readyOps()).thenReturn(SelectionKey.OP_READ);
    SocketChannel socketChannel1 = mock(SocketChannel.class);
    EzyChannel channel1 = new EzyNioSocketChannel(socketChannel1);
    handlerGroupManager.newHandlerGroup(channel1, EzyConnectionType.SOCKET);
    when(selectionKey1.channel()).thenReturn(socketChannel1);
    when(socketChannel1.isConnected()).thenReturn(true);
    when(socketChannel1.read(any(ByteBuffer.class))).then((Answer<Integer>) invocation -> {
        throw new IllegalStateException("server maintain");
    });
    EzyNioSocketAcceptor socketAcceptor = new EzyNioSocketAcceptor();
    socketAcceptor.setReadSelector(ownSelector);
    socketAcceptor.setHandlerGroupManager(handlerGroupManager);
    socketAcceptor.setAcceptableConnections(new ArrayList<>());
    EzyNioSocketReader socketReader = new EzyNioSocketReader();
    socketReader.setOwnSelector(ownSelector);
    socketReader.setAcceptableConnectionsHandler(socketAcceptor);
    socketReader.setSocketDataReceiver(socketDataReceiver);
    socketReader.handleEvent();
}
Also used : EzySimpleStreamingSetting(com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting) EzyEntityFactory(com.tvd12.ezyfox.factory.EzyEntityFactory) EzyNioSocketAcceptor(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor) Selector(java.nio.channels.Selector) EzyNioSocketChannel(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketChannel) EzySimpleServerContext(com.tvd12.ezyfoxserver.context.EzySimpleServerContext) Test(org.testng.annotations.Test) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) EzyConnectionType(com.tvd12.ezyfoxserver.constant.EzyConnectionType) Answer(org.mockito.stubbing.Answer) SelectorProvider(java.nio.channels.spi.SelectorProvider) EzyExecutors(com.tvd12.ezyfox.concurrent.EzyExecutors) EzyNioSessionManagerImpl(com.tvd12.ezyfoxserver.nio.wrapper.impl.EzyNioSessionManagerImpl) SocketChannel(java.nio.channels.SocketChannel) EzySimpleSessionManagementSetting(com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting) EzyHandlerGroupManagerImpl(com.tvd12.ezyfoxserver.nio.wrapper.impl.EzyHandlerGroupManagerImpl) EzySimpleSessionTokenGenerator(com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator) EzyStatistics(com.tvd12.ezyfoxserver.statistics.EzyStatistics) ExecutorService(java.util.concurrent.ExecutorService) EzyCodecFactory(com.tvd12.ezyfoxserver.codec.EzyCodecFactory) EzyNioSessionManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyNioSessionManager) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) EzyCommand(com.tvd12.ezyfoxserver.constant.EzyCommand) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) com.tvd12.ezyfox.codec(com.tvd12.ezyfox.codec) Sets(com.tvd12.ezyfox.collect.Sets) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) SelectionKey(java.nio.channels.SelectionKey) EzyHandlerGroupBuilderFactoryImpl(com.tvd12.ezyfoxserver.nio.builder.impl.EzyHandlerGroupBuilderFactoryImpl) com.tvd12.ezyfoxserver.socket(com.tvd12.ezyfoxserver.socket) EzyHandlerGroupBuilderFactory(com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) BaseTest(com.tvd12.test.base.BaseTest) EzyNioSocketReader(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketReader) Queue(java.util.Queue) EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzySimpleStatistics(com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics) SelectionKey(java.nio.channels.SelectionKey) EzyNioSocketChannel(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketChannel) SocketChannel(java.nio.channels.SocketChannel) EzyNioSocketChannel(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketChannel) EzyNioSocketAcceptor(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor) ByteBuffer(java.nio.ByteBuffer) EzyNioSocketReader(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketReader) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) Selector(java.nio.channels.Selector) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 18 with EzyChannel

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

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

the class EzyHandlerGroupManagerImplTest method mapSocketChannelConnectionNull.

@Test
public void mapSocketChannelConnectionNull() {
    // given
    EzyHandlerGroupManager sut = newHandlerGroupManager();
    EzySession session = mock(EzySession.class);
    EzyChannel channel = mock(EzyChannel.class);
    when(session.getChannel()).thenReturn(channel);
    // when
    sut.mapSocketChannel(null, session);
    // then
    verify(session, times(1)).getChannel();
}
Also used : EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 20 with EzyChannel

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

the class EzyHandlerGroupManagerImplTest method removeHandlerGroupUdpClientAddressIsNull.

@Test
public void removeHandlerGroupUdpClientAddressIsNull() {
    // given
    EzyHandlerGroupManager sut = newHandlerGroupManager();
    EzySession session = mock(EzySession.class);
    EzyChannel channel = mock(EzyChannel.class);
    when(session.getChannel()).thenReturn(channel);
    Object connection = new Object();
    when(channel.getConnection()).thenReturn(connection);
    SocketAddress udpClientAddress = mock(SocketAddress.class);
    when(session.getUdpClientAddress()).thenReturn(udpClientAddress);
    // when
    sut.removeHandlerGroup(session);
    // then
    verify(session, times(1)).getChannel();
    verify(session, times(1)).getUdpClientAddress();
    verify(channel, times(1)).getConnection();
}
Also used : EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

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