Search in sources :

Example 1 with EzyDatagramChannelPool

use of com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool 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();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) ByteBuffer(java.nio.ByteBuffer) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) EzyDatagramChannelPool(com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool) EzySimpleUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) Test(org.testng.annotations.Test)

Example 2 with EzyDatagramChannelPool

use of com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool in project ezyfox-server by youngmonkeys.

the class EzyDatagramChannelPoolTest method test.

@Test
public void test() {
    EzyDatagramChannelPool pool = new EzyDatagramChannelPool(16);
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", 9005);
    try {
        pool.bind(address);
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        Selector selector = Selector.open();
        pool.register(selector);
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        pool.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assert pool.getChannel() != null;
}
Also used : EzyDatagramChannelPool(com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) Selector(java.nio.channels.Selector) Test(org.testng.annotations.Test)

Example 3 with EzyDatagramChannelPool

use of com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool in project ezyfox-server by youngmonkeys.

the class EzyAbstractSessionTest method test.

@SuppressWarnings({ "unlikely-arg-type", "EqualsWithItself", "ConstantConditions", "EqualsBetweenInconvertibleTypes" })
@Test
public void test() {
    MyTestSession session = new MyTestSession();
    session.setId(100);
    session.setPrivateKey(new byte[] { 1, 2, 3 });
    session.addReadBytes(10);
    session.addWrittenBytes(10);
    session.setLoggedInTime(12345);
    session.setMaxWaitingTime(123);
    session.setMaxIdleTime(12345L);
    assertEquals(session.getPrivateKey(), new byte[] { 1, 2, 3 });
    assertEquals(session.getReadBytes(), 10);
    assertEquals(session.getWrittenBytes(), 10);
    assertEquals(session.getLoggedInTime(), 12345L);
    assertEquals(session.getMaxWaitingTime(), 123L);
    MyTestSession session2 = new MyTestSession();
    session2.setId(1);
    MyTestSession session3 = new MyTestSession();
    session3.setId(100);
    assert !session.equals(null);
    assert session.equals(session);
    assert !session.equals(new Object());
    assert !session.equals(session2);
    assert !session.equals(new PrivateSession());
    assert session.equals(session3);
    assert session.hashCode() != session2.hashCode();
    session.setClientId("clientId");
    assert session.getClientId().equals("clientId");
    session.setOwnerName("owner");
    assert session.getOwnerName().equals("owner");
    session.setLastReadTime(1);
    assert session.getLastReadTime() == 1;
    session.setLastWriteTime(2);
    assert session.getLastWriteTime() == 2;
    session.setLastActivityTime(3);
    assert session.getLastActivityTime() == 3;
    session.setReadRequests(4);
    assert session.getReadRequests() == 4;
    session.setWrittenResponses(5);
    assert session.getWrittenResponses() == 5;
    session.addWrittenResponses(1);
    assert session.getWrittenResponses() == 6;
    session.setDestroyed(false);
    assert !session.isDestroyed();
    session.setStreamingEnable(true);
    assert session.isStreamingEnable();
    session.setClientType("android");
    assert session.getClientType().equals("android");
    session.setClientVersion("1.0.0");
    assert session.getClientVersion().equals("1.0.0");
    session.setBeforeToken("before");
    assert session.getBeforeToken().equals("before");
    session.setDisconnectReason(EzyDisconnectReason.ADMIN_BAN);
    assert session.getDisconnectReason() == EzyDisconnectReason.ADMIN_BAN;
    session.setMaxIdleTime(6);
    assert session.getMaxIdleTime() == 6;
    session.setDroppedPackets(mock(EzyDroppedPackets.class));
    assert session.getDroppedPackets() != null;
    session.setImmediateDeliver(mock(EzyImmediateDeliver.class));
    assert session.getImmediateDeliver() != null;
    EzySessionTicketsQueue sessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    session.setSessionTicketsQueue(sessionTicketsQueue);
    assert session.getSessionTicketsQueue() == sessionTicketsQueue;
    EzySocketDisconnectionQueue disconnectionQueue = mock(EzySocketDisconnectionQueue.class);
    session.setDisconnectionQueue(disconnectionQueue);
    assert session.getDisconnectionQueue() == disconnectionQueue;
    assert !session.isDisconnectionRegistered();
    assert session.getDisconnectionLock() != null;
    assert session.getLocks().isEmpty();
    assert session.getLock("test") != null;
    assert !session.isActivated();
    session.send(mock(EzyPacket.class));
    session.setActivated(true);
    EzyPacketQueue packetQueue = new EzyNonBlockingPacketQueue(3);
    session.setPacketQueue(packetQueue);
    session.send(mock(EzyPacket.class));
    assert session.getPacketQueue() != null;
    session.send(mock(EzyPacket.class));
    session.sendNow(mock(EzyPacket.class));
    session.send(mock(EzyPacket.class));
    session.send(mock(EzyPacket.class));
    session.send(mock(EzyPacket.class));
    assert session.getChannel() == null;
    assert session.getConnection() == null;
    session.disconnect();
    session.close();
    session.destroy();
    session.destroy();
    assert session.isDestroyed();
    PrivateSession privateSession = new PrivateSession();
    privateSession.setDisconnectionQueue(disconnectionQueue);
    privateSession.disconnect();
    privateSession.disconnect();
    assert privateSession.getServerAddress() == null;
    EzyChannel channel = mock(EzyChannel.class);
    when(channel.getServerAddress()).thenReturn(new InetSocketAddress(2233));
    privateSession.setChannel(channel);
    assert privateSession.getServerAddress() != null;
    privateSession.close();
    assert privateSession.getConnection() == null;
    assert privateSession.getUdpClientAddress() == null;
    assert privateSession.getDatagramChannel() == null;
    assert privateSession.getDatagramChannelPool() == null;
    privateSession.setUdpClientAddress(new InetSocketAddress(12345));
    assert privateSession.getUdpClientAddress() != null;
    try {
        privateSession.setDatagramChannel(DatagramChannel.open());
        assert privateSession.getDatagramChannel() != null;
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        privateSession.setDatagramChannelPool(new EzyDatagramChannelPool(2));
        assert privateSession.getDatagramChannelPool() != null;
    } catch (Exception e) {
        e.printStackTrace();
    }
    privateSession.setRequestFrameInSecond(new EzyRequestFrameSecond(2));
    Asserts.assertFalse(privateSession.addReceivedRequests(1));
    Asserts.assertTrue(privateSession.addReceivedRequests(3));
    privateSession.setRequestFrameInSecond(new EzyRequestFrameSecond(5, System.currentTimeMillis() - 2 * 1000));
    Asserts.assertFalse(privateSession.addReceivedRequests(1));
    Asserts.assertFalse(privateSession.addReceivedRequests(3));
    Asserts.assertFalse(privateSession.getRequestFrameInSecond().isExpired());
    byte[] clientKey = RandomUtil.randomShortByteArray();
    privateSession.setClientKey(clientKey);
    Asserts.assertEquals(clientKey, privateSession.getClientKey());
    byte[] sessionKey = RandomUtil.randomShortByteArray();
    privateSession.setSessionKey(sessionKey);
    Asserts.assertEquals(sessionKey, privateSession.getSessionKey());
    EzyRequestQueue systemRquestQueue = mock(EzyRequestQueue.class);
    privateSession.setSystemRequestQueue(systemRquestQueue);
    Asserts.assertEquals(systemRquestQueue, privateSession.getSystemRequestQueue());
    EzyRequestQueue extensionRequestQueue = mock(EzyRequestQueue.class);
    privateSession.setExtensionRequestQueue(extensionRequestQueue);
    Asserts.assertEquals(extensionRequestQueue, privateSession.getExtensionRequestQueue());
    privateSession.destroy();
}
Also used : EzyDroppedPackets(com.tvd12.ezyfoxserver.entity.EzyDroppedPackets) InetSocketAddress(java.net.InetSocketAddress) MyTestSession(com.tvd12.ezyfoxserver.testing.MyTestSession) EzyImmediateDeliver(com.tvd12.ezyfoxserver.entity.EzyImmediateDeliver) EzyRequestFrameSecond(com.tvd12.ezyfoxserver.statistics.EzyRequestFrameSecond) BaseCoreTest(com.tvd12.ezyfoxserver.testing.BaseCoreTest) Test(org.testng.annotations.Test)

Example 4 with EzyDatagramChannelPool

use of com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool in project ezyfox-server by youngmonkeys.

the class EzyDatagramChannelPoolTest method testBindRegisterCloseChannelException.

@Test
public void testBindRegisterCloseChannelException() {
    EzyDatagramChannelPool pool = new EzyDatagramChannelPool(2) {

        @Override
        protected DatagramChannel openChannel() throws IOException {
            MyDatagramChannel1 channel = spy(MyDatagramChannel1.class);
            DatagramSocket socket = new DatagramSocket();
            when(channel.socket()).thenReturn(socket);
            return channel;
        }
    };
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", 9005);
    try {
        pool.bind(address);
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        Selector selector = Selector.open();
        pool.register(selector);
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        pool.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : EzyDatagramChannelPool(com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) Selector(java.nio.channels.Selector) Test(org.testng.annotations.Test)

Example 5 with EzyDatagramChannelPool

use of com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool in project ezyfox-server by youngmonkeys.

the class EzyAbstractHandlerGroupTest method writeUdpPacketToSocketClientAddress.

@Test
public void writeUdpPacketToSocketClientAddress() throws Exception {
    // given
    ExHandlerGroup sut = newHandlerGroup();
    EzyPacket packet = mock(EzyPacket.class);
    ByteBuffer writeBuffer = ByteBuffer.wrap(new byte[0]);
    EzyDatagramChannelPool udpChannelPool = mock(EzyDatagramChannelPool.class);
    EzySession session = FieldUtil.getFieldValue(sut, "session");
    when(session.getDatagramChannelPool()).thenReturn(udpChannelPool);
    // when
    MethodInvoker.create().object(sut).method("writeUdpPacketToSocket").param(EzyPacket.class, packet).param(Object.class, writeBuffer).call();
    // then
    verify(session, times(1)).getDatagramChannelPool();
}
Also used : ByteBuffer(java.nio.ByteBuffer) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Aggregations

Test (org.testng.annotations.Test)6 InetSocketAddress (java.net.InetSocketAddress)5 EzyDatagramChannelPool (com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool)3 ByteBuffer (java.nio.ByteBuffer)3 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)2 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)2 BaseTest (com.tvd12.test.base.BaseTest)2 IOException (java.io.IOException)2 Selector (java.nio.channels.Selector)2 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)1 EzyResponseApi (com.tvd12.ezyfoxserver.api.EzyResponseApi)1 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)1 EzyDroppedPackets (com.tvd12.ezyfoxserver.entity.EzyDroppedPackets)1 EzyImmediateDeliver (com.tvd12.ezyfoxserver.entity.EzyImmediateDeliver)1 EzyNioSession (com.tvd12.ezyfoxserver.nio.entity.EzyNioSession)1 EzySimpleNioUdpDataHandler (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler)1 ExEzyByteToObjectDecoder (com.tvd12.ezyfoxserver.nio.testing.handler.EzySimpleNioHandlerGroupTest.ExEzyByteToObjectDecoder)1 EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)1 EzySimpleSessionTokenGenerator (com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)1 EzySimpleSessionManagementSetting (com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting)1