use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.
the class EzyHandlerGroupManagerImplTest method newHandlerGroupManager.
public 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).socketSessionTicketsQueue(webSocketSessionTicketsQueue).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
return EzyHandlerGroupManagerImpl.builder().handlerGroupBuilderFactory(handlerGroupBuilderFactory).build();
}
use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager 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.nio.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.
the class EzySimpleNioUdpDataHandlerTest method testExceptionCase.
@Test
public void testExceptionCase() 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;
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);
when(handlerGroupManager.getHandlerGroup(packet.getAddress())).thenThrow(new IllegalArgumentException("test"));
handler.setHandlerGroupManager(handlerGroupManager);
handler.fireUdpPacketReceived(packet);
Thread.sleep(200);
handler.destroy();
}
use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.
the class EzySimpleNioUdpDataHandlerTest method handleReceivedUdpPacketTest.
@Test
public void handleReceivedUdpPacketTest() throws Exception {
// given
EzySimpleNioUdpDataHandler sut = new EzySimpleNioUdpDataHandler(1);
byte[] messageBytes = messageBytes();
InetSocketAddress udpAddress = new InetSocketAddress(3005);
InetSocketAddress tcpAddress = new InetSocketAddress(123);
EzyUdpReceivedPacket packet = mock(EzyUdpReceivedPacket.class);
when(packet.getBytes()).thenReturn(messageBytes);
when(packet.getAddress()).thenReturn(udpAddress);
SocketChannel socketChannel = mock(SocketChannel.class);
when(socketChannel.getRemoteAddress()).thenReturn(tcpAddress);
EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
when(handlerGroupManager.getSocketChannel(udpAddress)).thenReturn(socketChannel);
sut.setHandlerGroupManager(handlerGroupManager);
EzySocketDataReceiver socketDataReceiver = mock(EzySocketDataReceiver.class);
sut.setSocketDataReceiver(socketDataReceiver);
// when
MethodInvoker.create().object(sut).method("handleReceivedUdpPacket").param(EzyUdpReceivedPacket.class, packet).call();
// then
verify(handlerGroupManager, times(1)).getSocketChannel(udpAddress);
verify(socketChannel, times(1)).getRemoteAddress();
verify(socketDataReceiver, times(1)).udpReceive(any(SocketChannel.class), any(EzyMessage.class));
sut.destroy();
}
use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.
the class EzySimpleNioUdpDataHandlerTest method handleReceivedUdpPacketIsNotOneClientTest.
@Test
public void handleReceivedUdpPacketIsNotOneClientTest() throws Exception {
// given
EzySimpleNioUdpDataHandler sut = new EzySimpleNioUdpDataHandler(1);
byte[] messageBytes = messageBytes();
InetSocketAddress udpAddress = new InetSocketAddress("client1", 3005);
InetSocketAddress tcpAddress = new InetSocketAddress("client2", 123);
EzyUdpReceivedPacket packet = mock(EzyUdpReceivedPacket.class);
when(packet.getBytes()).thenReturn(messageBytes);
when(packet.getAddress()).thenReturn(udpAddress);
SocketChannel socketChannel = mock(SocketChannel.class);
when(socketChannel.getRemoteAddress()).thenReturn(tcpAddress);
EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
when(handlerGroupManager.getSocketChannel(udpAddress)).thenReturn(socketChannel);
sut.setHandlerGroupManager(handlerGroupManager);
// when
MethodInvoker.create().object(sut).method("handleReceivedUdpPacket").param(EzyUdpReceivedPacket.class, packet).call();
// then
verify(handlerGroupManager, times(1)).getSocketChannel(udpAddress);
verify(socketChannel, times(1)).getRemoteAddress();
sut.destroy();
}
Aggregations