use of com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup in project ezyfox-server by youngmonkeys.
the class EzySimpleNioHandlerGroupTest method writePacketToSocketSessionKeyIsInvalid.
@Test
public void writePacketToSocketSessionKeyIsInvalid() throws Exception {
// given
EzySimpleNioHandlerGroup sut = newHandlerGroup();
EzyNioSession session = FieldUtil.getFieldValue(sut, "session");
SelectionKey selectionKey = mock(SelectionKey.class);
when(session.getSelectionKey()).thenReturn(selectionKey);
when(selectionKey.isValid()).thenReturn(false);
EzyPacket packet = mock(EzyPacket.class);
when(packet.getData()).thenReturn(new byte[] { 1, 2, 3, 5 });
when(packet.isBinary()).thenReturn(true);
ByteBuffer writeBuffer = ByteBuffer.allocate(5);
// when
MethodInvoker.create().object(sut).method("writePacketToSocket").param(EzyPacket.class, packet).param(Object.class, writeBuffer).call();
// then
verify(session, times(1)).getSelectionKey();
verify(selectionKey, times(1)).isValid();
}
use of com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup in project ezyfox-server by youngmonkeys.
the class EzySimpleNioHandlerGroupTest method handleReceivedMessageHeaderIsNotRawBytes.
@Test
public void handleReceivedMessageHeaderIsNotRawBytes() throws Exception {
// given
EzySimpleNioHandlerGroup sut = newHandlerGroup();
EzyMessage message = mock(EzyMessage.class);
EzyMessageHeader header = mock(EzyMessageHeader.class);
when(message.getHeader()).thenReturn(header);
// when
MethodInvoker.create().object(sut).method("handleReceivedMessage").param(EzyMessage.class, message).call();
// then
verify(header, times(1)).isRawBytes();
EzySimpleMessageDataDecoder decoder = FieldUtil.getFieldValue(sut, "decoder");
EzyByteToObjectDecoder d = FieldUtil.getFieldValue(decoder, "decoder");
EzySession session = FieldUtil.getFieldValue(sut, "session");
verify(d, times(1)).decode(message, session.getSessionKey());
}
use of com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup 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.nio.handler.EzySimpleNioHandlerGroup 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();
}
use of com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup in project ezyfox-server by youngmonkeys.
the class EzySimpleNioHandlerGroupTest method handleReceivedMessageStreamingNotEnable.
@Test
public void handleReceivedMessageStreamingNotEnable() throws Exception {
// given
EzySimpleNioHandlerGroup sut = newHandlerGroup(false);
EzyMessage message = mock(EzyMessage.class);
EzyMessageHeader header = mock(EzyMessageHeader.class);
when(header.isRawBytes()).thenReturn(true);
when(message.getHeader()).thenReturn(header);
// when
MethodInvoker.create().object(sut).method("handleReceivedMessage").param(EzyMessage.class, message).call();
// then
verify(header, times(1)).isRawBytes();
}
Aggregations