Search in sources :

Example 6 with EzyMessage

use of com.tvd12.ezyfox.codec.EzyMessage in project ezyfox-server by youngmonkeys.

the class EzySimpleNioUdpDataHandlerTest method handleUdpHandshakeSessionTokenIsNotMatch.

@SuppressWarnings("rawtypes")
@Test
public void handleUdpHandshakeSessionTokenIsNotMatch() throws Exception {
    // 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);
    EzySession session = mock(EzySession.class);
    when(session.getToken()).thenReturn(RandomUtil.randomShortAlphabetString());
    when(sessionManager.getSession(sessionId)).thenReturn(session);
    EzyResponseApi responseApi = mock(EzyResponseApi.class);
    sut.setResponseApi(responseApi);
    // 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);
    verify(session, times(1)).getToken();
    verify(responseApi, times(1)).response(any());
    sut.destroy();
}
Also used : EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzyMessage(com.tvd12.ezyfox.codec.EzyMessage) ByteBuffer(java.nio.ByteBuffer) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) Test(org.testng.annotations.Test)

Example 7 with EzyMessage

use of com.tvd12.ezyfox.codec.EzyMessage 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();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) EzyMessage(com.tvd12.ezyfox.codec.EzyMessage) Test(org.testng.annotations.Test)

Example 8 with EzyMessage

use of com.tvd12.ezyfox.codec.EzyMessage in project ezyfox-server by youngmonkeys.

the class EzySocketDataReceiverTest method udpReceiveTest.

@Test
public void udpReceiveTest() 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);
    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();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) EzyMessage(com.tvd12.ezyfox.codec.EzyMessage) EzyNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup) Test(org.testng.annotations.Test)

Example 9 with EzyMessage

use of com.tvd12.ezyfox.codec.EzyMessage in project ezyfox-server by youngmonkeys.

the class EzySimpleNioHandlerGroupTest method handleReceivedMessageStreamingNotEnable.

@Test
public void handleReceivedMessageStreamingNotEnable() throws Exception {
    // given
    EzySimpleNioHandlerGroup sut = newHandlerGroup(false);
    EzyMessage message = mock(EzyMessage.class);
    EzyMessageHeader header = mock(EzyMessageHeader.class);
    when(header.isRawBytes()).thenReturn(true);
    when(message.getHeader()).thenReturn(header);
    // when
    MethodInvoker.create().object(sut).method("handleReceivedMessage").param(EzyMessage.class, message).call();
    // then
    verify(header, times(1)).isRawBytes();
}
Also used : EzySimpleNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 10 with EzyMessage

use of com.tvd12.ezyfox.codec.EzyMessage in project ezyfox-server by youngmonkeys.

the class EzySimpleNioHandlerGroupTest method handleReceivedMessageSessionStreamingNotEnable.

@Test
public void handleReceivedMessageSessionStreamingNotEnable() throws Exception {
    // given
    EzySimpleNioHandlerGroup sut = newHandlerGroup();
    EzyMessage message = mock(EzyMessage.class);
    EzyMessageHeader header = mock(EzyMessageHeader.class);
    when(header.isRawBytes()).thenReturn(true);
    when(message.getHeader()).thenReturn(header);
    EzySession session = FieldUtil.getFieldValue(sut, "session");
    when(session.isStreamingEnable()).thenReturn(false);
    // when
    MethodInvoker.create().object(sut).method("handleReceivedMessage").param(EzyMessage.class, message).call();
    // then
    verify(header, times(1)).isRawBytes();
    verify(session, times(1)).isStreamingEnable();
}
Also used : EzySimpleNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Aggregations

Test (org.testng.annotations.Test)10 EzyMessage (com.tvd12.ezyfox.codec.EzyMessage)8 InetSocketAddress (java.net.InetSocketAddress)6 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)4 EzySimpleNioUdpDataHandler (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler)4 DatagramChannel (java.nio.channels.DatagramChannel)4 SocketChannel (java.nio.channels.SocketChannel)4 EzySimpleNioHandlerGroup (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup)3 EzySocketDataReceiver (com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver)3 EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)3 BaseTest (com.tvd12.test.base.BaseTest)3 ByteBuffer (java.nio.ByteBuffer)3 EzyNioHandlerGroup (com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup)2 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)2 SocketAddress (java.net.SocketAddress)2 EzyMessageHeader (com.tvd12.ezyfox.codec.EzyMessageHeader)1 EzyResponseApi (com.tvd12.ezyfoxserver.api.EzyResponseApi)1 EzySimpleSocketStream (com.tvd12.ezyfoxserver.socket.EzySimpleSocketStream)1 EzySocketStream (com.tvd12.ezyfoxserver.socket.EzySocketStream)1