Search in sources :

Example 1 with EzyNioUdpDataHandler

use of com.tvd12.ezyfoxserver.nio.handler.EzyNioUdpDataHandler in project ezyfox-server by youngmonkeys.

the class EzyUdpServerBootstrap method newUdpDataHandler.

private EzyNioUdpDataHandler newUdpDataHandler() {
    int handlerThreadPoolSize = getSocketHandlerPoolSize();
    EzySimpleNioUdpDataHandler handler = new EzySimpleNioUdpDataHandler(handlerThreadPoolSize);
    handler.setResponseApi(getResponseApi());
    handler.setDatagramChannelPool(udpChannelPool);
    handler.setSessionManager(getSessionManager());
    handler.setSocketDataReceiver(socketDataReceiver);
    handler.setHandlerGroupManager(handlerGroupManager);
    return handler;
}
Also used : EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler)

Example 2 with EzyNioUdpDataHandler

use of com.tvd12.ezyfoxserver.nio.handler.EzyNioUdpDataHandler in project ezyfox-server by youngmonkeys.

the class EzyNioUdpReaderTest method testAddressNull.

@Test
public void testAddressNull() throws Exception {
    EzyNioUdpReader reader = new EzyNioUdpReader(1);
    Selector selector = spy(Selector.class);
    when(selector.select()).thenReturn(1);
    reader.setOwnSelector(selector);
    EzyNioUdpDataHandler dataHandler = mock(EzyNioUdpDataHandler.class);
    reader.setUdpDataHandler(dataHandler);
    SelectionKey selectionKey = spy(SelectionKey.class);
    when(selector.selectedKeys()).thenReturn(Sets.newHashSet(selectionKey));
    when(selectionKey.isValid()).thenReturn(true);
    when(selectionKey.readyOps()).thenReturn(SelectionKey.OP_READ);
    MyDatagramChannel channel = mock(MyDatagramChannel.class);
    when(selectionKey.channel()).thenReturn(channel);
    reader.handleEvent();
}
Also used : EzyNioUdpReader(com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader) EzyNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzyNioUdpDataHandler) Test(org.testng.annotations.Test)

Example 3 with EzyNioUdpDataHandler

use of com.tvd12.ezyfoxserver.nio.handler.EzyNioUdpDataHandler in project ezyfox-server by youngmonkeys.

the class EzyNioUdpReaderTest method testCancelledKeyExceptionCase.

@Test
public void testCancelledKeyExceptionCase() throws Exception {
    EzyNioUdpReader reader = new EzyNioUdpReader(1);
    Selector selector = spy(Selector.class);
    when(selector.select()).thenReturn(1);
    reader.setOwnSelector(selector);
    EzyNioUdpDataHandler dataHandler = mock(EzyNioUdpDataHandler.class);
    reader.setUdpDataHandler(dataHandler);
    SelectionKey selectionKey = spy(SelectionKey.class);
    when(selector.selectedKeys()).thenReturn(Sets.newHashSet(selectionKey));
    when(selectionKey.isValid()).thenReturn(true);
    when(selectionKey.readyOps()).thenReturn(SelectionKey.OP_READ);
    MyDatagramChannel channel = mock(MyDatagramChannel.class);
    when(selectionKey.channel()).thenReturn(channel);
    when(channel.receive(any(ByteBuffer.class))).thenThrow(new CancelledKeyException());
    reader.handleEvent();
}
Also used : EzyNioUdpReader(com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader) EzyNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzyNioUdpDataHandler) ByteBuffer(java.nio.ByteBuffer) Test(org.testng.annotations.Test)

Example 4 with EzyNioUdpDataHandler

use of com.tvd12.ezyfoxserver.nio.handler.EzyNioUdpDataHandler in project ezyfox-server by youngmonkeys.

the class EzyNioUdpReaderTest method test.

@Test
public void test() throws Exception {
    EzyNioUdpReader reader = new EzyNioUdpReader(1);
    Selector selector = spy(Selector.class);
    when(selector.select()).thenReturn(1);
    reader.setOwnSelector(selector);
    EzyNioUdpDataHandler dataHandler = mock(EzyNioUdpDataHandler.class);
    reader.setUdpDataHandler(dataHandler);
    SelectionKey selectionKey = spy(SelectionKey.class);
    when(selector.selectedKeys()).thenReturn(Sets.newHashSet(selectionKey));
    when(selectionKey.isValid()).thenReturn(true);
    when(selectionKey.readyOps()).thenReturn(SelectionKey.OP_READ);
    MyDatagramChannel channel = mock(MyDatagramChannel.class);
    when(selectionKey.channel()).thenReturn(channel);
    when(channel.receive(any(ByteBuffer.class))).thenAnswer((Answer<InetSocketAddress>) invocation -> {
        ByteBuffer buffer = invocation.getArgumentAt(0, ByteBuffer.class);
        buffer.put((byte) 0);
        return new InetSocketAddress(12346);
    });
    reader.handleEvent();
}
Also used : Sets(com.tvd12.ezyfox.collect.Sets) EzyNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzyNioUdpDataHandler) Set(java.util.Set) MethodUtil(com.tvd12.test.reflect.MethodUtil) Test(org.testng.annotations.Test) IOException(java.io.IOException) java.nio.channels(java.nio.channels) InetSocketAddress(java.net.InetSocketAddress) ByteBuffer(java.nio.ByteBuffer) EzyNioUdpReader(com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader) Matchers.any(org.mockito.Matchers.any) Answer(org.mockito.stubbing.Answer) Mockito(org.mockito.Mockito) SelectorProvider(java.nio.channels.spi.SelectorProvider) MethodInvoker(com.tvd12.test.reflect.MethodInvoker) InetSocketAddress(java.net.InetSocketAddress) EzyNioUdpReader(com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader) EzyNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzyNioUdpDataHandler) ByteBuffer(java.nio.ByteBuffer) Test(org.testng.annotations.Test)

Example 5 with EzyNioUdpDataHandler

use of com.tvd12.ezyfoxserver.nio.handler.EzyNioUdpDataHandler in project ezyfox-server by youngmonkeys.

the class EzyNioUdpReaderTest method testIOExceptionCase.

@Test
public void testIOExceptionCase() throws Exception {
    EzyNioUdpReader reader = new EzyNioUdpReader(1);
    Selector selector = spy(Selector.class);
    when(selector.select()).thenReturn(1);
    reader.setOwnSelector(selector);
    EzyNioUdpDataHandler dataHandler = mock(EzyNioUdpDataHandler.class);
    reader.setUdpDataHandler(dataHandler);
    SelectionKey selectionKey = spy(SelectionKey.class);
    when(selector.selectedKeys()).thenReturn(Sets.newHashSet(selectionKey));
    when(selectionKey.isValid()).thenReturn(true);
    when(selectionKey.readyOps()).thenReturn(SelectionKey.OP_READ);
    MyDatagramChannel channel = mock(MyDatagramChannel.class);
    when(selectionKey.channel()).thenReturn(channel);
    when(channel.receive(any(ByteBuffer.class))).thenThrow(new IOException("test"));
    reader.handleEvent();
}
Also used : EzyNioUdpReader(com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader) EzyNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzyNioUdpDataHandler) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Test(org.testng.annotations.Test)

Aggregations

EzyNioUdpDataHandler (com.tvd12.ezyfoxserver.nio.handler.EzyNioUdpDataHandler)6 EzyNioUdpReader (com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader)6 Test (org.testng.annotations.Test)6 ByteBuffer (java.nio.ByteBuffer)5 IOException (java.io.IOException)2 Sets (com.tvd12.ezyfox.collect.Sets)1 EzySimpleNioUdpDataHandler (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler)1 MethodInvoker (com.tvd12.test.reflect.MethodInvoker)1 MethodUtil (com.tvd12.test.reflect.MethodUtil)1 InetSocketAddress (java.net.InetSocketAddress)1 java.nio.channels (java.nio.channels)1 SelectorProvider (java.nio.channels.spi.SelectorProvider)1 Set (java.util.Set)1 Matchers.any (org.mockito.Matchers.any)1 Mockito (org.mockito.Mockito)1 Answer (org.mockito.stubbing.Answer)1