use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager 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.nio.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.
the class EzyNioSocketAcceptorTest 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.nio.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.
the class EzyNioSocketAcceptorTest method acceptConnectionTest.
@Test
public void acceptConnectionTest() throws Exception {
// given
EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
EzyNioHandlerGroup handlerGroup = mock(EzyNioHandlerGroup.class);
Selector readSelector = Selector.open();
EzyNioSocketAcceptor sut = new EzyNioSocketAcceptor();
sut.setReadSelector(readSelector);
SocketChannel clientChannel = SocketChannel.open();
when(handlerGroupManager.newHandlerGroup(any(), any())).thenReturn(handlerGroup);
sut.setHandlerGroupManager(handlerGroupManager);
EzyNioSession session = mock(EzyNioSession.class);
when(handlerGroup.getSession()).thenReturn(session);
// when
MethodInvoker.create().object(sut).method("acceptConnection").param(SocketChannel.class, clientChannel).call();
// then
verify(handlerGroupManager, times(1)).newHandlerGroup(any(), any());
}
use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.
the class EzyNioSocketReaderTest method test.
@Test
public void test() throws Exception {
EzyHandlerGroupManager handlerGroupManager = newHandlerGroupManager();
EzySocketDataReceiver socketDataReceiver = EzySocketDataReceiver.builder().threadPoolSize(1).handlerGroupManager(handlerGroupManager).build();
Selector ownSelector = spy(ExSelector.class);
when(ownSelector.selectNow()).thenReturn(1);
SelectionKey selectionKey1 = spy(ExSelectionKey.class);
SelectionKey selectionKey2 = spy(ExSelectionKey.class);
SelectionKey selectionKey3 = spy(ExSelectionKey.class);
SelectionKey selectionKey4 = spy(ExSelectionKey.class);
SelectionKey selectionKey5 = spy(ExSelectionKey.class);
when(ownSelector.selectedKeys()).thenReturn(Sets.newHashSet(selectionKey1, selectionKey2, selectionKey3, selectionKey4, selectionKey5));
when(selectionKey1.isValid()).thenReturn(true);
when(selectionKey1.readyOps()).thenReturn(SelectionKey.OP_READ);
when(selectionKey2.isValid()).thenReturn(true);
when(selectionKey2.readyOps()).thenReturn(SelectionKey.OP_WRITE);
when(selectionKey3.isValid()).thenReturn(false);
when(selectionKey4.isValid()).thenReturn(true);
when(selectionKey4.readyOps()).thenReturn(SelectionKey.OP_READ);
when(selectionKey5.isValid()).thenReturn(true);
when(selectionKey5.readyOps()).thenReturn(SelectionKey.OP_READ);
SocketChannel socketChannel1 = mock(SocketChannel.class);
EzyChannel channel1 = new EzyNioSocketChannel(socketChannel1);
handlerGroupManager.newHandlerGroup(channel1, EzyConnectionType.SOCKET);
when(selectionKey1.channel()).thenReturn(socketChannel1);
when(socketChannel1.isConnected()).thenReturn(true);
when(socketChannel1.read(any(ByteBuffer.class))).then((Answer<Integer>) invocation -> {
ByteBuffer buffer = invocation.getArgumentAt(0, ByteBuffer.class);
buffer.put("hello".getBytes());
return "hello".length();
});
SocketChannel socketChannel4 = mock(SocketChannel.class);
when(selectionKey4.channel()).thenReturn(socketChannel4);
SocketChannel socketChannel5 = spy(ExSocketChannel.class);
EzyChannel channel5 = new EzyNioSocketChannel(socketChannel5);
when(selectionKey5.channel()).thenReturn(socketChannel5);
doNothing().when(socketChannel5).close();
handlerGroupManager.newHandlerGroup(channel5, EzyConnectionType.SOCKET);
when(selectionKey5.channel()).thenReturn(socketChannel5);
when(socketChannel5.isConnected()).thenReturn(true);
when(socketChannel5.read(any(ByteBuffer.class))).then((Answer<Integer>) invocation -> -1);
EzyNioSocketAcceptor socketAcceptor = new EzyNioSocketAcceptor();
socketAcceptor.setReadSelector(ownSelector);
socketAcceptor.setHandlerGroupManager(handlerGroupManager);
socketAcceptor.setAcceptableConnections(new ArrayList<>());
EzyNioSocketReader socketReader = new EzyNioSocketReader();
socketReader.setOwnSelector(ownSelector);
socketReader.setAcceptableConnectionsHandler(socketAcceptor);
socketReader.setSocketDataReceiver(socketDataReceiver);
socketReader.handleEvent();
}
use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.
the class EzySocketDataReceiverTest method udpReceiveButExceptionTest.
@Test
public void udpReceiveButExceptionTest() throws Exception {
// given
EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
EzySocketDataReceiver sut = EzySocketDataReceiver.builder().handlerGroupManager(handlerGroupManager).threadPoolSize(1).build();
SocketChannel channel = mock(SocketChannel.class);
EzyMessage message = mock(EzyMessage.class);
EzyNioHandlerGroup handlerGroup = mock(EzyNioHandlerGroup.class);
doThrow(new RuntimeException()).when(handlerGroup).fireMessageReceived(message);
when(handlerGroupManager.getHandlerGroup(channel)).thenReturn(handlerGroup);
// when
sut.udpReceive(channel, message);
Thread.sleep(120);
// then
verify(handlerGroupManager, times(1)).getHandlerGroup(channel);
verify(handlerGroup, times(1)).fireMessageReceived(message);
sut.destroy();
}
Aggregations