use of com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketReader 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();
}
use of com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketReader 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();
}
use of com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketReader in project ezyfox-server by youngmonkeys.
the class EzySocketServerBootstrap method newReadingLoopHandler.
private EzySocketEventLoopHandler newReadingLoopHandler(EzyNioAcceptableConnectionsHandler acceptableConnectionsHandler) {
EzySocketEventLoopOneHandler loopHandler = new EzyNioSocketReadingLoopHandler();
loopHandler.setThreadPoolSize(getSocketReaderPoolSize());
EzyNioSocketReader eventHandler = new EzyNioSocketReader();
eventHandler.setOwnSelector(readSelector);
eventHandler.setSocketDataReceiver(socketDataReceiver);
eventHandler.setAcceptableConnectionsHandler(acceptableConnectionsHandler);
loopHandler.setEventHandler(eventHandler);
return loopHandler;
}
Aggregations