Search in sources :

Example 1 with DistributedSetCacheClientService

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();
}
Also used : Processor(org.apache.nifi.processor.Processor) TestRunner(org.apache.nifi.util.TestRunner) File(java.io.File) DistributedSetCacheClientService(org.apache.nifi.distributed.cache.client.DistributedSetCacheClientService) Test(org.junit.Test)

Example 2 with DistributedSetCacheClientService

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();
}
Also used : Processor(org.apache.nifi.processor.Processor) TestRunner(org.apache.nifi.util.TestRunner) File(java.io.File) DistributedSetCacheClientService(org.apache.nifi.distributed.cache.client.DistributedSetCacheClientService) Test(org.junit.Test)

Example 3 with DistributedSetCacheClientService

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();
}
Also used : Processor(org.apache.nifi.processor.Processor) TestRunner(org.apache.nifi.util.TestRunner) File(java.io.File) DistributedSetCacheClientService(org.apache.nifi.distributed.cache.client.DistributedSetCacheClientService) Test(org.junit.Test)

Example 4 with DistributedSetCacheClientService

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();
}
Also used : Processor(org.apache.nifi.processor.Processor) TestRunner(org.apache.nifi.util.TestRunner) DistributedSetCacheClientService(org.apache.nifi.distributed.cache.client.DistributedSetCacheClientService) Test(org.junit.Test)

Example 5 with DistributedSetCacheClientService

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;
}
Also used : MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) HashMap(java.util.HashMap) DistributedSetCacheClientService(org.apache.nifi.distributed.cache.client.DistributedSetCacheClientService)

Aggregations

DistributedSetCacheClientService (org.apache.nifi.distributed.cache.client.DistributedSetCacheClientService)5 Processor (org.apache.nifi.processor.Processor)4 TestRunner (org.apache.nifi.util.TestRunner)4 Test (org.junit.Test)4 File (java.io.File)3 HashMap (java.util.HashMap)1 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)1 MockConfigurationContext (org.apache.nifi.util.MockConfigurationContext)1 MockControllerServiceInitializationContext (org.apache.nifi.util.MockControllerServiceInitializationContext)1