Search in sources :

Example 1 with MockControllerServiceInitializationContext

use of org.apache.nifi.util.MockControllerServiceInitializationContext in project nifi by apache.

the class TestServerAndClient method testClientTermination.

@Test
public void testClientTermination() throws InitializationException, IOException, InterruptedException {
    /**
     * 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 DistributedMapCacheServer server = new MapServer();
    final MockControllerServiceInitializationContext serverInitContext = new MockControllerServiceInitializationContext(server, "server");
    server.initialize(serverInitContext);
    final Map<PropertyDescriptor, String> serverProperties = new HashMap<>();
    final MockConfigurationContext serverContext = new MockConfigurationContext(serverProperties, serverInitContext.getControllerServiceLookup());
    server.startServer(serverContext);
    DistributedMapCacheClientService client = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext = new MockControllerServiceInitializationContext(client, "client");
    client.initialize(clientInitContext);
    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");
    MockConfigurationContext clientContext = new MockConfigurationContext(clientProperties, clientInitContext.getControllerServiceLookup());
    client.cacheConfig(clientContext);
    final Serializer<String> valueSerializer = new StringSerializer();
    final Serializer<String> keySerializer = new StringSerializer();
    final Deserializer<String> deserializer = new StringDeserializer();
    final String original = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer, deserializer);
    assertEquals(null, original);
    final boolean contains = client.containsKey("testKey", keySerializer);
    assertTrue(contains);
    final boolean added = client.putIfAbsent("testKey", "test", keySerializer, valueSerializer);
    assertFalse(added);
    final String originalAfterPut = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer, deserializer);
    assertEquals("test", originalAfterPut);
    final boolean removed = client.remove("testKey", keySerializer);
    assertTrue(removed);
    final boolean containedAfterRemove = client.containsKey("testKey", keySerializer);
    assertFalse(containedAfterRemove);
    client = null;
    clientInitContext = null;
    clientContext = null;
    Thread.sleep(2000);
    System.gc();
    server.shutdownServer();
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) DistributedMapCacheServer(org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer) DistributedMapCacheClientService(org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService) MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) Test(org.junit.Test)

Example 2 with MockControllerServiceInitializationContext

use of org.apache.nifi.util.MockControllerServiceInitializationContext in project nifi by apache.

the class TestServerAndClient method testOptimisticLock.

@Test
public void testOptimisticLock() throws Exception {
    /**
     * 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 DistributedMapCacheServer server = new MapServer();
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    runner.addControllerService("server", server);
    runner.enableControllerService(server);
    DistributedMapCacheClientService client1 = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext1 = new MockControllerServiceInitializationContext(client1, "client1");
    client1.initialize(clientInitContext1);
    DistributedMapCacheClientService client2 = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext2 = new MockControllerServiceInitializationContext(client2, "client2");
    client1.initialize(clientInitContext2);
    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.PORT, String.valueOf(server.getPort()));
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");
    MockConfigurationContext clientContext1 = new MockConfigurationContext(clientProperties, clientInitContext1.getControllerServiceLookup());
    client1.cacheConfig(clientContext1);
    MockConfigurationContext clientContext2 = new MockConfigurationContext(clientProperties, clientInitContext2.getControllerServiceLookup());
    client2.cacheConfig(clientContext2);
    final Serializer<String> stringSerializer = new StringSerializer();
    final Deserializer<String> stringDeserializer = new StringDeserializer();
    final String key = "test-optimistic-lock";
    // Ensure there's no existing key
    assertFalse(client1.containsKey(key, stringSerializer));
    assertNull(client1.fetch(key, stringSerializer, stringDeserializer));
    // Client 1 inserts the key.
    client1.put(key, "valueC1-0", stringSerializer, stringSerializer);
    // Client 1 and 2 fetch the key
    AtomicCacheEntry<String, String, Long> c1 = client1.fetch(key, stringSerializer, stringDeserializer);
    AtomicCacheEntry<String, String, Long> c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals(new Long(0), c1.getRevision().orElse(0L));
    assertEquals("valueC1-0", c1.getValue());
    assertEquals(new Long(0), c2.getRevision().orElse(0L));
    assertEquals("valueC1-0", c2.getValue());
    // Client 1 replace
    c1.setValue("valueC1-1");
    boolean c1Result = client1.replace(c1, stringSerializer, stringSerializer);
    assertTrue("C1 should be able to replace the key", c1Result);
    // Client 2 replace with the old revision
    c2.setValue("valueC2-1");
    boolean c2Result = client2.replace(c2, stringSerializer, stringSerializer);
    assertFalse("C2 shouldn't be able to replace the key", c2Result);
    // Client 2 fetch the key again
    c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals("valueC1-1", c2.getValue());
    assertEquals(new Long(1), c2.getRevision().orElse(0L));
    // Now, Client 2 knows the correct revision so it can replace the key
    c2.setValue("valueC2-2");
    c2Result = client2.replace(c2, stringSerializer, stringSerializer);
    assertTrue("C2 should be able to replace the key", c2Result);
    // Assert the cache
    c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals("valueC2-2", c2.getValue());
    assertEquals(new Long(2), c2.getRevision().orElse(0L));
    client1.close();
    client2.close();
    server.shutdownServer();
}
Also used : Processor(org.apache.nifi.processor.Processor) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) DistributedMapCacheServer(org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer) DistributedMapCacheClientService(org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService) MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) Test(org.junit.Test)

Example 3 with MockControllerServiceInitializationContext

use of org.apache.nifi.util.MockControllerServiceInitializationContext in project nifi by apache.

the class TestDetectDuplicate method createClient.

private DistributedMapCacheClientImpl createClient() throws InitializationException {
    final DistributedMapCacheClientImpl client = new DistributedMapCacheClientImpl();
    final ComponentLog logger = new MockComponentLog("client", client);
    final MockControllerServiceInitializationContext clientInitContext = new MockControllerServiceInitializationContext(client, "client", logger, new MockStateManager(client));
    client.initialize(clientInitContext);
    return client;
}
Also used : MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) MockStateManager(org.apache.nifi.state.MockStateManager) MockComponentLog(org.apache.nifi.util.MockComponentLog) ComponentLog(org.apache.nifi.logging.ComponentLog) MockComponentLog(org.apache.nifi.util.MockComponentLog)

Example 4 with MockControllerServiceInitializationContext

use of org.apache.nifi.util.MockControllerServiceInitializationContext in project nifi by apache.

the class TestServerAndClient method testNonPersistentMapServerAndClient.

@Test
public void testNonPersistentMapServerAndClient() throws InitializationException, IOException, InterruptedException {
    /**
     * 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 DistributedMapCacheServer server = new MapServer();
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    runner.addControllerService("server", server);
    runner.enableControllerService(server);
    DistributedMapCacheClientService client = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext = new MockControllerServiceInitializationContext(client, "client");
    client.initialize(clientInitContext);
    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.PORT, String.valueOf(server.getPort()));
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");
    MockConfigurationContext clientContext = new MockConfigurationContext(clientProperties, clientInitContext.getControllerServiceLookup());
    client.cacheConfig(clientContext);
    final Serializer<String> valueSerializer = new StringSerializer();
    final Serializer<String> keySerializer = new StringSerializer();
    final Deserializer<String> deserializer = new StringDeserializer();
    final String original = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer, deserializer);
    assertEquals(null, original);
    LOGGER.debug("end getAndPutIfAbsent");
    final boolean contains = client.containsKey("testKey", keySerializer);
    assertTrue(contains);
    LOGGER.debug("end containsKey");
    final boolean added = client.putIfAbsent("testKey", "test", keySerializer, valueSerializer);
    assertFalse(added);
    LOGGER.debug("end putIfAbsent");
    final String originalAfterPut = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer, deserializer);
    assertEquals("test", originalAfterPut);
    LOGGER.debug("end getAndPutIfAbsent");
    final boolean removed = client.remove("testKey", keySerializer);
    assertTrue(removed);
    LOGGER.debug("end remove");
    client.put("testKey", "testValue", keySerializer, valueSerializer);
    assertTrue(client.containsKey("testKey", keySerializer));
    String removedValue = client.removeAndGet("testKey", keySerializer, deserializer);
    assertEquals("testValue", removedValue);
    removedValue = client.removeAndGet("testKey", keySerializer, deserializer);
    assertNull(removedValue);
    final Set<String> keys = client.keySet(deserializer);
    assertEquals(0, keys.size());
    // Test removeByPattern, the first two should be removed and the last should remain
    client.put("test.1", "1", keySerializer, keySerializer);
    client.put("test.2", "2", keySerializer, keySerializer);
    client.put("test3", "2", keySerializer, keySerializer);
    final long removedTwo = client.removeByPattern("test\\..*");
    assertEquals(2L, removedTwo);
    assertFalse(client.containsKey("test.1", keySerializer));
    assertFalse(client.containsKey("test.2", keySerializer));
    assertTrue(client.containsKey("test3", keySerializer));
    final boolean containedAfterRemove = client.containsKey("testKey", keySerializer);
    assertFalse(containedAfterRemove);
    client.putIfAbsent("testKey", "test", keySerializer, valueSerializer);
    client.close();
    try {
        client.containsKey("testKey", keySerializer);
        fail("Should be closed and not accessible");
    } catch (final Exception e) {
    }
    DistributedMapCacheClientService client2 = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext2 = new MockControllerServiceInitializationContext(client2, "client2");
    client2.initialize(clientInitContext2);
    MockConfigurationContext clientContext2 = new MockConfigurationContext(clientProperties, clientInitContext2.getControllerServiceLookup());
    client2.cacheConfig(clientContext2);
    assertFalse(client2.putIfAbsent("testKey", "test", keySerializer, valueSerializer));
    assertTrue(client2.containsKey("testKey", keySerializer));
    server.shutdownServer();
    Thread.sleep(1000);
    try {
        client2.containsKey("testKey", keySerializer);
        fail("Should have blown exception!");
    } catch (final ConnectException e) {
        client2 = null;
        clientContext2 = null;
        clientInitContext2 = null;
    }
    LOGGER.debug("end testNonPersistentMapServerAndClient");
}
Also used : Processor(org.apache.nifi.processor.Processor) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) DistributedMapCacheServer(org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer) InitializationException(org.apache.nifi.reporting.InitializationException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) SerializationException(org.apache.commons.lang3.SerializationException) DeserializationException(org.apache.nifi.distributed.cache.client.exception.DeserializationException) DistributedMapCacheClientService(org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService) MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 5 with MockControllerServiceInitializationContext

use of org.apache.nifi.util.MockControllerServiceInitializationContext in project nifi by apache.

the class TestServerAndClient method createMapClient.

private DistributedMapCacheClientService createMapClient(final int port) throws InitializationException {
    final DistributedMapCacheClientService client = new DistributedMapCacheClientService();
    final MockControllerServiceInitializationContext clientInitContext = new MockControllerServiceInitializationContext(client, "client");
    client.initialize(clientInitContext);
    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.PORT, String.valueOf(port));
    final MockConfigurationContext clientContext = new MockConfigurationContext(clientProperties, clientInitContext.getControllerServiceLookup());
    client.cacheConfig(clientContext);
    return client;
}
Also used : DistributedMapCacheClientService(org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService) MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) HashMap(java.util.HashMap)

Aggregations

MockControllerServiceInitializationContext (org.apache.nifi.util.MockControllerServiceInitializationContext)7 HashMap (java.util.HashMap)6 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)6 MockConfigurationContext (org.apache.nifi.util.MockConfigurationContext)6 DistributedMapCacheClientService (org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService)5 DistributedMapCacheServer (org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer)4 Test (org.junit.Test)4 Processor (org.apache.nifi.processor.Processor)3 TestRunner (org.apache.nifi.util.TestRunner)3 File (java.io.File)1 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 SSLContext (javax.net.ssl.SSLContext)1 SerializationException (org.apache.commons.lang3.SerializationException)1 AtomicCacheEntry (org.apache.nifi.distributed.cache.client.AtomicCacheEntry)1 DistributedSetCacheClientService (org.apache.nifi.distributed.cache.client.DistributedSetCacheClientService)1 DeserializationException (org.apache.nifi.distributed.cache.client.exception.DeserializationException)1 MapCacheServer (org.apache.nifi.distributed.cache.server.map.MapCacheServer)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1 StandardVersionNegotiator (org.apache.nifi.remote.StandardVersionNegotiator)1