Search in sources :

Example 1 with TransactionBatch

use of org.wildfly.clustering.ee.cache.tx.TransactionBatch in project wildfly by wildfly.

the class BeanExpirationSchedulerTestCase method testExpire.

@Test
public void testExpire() throws InterruptedException {
    Group group = mock(Group.class);
    Batcher<TransactionBatch> batcher = mock(Batcher.class);
    TransactionBatch batch = mock(TransactionBatch.class);
    BeanFactory<String, Object> factory = mock(BeanFactory.class);
    ExpirationConfiguration<Object> config = mock(ExpirationConfiguration.class);
    RemoveListener<Object> listener = mock(RemoveListener.class);
    BeanEntry<String> entry = mock(BeanEntry.class);
    BeanRemover<String, Object> remover = mock(BeanRemover.class);
    String beanId = "expiring";
    Duration timeout = Duration.ofMillis(10L);
    when(group.isSingleton()).thenReturn(true);
    when(batcher.createBatch()).thenReturn(batch);
    when(config.getTimeout()).thenReturn(timeout);
    when(config.getRemoveListener()).thenReturn(listener);
    when(entry.getLastAccessedTime()).thenReturn(Instant.now());
    when(remover.remove(beanId, listener)).thenReturn(true);
    try (Scheduler<String, ImmutableBeanEntry<String>> scheduler = new BeanExpirationScheduler<>(group, batcher, factory, config, remover, Duration.ZERO)) {
        scheduler.schedule(beanId, entry);
        Thread.sleep(500);
    }
    verify(batch).close();
}
Also used : Group(org.wildfly.clustering.group.Group) TransactionBatch(org.wildfly.clustering.ee.cache.tx.TransactionBatch) Duration(java.time.Duration) Test(org.junit.Test)

Example 2 with TransactionBatch

use of org.wildfly.clustering.ee.cache.tx.TransactionBatch 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);
    ImmutableSessionMetaDataFactory<Object> metaDataFactory = mock(ImmutableSessionMetaDataFactory.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.getLastAccessEndTime()).thenReturn(now);
    when(canceledSessionMetaData.getLastAccessEndTime()).thenReturn(now);
    when(remover.remove(expiringSessionId)).thenReturn(true);
    try (Scheduler<String, SessionExpirationMetaData> scheduler = new SessionExpirationScheduler<>(batcher, metaDataFactory, remover, Duration.ZERO)) {
        scheduler.schedule(immortalSessionId, immortalSessionMetaData);
        scheduler.schedule(canceledSessionId, canceledSessionMetaData);
        scheduler.schedule(expiringSessionId, expiringSessionMetaData);
        scheduler.cancel(canceledSessionId);
        TimeUnit.MILLISECONDS.sleep(500);
    }
    verify(remover, never()).remove(immortalSessionId);
    verify(remover, never()).remove(canceledSessionId);
    verify(batch).close();
}
Also used : TransactionBatch(org.wildfly.clustering.ee.cache.tx.TransactionBatch) Instant(java.time.Instant) SessionExpirationMetaData(org.wildfly.clustering.web.session.SessionExpirationMetaData) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) Test(org.junit.Test)

Example 3 with TransactionBatch

use of org.wildfly.clustering.ee.cache.tx.TransactionBatch in project wildfly by wildfly.

the class BeanExpirationSchedulerTestCase method testImmortal.

@Test
public void testImmortal() throws InterruptedException {
    Group group = mock(Group.class);
    Batcher<TransactionBatch> batcher = mock(Batcher.class);
    BeanFactory<String, Object> factory = mock(BeanFactory.class);
    ExpirationConfiguration<Object> config = mock(ExpirationConfiguration.class);
    RemoveListener<Object> listener = mock(RemoveListener.class);
    BeanEntry<String> entry = mock(BeanEntry.class);
    BeanRemover<String, Object> remover = mock(BeanRemover.class);
    String beanId = "immortal";
    when(group.isSingleton()).thenReturn(true);
    // Fun fact: the Jakarta Enterprise Beans specification allows a timeout value of 0, so only negative timeouts are treated as immortal
    when(config.getTimeout()).thenReturn(Duration.ofMinutes(-1L));
    when(config.getRemoveListener()).thenReturn(listener);
    when(entry.getLastAccessedTime()).thenReturn(Instant.now());
    try (Scheduler<String, ImmutableBeanEntry<String>> scheduler = new BeanExpirationScheduler<>(group, batcher, factory, config, remover, Duration.ZERO)) {
        scheduler.schedule(beanId, entry);
        Thread.sleep(500);
    }
    verify(batcher, never()).createBatch();
    verify(remover, never()).remove(beanId, listener);
}
Also used : Group(org.wildfly.clustering.group.Group) TransactionBatch(org.wildfly.clustering.ee.cache.tx.TransactionBatch) Test(org.junit.Test)

Example 4 with TransactionBatch

use of org.wildfly.clustering.ee.cache.tx.TransactionBatch in project wildfly by wildfly.

the class BeanExpirationSchedulerTestCase method testNotYetExpired.

@Test
public void testNotYetExpired() throws InterruptedException {
    Group group = mock(Group.class);
    Batcher<TransactionBatch> batcher = mock(Batcher.class);
    TransactionBatch batch = mock(TransactionBatch.class);
    BeanFactory<String, Object> factory = mock(BeanFactory.class);
    ExpirationConfiguration<Object> config = mock(ExpirationConfiguration.class);
    RemoveListener<Object> listener = mock(RemoveListener.class);
    BeanEntry<String> entry = mock(BeanEntry.class);
    BeanRemover<String, Object> remover = mock(BeanRemover.class);
    String beanId = "not-expired";
    Duration timeout = Duration.ofMillis(10L);
    when(batcher.createBatch()).thenReturn(batch);
    when(config.getTimeout()).thenReturn(Duration.ofMillis(1L));
    when(config.getRemoveListener()).thenReturn(listener);
    when(entry.getLastAccessedTime()).thenReturn(Instant.now().plus(Duration.ofMinutes(1)));
    when(factory.findValue(beanId)).thenReturn(entry);
    when(entry.isExpired(same(timeout))).thenReturn(false);
    try (Scheduler<String, ImmutableBeanEntry<String>> scheduler = new BeanExpirationScheduler<>(group, batcher, factory, config, remover, Duration.ZERO)) {
        scheduler.schedule(beanId, entry);
        Thread.sleep(500);
    }
    verify(remover, never()).remove(beanId, listener);
    verify(batch, never()).close();
}
Also used : Group(org.wildfly.clustering.group.Group) TransactionBatch(org.wildfly.clustering.ee.cache.tx.TransactionBatch) Duration(java.time.Duration) Test(org.junit.Test)

Example 5 with TransactionBatch

use of org.wildfly.clustering.ee.cache.tx.TransactionBatch in project wildfly by wildfly.

the class BeanExpirationSchedulerTestCase method testCancel.

@Test
public void testCancel() throws InterruptedException {
    Group group = mock(Group.class);
    Batcher<TransactionBatch> batcher = mock(Batcher.class);
    BeanFactory<String, Object> factory = mock(BeanFactory.class);
    ExpirationConfiguration<Object> config = mock(ExpirationConfiguration.class);
    RemoveListener<Object> listener = mock(RemoveListener.class);
    BeanEntry<String> entry = mock(BeanEntry.class);
    BeanRemover<String, Object> remover = mock(BeanRemover.class);
    String beanId = "canceled";
    Duration timeout = Duration.ofMinutes(1L);
    when(config.getTimeout()).thenReturn(timeout);
    when(config.getRemoveListener()).thenReturn(listener);
    when(entry.getLastAccessedTime()).thenReturn(Instant.now());
    try (Scheduler<String, ImmutableBeanEntry<String>> scheduler = new BeanExpirationScheduler<>(group, batcher, factory, config, remover, Duration.ZERO)) {
        scheduler.schedule(beanId, entry);
        Thread.sleep(500);
        scheduler.cancel(beanId);
    }
    verify(remover, never()).remove(beanId, listener);
    verify(batcher, never()).createBatch();
}
Also used : Group(org.wildfly.clustering.group.Group) TransactionBatch(org.wildfly.clustering.ee.cache.tx.TransactionBatch) Duration(java.time.Duration) Test(org.junit.Test)

Aggregations

TransactionBatch (org.wildfly.clustering.ee.cache.tx.TransactionBatch)6 Test (org.junit.Test)5 Duration (java.time.Duration)4 Group (org.wildfly.clustering.group.Group)4 Instant (java.time.Instant)1 ConcurrentManager (org.wildfly.clustering.ee.cache.ConcurrentManager)1 ConcurrentSessionManager (org.wildfly.clustering.web.cache.session.ConcurrentSessionManager)1 ImmutableSessionMetaData (org.wildfly.clustering.web.session.ImmutableSessionMetaData)1 SessionExpirationListener (org.wildfly.clustering.web.session.SessionExpirationListener)1 SessionExpirationMetaData (org.wildfly.clustering.web.session.SessionExpirationMetaData)1