use of com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver in project ezyfox-server by youngmonkeys.
the class EzyNioServerBootstrapTest method destroySocketDataReceiver.
@Test
public void destroySocketDataReceiver() {
// given
EzySocketDataReceiver dataReceiver = mock(EzySocketDataReceiver.class);
EzyNioServerBootstrap sut = new EzyNioServerBootstrap();
sut.setSocketDataReceiver(dataReceiver);
// when
sut.destroy();
// then
verify(dataReceiver, times(1)).destroy();
}
use of com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver 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.socket.EzySocketDataReceiver in project ezyfox-server by youngmonkeys.
the class EzySimpleNioUdpDataHandlerTest method handleReceivedUdpPacketWithSessionTest.
@Test
public void handleReceivedUdpPacketWithSessionTest() {
// 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);
Session session = mock(Session.class);
when(session.getRemoteAddress()).thenReturn(tcpAddress);
EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
when(handlerGroupManager.getSocketChannel(udpAddress)).thenReturn(session);
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(session, times(1)).getRemoteAddress();
verify(socketDataReceiver, times(1)).udpReceive(any(SocketChannel.class), any(EzyMessage.class));
sut.destroy();
}
use of com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver in project ezyfox-server by youngmonkeys.
the class EzyNioSocketReaderTest method testExceptionCase.
@Test
public void testExceptionCase() 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);
when(ownSelector.selectedKeys()).thenReturn(Sets.newHashSet(selectionKey1));
when(selectionKey1.isValid()).thenReturn(true);
when(selectionKey1.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 -> {
throw new IllegalStateException("server maintain");
});
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.socket.EzySocketDataReceiver in project ezyfox-server by youngmonkeys.
the class EzySocketDataReceiverTest method udpReceiveButHandlerGroupNullTest.
@Test
public void udpReceiveButHandlerGroupNullTest() 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);
// when
sut.udpReceive(channel, message);
Thread.sleep(120);
// then
verify(handlerGroupManager, times(1)).getHandlerGroup(channel);
sut.destroy();
}
Aggregations