Search in sources :

Example 1 with AsyncFuncRequest

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();
}
Also used : Assert.assertSame(org.testng.Assert.assertSame) Assert.assertNull(org.testng.Assert.assertNull) Assert.assertEquals(org.testng.Assert.assertEquals) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) Assert.assertNotNull(org.testng.Assert.assertNotNull) Executors(java.util.concurrent.Executors) Slf4j(lombok.extern.slf4j.Slf4j) Function(org.apache.pulsar.functions.api.Function) Assert(org.testng.Assert) AsyncFuncRequest(org.apache.pulsar.functions.instance.JavaInstance.AsyncFuncRequest) ExecutorService(java.util.concurrent.ExecutorService) Record(org.apache.pulsar.functions.api.Record) Mockito.mock(org.mockito.Mockito.mock) AsyncFuncRequest(org.apache.pulsar.functions.instance.JavaInstance.AsyncFuncRequest) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutorService(java.util.concurrent.ExecutorService) Test(org.testng.annotations.Test)

Example 2 with AsyncFuncRequest

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();
}
Also used : Assert.assertSame(org.testng.Assert.assertSame) Assert.assertNull(org.testng.Assert.assertNull) Assert.assertEquals(org.testng.Assert.assertEquals) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) Assert.assertNotNull(org.testng.Assert.assertNotNull) Executors(java.util.concurrent.Executors) Slf4j(lombok.extern.slf4j.Slf4j) Function(org.apache.pulsar.functions.api.Function) Assert(org.testng.Assert) AsyncFuncRequest(org.apache.pulsar.functions.instance.JavaInstance.AsyncFuncRequest) ExecutorService(java.util.concurrent.ExecutorService) Record(org.apache.pulsar.functions.api.Record) Mockito.mock(org.mockito.Mockito.mock) AsyncFuncRequest(org.apache.pulsar.functions.instance.JavaInstance.AsyncFuncRequest) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutorService(java.util.concurrent.ExecutorService) Test(org.testng.annotations.Test)

Example 3 with AsyncFuncRequest

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();
}
Also used : Assert.assertSame(org.testng.Assert.assertSame) Assert.assertNull(org.testng.Assert.assertNull) Assert.assertEquals(org.testng.Assert.assertEquals) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) Assert.assertNotNull(org.testng.Assert.assertNotNull) Executors(java.util.concurrent.Executors) Slf4j(lombok.extern.slf4j.Slf4j) Function(org.apache.pulsar.functions.api.Function) Assert(org.testng.Assert) AsyncFuncRequest(org.apache.pulsar.functions.instance.JavaInstance.AsyncFuncRequest) ExecutorService(java.util.concurrent.ExecutorService) Record(org.apache.pulsar.functions.api.Record) Mockito.mock(org.mockito.Mockito.mock) AsyncFuncRequest(org.apache.pulsar.functions.instance.JavaInstance.AsyncFuncRequest) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutorService(java.util.concurrent.ExecutorService) Test(org.testng.annotations.Test)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)3 ExecutorService (java.util.concurrent.ExecutorService)3 Executors (java.util.concurrent.Executors)3 Cleanup (lombok.Cleanup)3 Slf4j (lombok.extern.slf4j.Slf4j)3 Function (org.apache.pulsar.functions.api.Function)3 Record (org.apache.pulsar.functions.api.Record)3 AsyncFuncRequest (org.apache.pulsar.functions.instance.JavaInstance.AsyncFuncRequest)3 Mockito.mock (org.mockito.Mockito.mock)3 Assert (org.testng.Assert)3 Assert.assertEquals (org.testng.Assert.assertEquals)3 Assert.assertNotNull (org.testng.Assert.assertNotNull)3 Assert.assertNull (org.testng.Assert.assertNull)3 Assert.assertSame (org.testng.Assert.assertSame)3 Test (org.testng.annotations.Test)3