use of org.apache.nifi.distributed.cache.client.DistributedSetCacheClientService in project nifi by apache.
the class TestServerAndClient method testPersistentSetServerAndClientWithFIFOEvictions.
@Test
public void testPersistentSetServerAndClientWithFIFOEvictions() throws InitializationException, IOException {
/**
* This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
* See: https://issues.apache.org/jira/browse/NIFI-437
*/
Assume.assumeFalse("test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437", SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);
LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
final File dataFile = new File("target/cache-data");
deleteRecursively(dataFile);
// Create server
final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
final DistributedSetCacheServer server = new SetServer();
runner.addControllerService("server", server);
runner.setProperty(server, DistributedSetCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
runner.setProperty(server, DistributedSetCacheServer.MAX_CACHE_ENTRIES, "3");
runner.setProperty(server, DistributedSetCacheServer.EVICTION_POLICY, DistributedSetCacheServer.EVICTION_STRATEGY_FIFO);
runner.enableControllerService(server);
DistributedSetCacheClientService client = createClient(server.getPort());
final Serializer<String> serializer = new StringSerializer();
// add 3 entries to the cache. But, if we add too fast, we'll have the same millisecond
// for the entry time so we don't know which entry will be evicted. So we wait a few millis in between
final boolean added = client.addIfAbsent("test", serializer);
waitABit();
final boolean added2 = client.addIfAbsent("test2", serializer);
waitABit();
final boolean added3 = client.addIfAbsent("test3", serializer);
waitABit();
assertTrue(added);
assertTrue(added2);
assertTrue(added3);
final boolean contains = client.contains("test", serializer);
final boolean contains2 = client.contains("test2", serializer);
assertTrue(contains);
assertTrue(contains2);
final boolean addedAgain = client.addIfAbsent("test", serializer);
assertFalse(addedAgain);
final boolean added4 = client.addIfAbsent("test4", serializer);
assertTrue(added4);
// ensure that added3 was evicted because it was used least frequently
assertFalse(client.contains("test", serializer));
assertTrue(client.contains("test3", serializer));
server.shutdownServer();
client.close();
final DistributedSetCacheServer newServer = new SetServer();
runner.addControllerService("server2", newServer);
runner.setProperty(newServer, DistributedSetCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
runner.setProperty(newServer, DistributedSetCacheServer.MAX_CACHE_ENTRIES, "3");
runner.setProperty(newServer, DistributedSetCacheServer.EVICTION_POLICY, DistributedSetCacheServer.EVICTION_STRATEGY_FIFO);
runner.enableControllerService(newServer);
client = createClient(newServer.getPort());
assertFalse(client.contains("test", serializer));
assertTrue(client.contains("test2", serializer));
assertTrue(client.contains("test3", serializer));
assertTrue(client.contains("test4", serializer));
newServer.shutdownServer();
client.close();
}
use of org.apache.nifi.distributed.cache.client.DistributedSetCacheClientService in project nifi by apache.
the class TestServerAndClient method testPersistentSetServerAndClientWithLFUEvictions.
@Test
public void testPersistentSetServerAndClientWithLFUEvictions() throws InitializationException, IOException {
/**
* This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
* See: https://issues.apache.org/jira/browse/NIFI-437
*/
Assume.assumeFalse("test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437", SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);
LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
// Create server
final File dataFile = new File("target/cache-data");
deleteRecursively(dataFile);
// Create server
final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
final DistributedSetCacheServer server = new SetServer();
runner.addControllerService("server", server);
runner.setProperty(server, DistributedSetCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
runner.setProperty(server, DistributedSetCacheServer.MAX_CACHE_ENTRIES, "3");
runner.setProperty(server, DistributedSetCacheServer.EVICTION_POLICY, DistributedSetCacheServer.EVICTION_STRATEGY_LFU);
runner.enableControllerService(server);
DistributedSetCacheClientService client = createClient(server.getPort());
final Serializer<String> serializer = new StringSerializer();
final boolean added = client.addIfAbsent("test", serializer);
waitABit();
final boolean added2 = client.addIfAbsent("test2", serializer);
waitABit();
final boolean added3 = client.addIfAbsent("test3", serializer);
waitABit();
assertTrue(added);
assertTrue(added2);
assertTrue(added3);
final boolean contains = client.contains("test", serializer);
final boolean contains2 = client.contains("test2", serializer);
assertTrue(contains);
assertTrue(contains2);
final boolean addedAgain = client.addIfAbsent("test", serializer);
assertFalse(addedAgain);
final boolean added4 = client.addIfAbsent("test4", serializer);
assertTrue(added4);
// ensure that added3 was evicted because it was used least frequently
assertFalse(client.contains("test3", serializer));
server.shutdownServer();
final DistributedSetCacheServer newServer = new SetServer();
runner.addControllerService("server2", newServer);
runner.setProperty(newServer, DistributedSetCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
runner.enableControllerService(newServer);
client.close();
client = createClient(newServer.getPort());
assertTrue(client.contains("test", serializer));
assertTrue(client.contains("test2", serializer));
assertFalse(client.contains("test3", serializer));
assertTrue(client.contains("test4", serializer));
newServer.shutdownServer();
client.close();
}
use of org.apache.nifi.distributed.cache.client.DistributedSetCacheClientService in project nifi by apache.
the class TestServerAndClient method testPersistentSetServerAndClient.
@Test
public void testPersistentSetServerAndClient() throws InitializationException, IOException {
/**
* This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
* See: https://issues.apache.org/jira/browse/NIFI-437
*/
Assume.assumeFalse("test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437", SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);
LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
final File dataFile = new File("target/cache-data");
deleteRecursively(dataFile);
// Create server
final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
final DistributedSetCacheServer server = new SetServer();
runner.addControllerService("server", server);
runner.setProperty(server, DistributedSetCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
runner.enableControllerService(server);
DistributedSetCacheClientService client = createClient(server.getPort());
final Serializer<String> serializer = new StringSerializer();
final boolean added = client.addIfAbsent("test", serializer);
final boolean added2 = client.addIfAbsent("test2", serializer);
assertTrue(added);
assertTrue(added2);
final boolean contains = client.contains("test", serializer);
final boolean contains2 = client.contains("test2", serializer);
assertTrue(contains);
assertTrue(contains2);
final boolean addedAgain = client.addIfAbsent("test", serializer);
assertFalse(addedAgain);
final boolean removed = client.remove("test", serializer);
assertTrue(removed);
final boolean containedAfterRemove = client.contains("test", serializer);
assertFalse(containedAfterRemove);
server.shutdownServer();
client.close();
final DistributedSetCacheServer newServer = new SetServer();
runner.addControllerService("server2", newServer);
runner.setProperty(newServer, DistributedSetCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
runner.enableControllerService(newServer);
client = createClient(newServer.getPort());
assertFalse(client.contains("test", serializer));
assertTrue(client.contains("test2", serializer));
newServer.shutdownServer();
client.close();
}
use of org.apache.nifi.distributed.cache.client.DistributedSetCacheClientService in project nifi by apache.
the class TestServerAndClient method testNonPersistentSetServerAndClient.
@Test
public void testNonPersistentSetServerAndClient() throws InitializationException, IOException {
/**
* This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
* See: https://issues.apache.org/jira/browse/NIFI-437
*/
Assume.assumeFalse("test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437", SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);
LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
// Create server
final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
final DistributedSetCacheServer server = new SetServer();
runner.addControllerService("server", server);
runner.enableControllerService(server);
final DistributedSetCacheClientService client = createClient(server.getPort());
final Serializer<String> serializer = new StringSerializer();
final boolean added = client.addIfAbsent("test", serializer);
assertTrue(added);
final boolean contains = client.contains("test", serializer);
assertTrue(contains);
final boolean addedAgain = client.addIfAbsent("test", serializer);
assertFalse(addedAgain);
final boolean removed = client.remove("test", serializer);
assertTrue(removed);
final boolean containedAfterRemove = client.contains("test", serializer);
assertFalse(containedAfterRemove);
server.shutdownServer();
}
use of org.apache.nifi.distributed.cache.client.DistributedSetCacheClientService in project nifi by apache.
the class TestServerAndClient method createClient.
private DistributedSetCacheClientService createClient(final int port) throws InitializationException {
final DistributedSetCacheClientService client = new DistributedSetCacheClientService();
final MockControllerServiceInitializationContext clientInitContext = new MockControllerServiceInitializationContext(client, "client");
client.initialize(clientInitContext);
final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
clientProperties.put(DistributedSetCacheClientService.HOSTNAME, "localhost");
clientProperties.put(DistributedSetCacheClientService.PORT, String.valueOf(port));
final MockConfigurationContext clientContext = new MockConfigurationContext(clientProperties, clientInitContext.getControllerServiceLookup());
client.onConfigured(clientContext);
return client;
}
Aggregations