use of org.wildfly.clustering.web.session.ImmutableSessionMetaData in project wildfly by wildfly.
the class RecordableInactiveSessionStatisticsTestCase method test.
@Test
public void test() {
ImmutableSession session = mock(ImmutableSession.class);
ImmutableSessionMetaData metaData = mock(ImmutableSessionMetaData.class);
Instant now = Instant.now();
Instant created = now.minus(Duration.ofMinutes(20L));
when(session.getMetaData()).thenReturn(metaData);
when(metaData.isExpired()).thenReturn(false);
when(metaData.getCreationTime()).thenReturn(created);
this.statistics.record(session);
assertEquals(0L, this.statistics.getExpiredSessionCount());
assertEquals(20L, this.statistics.getMaxSessionLifetime().toMinutes());
assertEquals(20L, this.statistics.getMeanSessionLifetime().toMinutes());
now = Instant.now();
created = now.minus(Duration.ofMinutes(10L));
when(metaData.isExpired()).thenReturn(true);
when(metaData.getCreationTime()).thenReturn(created);
this.statistics.record(session);
assertEquals(1L, this.statistics.getExpiredSessionCount());
assertEquals(20L, this.statistics.getMaxSessionLifetime().toMinutes());
assertEquals(15L, this.statistics.getMeanSessionLifetime().toMinutes());
this.statistics.reset();
assertEquals(0L, this.statistics.getExpiredSessionCount());
assertEquals(0L, this.statistics.getMaxSessionLifetime().toMinutes());
assertEquals(0L, this.statistics.getMeanSessionLifetime().toMinutes());
}
use of org.wildfly.clustering.web.session.ImmutableSessionMetaData in project wildfly by wildfly.
the class DistributableSessionManagerTestCase method getSessionByIdentifier.
@Test
public void getSessionByIdentifier() {
Batcher<Batch> batcher = mock(Batcher.class);
Batch batch = mock(Batch.class);
ImmutableSession session = mock(ImmutableSession.class);
ImmutableSessionAttributes attributes = mock(ImmutableSessionAttributes.class);
ImmutableSessionMetaData metaData = mock(ImmutableSessionMetaData.class);
String id = "session";
String name = "name";
Object value = new Object();
Set<String> names = Collections.singleton(name);
Instant creationTime = Instant.now();
Instant lastAccessedTime = Instant.now();
Duration maxInactiveInterval = Duration.ofMinutes(30L);
when(this.manager.getBatcher()).thenReturn(batcher);
when(this.manager.viewSession(id)).thenReturn(session);
when(session.getId()).thenReturn(id);
when(session.getAttributes()).thenReturn(attributes);
when(attributes.getAttributeNames()).thenReturn(names);
when(attributes.getAttribute(name)).thenReturn(value);
when(session.getMetaData()).thenReturn(metaData);
when(metaData.getCreationTime()).thenReturn(creationTime);
when(metaData.getLastAccessedTime()).thenReturn(lastAccessedTime);
when(metaData.getMaxInactiveInterval()).thenReturn(maxInactiveInterval);
when(batcher.createBatch()).thenReturn(batch);
io.undertow.server.session.Session result = this.adapter.getSession(id);
assertSame(this.adapter, result.getSessionManager());
assertSame(id, result.getId());
assertEquals(creationTime.toEpochMilli(), result.getCreationTime());
assertEquals(lastAccessedTime.toEpochMilli(), result.getLastAccessedTime());
assertEquals(maxInactiveInterval.getSeconds(), result.getMaxInactiveInterval());
assertEquals(names, result.getAttributeNames());
assertSame(value, result.getAttribute(name));
verify(batch).close();
}
use of org.wildfly.clustering.web.session.ImmutableSessionMetaData in project wildfly by wildfly.
the class InfinispanSessionManager method start.
@Override
public void start() {
this.executor = Executors.newSingleThreadExecutor(createThreadFactory());
if (this.recorder != null) {
this.recorder.reset();
}
this.identifierFactory.start();
final List<Scheduler> schedulers = new ArrayList<>(2);
schedulers.add(new SessionExpirationScheduler(this.batcher, new ExpiredSessionRemover<>(this.factory, this.expirationListener)));
if (this.maxActiveSessions >= 0) {
schedulers.add(new SessionEvictionScheduler(this.cache.getName() + ".eviction", this.factory, this.batcher, this.dispatcherFactory, this.maxActiveSessions));
}
this.scheduler = new Scheduler() {
@Override
public void schedule(String sessionId, ImmutableSessionMetaData metaData) {
schedulers.forEach(scheduler -> scheduler.schedule(sessionId, metaData));
}
@Override
public void cancel(String sessionId) {
schedulers.forEach(scheduler -> scheduler.cancel(sessionId));
}
@Override
public void cancel(Locality locality) {
schedulers.forEach(scheduler -> scheduler.cancel(locality));
}
@Override
public void close() {
schedulers.forEach(scheduler -> scheduler.close());
}
};
this.dispatcher = this.dispatcherFactory.createCommandDispatcher(this.cache.getName() + ".schedulers", this.scheduler);
this.cache.addListener(this, this.filter);
this.schedule(new SimpleLocality(false), this.locality);
}
use of org.wildfly.clustering.web.session.ImmutableSessionMetaData in project wildfly by wildfly.
the class UndertowSessionExpirationListenerTestCase method sessionExpired.
@Test
public void sessionExpired() {
Deployment deployment = mock(Deployment.class);
UndertowSessionManager manager = mock(UndertowSessionManager.class);
SessionManager<LocalSessionContext, Batch> delegateManager = mock(SessionManager.class);
Batcher<Batch> batcher = mock(Batcher.class);
Batch batch = mock(Batch.class);
SessionListener listener = mock(SessionListener.class);
ImmutableSession session = mock(ImmutableSession.class);
ImmutableSessionAttributes attributes = mock(ImmutableSessionAttributes.class);
ImmutableSessionMetaData metaData = mock(ImmutableSessionMetaData.class);
ArgumentCaptor<Session> capturedSession = ArgumentCaptor.forClass(Session.class);
String expectedSessionId = "session";
SessionListeners listeners = new SessionListeners();
listeners.addSessionListener(listener);
SessionExpirationListener expirationListener = new UndertowSessionExpirationListener(deployment, listeners);
when(deployment.getSessionManager()).thenReturn(manager);
when(manager.getSessionManager()).thenReturn(delegateManager);
when(delegateManager.getBatcher()).thenReturn(batcher);
when(batcher.suspendBatch()).thenReturn(batch);
when(session.getId()).thenReturn(expectedSessionId);
when(session.getAttributes()).thenReturn(attributes);
when(attributes.getAttributeNames()).thenReturn(Collections.emptySet());
when(session.getMetaData()).thenReturn(metaData);
when(metaData.getCreationTime()).thenReturn(Instant.now());
when(metaData.getLastAccessedTime()).thenReturn(Instant.now());
when(metaData.getMaxInactiveInterval()).thenReturn(Duration.ZERO);
expirationListener.sessionExpired(session);
verify(batcher).suspendBatch();
verify(listener).sessionDestroyed(capturedSession.capture(), isNull(HttpServerExchange.class), same(SessionListener.SessionDestroyedReason.TIMEOUT));
verify(batcher).resumeBatch(batch);
assertSame(expectedSessionId, capturedSession.getValue().getId());
assertSame(manager, capturedSession.getValue().getSessionManager());
}
use of org.wildfly.clustering.web.session.ImmutableSessionMetaData 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);
this.listener.sessionExpired(session);
}
return this.factory.remove(id);
}
}
return false;
}
Aggregations