use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.
the class EzySimpleNioUdpDataHandlerTest method handleReceivedUdpPacketWithSessionTest.
@Test
public void handleReceivedUdpPacketWithSessionTest() {
// given
EzySimpleNioUdpDataHandler sut = new EzySimpleNioUdpDataHandler(1);
byte[] messageBytes = messageBytes();
InetSocketAddress udpAddress = new InetSocketAddress(3005);
InetSocketAddress tcpAddress = new InetSocketAddress(123);
EzyUdpReceivedPacket packet = mock(EzyUdpReceivedPacket.class);
when(packet.getBytes()).thenReturn(messageBytes);
when(packet.getAddress()).thenReturn(udpAddress);
Session session = mock(Session.class);
when(session.getRemoteAddress()).thenReturn(tcpAddress);
EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
when(handlerGroupManager.getSocketChannel(udpAddress)).thenReturn(session);
sut.setHandlerGroupManager(handlerGroupManager);
EzySocketDataReceiver socketDataReceiver = mock(EzySocketDataReceiver.class);
sut.setSocketDataReceiver(socketDataReceiver);
// when
MethodInvoker.create().object(sut).method("handleReceivedUdpPacket").param(EzyUdpReceivedPacket.class, packet).call();
// then
verify(handlerGroupManager, times(1)).getSocketChannel(udpAddress);
verify(session, times(1)).getRemoteAddress();
verify(socketDataReceiver, times(1)).udpReceive(any(SocketChannel.class), any(EzyMessage.class));
sut.destroy();
}
use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager 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.wrapper.EzyHandlerGroupManager 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;
}
use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager 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.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.
the class EzyNioSocketReaderTest method newHandlerGroupManager.
private EzyHandlerGroupManager newHandlerGroupManager() {
EzyNioSessionManager sessionManager = (EzyNioSessionManager) EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
EzyCodecFactory codecFactory = mock(EzyCodecFactory.class);
when(codecFactory.newDecoder(any())).thenReturn(decoder);
ExecutorService statsThreadPool = EzyExecutors.newSingleThreadExecutor("stats");
EzySocketStreamQueue streamQueue = new EzyBlockingSocketStreamQueue();
EzySocketDisconnectionQueue disconnectionQueue = new EzyBlockingSocketDisconnectionQueue();
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
EzySimpleSettings settings = new EzySimpleSettings();
EzySimpleStreamingSetting streaming = settings.getStreaming();
streaming.setEnable(true);
EzySimpleServer server = new EzySimpleServer();
server.setSettings(settings);
server.setSessionManager(sessionManager);
EzySimpleServerContext serverContext = new EzySimpleServerContext();
serverContext.setServer(server);
serverContext.init();
EzySessionTicketsQueue socketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
EzySessionTicketsQueue webSocketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
EzyStatistics statistics = new EzySimpleStatistics();
EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = EzyHandlerGroupBuilderFactoryImpl.builder().statistics(statistics).statsThreadPool(statsThreadPool).streamQueue(streamQueue).disconnectionQueue(disconnectionQueue).codecFactory(codecFactory).serverContext(serverContext).socketSessionTicketsQueue(socketSessionTicketsQueue).webSocketSessionTicketsQueue(webSocketSessionTicketsQueue).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
return EzyHandlerGroupManagerImpl.builder().handlerGroupBuilderFactory(handlerGroupBuilderFactory).build();
}
Aggregations