use of com.tvd12.ezyfoxserver.socket.EzyChannel in project ezyfox-server by youngmonkeys.
the class EzySimpleWsHandlerGroupTest method newHandlerGroup.
@SuppressWarnings("rawtypes")
private EzySimpleWsHandlerGroup 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);
EzyServerControllers controllers = mock(EzyServerControllers.class);
server.setControllers(controllers);
EzySimpleConfig config = new EzySimpleConfig();
server.setConfig(config);
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);
ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
EzySocketStreamQueue streamQueue = new EzyBlockingSocketStreamQueue();
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
EzySimpleSession session = mock(EzySimpleSession.class);
when(session.getChannel()).thenReturn(channel);
EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = EzyHandlerGroupBuilderFactoryImpl.builder().statistics(statistics).serverContext(serverContext).statsThreadPool(statsThreadPool).streamQueue(streamQueue).codecFactory(new ExCodecFactory()).sessionTicketsRequestQueues(sessionTicketsRequestQueues).socketSessionTicketsQueue(socketSessionTicketsQueue).webSocketSessionTicketsQueue(webSocketSessionTicketsQueue).build();
return (EzySimpleWsHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.WEBSOCKET).session(session).build();
}
use of com.tvd12.ezyfoxserver.socket.EzyChannel in project ezyfox-server by youngmonkeys.
the class EzyHandlerGroupManagerImplTest method mapSocketChannelNonContainsConnection.
@Test
public void mapSocketChannelNonContainsConnection() {
// given
EzyHandlerGroupManager sut = newHandlerGroupManager();
EzySession session = mock(EzySession.class);
EzyChannel channel = mock(EzyChannel.class);
when(session.getChannel()).thenReturn(channel);
Object connection = new Object();
when(channel.getConnection()).thenReturn(connection);
// when
sut.mapSocketChannel(null, session);
// then
verify(session, times(1)).getChannel();
verify(channel, times(1)).getConnection();
}
use of com.tvd12.ezyfoxserver.socket.EzyChannel 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.EzyChannel in project ezyfox-server by youngmonkeys.
the class EzySimpleDataHandlerTest method checkMaxRequestPerSecondCase.
@SuppressWarnings("rawtypes")
@Test
public void checkMaxRequestPerSecondCase() throws Exception {
int zoneId = 1;
EzyServerContext serverContext = mock(EzyServerContext.class);
EzyZoneContext zoneContext = mock(EzyZoneContext.class);
EzyZone zone = mock(EzyZone.class);
when(zoneContext.getZone()).thenReturn(zone);
EzyZoneUserManager zoneUserManager = mock(EzyZoneUserManager.class);
when(zone.getUserManager()).thenReturn(zoneUserManager);
when(serverContext.getZoneContext(zoneId)).thenReturn(zoneContext);
EzyAbstractSession session = spy(EzyAbstractSession.class);
EzyChannel channel = mock(EzyChannel.class);
when(session.getChannel()).thenReturn(channel);
EzyServer server = mock(EzyServer.class);
when(serverContext.getServer()).thenReturn(server);
EzyServerControllers controllers = mock(EzyServerControllers.class);
EzyInterceptor streamingInterceptor = mock(EzyInterceptor.class);
when(controllers.getStreamingInterceptor()).thenReturn(streamingInterceptor);
EzyStreamingController streamingController = mock(EzyStreamingController.class);
when(controllers.getStreamingController()).thenReturn(streamingController);
EzyInterceptor loginInterceptor = mock(EzyInterceptor.class);
when(controllers.getInterceptor(EzyCommand.LOGIN)).thenReturn(loginInterceptor);
EzyController loginController = mock(EzyController.class);
when(controllers.getController(EzyCommand.LOGIN)).thenReturn(loginController);
when(server.getControllers()).thenReturn(controllers);
EzySessionManager sessionManager = mock(EzySessionManager.class);
when(server.getSessionManager()).thenReturn(sessionManager);
EzyCloseSession closeSession = mock(EzyCloseSession.class);
when(serverContext.get(EzyCloseSession.class)).thenReturn(closeSession);
EzySettings settings = mock(EzySettings.class);
when(settings.isDebug()).thenReturn(true);
when(server.getSettings()).thenReturn(settings);
EzySessionManagementSetting sessionManagementSetting = mock(EzySessionManagementSetting.class);
when(settings.getSessionManagement()).thenReturn(sessionManagementSetting);
EzyLoggerSetting loggerSetting = mock(EzyLoggerSetting.class);
when(settings.getLogger()).thenReturn(loggerSetting);
EzyLoggerSetting.EzyIgnoredCommandsSetting ignoredCommandsSetting = mock(EzyLoggerSetting.EzyIgnoredCommandsSetting.class);
when(loggerSetting.getIgnoredCommands()).thenReturn(ignoredCommandsSetting);
when(ignoredCommandsSetting.getCommands()).thenReturn(new HashSet<>());
EzySessionManagementSetting.EzyMaxRequestPerSecond maxRequestPerSecond = mock(EzySessionManagementSetting.EzyMaxRequestPerSecond.class);
when(maxRequestPerSecond.getValue()).thenReturn(3);
when(maxRequestPerSecond.getAction()).thenReturn(EzyMaxRequestPerSecondAction.DISCONNECT_SESSION);
when(sessionManagementSetting.getSessionMaxRequestPerSecond()).thenReturn(maxRequestPerSecond);
MyTestDataHandler handler = new MyTestDataHandler(serverContext, session);
when(session.getDelegate()).thenReturn(handler);
when(session.isActivated()).thenReturn(true);
EzyArray loginData = EzyEntityFactory.newArrayBuilder().append("zone").append("username").append("password").append(EzyEntityFactory.newObject()).build();
EzyArray requestData = EzyEntityFactory.newArrayBuilder().append(EzyCommand.LOGIN.getId()).append(loginData).build();
handler.dataReceived(EzyCommand.LOGIN, requestData);
EzySimpleUser user = new EzySimpleUser();
user.addSession(session);
user.setZoneId(zoneId);
handler.onSessionLoggedIn(user);
handler.streamingReceived(new byte[] { 1, 2, 3 });
EzyArray accessAppData = EzyEntityFactory.newArrayBuilder().append("app").append(EzyEntityFactory.newObject()).build();
EzyArray requestData2 = EzyEntityFactory.newArrayBuilder().append(EzyCommand.APP_ACCESS.getId()).append(accessAppData).build();
handler.dataReceived(EzyCommand.APP_ACCESS, requestData2);
handler.dataReceived(EzyCommand.LOGIN, requestData);
Thread.sleep(1200);
handler.dataReceived(EzyCommand.LOGIN, requestData);
}
use of com.tvd12.ezyfoxserver.socket.EzyChannel in project ezyfox-server by youngmonkeys.
the class EzyUserDataHandlerTest method createHandler.
@SuppressWarnings("rawtypes")
private MyTestDataHandler createHandler() {
int zoneId = 1;
EzyServerContext serverContext = mock(EzyServerContext.class);
EzyZoneContext zoneContext = mock(EzyZoneContext.class);
EzyZone zone = mock(EzyZone.class);
when(zoneContext.getZone()).thenReturn(zone);
EzyZoneUserManager zoneUserManager = mock(EzyZoneUserManager.class);
when(zone.getUserManager()).thenReturn(zoneUserManager);
when(serverContext.getZoneContext(zoneId)).thenReturn(zoneContext);
EzyAbstractSession session = spy(EzyAbstractSession.class);
EzyChannel channel = mock(EzyChannel.class);
when(session.getChannel()).thenReturn(channel);
EzyServer server = mock(EzyServer.class);
when(serverContext.getServer()).thenReturn(server);
EzyServerControllers controllers = mock(EzyServerControllers.class);
EzyInterceptor streamingInteceptor = mock(EzyInterceptor.class);
when(controllers.getStreamingInterceptor()).thenReturn(streamingInteceptor);
EzyStreamingController streamingController = mock(EzyStreamingController.class);
when(controllers.getStreamingController()).thenReturn(streamingController);
EzyInterceptor loginInteceptor = mock(EzyInterceptor.class);
when(controllers.getInterceptor(EzyCommand.LOGIN)).thenReturn(loginInteceptor);
EzyController loginController = mock(EzyController.class);
when(controllers.getController(EzyCommand.LOGIN)).thenReturn(loginController);
when(server.getControllers()).thenReturn(controllers);
EzySessionManager sessionManager = mock(EzySessionManager.class);
when(server.getSessionManager()).thenReturn(sessionManager);
EzyCloseSession closeSession = mock(EzyCloseSession.class);
when(serverContext.get(EzyCloseSession.class)).thenReturn(closeSession);
EzySettings settings = mock(EzySettings.class);
when(settings.isDebug()).thenReturn(true);
when(server.getSettings()).thenReturn(settings);
EzySessionManagementSetting sessionManagementSetting = mock(EzySessionManagementSetting.class);
when(settings.getSessionManagement()).thenReturn(sessionManagementSetting);
EzyLoggerSetting loggerSetting = mock(EzyLoggerSetting.class);
when(settings.getLogger()).thenReturn(loggerSetting);
EzyLoggerSetting.EzyIgnoredCommandsSetting ignoredCommandsSetting = mock(EzyLoggerSetting.EzyIgnoredCommandsSetting.class);
when(loggerSetting.getIgnoredCommands()).thenReturn(ignoredCommandsSetting);
when(ignoredCommandsSetting.getCommands()).thenReturn(Sets.newHashSet(EzyCommand.PING));
EzySessionManagementSetting.EzyMaxRequestPerSecond maxRequestPerSecond = mock(EzySessionManagementSetting.EzyMaxRequestPerSecond.class);
when(maxRequestPerSecond.getValue()).thenReturn(3);
when(maxRequestPerSecond.getAction()).thenReturn(EzyMaxRequestPerSecondAction.DISCONNECT_SESSION);
when(sessionManagementSetting.getSessionMaxRequestPerSecond()).thenReturn(maxRequestPerSecond);
return new MyTestDataHandler(serverContext, session);
}
Aggregations