use of com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor in project ezyfox-server by youngmonkeys.
the class EzySocketServerBootstrap method newSocketAcceptanceLoopHandler.
private EzySocketEventLoopHandler newSocketAcceptanceLoopHandler(EzyNioSocketAcceptor socketAcceptor) {
EzySocketEventLoopOneHandler loopHandler = new EzyNioSocketAcceptanceLoopHandler();
loopHandler.setThreadPoolSize(getSocketAcceptorPoolSize());
socketAcceptor.setTcpNoDelay(getSocketTcpNoDelay());
socketAcceptor.setReadSelector(readSelector);
socketAcceptor.setOwnSelector(acceptSelector);
socketAcceptor.setAcceptableConnections(new ArrayList<>());
socketAcceptor.setHandlerGroupManager(handlerGroupManager);
loopHandler.setEventHandler(socketAcceptor);
return loopHandler;
}
use of com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor in project ezyfox-server by youngmonkeys.
the class EzyNioSocketAcceptorTest method acceptConnectionTest.
@Test
public void acceptConnectionTest() throws Exception {
// given
EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
EzyNioHandlerGroup handlerGroup = mock(EzyNioHandlerGroup.class);
Selector readSelector = Selector.open();
EzyNioSocketAcceptor sut = new EzyNioSocketAcceptor();
sut.setReadSelector(readSelector);
SocketChannel clientChannel = SocketChannel.open();
when(handlerGroupManager.newHandlerGroup(any(), any())).thenReturn(handlerGroup);
sut.setHandlerGroupManager(handlerGroupManager);
EzyNioSession session = mock(EzyNioSession.class);
when(handlerGroup.getSession()).thenReturn(session);
// when
MethodInvoker.create().object(sut).method("acceptConnection").param(SocketChannel.class, clientChannel).call();
// then
verify(handlerGroupManager, times(1)).newHandlerGroup(any(), any());
}
use of com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor 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.EzyNioSocketAcceptor 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;
}
use of com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor 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;
}
Aggregations