Search in sources :

Example 21 with ImmutableSession

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

the class OOBSessionTestCase method getAttribute.

@Test
public void getAttribute() {
    Batcher<Batch> batcher = mock(Batcher.class);
    Batch batch = mock(Batch.class);
    String attributeName = "foo";
    when(this.manager.getBatcher()).thenReturn(batcher);
    when(batcher.createBatch()).thenReturn(batch);
    when(this.manager.readSession(this.id)).thenReturn(null);
    Assert.assertThrows(IllegalStateException.class, () -> this.session.getAttributes().getAttribute(attributeName));
    verify(batch).close();
    reset(batch);
    ImmutableSession session = mock(ImmutableSession.class);
    ImmutableSessionAttributes attributes = mock(ImmutableSessionAttributes.class);
    Object expected = new Object();
    when(this.manager.readSession(this.id)).thenReturn(session);
    when(session.getAttributes()).thenReturn(attributes);
    when(attributes.getAttribute(attributeName)).thenReturn(expected);
    Object result = this.session.getAttributes().getAttribute(attributeName);
    Assert.assertSame(expected, result);
    verify(batch).close();
}
Also used : ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Batch(org.wildfly.clustering.ee.Batch) Test(org.junit.Test)

Example 22 with ImmutableSession

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

the class UndertowSessionExpirationListener method sessionExpired.

@Override
public void sessionExpired(ImmutableSession session) {
    UndertowSessionManager manager = (UndertowSessionManager) this.deployment.getSessionManager();
    Session undertowSession = new DistributableImmutableSession(manager, session);
    Batcher<Batch> batcher = manager.getSessionManager().getBatcher();
    // Perform listener invocation in isolated batch context
    Batch batch = batcher.suspendBatch();
    try {
        this.listeners.sessionDestroyed(undertowSession, null, SessionListener.SessionDestroyedReason.TIMEOUT);
    } finally {
        batcher.resumeBatch(batch);
    }
    // Trigger attribute listeners
    ImmutableSessionAttributes attributes = session.getAttributes();
    for (String name : attributes.getAttributeNames()) {
        Object value = attributes.getAttribute(name);
        manager.getSessionListeners().attributeRemoved(undertowSession, name, value);
    }
}
Also used : ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) Batch(org.wildfly.clustering.ee.Batch) Session(io.undertow.server.session.Session) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession)

Example 23 with ImmutableSession

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

the class OOBSessionTestCase method isExpired.

@Test
public void isExpired() {
    Batcher<Batch> batcher = mock(Batcher.class);
    Batch batch = mock(Batch.class);
    when(this.manager.getBatcher()).thenReturn(batcher);
    when(batcher.createBatch()).thenReturn(batch);
    when(this.manager.readSession(this.id)).thenReturn(null);
    Assert.assertThrows(IllegalStateException.class, this.session.getMetaData()::isExpired);
    verify(batch).close();
    reset(batch);
    ImmutableSession session = mock(ImmutableSession.class);
    ImmutableSessionMetaData metaData = mock(ImmutableSessionMetaData.class);
    boolean expected = true;
    when(this.manager.readSession(this.id)).thenReturn(session);
    when(session.getMetaData()).thenReturn(metaData);
    when(metaData.isExpired()).thenReturn(expected);
    boolean result = this.session.getMetaData().isExpired();
    Assert.assertEquals(expected, result);
    verify(batch).close();
}
Also used : ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Batch(org.wildfly.clustering.ee.Batch) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) Test(org.junit.Test)

Example 24 with ImmutableSession

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

the class ExpiredSessionRemoverTestCase method test.

@Test
public void test() {
    SessionFactory<Object, UUID, UUID, Object> factory = mock(SessionFactory.class);
    SessionMetaDataFactory<UUID> metaDataFactory = mock(SessionMetaDataFactory.class);
    SessionAttributesFactory<Object, 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();
    ExpiredSessionRemover<Object, UUID, UUID, Object> subject = new ExpiredSessionRemover<>(factory);
    try (Registration regisration = subject.register(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) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) Registration(org.wildfly.clustering.Registration) UUID(java.util.UUID) Test(org.junit.Test)

Example 25 with ImmutableSession

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

the class ExpiredSessionRemover method remove.

@Override
public boolean remove(String id) {
    MV metaDataValue = this.factory.getMetaDataFactory().tryValue(id);
    if (metaDataValue != null) {
        ImmutableSessionMetaData metaData = this.factory.getMetaDataFactory().createImmutableSessionMetaData(id, metaDataValue);
        if (metaData.isExpired()) {
            AV attributesValue = this.factory.getAttributesFactory().findValue(id);
            if (attributesValue != null) {
                ImmutableSessionAttributes attributes = this.factory.getAttributesFactory().createImmutableSessionAttributes(id, attributesValue);
                ImmutableSession session = this.factory.createImmutableSession(id, metaData, attributes);
                InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s has expired.", id);
                for (SessionExpirationListener listener : this.listeners) {
                    listener.sessionExpired(session);
                }
            }
            return this.factory.remove(id);
        }
        InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s is not yet expired.", id);
    } else {
        InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s was not found or is currently in use.", id);
    }
    return false;
}
Also used : ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData)

Aggregations

ImmutableSession (org.wildfly.clustering.web.session.ImmutableSession)35 Test (org.junit.Test)27 Batch (org.wildfly.clustering.ee.Batch)13 ImmutableSessionMetaData (org.wildfly.clustering.web.session.ImmutableSessionMetaData)13 ServletContext (javax.servlet.ServletContext)12 ImmutableSessionAttributes (org.wildfly.clustering.web.session.ImmutableSessionAttributes)9 Map (java.util.Map)7 Instant (java.time.Instant)5 SessionExpirationListener (org.wildfly.clustering.web.session.SessionExpirationListener)4 SessionMetaData (org.wildfly.clustering.web.session.SessionMetaData)4 Session (io.undertow.server.session.Session)2 Duration (java.time.Duration)2 SimpleImmutableSession (org.wildfly.clustering.web.cache.session.SimpleImmutableSession)2 ValidSession (org.wildfly.clustering.web.cache.session.ValidSession)2 SessionAttributes (org.wildfly.clustering.web.session.SessionAttributes)2 SessionListener (io.undertow.server.session.SessionListener)1 SessionListeners (io.undertow.server.session.SessionListeners)1 Deployment (io.undertow.servlet.api.Deployment)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1