use of com.tvd12.ezyfoxserver.socket.EzySocketDisconnectionQueue in project ezyfox-server by youngmonkeys.
the class EzyAbstractSessionTest method test.
@SuppressWarnings({ "unlikely-arg-type", "EqualsWithItself", "ConstantConditions", "EqualsBetweenInconvertibleTypes" })
@Test
public void test() {
MyTestSession session = new MyTestSession();
session.setId(100);
session.setPrivateKey(new byte[] { 1, 2, 3 });
session.addReadBytes(10);
session.addWrittenBytes(10);
session.setLoggedInTime(12345);
session.setMaxWaitingTime(123);
session.setMaxIdleTime(12345L);
assertEquals(session.getPrivateKey(), new byte[] { 1, 2, 3 });
assertEquals(session.getReadBytes(), 10);
assertEquals(session.getWrittenBytes(), 10);
assertEquals(session.getLoggedInTime(), 12345L);
assertEquals(session.getMaxWaitingTime(), 123L);
MyTestSession session2 = new MyTestSession();
session2.setId(1);
MyTestSession session3 = new MyTestSession();
session3.setId(100);
assert !session.equals(null);
assert session.equals(session);
assert !session.equals(new Object());
assert !session.equals(session2);
assert !session.equals(new PrivateSession());
assert session.equals(session3);
assert session.hashCode() != session2.hashCode();
session.setClientId("clientId");
assert session.getClientId().equals("clientId");
session.setOwnerName("owner");
assert session.getOwnerName().equals("owner");
session.setLastReadTime(1);
assert session.getLastReadTime() == 1;
session.setLastWriteTime(2);
assert session.getLastWriteTime() == 2;
session.setLastActivityTime(3);
assert session.getLastActivityTime() == 3;
session.setReadRequests(4);
assert session.getReadRequests() == 4;
session.setWrittenResponses(5);
assert session.getWrittenResponses() == 5;
session.addWrittenResponses(1);
assert session.getWrittenResponses() == 6;
session.setDestroyed(false);
assert !session.isDestroyed();
session.setStreamingEnable(true);
assert session.isStreamingEnable();
session.setClientType("android");
assert session.getClientType().equals("android");
session.setClientVersion("1.0.0");
assert session.getClientVersion().equals("1.0.0");
session.setBeforeToken("before");
assert session.getBeforeToken().equals("before");
session.setDisconnectReason(EzyDisconnectReason.ADMIN_BAN);
assert session.getDisconnectReason() == EzyDisconnectReason.ADMIN_BAN;
session.setMaxIdleTime(6);
assert session.getMaxIdleTime() == 6;
session.setDroppedPackets(mock(EzyDroppedPackets.class));
assert session.getDroppedPackets() != null;
session.setImmediateDeliver(mock(EzyImmediateDeliver.class));
assert session.getImmediateDeliver() != null;
EzySessionTicketsQueue sessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
session.setSessionTicketsQueue(sessionTicketsQueue);
assert session.getSessionTicketsQueue() == sessionTicketsQueue;
EzySocketDisconnectionQueue disconnectionQueue = mock(EzySocketDisconnectionQueue.class);
session.setDisconnectionQueue(disconnectionQueue);
assert session.getDisconnectionQueue() == disconnectionQueue;
assert !session.isDisconnectionRegistered();
assert session.getDisconnectionLock() != null;
assert session.getLocks().isEmpty();
assert session.getLock("test") != null;
assert !session.isActivated();
session.send(mock(EzyPacket.class));
session.setActivated(true);
EzyPacketQueue packetQueue = new EzyNonBlockingPacketQueue(3);
session.setPacketQueue(packetQueue);
session.send(mock(EzyPacket.class));
assert session.getPacketQueue() != null;
session.send(mock(EzyPacket.class));
session.sendNow(mock(EzyPacket.class));
session.send(mock(EzyPacket.class));
session.send(mock(EzyPacket.class));
session.send(mock(EzyPacket.class));
assert session.getChannel() == null;
assert session.getConnection() == null;
session.disconnect();
session.close();
session.destroy();
session.destroy();
assert session.isDestroyed();
PrivateSession privateSession = new PrivateSession();
privateSession.setDisconnectionQueue(disconnectionQueue);
privateSession.disconnect();
privateSession.disconnect();
assert privateSession.getServerAddress() == null;
EzyChannel channel = mock(EzyChannel.class);
when(channel.getServerAddress()).thenReturn(new InetSocketAddress(2233));
privateSession.setChannel(channel);
assert privateSession.getServerAddress() != null;
privateSession.close();
assert privateSession.getConnection() == null;
assert privateSession.getUdpClientAddress() == null;
assert privateSession.getDatagramChannel() == null;
assert privateSession.getDatagramChannelPool() == null;
privateSession.setUdpClientAddress(new InetSocketAddress(12345));
assert privateSession.getUdpClientAddress() != null;
try {
privateSession.setDatagramChannel(DatagramChannel.open());
assert privateSession.getDatagramChannel() != null;
} catch (Exception e) {
e.printStackTrace();
}
try {
privateSession.setDatagramChannelPool(new EzyDatagramChannelPool(2));
assert privateSession.getDatagramChannelPool() != null;
} catch (Exception e) {
e.printStackTrace();
}
privateSession.setRequestFrameInSecond(new EzyRequestFrameSecond(2));
Asserts.assertFalse(privateSession.addReceivedRequests(1));
Asserts.assertTrue(privateSession.addReceivedRequests(3));
privateSession.setRequestFrameInSecond(new EzyRequestFrameSecond(5, System.currentTimeMillis() - 2 * 1000));
Asserts.assertFalse(privateSession.addReceivedRequests(1));
Asserts.assertFalse(privateSession.addReceivedRequests(3));
Asserts.assertFalse(privateSession.getRequestFrameInSecond().isExpired());
byte[] clientKey = RandomUtil.randomShortByteArray();
privateSession.setClientKey(clientKey);
Asserts.assertEquals(clientKey, privateSession.getClientKey());
byte[] sessionKey = RandomUtil.randomShortByteArray();
privateSession.setSessionKey(sessionKey);
Asserts.assertEquals(sessionKey, privateSession.getSessionKey());
EzyRequestQueue systemRquestQueue = mock(EzyRequestQueue.class);
privateSession.setSystemRequestQueue(systemRquestQueue);
Asserts.assertEquals(systemRquestQueue, privateSession.getSystemRequestQueue());
EzyRequestQueue extensionRequestQueue = mock(EzyRequestQueue.class);
privateSession.setExtensionRequestQueue(extensionRequestQueue);
Asserts.assertEquals(extensionRequestQueue, privateSession.getExtensionRequestQueue());
privateSession.destroy();
}
use of com.tvd12.ezyfoxserver.socket.EzySocketDisconnectionQueue 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.EzySocketDisconnectionQueue 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();
}
use of com.tvd12.ezyfoxserver.socket.EzySocketDisconnectionQueue in project ezyfox-server by youngmonkeys.
the class EzyWsHandlerTest method test.
@Test
public void test() throws Exception {
EzySimpleSessionManagementSetting sessionManagementSetting = new EzySimpleSessionManagementSetting();
EzyNioSessionManager sessionManager = (EzyNioSessionManager) EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
EzyCodecFactory codecFactory = mock(EzyCodecFactory.class);
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();
EzyHandlerGroupManager handlerGroupManager = EzyHandlerGroupManagerImpl.builder().handlerGroupBuilderFactory(handlerGroupBuilderFactory).build();
EzySocketDataReceiver socketDataReceiver = EzySocketDataReceiver.builder().threadPoolSize(1).handlerGroupManager(handlerGroupManager).build();
EzyWsHandler handler = EzyWsHandler.builder().sessionManagementSetting(sessionManagementSetting).sessionManager(sessionManager).handlerGroupManager(handlerGroupManager).socketDataReceiver(socketDataReceiver).build();
Session session = mock(Session.class);
handler.onConnect(session);
handler.onMessage(session, "hello");
handler.onMessage(session, "hello".getBytes(), 0, 5);
handler.onError(session, new TimeoutException("timeout"));
handler.onError(session, new IllegalStateException("maintain"));
handler.onClose(session, 39999, "test");
handler.onClose(session, 3000, "test");
Session session2 = mock(Session.class);
handler.onMessage(session2, "hello");
handler.onMessage(session2, "hello".getBytes(), 0, 5);
handler.onError(session2, new TimeoutException("timeout"));
handler.onError(session2, new IllegalStateException("maintain"));
handler.onClose(session2, 39999, "test");
handler.onClose(session2, 3000, "test");
}
use of com.tvd12.ezyfoxserver.socket.EzySocketDisconnectionQueue 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();
}
Aggregations