Search in sources :

Example 6 with ImmutableSessionMetaData

use of org.wildfly.clustering.web.session.ImmutableSessionMetaData in project wildfly by wildfly.

the class ExpiredSessionRemoverTestCase method test.

@Test
public void test() {
    SessionFactory<UUID, UUID, Object> factory = mock(SessionFactory.class);
    SessionMetaDataFactory<UUID, Object> metaDataFactory = mock(SessionMetaDataFactory.class);
    SessionAttributesFactory<UUID> attributesFactory = mock(SessionAttributesFactory.class);
    SessionExpirationListener listener = mock(SessionExpirationListener.class);
    ImmutableSessionAttributes expiredAttributes = mock(ImmutableSessionAttributes.class);
    ImmutableSessionMetaData validMetaData = mock(ImmutableSessionMetaData.class);
    ImmutableSessionMetaData expiredMetaData = mock(ImmutableSessionMetaData.class);
    ImmutableSession expiredSession = mock(ImmutableSession.class);
    String missingSessionId = "missing";
    String expiredSessionId = "expired";
    String validSessionId = "valid";
    UUID expiredMetaDataValue = UUID.randomUUID();
    UUID expiredAttributesValue = UUID.randomUUID();
    UUID validMetaDataValue = UUID.randomUUID();
    Remover<String> subject = new ExpiredSessionRemover<>(factory, listener);
    when(factory.getMetaDataFactory()).thenReturn(metaDataFactory);
    when(factory.getAttributesFactory()).thenReturn(attributesFactory);
    when(metaDataFactory.tryValue(missingSessionId)).thenReturn(null);
    when(metaDataFactory.tryValue(expiredSessionId)).thenReturn(expiredMetaDataValue);
    when(metaDataFactory.tryValue(validSessionId)).thenReturn(validMetaDataValue);
    when(metaDataFactory.createImmutableSessionMetaData(expiredSessionId, expiredMetaDataValue)).thenReturn(expiredMetaData);
    when(metaDataFactory.createImmutableSessionMetaData(validSessionId, validMetaDataValue)).thenReturn(validMetaData);
    when(expiredMetaData.isExpired()).thenReturn(true);
    when(validMetaData.isExpired()).thenReturn(false);
    when(attributesFactory.findValue(expiredSessionId)).thenReturn(expiredAttributesValue);
    when(attributesFactory.createImmutableSessionAttributes(expiredSessionId, expiredAttributesValue)).thenReturn(expiredAttributes);
    when(factory.createImmutableSession(same(expiredSessionId), same(expiredMetaData), same(expiredAttributes))).thenReturn(expiredSession);
    subject.remove(missingSessionId);
    subject.remove(expiredSessionId);
    subject.remove(validSessionId);
    verify(factory).remove(expiredSessionId);
    verify(factory, never()).remove(missingSessionId);
    verify(factory, never()).remove(validSessionId);
    verify(listener).sessionExpired(expiredSession);
}
Also used : ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) UUID(java.util.UUID) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) Test(org.junit.Test)

Example 7 with ImmutableSessionMetaData

use of org.wildfly.clustering.web.session.ImmutableSessionMetaData in project wildfly by wildfly.

the class InfinispanSessionFactoryTestCase method createImmutableSession.

@Test
public void createImmutableSession() {
    Map.Entry<InfinispanSessionMetaData<Object>, Object> entry = mock(Map.Entry.class);
    SessionCreationMetaData creationMetaData = mock(SessionCreationMetaData.class);
    SessionAccessMetaData accessMetaData = mock(SessionAccessMetaData.class);
    InfinispanSessionMetaData<Object> metaDataValue = new InfinispanSessionMetaData<>(creationMetaData, accessMetaData, null);
    Object attributesValue = new Object();
    ImmutableSessionMetaData metaData = mock(ImmutableSessionMetaData.class);
    ImmutableSessionAttributes attributes = mock(ImmutableSessionAttributes.class);
    String id = "id";
    when(entry.getKey()).thenReturn(metaDataValue);
    when(entry.getValue()).thenReturn(attributesValue);
    when(this.metaDataFactory.createImmutableSessionMetaData(id, metaDataValue)).thenReturn(metaData);
    when(this.attributesFactory.createImmutableSessionAttributes(id, attributesValue)).thenReturn(attributes);
    ImmutableSession result = this.factory.createImmutableSession(id, entry);
    assertSame(id, result.getId());
    assertSame(metaData, result.getMetaData());
    assertSame(attributes, result.getAttributes());
}
Also used : ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) Map(java.util.Map) Test(org.junit.Test)

Example 8 with ImmutableSessionMetaData

use of org.wildfly.clustering.web.session.ImmutableSessionMetaData in project wildfly by wildfly.

the class SessionEvictionSchedulerTestCase method test.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void test() throws Exception {
    String name = "cache";
    String evictedSessionId = "evicted";
    String activeSessionId = "active";
    ImmutableSessionMetaData evictedSessionMetaData = mock(ImmutableSessionMetaData.class);
    ImmutableSessionMetaData activeSessionMetaData = mock(ImmutableSessionMetaData.class);
    CommandDispatcherFactory dispatcherFactory = mock(CommandDispatcherFactory.class);
    CommandDispatcher<SessionEvictionContext> dispatcher = mock(CommandDispatcher.class);
    Evictor<String> evictor = mock(Evictor.class);
    Batcher<TransactionBatch> batcher = mock(Batcher.class);
    TransactionBatch batch = mock(TransactionBatch.class);
    ArgumentCaptor<Command> capturedCommand = ArgumentCaptor.forClass(Command.class);
    ArgumentCaptor<SessionEvictionContext> capturedContext = ArgumentCaptor.forClass(SessionEvictionContext.class);
    when(dispatcherFactory.createCommandDispatcher(same(name), capturedContext.capture())).thenReturn(dispatcher);
    try (Scheduler scheduler = new SessionEvictionScheduler(name, evictor, batcher, dispatcherFactory, 1)) {
        SessionEvictionContext context = capturedContext.getValue();
        assertSame(scheduler, context);
        scheduler.schedule(evictedSessionId, evictedSessionMetaData);
        verifyZeroInteractions(dispatcher);
        scheduler.schedule(activeSessionId, activeSessionMetaData);
        verify(dispatcher).submitOnCluster(capturedCommand.capture());
        when(batcher.createBatch()).thenReturn(batch);
        capturedCommand.getValue().execute(context);
        verify(evictor).evict(evictedSessionId);
        verify(batch).close();
        verify(evictor, never()).evict(activeSessionId);
    }
    verify(dispatcher).close();
}
Also used : TransactionBatch(org.wildfly.clustering.ee.infinispan.TransactionBatch) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) Command(org.wildfly.clustering.dispatcher.Command) CommandDispatcherFactory(org.wildfly.clustering.dispatcher.CommandDispatcherFactory) Test(org.junit.Test)

Example 9 with ImmutableSessionMetaData

use of org.wildfly.clustering.web.session.ImmutableSessionMetaData in project wildfly by wildfly.

the class SessionExpirationSchedulerTestCase method test.

@Test
public void test() throws InterruptedException {
    Batcher<TransactionBatch> batcher = mock(Batcher.class);
    TransactionBatch batch = mock(TransactionBatch.class);
    Remover<String> remover = mock(Remover.class);
    ImmutableSessionMetaData immortalSessionMetaData = mock(ImmutableSessionMetaData.class);
    ImmutableSessionMetaData expiringSessionMetaData = mock(ImmutableSessionMetaData.class);
    ImmutableSessionMetaData canceledSessionMetaData = mock(ImmutableSessionMetaData.class);
    String immortalSessionId = "immortal";
    String expiringSessionId = "expiring";
    String canceledSessionId = "canceled";
    when(batcher.createBatch()).thenReturn(batch);
    when(immortalSessionMetaData.getMaxInactiveInterval()).thenReturn(Duration.ZERO);
    when(expiringSessionMetaData.getMaxInactiveInterval()).thenReturn(Duration.ofMillis(1L));
    when(canceledSessionMetaData.getMaxInactiveInterval()).thenReturn(Duration.ofSeconds(100L));
    Instant now = Instant.now();
    when(expiringSessionMetaData.getLastAccessedTime()).thenReturn(now);
    when(canceledSessionMetaData.getLastAccessedTime()).thenReturn(now);
    try (Scheduler scheduler = new SessionExpirationScheduler(batcher, remover)) {
        scheduler.schedule(immortalSessionId, immortalSessionMetaData);
        scheduler.schedule(canceledSessionId, canceledSessionMetaData);
        scheduler.schedule(expiringSessionId, expiringSessionMetaData);
        TimeUnit.SECONDS.sleep(1L);
        scheduler.cancel(canceledSessionId);
        scheduler.schedule(canceledSessionId, canceledSessionMetaData);
    }
    verify(remover, never()).remove(immortalSessionId);
    verify(remover).remove(expiringSessionId);
    verify(remover, never()).remove(canceledSessionId);
    verify(batch).close();
}
Also used : TransactionBatch(org.wildfly.clustering.ee.infinispan.TransactionBatch) Instant(java.time.Instant) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) Test(org.junit.Test)

Aggregations

ImmutableSessionMetaData (org.wildfly.clustering.web.session.ImmutableSessionMetaData)9 Test (org.junit.Test)7 ImmutableSession (org.wildfly.clustering.web.session.ImmutableSession)7 ImmutableSessionAttributes (org.wildfly.clustering.web.session.ImmutableSessionAttributes)6 Instant (java.time.Instant)3 Batch (org.wildfly.clustering.ee.Batch)3 Duration (java.time.Duration)2 Map (java.util.Map)2 TransactionBatch (org.wildfly.clustering.ee.infinispan.TransactionBatch)2 SessionExpirationListener (org.wildfly.clustering.web.session.SessionExpirationListener)2 HttpServerExchange (io.undertow.server.HttpServerExchange)1 Session (io.undertow.server.session.Session)1 SessionListener (io.undertow.server.session.SessionListener)1 SessionListeners (io.undertow.server.session.SessionListeners)1 Deployment (io.undertow.servlet.api.Deployment)1 PrivilegedAction (java.security.PrivilegedAction)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Set (java.util.Set)1