use of org.apache.pulsar.functions.instance.JavaInstance.AsyncFuncRequest in project incubator-pulsar by apache.
the class JavaInstanceTest method testAsyncFunctionMaxPending.
@Test
public void testAsyncFunctionMaxPending() throws Exception {
InstanceConfig instanceConfig = new InstanceConfig();
int pendingQueueSize = 3;
instanceConfig.setMaxPendingAsyncRequests(pendingQueueSize);
@Cleanup("shutdownNow") ExecutorService executor = Executors.newCachedThreadPool();
Function<String, CompletableFuture<String>> function = (input, context) -> {
log.info("input string: {}", input);
CompletableFuture<String> result = new CompletableFuture<>();
executor.submit(() -> {
try {
Thread.sleep(500);
result.complete(String.format("%s-lambda", input));
} catch (Exception e) {
result.completeExceptionally(e);
}
});
return result;
};
JavaInstance instance = new JavaInstance(mock(ContextImpl.class), function, instanceConfig);
String testString = "ABC123";
long startTime = System.currentTimeMillis();
assertEquals(pendingQueueSize, instance.getPendingAsyncRequests().remainingCapacity());
assertNull(instance.handleMessage(mock(Record.class), testString));
assertEquals(pendingQueueSize - 1, instance.getPendingAsyncRequests().remainingCapacity());
assertNull(instance.handleMessage(mock(Record.class), testString));
assertEquals(pendingQueueSize - 2, instance.getPendingAsyncRequests().remainingCapacity());
assertNull(instance.handleMessage(mock(Record.class), testString));
// no space left
assertEquals(0, instance.getPendingAsyncRequests().remainingCapacity());
for (int i = 0; i < 3; i++) {
AsyncFuncRequest request = instance.getPendingAsyncRequests().poll();
Assert.assertEquals(request.getProcessResult().get(), testString + "-lambda");
}
long endTime = System.currentTimeMillis();
log.info("start:{} end:{} during:{}", startTime, endTime, endTime - startTime);
instance.close();
}
use of org.apache.pulsar.functions.instance.JavaInstance.AsyncFuncRequest in project pulsar by apache.
the class JavaInstanceTest method testAsyncFunctionMaxPending.
@Test
public void testAsyncFunctionMaxPending() throws Exception {
InstanceConfig instanceConfig = new InstanceConfig();
int pendingQueueSize = 3;
instanceConfig.setMaxPendingAsyncRequests(pendingQueueSize);
@Cleanup("shutdownNow") ExecutorService executor = Executors.newCachedThreadPool();
Function<String, CompletableFuture<String>> function = (input, context) -> {
log.info("input string: {}", input);
CompletableFuture<String> result = new CompletableFuture<>();
executor.submit(() -> {
try {
Thread.sleep(500);
result.complete(String.format("%s-lambda", input));
} catch (Exception e) {
result.completeExceptionally(e);
}
});
return result;
};
JavaInstance instance = new JavaInstance(mock(ContextImpl.class), function, instanceConfig);
String testString = "ABC123";
long startTime = System.currentTimeMillis();
assertEquals(pendingQueueSize, instance.getPendingAsyncRequests().remainingCapacity());
assertNull(instance.handleMessage(mock(Record.class), testString));
assertEquals(pendingQueueSize - 1, instance.getPendingAsyncRequests().remainingCapacity());
assertNull(instance.handleMessage(mock(Record.class), testString));
assertEquals(pendingQueueSize - 2, instance.getPendingAsyncRequests().remainingCapacity());
assertNull(instance.handleMessage(mock(Record.class), testString));
// no space left
assertEquals(0, instance.getPendingAsyncRequests().remainingCapacity());
for (int i = 0; i < 3; i++) {
AsyncFuncRequest request = instance.getPendingAsyncRequests().poll();
Assert.assertEquals(request.getProcessResult().get(), testString + "-lambda");
}
long endTime = System.currentTimeMillis();
log.info("start:{} end:{} during:{}", startTime, endTime, endTime - startTime);
instance.close();
}
use of org.apache.pulsar.functions.instance.JavaInstance.AsyncFuncRequest in project pulsar by yahoo.
the class JavaInstanceTest method testAsyncFunctionMaxPending.
@Test
public void testAsyncFunctionMaxPending() throws Exception {
InstanceConfig instanceConfig = new InstanceConfig();
int pendingQueueSize = 3;
instanceConfig.setMaxPendingAsyncRequests(pendingQueueSize);
@Cleanup("shutdownNow") ExecutorService executor = Executors.newCachedThreadPool();
Function<String, CompletableFuture<String>> function = (input, context) -> {
log.info("input string: {}", input);
CompletableFuture<String> result = new CompletableFuture<>();
executor.submit(() -> {
try {
Thread.sleep(500);
result.complete(String.format("%s-lambda", input));
} catch (Exception e) {
result.completeExceptionally(e);
}
});
return result;
};
JavaInstance instance = new JavaInstance(mock(ContextImpl.class), function, instanceConfig);
String testString = "ABC123";
long startTime = System.currentTimeMillis();
assertEquals(pendingQueueSize, instance.getPendingAsyncRequests().remainingCapacity());
assertNull(instance.handleMessage(mock(Record.class), testString));
assertEquals(pendingQueueSize - 1, instance.getPendingAsyncRequests().remainingCapacity());
assertNull(instance.handleMessage(mock(Record.class), testString));
assertEquals(pendingQueueSize - 2, instance.getPendingAsyncRequests().remainingCapacity());
assertNull(instance.handleMessage(mock(Record.class), testString));
// no space left
assertEquals(0, instance.getPendingAsyncRequests().remainingCapacity());
for (int i = 0; i < 3; i++) {
AsyncFuncRequest request = instance.getPendingAsyncRequests().poll();
Assert.assertEquals(request.getProcessResult().get(), testString + "-lambda");
}
long endTime = System.currentTimeMillis();
log.info("start:{} end:{} during:{}", startTime, endTime, endTime - startTime);
instance.close();
}
Aggregations