use of com.tvd12.ezyfoxserver.statistics.EzyStatistics in project ezyfox-server by youngmonkeys.
the class EzyHandlerGroupManagerImplTest method test.
@Test
public void test() {
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).socketSessionTicketsQueue(webSocketSessionTicketsQueue).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
EzyHandlerGroupManager handlerGroupManager = EzyHandlerGroupManagerImpl.builder().handlerGroupBuilderFactory(handlerGroupBuilderFactory).build();
handlerGroupManager.removeHandlerGroup(null);
EzySession session1 = mock(EzyAbstractSession.class);
handlerGroupManager.removeHandlerGroup(session1);
EzySession session2 = mock(EzyAbstractSession.class);
EzyChannel channel2 = mock(EzyChannel.class);
when(session2.getChannel()).thenReturn(channel2);
handlerGroupManager.removeHandlerGroup(session2);
EzyChannel channel3 = mock(EzyChannel.class);
Session connection3 = mock(Session.class);
when(channel3.getConnection()).thenReturn(connection3);
EzyHandlerGroup handlerGroup3 = handlerGroupManager.newHandlerGroup(channel3, EzyConnectionType.WEBSOCKET);
EzySession session3 = mock(EzyAbstractSession.class);
when(session3.getChannel()).thenReturn(channel3);
assert handlerGroupManager.getDataHandlerGroup(null) == null;
assert handlerGroupManager.getDataHandlerGroup(session1) == null;
assert handlerGroupManager.getDataHandlerGroup(session2) == null;
assert handlerGroupManager.getWriterGroup(session3) == handlerGroup3;
InetSocketAddress udpAddress = new InetSocketAddress("127.0.0.1", 12345);
handlerGroupManager.mapSocketChannel(udpAddress, session3);
assert handlerGroupManager.getSocketChannel(udpAddress) != null;
handlerGroupManager.unmapHandlerGroup(udpAddress);
handlerGroupManager.removeHandlerGroup(session3);
handlerGroupManager.destroy();
}
use of com.tvd12.ezyfoxserver.statistics.EzyStatistics in project ezyfox-server by youngmonkeys.
the class EzyAbstractHandlerGroupTest method test.
@SuppressWarnings("rawtypes")
@Test
public void test() throws Exception {
EzyStatistics statistics = new EzySimpleStatistics();
EzySimpleSettings settings = new EzySimpleSettings();
EzySimpleStreamingSetting streaming = settings.getStreaming();
streaming.setEnable(true);
EzySimpleServer server = new EzySimpleServer();
server.setSettings(settings);
EzySimpleServerContext serverContext = new EzySimpleServerContext();
serverContext.setServer(server);
serverContext.init();
EzySessionManager sessionManager = EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
EzyChannel channel = mock(EzyChannel.class);
when(channel.isConnected()).thenReturn(true);
when(channel.getConnection()).thenReturn(SocketChannel.open());
when(channel.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
EzyNioSession session = (EzyNioSession) sessionManager.provideSession(channel);
ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
when(channel.write(any(ByteBuffer.class), anyBoolean())).thenReturn(123456);
ExHandlerGroup group = (ExHandlerGroup) new ExHandlerGroup.Builder().session(session).decoder(decoder).sessionCount(new AtomicInteger()).networkStats((EzyNetworkStats) statistics.getSocketStats().getNetworkStats()).sessionStats((EzySessionStats) statistics.getSocketStats().getSessionStats()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
EzyChannel channelX = mock(EzyChannel.class);
MethodInvoker.create().object(group).method("canWriteBytes").param(EzyChannel.class, null).invoke();
MethodInvoker.create().object(group).method("canWriteBytes").param(EzyChannel.class, channelX).invoke();
sessionTicketsRequestQueues = mock(EzySessionTicketsRequestQueues.class);
when(sessionTicketsRequestQueues.addRequest(any())).thenReturn(false);
group = (ExHandlerGroup) new ExHandlerGroup.Builder().session(session).decoder(decoder).sessionCount(new AtomicInteger()).networkStats((EzyNetworkStats) statistics.getSocketStats().getNetworkStats()).sessionStats((EzySessionStats) statistics.getSocketStats().getSessionStats()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
MethodInvoker.create().object(group).method("handleReceivedData").param(Object.class, EzyEntityFactory.newArrayBuilder().append(EzyCommand.PING.getId()).append(EzyEntityFactory.EMPTY_OBJECT).build()).param(int.class, 100).invoke();
((EzyDatagramChannelPoolAware) session).setDatagramChannelPool(new EzyDatagramChannelPool(1));
((EzyUdpClientAddressAware) session).setUdpClientAddress(new InetSocketAddress("127.0.0.1", 12348));
EzyPacket packet = mock(EzyPacket.class);
when(packet.getData()).thenReturn(new byte[] { 1, 2, 3 });
ByteBuffer buffer = ByteBuffer.allocate(100);
MethodInvoker.create().object(group).method("executeSendingPacket").param(EzyPacket.class, packet).param(Object.class, buffer).invoke();
MethodInvoker.create().object(group).method("executeSendingPacket").param(EzyPacket.class, packet).param(Object.class, new Object()).invoke();
Asserts.assertNotNull(group.getSession());
}
use of com.tvd12.ezyfoxserver.statistics.EzyStatistics in project ezyfox-server by youngmonkeys.
the class EzyAbstractHandlerGroupTest method newHandlerGroup.
@SuppressWarnings("rawtypes")
private ExHandlerGroup newHandlerGroup() throws Exception {
EzyStatistics statistics = new EzySimpleStatistics();
EzySimpleSettings settings = new EzySimpleSettings();
EzySimpleStreamingSetting streaming = settings.getStreaming();
streaming.setEnable(true);
EzySimpleServer server = new EzySimpleServer();
server.setSettings(settings);
EzySimpleServerContext serverContext = new EzySimpleServerContext();
serverContext.setServer(server);
serverContext.init();
EzyChannel channel = mock(EzyChannel.class);
when(channel.isConnected()).thenReturn(true);
when(channel.getConnection()).thenReturn(SocketChannel.open());
when(channel.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
EzySimpleSession session = mock(EzySimpleSession.class);
EzySessionManager sessionManager = mock(EzySessionManager.class);
server.setSessionManager(sessionManager);
EzyResponseApi responseApi = mock(EzyResponseApi.class);
server.setResponseApi(responseApi);
ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = mock(EzySessionTicketsRequestQueues.class);
when(channel.write(any(ByteBuffer.class), anyBoolean())).thenReturn(123456);
return (ExHandlerGroup) new ExHandlerGroup.Builder().session(session).decoder(decoder).sessionCount(new AtomicInteger()).networkStats((EzyNetworkStats) statistics.getSocketStats().getNetworkStats()).sessionStats((EzySessionStats) statistics.getSocketStats().getSessionStats()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
}
use of com.tvd12.ezyfoxserver.statistics.EzyStatistics in project ezyfox-server by youngmonkeys.
the class EzySimpleNioHandlerGroupTest method test.
@SuppressWarnings("rawtypes")
@Test
public void test() throws Exception {
EzySessionTicketsQueue socketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
EzySessionTicketsQueue webSocketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
EzySessionManager sessionManager = EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
EzyStatistics statistics = new EzySimpleStatistics();
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();
EzyChannel channel = mock(EzyChannel.class);
when(channel.isConnected()).thenReturn(true);
when(channel.getConnection()).thenReturn(SocketChannel.open());
when(channel.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
EzyNioSession session = (EzyNioSession) sessionManager.provideSession(channel);
ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = EzyHandlerGroupBuilderFactoryImpl.builder().statistics(statistics).serverContext(serverContext).statsThreadPool(statsThreadPool).codecFactory(new ExCodecFactory()).sessionTicketsRequestQueues(sessionTicketsRequestQueues).socketSessionTicketsQueue(socketSessionTicketsQueue).webSocketSessionTicketsQueue(webSocketSessionTicketsQueue).build();
EzySimpleNioHandlerGroup group = (EzySimpleNioHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.SOCKET).build();
group.fireBytesReceived("hello".getBytes());
EzySimplePacket packet = new EzySimplePacket();
packet.setBinary(true);
packet.setData("world".getBytes());
packet.setTransportType(EzyTransportType.TCP);
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
group.firePacketSend(packet, writeBuffer);
group.sendPacketNow(packet);
group.fireChannelRead(EzyCommand.PING, EzyEntityArrays.newArray(EzyCommand.PING.getId(), EzyEntityFactory.EMPTY_ARRAY));
group.fireStreamBytesReceived(new byte[] { 0, 1, 2 });
EzyPacket droppedPacket = mock(EzyPacket.class);
when(droppedPacket.getSize()).thenReturn(12);
group.addDroppedPacket(droppedPacket);
EzyPacket failedPacket = mock(EzyPacket.class);
when(failedPacket.getData()).thenReturn(new byte[] { 1, 2, 3 });
when(failedPacket.isBinary()).thenReturn(true);
when(channel.write(any(ByteBuffer.class), anyBoolean())).thenThrow(new IllegalStateException("maintain"));
group.firePacketSend(failedPacket, writeBuffer);
group.fireChannelInactive();
Thread.sleep(2000);
group.destroy();
group.destroy();
EzySocketStreamQueue streamQueue = new EzyBlockingSocketStreamQueue();
group = (EzySimpleNioHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.SOCKET).session(session).streamQueue(streamQueue).decoder(new ExStreamEzyByteToObjectDecoder()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
((EzyAbstractSession) session).setStreamingEnable(false);
group.fireBytesReceived("hello".getBytes());
Thread.sleep(500);
((EzyAbstractSession) session).setStreamingEnable(true);
group.fireBytesReceived("hello".getBytes());
Thread.sleep(1000);
streamQueue = mock(EzySocketStreamQueue.class);
when(streamQueue.add(any())).thenThrow(new IllegalStateException("maintain"));
group = (EzySimpleNioHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.SOCKET).session(session).streamQueue(streamQueue).decoder(new ExStreamEzyByteToObjectDecoder()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
group.fireBytesReceived("hello".getBytes());
Thread.sleep(1000);
group = (EzySimpleNioHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.SOCKET).session(session).decoder(new ErrorEzyByteToObjectDecoder()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
group.fireBytesReceived("hello".getBytes());
group.fireMessageReceived(mock(EzyMessage.class));
Thread.sleep(300);
}
use of com.tvd12.ezyfoxserver.statistics.EzyStatistics in project ezyfox-server by youngmonkeys.
the class EzySimpleNioHandlerGroupTest method newHandlerGroup.
@SuppressWarnings("rawtypes")
private EzySimpleNioHandlerGroup newHandlerGroup(boolean streamEnable) throws IOException {
EzySessionTicketsQueue socketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
EzySessionTicketsQueue webSocketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
EzySessionManager sessionManager = EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
EzyStatistics statistics = new EzySimpleStatistics();
EzySimpleSettings settings = new EzySimpleSettings();
EzySimpleStreamingSetting streaming = settings.getStreaming();
streaming.setEnable(streamEnable);
EzySimpleServer server = new EzySimpleServer();
server.setSettings(settings);
server.setSessionManager(sessionManager);
EzySimpleServerContext serverContext = new EzySimpleServerContext();
serverContext.setServer(server);
serverContext.init();
EzyChannel channel = mock(EzyChannel.class);
when(channel.isConnected()).thenReturn(true);
when(channel.getConnection()).thenReturn(SocketChannel.open());
when(channel.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
EzySimpleSession session = mock(EzySimpleSession.class);
when(session.getChannel()).thenReturn(channel);
ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
SelectionKey selectionKey = mock(SelectionKey.class);
when(selectionKey.isValid()).thenReturn(true);
when(session.getProperty(EzyNioSession.SELECTION_KEY)).thenReturn(selectionKey);
EzyCodecFactory codecFactory = mock(EzyCodecFactory.class);
EzyByteToObjectDecoder decoder = mock(EzyByteToObjectDecoder.class);
when(codecFactory.newDecoder(any())).thenReturn(decoder);
EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = EzyHandlerGroupBuilderFactoryImpl.builder().statistics(statistics).serverContext(serverContext).statsThreadPool(statsThreadPool).codecFactory(codecFactory).sessionTicketsRequestQueues(sessionTicketsRequestQueues).socketSessionTicketsQueue(socketSessionTicketsQueue).webSocketSessionTicketsQueue(webSocketSessionTicketsQueue).build();
return (EzySimpleNioHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.SOCKET).session(session).decoder(decoder).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
}
Aggregations