Search in sources :

Example 16 with EzyHandlerGroupManager

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

the class EzySimpleNioUdpDataHandlerTest method handleReceivedUdpPacketWithSessionTest.

@Test
public void handleReceivedUdpPacketWithSessionTest() {
    // given
    EzySimpleNioUdpDataHandler sut = new EzySimpleNioUdpDataHandler(1);
    byte[] messageBytes = messageBytes();
    InetSocketAddress udpAddress = new InetSocketAddress(3005);
    InetSocketAddress tcpAddress = new InetSocketAddress(123);
    EzyUdpReceivedPacket packet = mock(EzyUdpReceivedPacket.class);
    when(packet.getBytes()).thenReturn(messageBytes);
    when(packet.getAddress()).thenReturn(udpAddress);
    Session session = mock(Session.class);
    when(session.getRemoteAddress()).thenReturn(tcpAddress);
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    when(handlerGroupManager.getSocketChannel(udpAddress)).thenReturn(session);
    sut.setHandlerGroupManager(handlerGroupManager);
    EzySocketDataReceiver socketDataReceiver = mock(EzySocketDataReceiver.class);
    sut.setSocketDataReceiver(socketDataReceiver);
    // when
    MethodInvoker.create().object(sut).method("handleReceivedUdpPacket").param(EzyUdpReceivedPacket.class, packet).call();
    // then
    verify(handlerGroupManager, times(1)).getSocketChannel(udpAddress);
    verify(session, times(1)).getRemoteAddress();
    verify(socketDataReceiver, times(1)).udpReceive(any(SocketChannel.class), any(EzyMessage.class));
    sut.destroy();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) InetSocketAddress(java.net.InetSocketAddress) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) EzyMessage(com.tvd12.ezyfox.codec.EzyMessage) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Session(org.eclipse.jetty.websocket.api.Session) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzySimpleSession(com.tvd12.ezyfoxserver.nio.entity.EzySimpleSession) Test(org.testng.annotations.Test)

Example 17 with EzyHandlerGroupManager

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

the class EzyNioSocketAcceptorTest method test.

@Test
public void test() throws Exception {
    EzyHandlerGroupManager handlerGroupManager = newHandlerGroupManager();
    Selector ownSelector = spy(ExSelector.class);
    SelectionKey selectionKey1 = spy(ExSelectionKey.class);
    SelectionKey selectionKey2 = spy(ExSelectionKey.class);
    when(ownSelector.selectedKeys()).thenReturn(Sets.newHashSet(selectionKey1, selectionKey2));
    when(selectionKey1.isValid()).thenReturn(true);
    when(selectionKey1.readyOps()).thenReturn(SelectionKey.OP_ACCEPT);
    ExServerSocketChannel serverChannel1 = spy(ExServerSocketChannel.class);
    when(selectionKey1.channel()).thenReturn(serverChannel1);
    SocketChannel channel1 = spy(ExSocketChannel.class);
    when(serverChannel1.accept()).thenReturn(channel1);
    Socket socket1 = new Socket();
    when(channel1.socket()).thenReturn(socket1);
    Selector readSelector = spy(ExSelector.class);
    channel1.configureBlocking(false);
    when(channel1.register(readSelector, SelectionKey.OP_READ)).thenReturn(selectionKey1);
    EzyNioSocketAcceptor acceptor = new EzyNioSocketAcceptor();
    acceptor.setAcceptableConnections(new ArrayList<>());
    acceptor.setHandlerGroupManager(handlerGroupManager);
    acceptor.setOwnSelector(ownSelector);
    acceptor.setReadSelector(readSelector);
    acceptor.handleEvent();
    List<SocketChannel> acceptableConnections = FieldUtil.getFieldValue(acceptor, "acceptableConnections");
    assert acceptableConnections.size() == 1;
    acceptor.handleAcceptableConnections();
    assert acceptableConnections.size() == 0;
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzyNioSocketAcceptor(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor) Socket(java.net.Socket) Selector(java.nio.channels.Selector) AbstractSelector(java.nio.channels.spi.AbstractSelector) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 18 with EzyHandlerGroupManager

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

the class EzyNioSocketAcceptorTest method acceptConnectionExceptionCase.

@Test
public void acceptConnectionExceptionCase() throws Exception {
    EzyHandlerGroupManager handlerGroupManager = newHandlerGroupManager();
    Selector ownSelector = spy(ExSelector.class);
    SelectionKey selectionKey1 = spy(ExSelectionKey.class);
    SelectionKey selectionKey2 = spy(ExSelectionKey.class);
    when(ownSelector.selectedKeys()).thenReturn(Sets.newHashSet(selectionKey1, selectionKey2));
    when(selectionKey1.isValid()).thenReturn(true);
    when(selectionKey1.readyOps()).thenReturn(SelectionKey.OP_ACCEPT);
    ExServerSocketChannel serverChannel1 = spy(ExServerSocketChannel.class);
    when(selectionKey1.channel()).thenReturn(serverChannel1);
    SocketChannel channel1 = spy(ExSocketChannel.class);
    when(serverChannel1.accept()).thenReturn(channel1);
    when(channel1.socket()).thenThrow(new IllegalStateException("server maintain"));
    Selector readSelector = spy(ExSelector.class);
    EzyNioSocketAcceptor acceptor = new EzyNioSocketAcceptor();
    acceptor.setAcceptableConnections(new ArrayList<>());
    acceptor.setHandlerGroupManager(handlerGroupManager);
    acceptor.setOwnSelector(ownSelector);
    acceptor.setReadSelector(readSelector);
    acceptor.handleEvent();
    List<SocketChannel> acceptableConnections = FieldUtil.getFieldValue(acceptor, "acceptableConnections");
    assert acceptableConnections.size() == 1;
    acceptor.handleAcceptableConnections();
    assert acceptableConnections.size() == 0;
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzyNioSocketAcceptor(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor) Selector(java.nio.channels.Selector) AbstractSelector(java.nio.channels.spi.AbstractSelector) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 19 with EzyHandlerGroupManager

use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager 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 20 with EzyHandlerGroupManager

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

the class EzyNioSocketReaderTest method newHandlerGroupManager.

private EzyHandlerGroupManager newHandlerGroupManager() {
    EzyNioSessionManager sessionManager = (EzyNioSessionManager) EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
    ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
    EzyCodecFactory codecFactory = mock(EzyCodecFactory.class);
    when(codecFactory.newDecoder(any())).thenReturn(decoder);
    ExecutorService statsThreadPool = EzyExecutors.newSingleThreadExecutor("stats");
    EzySocketStreamQueue streamQueue = new EzyBlockingSocketStreamQueue();
    EzySocketDisconnectionQueue disconnectionQueue = new EzyBlockingSocketDisconnectionQueue();
    EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
    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();
    EzySessionTicketsQueue socketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzySessionTicketsQueue webSocketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzyStatistics statistics = new EzySimpleStatistics();
    EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = EzyHandlerGroupBuilderFactoryImpl.builder().statistics(statistics).statsThreadPool(statsThreadPool).streamQueue(streamQueue).disconnectionQueue(disconnectionQueue).codecFactory(codecFactory).serverContext(serverContext).socketSessionTicketsQueue(socketSessionTicketsQueue).webSocketSessionTicketsQueue(webSocketSessionTicketsQueue).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
    return EzyHandlerGroupManagerImpl.builder().handlerGroupBuilderFactory(handlerGroupBuilderFactory).build();
}
Also used : EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzyHandlerGroupBuilderFactory(com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory) EzySimpleServerContext(com.tvd12.ezyfoxserver.context.EzySimpleServerContext) EzySimpleStreamingSetting(com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting) EzyStatistics(com.tvd12.ezyfoxserver.statistics.EzyStatistics) EzyNioSessionManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyNioSessionManager) EzyCodecFactory(com.tvd12.ezyfoxserver.codec.EzyCodecFactory) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) ExecutorService(java.util.concurrent.ExecutorService) EzySimpleStatistics(com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics) EzySimpleSessionTokenGenerator(com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)

Aggregations

EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)29 Test (org.testng.annotations.Test)28 BaseTest (com.tvd12.test.base.BaseTest)14 EzySocketDataReceiver (com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver)13 SocketChannel (java.nio.channels.SocketChannel)13 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)9 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)9 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)9 EzyCodecFactory (com.tvd12.ezyfoxserver.codec.EzyCodecFactory)8 EzyHandlerGroupBuilderFactory (com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory)8 EzySimpleStreamingSetting (com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting)8 ExecutorService (java.util.concurrent.ExecutorService)8 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)7 EzyNioSessionManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyNioSessionManager)7 EzySimpleSessionTokenGenerator (com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)7 EzySimpleStatistics (com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics)7 EzyStatistics (com.tvd12.ezyfoxserver.statistics.EzyStatistics)7 InetSocketAddress (java.net.InetSocketAddress)7 ByteBuffer (java.nio.ByteBuffer)7 EzySimpleNioUdpDataHandler (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler)6