Search in sources :

Example 1 with EzySocketDataReceiver

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

the class EzyNioSocketReaderTest method test.

@Test
public void test() 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);
    SelectionKey selectionKey2 = spy(ExSelectionKey.class);
    SelectionKey selectionKey3 = spy(ExSelectionKey.class);
    SelectionKey selectionKey4 = spy(ExSelectionKey.class);
    SelectionKey selectionKey5 = spy(ExSelectionKey.class);
    when(ownSelector.selectedKeys()).thenReturn(Sets.newHashSet(selectionKey1, selectionKey2, selectionKey3, selectionKey4, selectionKey5));
    when(selectionKey1.isValid()).thenReturn(true);
    when(selectionKey1.readyOps()).thenReturn(SelectionKey.OP_READ);
    when(selectionKey2.isValid()).thenReturn(true);
    when(selectionKey2.readyOps()).thenReturn(SelectionKey.OP_WRITE);
    when(selectionKey3.isValid()).thenReturn(false);
    when(selectionKey4.isValid()).thenReturn(true);
    when(selectionKey4.readyOps()).thenReturn(SelectionKey.OP_READ);
    when(selectionKey5.isValid()).thenReturn(true);
    when(selectionKey5.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 -> {
        ByteBuffer buffer = invocation.getArgumentAt(0, ByteBuffer.class);
        buffer.put("hello".getBytes());
        return "hello".length();
    });
    SocketChannel socketChannel4 = mock(SocketChannel.class);
    when(selectionKey4.channel()).thenReturn(socketChannel4);
    SocketChannel socketChannel5 = spy(ExSocketChannel.class);
    EzyChannel channel5 = new EzyNioSocketChannel(socketChannel5);
    when(selectionKey5.channel()).thenReturn(socketChannel5);
    doNothing().when(socketChannel5).close();
    handlerGroupManager.newHandlerGroup(channel5, EzyConnectionType.SOCKET);
    when(selectionKey5.channel()).thenReturn(socketChannel5);
    when(socketChannel5.isConnected()).thenReturn(true);
    when(socketChannel5.read(any(ByteBuffer.class))).then((Answer<Integer>) invocation -> -1);
    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 2 with EzySocketDataReceiver

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

the class EzySocketDataReceiverTest method udpReceiveButExceptionTest.

@Test
public void udpReceiveButExceptionTest() throws Exception {
    // given
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    EzySocketDataReceiver sut = EzySocketDataReceiver.builder().handlerGroupManager(handlerGroupManager).threadPoolSize(1).build();
    SocketChannel channel = mock(SocketChannel.class);
    EzyMessage message = mock(EzyMessage.class);
    EzyNioHandlerGroup handlerGroup = mock(EzyNioHandlerGroup.class);
    doThrow(new RuntimeException()).when(handlerGroup).fireMessageReceived(message);
    when(handlerGroupManager.getHandlerGroup(channel)).thenReturn(handlerGroup);
    // when
    sut.udpReceive(channel, message);
    Thread.sleep(120);
    // then
    verify(handlerGroupManager, times(1)).getHandlerGroup(channel);
    verify(handlerGroup, times(1)).fireMessageReceived(message);
    sut.destroy();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) EzyMessage(com.tvd12.ezyfox.codec.EzyMessage) EzyNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup) Test(org.testng.annotations.Test)

Example 3 with EzySocketDataReceiver

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

the class EzySocketDataReceiverTest method doWsReceiveButException.

@Test
public void doWsReceiveButException() throws Exception {
    // given
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    EzySocketDataReceiver sut = EzySocketDataReceiver.builder().handlerGroupManager(handlerGroupManager).build();
    Session session = mock(Session.class);
    String message = RandomUtil.randomShortAlphabetString();
    EzyWsHandlerGroup handlerGroup = mock(EzyWsHandlerGroup.class);
    doThrow(new RuntimeException()).when(handlerGroup).fireBytesReceived(message);
    when(handlerGroupManager.getHandlerGroup(session)).thenReturn(handlerGroup);
    // when
    MethodInvoker.create().object(sut).method("doWsReceive").param(Session.class, session).param(String.class, message).call();
    // then
    verify(handlerGroupManager, times(1)).getHandlerGroup(session);
    verify(handlerGroup, times(1)).fireBytesReceived(message);
}
Also used : EzyWsHandlerGroup(com.tvd12.ezyfoxserver.nio.websocket.EzyWsHandlerGroup) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) Session(org.eclipse.jetty.websocket.api.Session) Test(org.testng.annotations.Test)

Example 4 with EzySocketDataReceiver

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

the class EzySocketDataReceiverTest method doWsReceive2ButException.

@Test
public void doWsReceive2ButException() throws Exception {
    // given
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    EzySocketDataReceiver sut = EzySocketDataReceiver.builder().handlerGroupManager(handlerGroupManager).build();
    Session session = mock(Session.class);
    byte[] payload = new byte[] { 1, 2, 3 };
    EzyWsHandlerGroup handlerGroup = mock(EzyWsHandlerGroup.class);
    doThrow(new RuntimeException()).when(handlerGroup).fireBytesReceived(payload, 0, payload.length);
    when(handlerGroupManager.getHandlerGroup(session)).thenReturn(handlerGroup);
    // when
    MethodInvoker.create().object(sut).method("doWsReceive").param(Session.class, session).param(byte[].class, payload).param(int.class, 0).param(int.class, payload.length).call();
    // then
    verify(handlerGroupManager, times(1)).getHandlerGroup(session);
    verify(handlerGroup, times(1)).fireBytesReceived(payload, 0, payload.length);
}
Also used : EzyWsHandlerGroup(com.tvd12.ezyfoxserver.nio.websocket.EzyWsHandlerGroup) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) Session(org.eclipse.jetty.websocket.api.Session) Test(org.testng.annotations.Test)

Example 5 with EzySocketDataReceiver

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

the class EzySocketDataReceiverTest method tcpReadBytesClosedChannelException.

@Test
public void tcpReadBytesClosedChannelException() throws Exception {
    // given
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    EzySocketDataReceiver sut = EzySocketDataReceiver.builder().handlerGroupManager(handlerGroupManager).build();
    ByteBuffer buffer = ByteBuffer.wrap(new byte[] { 1, 2, 3 });
    SocketChannel channel = mock(SocketChannel.class);
    when(channel.read(buffer)).thenThrow(new ClosedChannelException());
    // when
    MethodInvoker.create().object(sut).method("tcpReadBytes").param(SocketChannel.class, channel).param(ByteBuffer.class, buffer).call();
    // then
    verify(handlerGroupManager, times(1)).getHandlerGroup(channel);
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) ByteBuffer(java.nio.ByteBuffer) Test(org.testng.annotations.Test)

Aggregations

EzySocketDataReceiver (com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver)14 EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)13 Test (org.testng.annotations.Test)13 SocketChannel (java.nio.channels.SocketChannel)9 EzyMessage (com.tvd12.ezyfox.codec.EzyMessage)5 EzyCodecFactory (com.tvd12.ezyfoxserver.codec.EzyCodecFactory)4 EzyHandlerGroupBuilderFactory (com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory)4 BaseTest (com.tvd12.test.base.BaseTest)4 ByteBuffer (java.nio.ByteBuffer)4 Session (org.eclipse.jetty.websocket.api.Session)4 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)3 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)3 EzyNioSessionManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyNioSessionManager)3 EzySimpleSessionTokenGenerator (com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)3 EzySimpleSessionManagementSetting (com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting)3 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)3 EzySimpleStreamingSetting (com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting)3 EzySimpleStatistics (com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics)3 EzyStatistics (com.tvd12.ezyfoxserver.statistics.EzyStatistics)3 ExecutorService (java.util.concurrent.ExecutorService)3