use of com.tvd12.ezyfoxserver.wrapper.EzySessionManager in project ezyfox-server by youngmonkeys.
the class EzySimpleDataHandlerTest method normalCase.
@SuppressWarnings("rawtypes")
@Test
public void normalCase() {
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);
handler.dataReceived(EzyCommand.LOGIN, requestData);
handler.exceptionCaught(new EzyMaxRequestSizeException(1024, 512));
handler.channelInactive(EzyDisconnectReason.ADMIN_BAN);
handler.channelInactive(EzyDisconnectReason.ADMIN_BAN);
handler.destroy();
System.out.println(handler);
}
use of com.tvd12.ezyfoxserver.wrapper.EzySessionManager in project ezyfox-server by youngmonkeys.
the class EzyAbstractHandlerGroupTest method fireExceptionCaughtException.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void fireExceptionCaughtException() throws Exception {
// given
ExHandlerGroup sut = newHandlerGroup();
EzySession session = FieldUtil.getFieldValue(sut, "session");
EzyNioDataHandler handler = FieldUtil.getFieldValue(sut, "handler");
EzySessionManager sessionManager = FieldUtil.getFieldValue(handler, "sessionManager");
doThrow(new RuntimeException()).when(sessionManager).removeSession(session, MAX_REQUEST_SIZE);
EzyMaxRequestSizeException exception = new EzyMaxRequestSizeException("just test");
// when
sut.fireExceptionCaught(exception);
// then
verify(sessionManager, times(1)).removeSession(session, MAX_REQUEST_SIZE);
}
use of com.tvd12.ezyfoxserver.wrapper.EzySessionManager in project ezyfox-server by youngmonkeys.
the class EzySimpleNioUdpDataHandlerTest method test.
@SuppressWarnings("rawtypes")
@Test
public void test() throws Exception {
String sessionToken = "12345678";
long sessionId = 12345L;
EzySimpleNioUdpDataHandler handler = new EzySimpleNioUdpDataHandler(1);
int tokenSize = sessionToken.length();
int messageSize = 0;
// sessionIdSize
messageSize += 8;
// tokenLengthSize
messageSize += 2;
// messageSize
messageSize += tokenSize;
ByteBuffer buffer = ByteBuffer.allocate(1 + 2 + messageSize);
byte header = 0;
header |= 1 << 5;
buffer.put(header);
buffer.putShort((short) messageSize);
buffer.putLong(sessionId);
buffer.putShort((short) tokenSize);
buffer.put(sessionToken.getBytes());
buffer.flip();
byte[] bytes = EzyByteBuffers.getBytes(buffer);
EzyUdpReceivedPacket packet = new EzySimpleUdpReceivedPacket(DatagramChannel.open(), new InetSocketAddress(12345), bytes);
EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
EzySessionManager sessionManager = mock(EzySessionManager.class);
EzySession session = spy(EzyAbstractSession.class);
session.setToken(sessionToken);
when(sessionManager.getSession(sessionId)).thenReturn(session);
EzyResponseApi responseApi = mock(EzyResponseApi.class);
EzyDatagramChannelPool channelPool = new EzyDatagramChannelPool(1);
handler.setHandlerGroupManager(handlerGroupManager);
handler.setSessionManager(sessionManager);
handler.setResponseApi(responseApi);
handler.setDatagramChannelPool(channelPool);
handler.fireUdpPacketReceived(packet);
Thread.sleep(200);
handler.destroy();
}
use of com.tvd12.ezyfoxserver.wrapper.EzySessionManager 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.wrapper.EzySessionManager in project ezyfox-server by youngmonkeys.
the class EzyBootstrap method startSessionManager.
@SuppressWarnings("rawtypes")
protected void startSessionManager() throws Exception {
EzySessionManager sessionManager = context.getServer().getSessionManager();
((EzyStartable) sessionManager).start();
}
Aggregations