use of org.apache.pulsar.functions.api.utils.DefaultSerDe in project incubator-pulsar by apache.
the class CmdFunctionsTest method setup.
@BeforeMethod
public void setup() throws Exception {
this.admin = mock(PulsarAdminWithFunctions.class);
this.functions = mock(Functions.class);
when(admin.functions()).thenReturn(functions);
when(admin.getServiceUrl()).thenReturn(URI.create("http://localhost:1234").toURL());
when(admin.getClientConf()).thenReturn(new ClientConfigurationData());
this.cmd = new CmdFunctions(admin);
// mock reflections
mockStatic(Reflections.class);
when(Reflections.classExistsInJar(any(File.class), anyString())).thenReturn(true);
when(Reflections.classExists(anyString())).thenReturn(true);
when(Reflections.classInJarImplementsIface(any(File.class), anyString(), eq(Function.class))).thenReturn(true);
when(Reflections.classImplementsIface(anyString(), any())).thenReturn(true);
when(Reflections.createInstance(eq(DummyFunction.class.getName()), any(File.class))).thenReturn(new DummyFunction());
when(Reflections.createInstance(eq(DefaultSerDe.class.getName()), any(File.class))).thenReturn(new DefaultSerDe(String.class));
}
use of org.apache.pulsar.functions.api.utils.DefaultSerDe 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()));
}
use of org.apache.pulsar.functions.api.utils.DefaultSerDe in project incubator-pulsar by apache.
the class ContextImpl method publish.
@Override
public <O> CompletableFuture<Void> publish(String topicName, O object, String serDeClassName) {
if (!publishProducers.containsKey(topicName)) {
try {
publishProducers.put(topicName, pulsarClient.createProducer(topicName, producerConfiguration));
} catch (PulsarClientException ex) {
CompletableFuture<Void> retval = new CompletableFuture<>();
retval.completeExceptionally(ex);
return retval;
}
}
if (!publishSerializers.containsKey(serDeClassName)) {
SerDe serDe;
if (serDeClassName.equals(DefaultSerDe.class.getName())) {
if (!DefaultSerDe.IsSupportedType(object.getClass())) {
throw new RuntimeException("Default Serializer does not support " + object.getClass());
}
serDe = new DefaultSerDe(object.getClass());
} else {
try {
Class<? extends SerDe> serDeClass = (Class<? extends SerDe>) Class.forName(serDeClassName);
serDe = Reflections.createInstance(serDeClassName, serDeClass, classLoader);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
publishSerializers.put(serDeClassName, serDe);
}
byte[] bytes = publishSerializers.get(serDeClassName).serialize(object);
return publishProducers.get(topicName).sendAsync(bytes).thenApply(msgId -> null);
}
Aggregations