Search in sources :

Example 6 with EzyNioUdpReader

use of com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader 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)

Example 7 with EzyNioUdpReader

use of com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader in project ezyfox-server by youngmonkeys.

the class EzyNioUdpReaderTest method testClosedSelectorExceptionCase.

@Test
public void testClosedSelectorExceptionCase() 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 ClosedSelectorException());
    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 8 with EzyNioUdpReader

use of com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader in project ezyfox-server by youngmonkeys.

the class EzyNioUdpReaderTest method processReadBytesByteCountLessIsZero.

@Test
public void processReadBytesByteCountLessIsZero() throws IOException {
    // given
    EzyNioUdpReader sut = new EzyNioUdpReader(1024);
    DatagramChannel channel = mock(DatagramChannel.class);
    InetSocketAddress address = new InetSocketAddress(3005);
    when(channel.receive(any())).thenReturn(address);
    // when
    MethodInvoker.create().object(sut).method("processReadBytes").param(DatagramChannel.class, channel).call();
    // then
    verify(channel, times(1)).receive(any());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) EzyNioUdpReader(com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader) Test(org.testng.annotations.Test)

Example 9 with EzyNioUdpReader

use of com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader in project ezyfox-server by youngmonkeys.

the class EzyNioUdpReaderTest method processReadyKeyNotReadable.

@Test
public void processReadyKeyNotReadable() {
    // given
    EzyNioUdpReader sut = new EzyNioUdpReader(1024);
    SelectionKey selectionKey = mock(SelectionKey.class);
    when(selectionKey.readyOps()).thenReturn(SelectionKey.OP_WRITE);
    // when
    MethodInvoker.create().object(sut).method("processReadyKey").param(SelectionKey.class, selectionKey).call();
    // then
    verify(selectionKey, times(1)).readyOps();
}
Also used : EzyNioUdpReader(com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader) Test(org.testng.annotations.Test)

Example 10 with EzyNioUdpReader

use of com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader in project ezyfox-server by youngmonkeys.

the class EzyNioUdpReaderTest method testExceptionCase.

@Test
public void testExceptionCase() 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 IllegalStateException("test"));
    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)

Aggregations

EzyNioUdpReader (com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReader)10 Test (org.testng.annotations.Test)9 EzyNioUdpDataHandler (com.tvd12.ezyfoxserver.nio.handler.EzyNioUdpDataHandler)6 ByteBuffer (java.nio.ByteBuffer)5 IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 Sets (com.tvd12.ezyfox.collect.Sets)1 EzyNioUdpReadingLoopHandler (com.tvd12.ezyfoxserver.nio.udp.EzyNioUdpReadingLoopHandler)1 EzySocketEventLoopOneHandler (com.tvd12.ezyfoxserver.socket.EzySocketEventLoopOneHandler)1 MethodInvoker (com.tvd12.test.reflect.MethodInvoker)1 MethodUtil (com.tvd12.test.reflect.MethodUtil)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