BAEL-1368: infinispan article (#3633)

This commit is contained in:
Allan Vital
2018-02-11 11:36:33 -02:00
committed by Predrag Maric
parent 1c043dc202
commit 6eeff13c45
9 changed files with 494 additions and 1 deletions
@@ -0,0 +1,56 @@
package com.baeldung.infinispan;
import com.baeldung.infinispan.listener.CacheListener;
import com.baeldung.infinispan.repository.HelloWorldRepository;
import com.baeldung.infinispan.service.HelloWorldService;
import com.baeldung.infinispan.service.TransactionalService;
import org.infinispan.Cache;
import org.infinispan.manager.DefaultCacheManager;
import org.junit.After;
import org.junit.Before;
public class ConfigurationTest {
private DefaultCacheManager cacheManager;
private HelloWorldRepository repository = new HelloWorldRepository();
protected HelloWorldService helloWorldService;
protected TransactionalService transactionalService;
@Before
public void setup() {
CacheConfiguration configuration = new CacheConfiguration();
CacheListener listener = new CacheListener();
cacheManager = configuration.cacheManager();
Cache<String, Integer> transactionalCache =
configuration.transactionalCache(cacheManager, listener);
Cache<String, String> simpleHelloWorldCache =
configuration.simpleHelloWorldCache(cacheManager, listener);
Cache<String, String> expiringHelloWorldCache =
configuration.expiringHelloWorldCache(cacheManager, listener);
Cache<String, String> evictingHelloWorldCache =
configuration.evictingHelloWorldCache(cacheManager, listener);
Cache<String, String> passivatingHelloWorldCache =
configuration.passivatingHelloWorldCache(cacheManager, listener);
this.helloWorldService = new HelloWorldService(repository,
listener, simpleHelloWorldCache, expiringHelloWorldCache, evictingHelloWorldCache,
passivatingHelloWorldCache);
this.transactionalService = new TransactionalService(transactionalCache);
}
@After
public void tearDown() {
cacheManager.stop();
}
}
@@ -0,0 +1,105 @@
package com.baeldung.infinispan.service;
import com.baeldung.infinispan.ConfigurationTest;
import org.junit.Test;
import static org.assertj.core.api.Java6Assertions.assertThat;
public class HelloWorldServiceUnitTest extends ConfigurationTest {
@Test
public void whenGetIsCalledTwoTimes_thenTheSecondShouldHitTheCache() {
long milis = System.currentTimeMillis();
helloWorldService.findSimpleHelloWorld();
long executionTime = System.currentTimeMillis() - milis;
assertThat(executionTime).isGreaterThanOrEqualTo(1000);
milis = System.currentTimeMillis();
helloWorldService.findSimpleHelloWorld();
executionTime = System.currentTimeMillis() - milis;
assertThat(executionTime).isLessThan(100);
}
@Test
public void whenGetIsCalledTwoTimesQuickly_thenTheSecondShouldHitTheCache() {
long milis = System.currentTimeMillis();
helloWorldService.findExpiringHelloWorld();
long executionTime = System.currentTimeMillis() - milis;
assertThat(executionTime).isGreaterThanOrEqualTo(1000);
milis = System.currentTimeMillis();
helloWorldService.findExpiringHelloWorld();
executionTime = System.currentTimeMillis() - milis;
assertThat(executionTime).isLessThan(100);
}
@Test
public void whenGetIsCalledTwoTimesSparsely_thenNeitherShouldHitTheCache()
throws InterruptedException {
long milis = System.currentTimeMillis();
helloWorldService.findSimpleHelloWorldInExpiringCache();
long executionTime = System.currentTimeMillis() - milis;
assertThat(executionTime).isGreaterThanOrEqualTo(1000);
Thread.sleep(1100);
milis = System.currentTimeMillis();
helloWorldService.findSimpleHelloWorldInExpiringCache();
executionTime = System.currentTimeMillis() - milis;
assertThat(executionTime).isGreaterThanOrEqualTo(1000);
}
@Test
public void givenOneEntryIsConfigured_whenTwoAreAdded_thenFirstShouldntBeAvailable()
throws InterruptedException {
long milis = System.currentTimeMillis();
helloWorldService.findEvictingHelloWorld("key 1");
long executionTime = System.currentTimeMillis() - milis;
assertThat(executionTime).isGreaterThanOrEqualTo(1000);
milis = System.currentTimeMillis();
helloWorldService.findEvictingHelloWorld("key 2");
executionTime = System.currentTimeMillis() - milis;
assertThat(executionTime).isGreaterThanOrEqualTo(1000);
milis = System.currentTimeMillis();
helloWorldService.findEvictingHelloWorld("key 1");
executionTime = System.currentTimeMillis() - milis;
assertThat(executionTime).isGreaterThanOrEqualTo(1000);
}
@Test
public void givenOneEntryIsConfigured_whenTwoAreAdded_thenTheFirstShouldBeAvailable()
throws InterruptedException {
long milis = System.currentTimeMillis();
helloWorldService.findPassivatingHelloWorld("key 1");
long executionTime = System.currentTimeMillis() - milis;
assertThat(executionTime).isGreaterThanOrEqualTo(1000);
milis = System.currentTimeMillis();
helloWorldService.findPassivatingHelloWorld("key 2");
executionTime = System.currentTimeMillis() - milis;
assertThat(executionTime).isGreaterThanOrEqualTo(1000);
milis = System.currentTimeMillis();
helloWorldService.findPassivatingHelloWorld("key 1");
executionTime = System.currentTimeMillis() - milis;
assertThat(executionTime).isLessThan(100);
}
}
@@ -0,0 +1,24 @@
package com.baeldung.infinispan.service;
import com.baeldung.infinispan.ConfigurationTest;
import org.junit.Test;
import static org.assertj.core.api.Java6Assertions.assertThat;
public class TransactionalServiceUnitTest extends ConfigurationTest {
@Test
public void whenLockingAnEntry_thenItShouldBeInaccessible() throws InterruptedException {
Runnable backGroundJob = () -> transactionalService.startBackgroundBatch();
Thread backgroundThread = new Thread(backGroundJob);
transactionalService.getQuickHowManyVisits();
backgroundThread.start();
Thread.sleep(100); //lets wait our thread warm up
long milis = System.currentTimeMillis();
transactionalService.getQuickHowManyVisits();
long executionTime = System.currentTimeMillis() - milis;
assertThat(executionTime).isGreaterThan(500).isLessThan(1000);
}
}