use of com.tvd12.ezyfoxserver.socket.EzyPacket in project ezyfox-server by youngmonkeys.
the class EzyAbstractSessionTest method addPacketToSessionQueueWithTicketsQueueIsNull.
@Test
public void addPacketToSessionQueueWithTicketsQueueIsNull() {
// given
EzyPacket packet = mock(EzyPacket.class);
MyTestSession session = new MyTestSession();
session.setActivated(true);
EzyPacketQueue packetQueue = mock(EzyPacketQueue.class);
when(packetQueue.isEmpty()).thenReturn(true);
when(packetQueue.add(packet)).thenReturn(true);
session.setPacketQueue(packetQueue);
// when
session.send(packet);
// then
Asserts.assertNull(session.getSessionTicketsQueue());
}
use of com.tvd12.ezyfoxserver.socket.EzyPacket in project ezyfox-server by youngmonkeys.
the class EzySimpleUserTest method test.
@Test
public void test() {
EzySimpleUser user = new EzySimpleUser();
user.setId(1);
user.setPassword("abc");
assert user.getPassword().equals("abc");
user.setName("dungtv1");
EzySimpleUser user2 = new EzySimpleUser();
user2.setPassword("abc");
user2.setId(2);
assert user2.getPassword().equals("abc");
user.setName("dungtv2");
assert !user.equals(user2);
assert user.hashCode() != user2.hashCode();
assert user.getLocks() != null;
assert user.getSessionMap() != null;
assert user.getMaxSessions() == 30;
assert user.getStartIdleTime() > 0;
assert !user.isDestroyed();
assert user.getSession() == null;
EzyAbstractSession session = spy(EzyAbstractSession.class);
EzyImmediateDeliver immediateDeliver = mock(EzyImmediateDeliver.class);
EzySocketDisconnectionQueue disconnectionQueue = mock(EzySocketDisconnectionQueue.class);
session.setImmediateDeliver(immediateDeliver);
session.setDisconnectionQueue(disconnectionQueue);
user.addSession(session);
assert user.getSession() == session;
Lock lock = user.getLock("test");
assert lock != null;
EzyPacket packet = mock(EzyPacket.class);
user.send(packet);
user.sendNow(packet);
user.disconnect(EzyDisconnectReason.IDLE);
user.disconnect();
user.removeSession(session);
user.setMaxIdleTime(100 * 60 * 1000);
assert !user.isIdle();
user.setMaxIdleTime(-1);
assert user.isIdle();
user.destroy();
user.destroy();
assert user.getSessions().size() == 0;
}
use of com.tvd12.ezyfoxserver.socket.EzyPacket in project ezyfox-server by youngmonkeys.
the class EzyAbstractStreamingApi method createPacket.
private EzyPacket createPacket(byte[] bytes, EzyBytesPackage pack) {
EzySimplePacket packet = new EzySimplePacket();
packet.setTransportType(pack.getTransportType());
packet.setData(bytes);
return packet;
}
use of com.tvd12.ezyfoxserver.socket.EzyPacket in project ezyfox-server by youngmonkeys.
the class EzySimpleWsHandlerGroupTest 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);
EzyServerControllers controllers = mock(EzyServerControllers.class);
server.setControllers(controllers);
EzyInterceptor interceptor = mock(EzyInterceptor.class);
when(controllers.getInterceptor(EzyCommand.PING)).thenReturn(interceptor);
EzyController controller = mock(EzyController.class);
when(controllers.getController(EzyCommand.PING)).thenReturn(controller);
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.WEBSOCKET);
EzyNioSession session = (EzyNioSession) sessionManager.provideSession(channel);
ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
EzySocketStreamQueue streamQueue = new EzyBlockingSocketStreamQueue();
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = EzyHandlerGroupBuilderFactoryImpl.builder().statistics(statistics).serverContext(serverContext).statsThreadPool(statsThreadPool).streamQueue(streamQueue).codecFactory(new ExCodecFactory()).sessionTicketsRequestQueues(sessionTicketsRequestQueues).socketSessionTicketsQueue(socketSessionTicketsQueue).webSocketSessionTicketsQueue(webSocketSessionTicketsQueue).build();
EzySimpleWsHandlerGroup group = (EzySimpleWsHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.WEBSOCKET).build();
group.fireBytesReceived("hello");
group.fireBytesReceived(new byte[] { 127, 2, 3, 4, 5, 6 }, 0, 5);
((EzyAbstractSession) session).setStreamingEnable(true);
group.fireBytesReceived(new byte[] { 127, 2, 3, 4, 5, 6 }, 0, 5);
EzySimplePacket packet = new EzySimplePacket();
packet.setBinary(false);
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));
EzyInterceptor streamInterceptor = mock(EzyInterceptor.class);
when(controllers.getStreamingInterceptor()).thenReturn(streamInterceptor);
EzyStreamingController streamController = mock(EzyStreamingController.class);
when(controllers.getStreamingController()).thenReturn(streamController);
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(false);
when(channel.write(any(ByteBuffer.class), anyBoolean())).thenThrow(new IllegalStateException("maintain"));
group.firePacketSend(failedPacket, writeBuffer);
MethodInvoker.create().object(group).method("executeHandleReceivedBytes").param("hello").invoke();
MethodInvoker.create().object(group).method("executeHandleReceivedBytes").param("hello".getBytes()).invoke();
group.fireChannelInactive();
Thread.sleep(2000);
group.destroy();
group.destroy();
EzySocketStreamQueue streamQueue1 = mock(EzySocketStreamQueue.class);
when(streamQueue1.add(any())).thenThrow(new IllegalStateException("queue full"));
group = (EzySimpleWsHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.WEBSOCKET).session(session).decoder(decoder).serverContext(serverContext).statsThreadPool(statsThreadPool).streamQueue(streamQueue1).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
group.fireBytesReceived(new byte[] { 127, 2, 3, 4, 5, 6 }, 0, 5);
}
use of com.tvd12.ezyfoxserver.socket.EzyPacket in project ezyfox-server by youngmonkeys.
the class EzySocketWriterTest method processSessionTicketsQueue0ExceptionCase.
@Test
public void processSessionTicketsQueue0ExceptionCase() {
EzySessionTicketsQueue sessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
EzySocketWriterGroupFetcher writerGroupFetcher = mock(EzySocketWriterGroupFetcher.class);
when(writerGroupFetcher.getWriterGroup(any(EzySession.class))).thenThrow(new IllegalArgumentException());
EzySocketWriter socketWriter = new EzySocketWriter();
socketWriter.setSessionTicketsQueue(sessionTicketsQueue);
socketWriter.setWriterGroupFetcher(writerGroupFetcher);
EzyPacketQueue packetQueue = new EzyNonBlockingPacketQueue();
EzyPacket packet = new EzySimplePacket();
packetQueue.add(packet);
EzyAbstractSession session = spy(EzyAbstractSession.class);
session.setActivated(true);
session.setSessionTicketsQueue(sessionTicketsQueue);
session.setPacketQueue(packetQueue);
sessionTicketsQueue.add(session);
socketWriter.handleEvent();
}
Aggregations