Search in sources :

Example 1 with Producers

use of org.apache.pulsar.functions.instance.producers.Producers in project incubator-pulsar by apache.

the class JavaInstanceRunnableProcessTest method testSetupJavaInstance.

/**
 * Test the basic run logic of instance.
 */
@Test
public void testSetupJavaInstance() throws Exception {
    JavaInstanceRunnable runnable = new JavaInstanceRunnable(config, fnCache, "test-jar-file", mockClient, TEST_STORAGE_SERVICE_URL);
    runnable.setupJavaInstance();
    // verify
    // 1. verify jar is loaded
    verify(fnCache, times(1)).registerFunctionInstance(eq(config.getFunctionId()), eq(config.getInstanceId()), eq(Arrays.asList("test-jar-file")), eq(Collections.emptyList()));
    verify(fnCache, times(1)).getClassLoader(eq(config.getFunctionId()));
    // 2. verify serde is setup
    for (String inputTopic : fnConfig.getInputsList()) {
        assertTrue(runnable.getInputSerDe().containsKey(inputTopic));
        assertTrue(runnable.getInputSerDe().get(inputTopic) instanceof DefaultSerDe);
        DefaultSerDe serDe = (DefaultSerDe) runnable.getInputSerDe().get(inputTopic);
        assertEquals(String.class, Whitebox.getInternalState(serDe, "type"));
    }
    // 3. verify producers and consumers are setup
    Producers producers = runnable.getOutputProducer();
    assertTrue(producers instanceof SimpleOneOuputTopicProducers);
    assertSame(mockProducers.get(Pair.of(fnConfig.getOutput(), null)).getProducer(), ((SimpleOneOuputTopicProducers) producers).getProducer());
    assertEquals(mockConsumers.size(), runnable.getInputConsumers().size());
    for (Map.Entry<String, Consumer> consumerEntry : runnable.getInputConsumers().entrySet()) {
        String topic = consumerEntry.getKey();
        Consumer mockConsumer = mockConsumers.get(Pair.of(topic, FunctionConfigUtils.getFullyQualifiedName(fnConfig))).getConsumer();
        assertSame(mockConsumer, consumerEntry.getValue());
    }
    // 4. verify state table
    assertSame(mockStorageClient, runnable.getStorageClient());
    assertSame(mockTable, runnable.getStateTable());
    runnable.close();
    // verify close
    for (ConsumerInstance consumer : mockConsumers.values()) {
        verify(consumer.getConsumer(), times(1)).close();
    }
    assertTrue(runnable.getInputConsumers().isEmpty());
    for (ProducerInstance producer : mockProducers.values()) {
        verify(producer.getProducer(), times(1)).close();
    }
    verify(mockTable, times(1)).close();
    verify(mockStorageClient, times(1)).close();
    // function is unregistered
    verify(fnCache, times(1)).unregisterFunctionInstance(eq(config.getFunctionId()), eq(config.getInstanceId()));
}
Also used : Producers(org.apache.pulsar.functions.instance.producers.Producers) SimpleOneOuputTopicProducers(org.apache.pulsar.functions.instance.producers.SimpleOneOuputTopicProducers) Consumer(org.apache.pulsar.client.api.Consumer) DefaultSerDe(org.apache.pulsar.functions.api.utils.DefaultSerDe) Matchers.anyString(org.mockito.Matchers.anyString) SimpleOneOuputTopicProducers(org.apache.pulsar.functions.instance.producers.SimpleOneOuputTopicProducers) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Consumer (org.apache.pulsar.client.api.Consumer)1 DefaultSerDe (org.apache.pulsar.functions.api.utils.DefaultSerDe)1 Producers (org.apache.pulsar.functions.instance.producers.Producers)1 SimpleOneOuputTopicProducers (org.apache.pulsar.functions.instance.producers.SimpleOneOuputTopicProducers)1 Matchers.anyString (org.mockito.Matchers.anyString)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 Test (org.testng.annotations.Test)1