Search in sources :

Example 1 with MyTestSessionManager

use of com.tvd12.ezyfoxserver.testing.MyTestSessionManager in project ezyfox-server by youngmonkeys.

the class EzySimpleSessionManagerTest method testNormalCase.

@Test
public void testNormalCase() throws Exception {
    MyTestSessionManager manager = (MyTestSessionManager) new MyTestSessionManager.Builder().validationDelay(5).validationInterval(5).build();
    MyTestSession session = manager.provideSession(EzyConnectionType.SOCKET);
    manager.removeSession(session);
    session = manager.provideSession(EzyConnectionType.SOCKET);
    session.setDelegate(new EzyAbstractSessionDelegate() {
    });
    manager.removeSession(session, EzyDisconnectReason.IDLE);
    manager.removeSession(null, EzyDisconnectReason.IDLE);
    session = manager.provideSession(EzyConnectionType.SOCKET);
    session.setDelegate(new EzyAbstractSessionDelegate() {
    });
    session.setLoggedIn(true);
    manager.addLoggedInSession(session);
    assertEquals(manager.getLoggedInSessions(), Lists.newArrayList(session));
    session = manager.provideSession(EzyConnectionType.SOCKET);
    session.setActivated(true);
    session.setLoggedIn(false);
    session.setCreationTime(System.currentTimeMillis() - 1000);
    session.setMaxWaitingTime(100);
    session = manager.provideSession(EzyConnectionType.SOCKET);
    session.setLoggedIn(false);
    session.setCreationTime(System.currentTimeMillis());
    session.setMaxWaitingTime(10000);
    assert manager.getAllSessionCount() == 5;
    assert manager.getAliveSessionCount() == 5;
    assert manager.getLoggedInSessionCount() == 1;
    session = manager.provideSession(EzyConnectionType.SOCKET);
    session.setActivated(true);
    session.setLoggedIn(true);
    session.setLastReadTime(0);
    session.setCreationTime(System.currentTimeMillis());
    session.setMaxWaitingTime(10000);
    try {
        manager.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Thread.sleep(300);
    Object connection = new Object();
    EzyChannel channel = mock(EzyChannel.class);
    when(channel.getConnection()).thenReturn(connection);
    when(channel.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
    session = manager.provideSession(channel);
    assert manager.getSession(session.getId()) == session;
    assert manager.getSession(connection) == session;
    manager.clearSession(session);
    Thread.sleep(300);
    manager.destroy();
}
Also used : EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel) EzyAbstractSessionDelegate(com.tvd12.ezyfoxserver.delegate.EzyAbstractSessionDelegate) MyTestSessionManager(com.tvd12.ezyfoxserver.testing.MyTestSessionManager) MyTestSession(com.tvd12.ezyfoxserver.testing.MyTestSession) EzyMaxSessionException(com.tvd12.ezyfoxserver.exception.EzyMaxSessionException) BaseCoreTest(com.tvd12.ezyfoxserver.testing.BaseCoreTest) Test(org.testng.annotations.Test)

Example 2 with MyTestSessionManager

use of com.tvd12.ezyfoxserver.testing.MyTestSessionManager in project ezyfox-server by youngmonkeys.

the class EzySimpleSessionManagerTest method test1.

@Test
public void test1() {
    MyTestSessionManager manager = (MyTestSessionManager) new MyTestSessionManager.Builder().tokenGenerator(() -> UUID.randomUUID().toString()).build();
    MyTestSession session = manager.provideSession(EzyConnectionType.SOCKET);
    session.setLoggedIn(true);
    manager.addLoggedInSession(session);
    MyTestSession session2 = manager.provideSession(EzyConnectionType.SOCKET);
    assert session.getId() != session2.getId();
    assertEquals(manager.getAllSessions(), Lists.newArrayList(session, session2));
}
Also used : MyTestSessionManager(com.tvd12.ezyfoxserver.testing.MyTestSessionManager) MyTestSession(com.tvd12.ezyfoxserver.testing.MyTestSession) BaseCoreTest(com.tvd12.ezyfoxserver.testing.BaseCoreTest) Test(org.testng.annotations.Test)

Example 3 with MyTestSessionManager

use of com.tvd12.ezyfoxserver.testing.MyTestSessionManager in project ezyfox-server by youngmonkeys.

the class EzySimpleSessionManagerTest method isUnloggedInSessionFalseTest.

@Test
public void isUnloggedInSessionFalseTest() {
    // given
    MyTestSessionManager manager = new MyTestSessionManager.Builder().build();
    EzySession session = mock(EzySession.class);
    when(session.isLoggedIn()).thenReturn(false);
    when(session.isActivated()).thenReturn(true);
    when(session.getMaxWaitingTime()).thenReturn(1000L);
    when(session.getCreationTime()).thenReturn(System.currentTimeMillis() - 100L);
    // when
    Boolean result = MethodInvoker.create().object(manager).method("isUnloggedInSession").param(EzySession.class, session).invoke(Boolean.class);
    // then
    Asserts.assertFalse(result);
}
Also used : MyTestSessionManager(com.tvd12.ezyfoxserver.testing.MyTestSessionManager) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) BaseCoreTest(com.tvd12.ezyfoxserver.testing.BaseCoreTest) Test(org.testng.annotations.Test)

Example 4 with MyTestSessionManager

use of com.tvd12.ezyfoxserver.testing.MyTestSessionManager in project ezyfox-server by youngmonkeys.

the class EzySimpleSessionManagerTest method checkMaxSessionsTest.

@Test
public void checkMaxSessionsTest() {
    // given
    MyTestSessionManager manager = (MyTestSessionManager) new MyTestSessionManager.Builder().maxSessions(0).build();
    // when
    Throwable e = Asserts.assertThrows(() -> manager.provideSession(EzyConnectionType.SOCKET));
    // then
    Asserts.assertEquals(EzyMaxSessionException.class, e.getClass());
}
Also used : MyTestSessionManager(com.tvd12.ezyfoxserver.testing.MyTestSessionManager) BaseCoreTest(com.tvd12.ezyfoxserver.testing.BaseCoreTest) Test(org.testng.annotations.Test)

Example 5 with MyTestSessionManager

use of com.tvd12.ezyfoxserver.testing.MyTestSessionManager in project ezyfox-server by youngmonkeys.

the class EzySimpleSessionManagerTest method clearNullSessionTest.

@Test
public void clearNullSessionTest() {
    // given
    MyTestSessionManager manager = new MyTestSessionManager.Builder().build();
    // when
    // then
    manager.clearSession(null);
}
Also used : MyTestSessionManager(com.tvd12.ezyfoxserver.testing.MyTestSessionManager) BaseCoreTest(com.tvd12.ezyfoxserver.testing.BaseCoreTest) Test(org.testng.annotations.Test)

Aggregations

BaseCoreTest (com.tvd12.ezyfoxserver.testing.BaseCoreTest)6 MyTestSessionManager (com.tvd12.ezyfoxserver.testing.MyTestSessionManager)6 Test (org.testng.annotations.Test)6 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)2 MyTestSession (com.tvd12.ezyfoxserver.testing.MyTestSession)2 EzyAbstractSessionDelegate (com.tvd12.ezyfoxserver.delegate.EzyAbstractSessionDelegate)1 EzyMaxSessionException (com.tvd12.ezyfoxserver.exception.EzyMaxSessionException)1 EzyChannel (com.tvd12.ezyfoxserver.socket.EzyChannel)1