Search in sources :

Example 16 with EchoRequest

use of org.opennms.core.rpc.echo.EchoRequest in project opennms by OpenNMS.

the class EchoRpcIT method throwsRequestTimedOutExceptionOnTimeout.

@Test(timeout = CamelRpcClientPreProcessor.CAMEL_JMS_REQUEST_TIMEOUT_DEFAULT * 4)
public void throwsRequestTimedOutExceptionOnTimeout() throws Exception {
    assertNotEquals(REMOTE_LOCATION_NAME, identity.getLocation());
    EchoRpcModule echoRpcModule = new EchoRpcModule();
    CamelContext context = getContext();
    context.getShutdownStrategy().setTimeout(5);
    context.getShutdownStrategy().setTimeUnit(TimeUnit.SECONDS);
    CamelRpcServerRouteManager routeManager = getRouteManager(context);
    routeManager.bind(echoRpcModule);
    EchoRequest request = new EchoRequest("HELLO!!!");
    request.setLocation(REMOTE_LOCATION_NAME);
    request.setDelay(CamelRpcClientPreProcessor.CAMEL_JMS_REQUEST_TIMEOUT_DEFAULT * 2);
    try {
        echoClient.execute(request).get();
        fail("Did not get ExecutionException");
    } catch (ExecutionException e) {
        assertTrue("Cause is not of type RequestTimedOutException: " + ExceptionUtils.getStackTrace(e), e.getCause() instanceof RequestTimedOutException);
        // Verify that the exchange error was logged
        MockLogAppender.assertLogMatched(Level.ERROR, "Message History");
        MockLogAppender.assertLogMatched(Level.ERROR, "direct://executeRpc");
        // Verify that the message body was suppressed
        MockLogAppender.assertNoLogMatched(Level.ERROR, "HELLO!!!");
    }
    routeManager.unbind(echoRpcModule);
    context.stop();
}
Also used : CamelContext(org.apache.camel.CamelContext) EchoRpcModule(org.opennms.core.rpc.echo.EchoRpcModule) RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) EchoRequest(org.opennms.core.rpc.echo.EchoRequest) RemoteExecutionException(org.opennms.core.rpc.api.RemoteExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 17 with EchoRequest

use of org.opennms.core.rpc.echo.EchoRequest in project opennms by OpenNMS.

the class EchoRpcIT method failsWithTimeoutWhenSystemIdDoesNotExist.

/**
 * Issues a RPC to a location at which a listener is registered,
 * but specifies a system id that is not equal to the listener's.
 * Since no matching system can process the request, the request
 * should time out.
 */
@Test(timeout = 60000)
public void failsWithTimeoutWhenSystemIdDoesNotExist() throws Exception {
    assertNotEquals(REMOTE_LOCATION_NAME, identity.getLocation());
    EchoRpcModule echoRpcModule = new EchoRpcModule();
    CamelContext context = getContext();
    context.start();
    MinionIdentity minionIdentity = new MockMinionIdentity(REMOTE_LOCATION_NAME);
    CamelRpcServerRouteManager routeManager = getRouteManager(context);
    routeManager.bind(echoRpcModule);
    EchoRequest request = new EchoRequest("HELLO!!!");
    // Use a different system id, other than the one that's actually listening
    request.setSystemId(minionIdentity.getId() + "!");
    request.setLocation(REMOTE_LOCATION_NAME);
    try {
        echoClient.execute(request).get();
        fail("Did not get ExecutionException");
    } catch (ExecutionException e) {
        assertTrue("Cause is not of type RequestTimedOutException: " + ExceptionUtils.getStackTrace(e), e.getCause() instanceof RequestTimedOutException);
    }
    routeManager.unbind(echoRpcModule);
    context.stop();
}
Also used : CamelContext(org.apache.camel.CamelContext) EchoRpcModule(org.opennms.core.rpc.echo.EchoRpcModule) RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) EchoRequest(org.opennms.core.rpc.echo.EchoRequest) MinionIdentity(org.opennms.minion.core.api.MinionIdentity) RemoteExecutionException(org.opennms.core.rpc.api.RemoteExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 18 with EchoRequest

use of org.opennms.core.rpc.echo.EchoRequest in project opennms by OpenNMS.

the class EchoRpcThreadIT method canProcessManyRequestsAsynchronously.

@Test(timeout = 60000)
public void canProcessManyRequestsAsynchronously() throws Exception {
    // Execute a request via a remote location
    assertNotEquals(REMOTE_LOCATION_NAME, identity.getLocation());
    // Lock the run method in our RPC module, we want to validate
    // the number of threads that are "running" the module
    CompletableFuture<Integer> runLockedFuture = lockingRpcModule.getRunLocker().waitForThreads(NTHREADS);
    // Fire off NTHREADS request
    ThreadLockingEchoClient client = new ThreadLockingEchoClient(rpcClientFactory, lockingRpcModule);
    List<CompletableFuture<EchoResponse>> futures = new ArrayList<>();
    for (int i = 0; i < NTHREADS; i++) {
        EchoRequest request = new EchoRequest("ping");
        request.setTimeToLiveMs(30000L);
        request.setLocation(REMOTE_LOCATION_NAME);
        futures.add(client.execute(request));
    }
    // Wait for all the threads calling run() to be locked
    runLockedFuture.get();
    // Release and verify that all the futures return
    lockingRpcModule.getRunLocker().release();
    CompletableFuture.allOf(futures.toArray(new CompletableFuture<?>[NTHREADS])).get();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) EchoRequest(org.opennms.core.rpc.echo.EchoRequest) Test(org.junit.Test) CamelBlueprintTest(org.opennms.core.test.camel.CamelBlueprintTest)

Aggregations

EchoRequest (org.opennms.core.rpc.echo.EchoRequest)18 Test (org.junit.Test)14 EchoResponse (org.opennms.core.rpc.echo.EchoResponse)11 CamelContext (org.apache.camel.CamelContext)8 EchoRpcModule (org.opennms.core.rpc.echo.EchoRpcModule)8 ExecutionException (java.util.concurrent.ExecutionException)6 RemoteExecutionException (org.opennms.core.rpc.api.RemoteExecutionException)5 DefaultExchange (org.apache.camel.impl.DefaultExchange)3 CamelBlueprintTest (org.opennms.core.test.camel.CamelBlueprintTest)3 ConsoleReporter (com.codahale.metrics.ConsoleReporter)2 Counter (com.codahale.metrics.Counter)2 Histogram (com.codahale.metrics.Histogram)2 MetricRegistry (com.codahale.metrics.MetricRegistry)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 RequestTimedOutException (org.opennms.core.rpc.api.RequestTimedOutException)2 MinionIdentity (org.opennms.minion.core.api.MinionIdentity)2 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 RequestRejectedException (org.opennms.core.rpc.api.RequestRejectedException)1 MyEchoException (org.opennms.core.rpc.echo.MyEchoException)1