use of com.tvd12.ezyfoxserver.wrapper.EzySessionManager 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.wrapper.EzySessionManager in project ezyfox-server by youngmonkeys.
the class EzySimpleNioHandlerGroupTest method builderTest.
@Test
public void builderTest() {
// given
EzySimpleSettings settings = new EzySimpleSettings();
EzySimpleServer server = new EzySimpleServer();
server.setSettings(settings);
@SuppressWarnings("rawtypes") EzySessionManager sessionManager = EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
server.setSessionManager(sessionManager);
EzySimpleServerContext serverContext = new EzySimpleServerContext();
serverContext.setServer(server);
serverContext.init();
EzySessionTicketsQueue sessionTicketsQueue = mock(EzySessionTicketsQueue.class);
EzySocketDisconnectionQueue disconnectionQueue = mock(EzySocketDisconnectionQueue.class);
Object connection = new Object();
EzyChannel channel = mock(EzyChannel.class);
when(channel.getConnection()).thenReturn(connection);
EzyNioSession session = (EzyNioSession) sessionManager.provideSession(channel);
EzySessionStats sessionStats = mock(EzySessionStats.class);
doNothing().when(sessionStats).setCurrentSessions(anyInt());
// when
EzySimpleNioHandlerGroup sut = (EzySimpleNioHandlerGroup) EzySimpleNioHandlerGroup.builder().session(session).serverContext(serverContext).sessionCount(new AtomicInteger()).sessionStats(sessionStats).sessionTicketsQueue(sessionTicketsQueue).disconnectionQueue(disconnectionQueue).build();
// then
Asserts.assertEquals(FieldUtil.getFieldValue(sut, "disconnectionQueue"), disconnectionQueue);
Asserts.assertEquals(FieldUtil.getFieldValue(sut, "sessionTicketsQueue"), sessionTicketsQueue);
}
use of com.tvd12.ezyfoxserver.wrapper.EzySessionManager in project ezyfox-server by youngmonkeys.
the class EzySimpleNioHandlerGroupTest method writeSuccessTest.
@SuppressWarnings("rawtypes")
@Test
public void writeSuccessTest() 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);
ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
when(channel.write(any(ByteBuffer.class), anyBoolean())).thenReturn(123456);
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).session(session).decoder(decoder).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
EzySimplePacket packet = new EzySimplePacket();
packet.setBinary(true);
packet.setData("world".getBytes());
packet.setTransportType(EzyTransportType.TCP);
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
group.firePacketSend(packet, writeBuffer);
Thread.sleep(1000);
}
use of com.tvd12.ezyfoxserver.wrapper.EzySessionManager in project ezyfox-server by youngmonkeys.
the class EzySimpleNioHandlerGroupTest method writeFailureTest.
@SuppressWarnings("rawtypes")
@Test
public void writeFailureTest() 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);
ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
SelectionKey selectionKey = mock(SelectionKey.class);
when(selectionKey.isValid()).thenReturn(true);
session.setProperty(EzyNioSession.SELECTION_KEY, selectionKey);
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).session(session).decoder(decoder).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
EzySimplePacket packet = new EzySimplePacket();
packet.setBinary(true);
packet.setData("world".getBytes());
packet.setTransportType(EzyTransportType.TCP);
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
group.firePacketSend(packet, writeBuffer);
Thread.sleep(1000);
}
use of com.tvd12.ezyfoxserver.wrapper.EzySessionManager in project ezyfox-server by youngmonkeys.
the class EzySimpleNioUdpDataHandlerTest method handleUdpHandshakeSessionIsNull.
@SuppressWarnings("rawtypes")
@Test
public void handleUdpHandshakeSessionIsNull() {
// given
EzySimpleNioUdpDataHandler sut = new EzySimpleNioUdpDataHandler(1);
DatagramChannel channel = mock(DatagramChannel.class);
InetSocketAddress address = new InetSocketAddress(3005);
EzyMessage message = mock(EzyMessage.class);
String sessionToken = RandomUtil.randomAlphabetString(8);
long sessionId = 12345L;
int tokenSize = sessionToken.length();
int messageSize = 0;
// sessionIdSize
messageSize += 8;
// tokenLengthSize
messageSize += 2;
// messageSize
messageSize += tokenSize;
ByteBuffer buffer = ByteBuffer.allocate(messageSize);
buffer.putLong(sessionId);
buffer.putShort((short) tokenSize);
buffer.put(sessionToken.getBytes());
buffer.flip();
byte[] bytes = EzyByteBuffers.getBytes(buffer);
when(message.getContent()).thenReturn(bytes);
EzySessionManager sessionManager = mock(EzySessionManager.class);
sut.setSessionManager(sessionManager);
// when
MethodInvoker.create().object(sut).method("handleUdpHandshake").param(DatagramChannel.class, channel).param(InetSocketAddress.class, address).param(EzyMessage.class, message).call();
// then
verify(message, times(1)).getContent();
verify(sessionManager, times(1)).getSession(sessionId);
sut.destroy();
}
Aggregations