Search in sources :

Example 1 with EzyPacket

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

the class EzyAbstractSessionTest method addPacketToSessionQueueWithDroppedPacketsNowIsNull.

@Test
public void addPacketToSessionQueueWithDroppedPacketsNowIsNull() {
    // given
    EzyPacket packet = mock(EzyPacket.class);
    MyTestSession session = new MyTestSession();
    session.setActivated(true);
    EzyPacketQueue packetQueue = mock(EzyPacketQueue.class);
    when(packetQueue.isEmpty()).thenReturn(true);
    when(packetQueue.add(packet)).thenReturn(false);
    session.setPacketQueue(packetQueue);
    // when
    session.send(packet);
    // then
    Asserts.assertNull(session.getDroppedPackets());
}
Also used : MyTestSession(com.tvd12.ezyfoxserver.testing.MyTestSession) BaseCoreTest(com.tvd12.ezyfoxserver.testing.BaseCoreTest) Test(org.testng.annotations.Test)

Example 2 with EzyPacket

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

the class EzyAbstractHandlerGroupTest method executeSendingPacketCanNotWriteBytes.

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

Example 3 with EzyPacket

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

the class EzySimpleNioHandlerGroupTest method writePacketToSocketSessionKeyIsInvalid.

@Test
public void writePacketToSocketSessionKeyIsInvalid() throws Exception {
    // given
    EzySimpleNioHandlerGroup sut = newHandlerGroup();
    EzyNioSession session = FieldUtil.getFieldValue(sut, "session");
    SelectionKey selectionKey = mock(SelectionKey.class);
    when(session.getSelectionKey()).thenReturn(selectionKey);
    when(selectionKey.isValid()).thenReturn(false);
    EzyPacket packet = mock(EzyPacket.class);
    when(packet.getData()).thenReturn(new byte[] { 1, 2, 3, 5 });
    when(packet.isBinary()).thenReturn(true);
    ByteBuffer writeBuffer = ByteBuffer.allocate(5);
    // when
    MethodInvoker.create().object(sut).method("writePacketToSocket").param(EzyPacket.class, packet).param(Object.class, writeBuffer).call();
    // then
    verify(session, times(1)).getSelectionKey();
    verify(selectionKey, times(1)).isValid();
}
Also used : SelectionKey(java.nio.channels.SelectionKey) EzySimpleNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession) ByteBuffer(java.nio.ByteBuffer) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 4 with EzyPacket

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

the class EzyPacketTest method test.

@Test
public void test() {
    EzyPacket packet = new EzySimplePacket();
    packet.setFragment(new byte[] { 1, 2, 3 });
    long start = System.currentTimeMillis();
    for (int i = 0; i < 1000000; ++i) {
        packet.getSize();
    }
    long offset = System.currentTimeMillis() - start;
    System.out.println(offset);
}
Also used : EzyPacket(com.tvd12.ezyfoxserver.socket.EzyPacket) EzySimplePacket(com.tvd12.ezyfoxserver.socket.EzySimplePacket) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 5 with EzyPacket

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

the class EzySocketWriterTest method test.

@Test
public void test() throws Exception {
    EzySessionTicketsQueue sessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzySocketWriterGroupFetcher writerGroupFetcher = mock(EzySocketWriterGroupFetcher.class);
    EzySocketWriterGroup writerGroup = mock(EzySocketWriterGroup.class);
    when(writerGroupFetcher.getWriterGroup(any(EzySession.class))).thenReturn(writerGroup);
    EzySocketWriter socketWriter = new EzySocketWriter();
    socketWriter.setSessionTicketsQueue(sessionTicketsQueue);
    socketWriter.setWriterGroupFetcher(writerGroupFetcher);
    EzyPacketQueue packetQueue = new EzyNonBlockingPacketQueue();
    EzyPacket packet = new EzySimplePacket();
    packetQueue.add(packet);
    packetQueue.add(packet);
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    session.setActivated(true);
    session.setSessionTicketsQueue(sessionTicketsQueue);
    session.setPacketQueue(packetQueue);
    sessionTicketsQueue.add(session);
    socketWriter.handleEvent();
    packet.release();
    socketWriter.handleEvent();
    socketWriter.destroy();
}
Also used : EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)12 BaseTest (com.tvd12.test.base.BaseTest)10 ByteBuffer (java.nio.ByteBuffer)6 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)5 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)4 EzyNioSession (com.tvd12.ezyfoxserver.nio.entity.EzyNioSession)4 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)3 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)3 EzySimpleSessionTokenGenerator (com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)3 EzySimpleSessionManagementSetting (com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting)3 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)3 EzySimpleStreamingSetting (com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting)3 EzySimpleStatistics (com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics)3 EzyStatistics (com.tvd12.ezyfoxserver.statistics.EzyStatistics)3 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)3 ExecutorService (java.util.concurrent.ExecutorService)3 EzyHandlerGroupBuilderFactory (com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory)2 EzySimpleNioHandlerGroup (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup)2 EzyPacket (com.tvd12.ezyfoxserver.socket.EzyPacket)2 EzySimplePacket (com.tvd12.ezyfoxserver.socket.EzySimplePacket)2