Search in sources :

Example 1 with TestConsumer

use of com.navercorp.test.pinpoint.plugin.rabbitmq.TestConsumer in project pinpoint by naver.

the class RabbitMQTestRunner method runPushTest.

void runPushTest(int numMessages) throws Exception {
    final String message = "hello rabbit mq";
    // producer side
    final Connection producerConnection = connectionFactory.newConnection();
    final Channel producerChannel = producerConnection.createChannel();
    producerChannel.exchangeDeclare(RabbitMQTestConstants.EXCHANGE, "direct", false);
    producerChannel.queueDeclare(RabbitMQTestConstants.QUEUE_PUSH, false, false, false, null);
    producerChannel.queueBind(RabbitMQTestConstants.QUEUE_PUSH, RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PUSH);
    for (int i = 0; i < numMessages; i++) {
        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
        producerChannel.basicPublish(RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PUSH, false, false, builder.appId("test").build(), message.getBytes());
    }
    producerChannel.close();
    producerConnection.close();
    // consumer side
    final Connection consumerConnection = connectionFactory.newConnection();
    final Channel consumerChannel = consumerConnection.createChannel();
    final String remoteAddress = consumerConnection.getAddress().getHostAddress() + ":" + consumerConnection.getPort();
    consumerChannel.queueDeclare(RabbitMQTestConstants.QUEUE_PUSH, false, false, false, null);
    TestConsumer<String> consumer = new TestConsumer<>(consumerChannel, MessageConverter.FOR_TEST);
    consumerChannel.basicConsume(RabbitMQTestConstants.QUEUE_PUSH, true, consumer);
    List<String> actualMessages = new ArrayList<>(numMessages);
    for (int i = 0; i < numMessages; i++) {
        actualMessages.add(consumer.getMessage(10, TimeUnit.SECONDS));
    }
    Assert.assertEquals(numMessages, actualMessages.size());
    for (String actualMessage : actualMessages) {
        Assert.assertEquals(message, actualMessage);
    }
    consumerChannel.close();
    consumerConnection.close();
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    // Wait till all traces are recorded (consumer traces are recorded from another thread)
    int expectedTraceCountPerMessage = 6;
    awaitAndVerifyTraceCount(verifier, expectedTraceCountPerMessage * numMessages, 5000L);
    verifier.printCache();
    Class<?> producerChannelClass = producerChannel.getClass();
    Method channelBasicPublish = producerChannelClass.getDeclaredMethod("basicPublish", String.class, String.class, boolean.class, boolean.class, AMQP.BasicProperties.class, byte[].class);
    ExpectedTrace channelBasicPublishTrace = Expectations.event(// serviceType
    RabbitMQTestConstants.RABBITMQ_CLIENT, // method
    channelBasicPublish, // rpc
    null, // endPoint
    remoteAddress, // destinationId
    "exchange-" + RabbitMQTestConstants.EXCHANGE, Expectations.annotation("rabbitmq.exchange", RabbitMQTestConstants.EXCHANGE), Expectations.annotation("rabbitmq.routingkey", RabbitMQTestConstants.ROUTING_KEY_PUSH));
    ExpectedTrace rabbitMqConsumerInvocationTrace = Expectations.root(// serviceType
    RabbitMQTestConstants.RABBITMQ_CLIENT, // method
    "RabbitMQ Consumer Invocation", // rpc
    "rabbitmq://exchange=" + RabbitMQTestConstants.EXCHANGE, // endPoint (collected but API to retrieve local address is not available in all versions, so skip)
    null, // remoteAddress
    remoteAddress, Expectations.annotation("rabbitmq.routingkey", RabbitMQTestConstants.ROUTING_KEY_PUSH));
    Class<?> consumerDispatchClass = Class.forName("com.rabbitmq.client.impl.ConsumerDispatcher");
    Method consumerDispatchHandleDelivery = consumerDispatchClass.getDeclaredMethod("handleDelivery", Consumer.class, String.class, Envelope.class, AMQP.BasicProperties.class, byte[].class);
    ExpectedTrace consumerDispatcherHandleDeliveryTrace = Expectations.event(RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL, // method
    consumerDispatchHandleDelivery);
    ExpectedTrace asynchronousInvocationTrace = Expectations.event(ServiceType.ASYNC.getName(), "Asynchronous Invocation");
    Class<?> consumerClass = consumer.getClass();
    Method consumerHandleDelivery = consumerClass.getDeclaredMethod("handleDelivery", String.class, Envelope.class, AMQP.BasicProperties.class, byte[].class);
    ExpectedTrace consumerHandleDeliveryTrace = Expectations.event(RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL, consumerHandleDelivery);
    Class<?> propagationMarkerClass = PropagationMarker.class;
    Method propagationMarkerMark = propagationMarkerClass.getDeclaredMethod("mark");
    ExpectedTrace markTrace = Expectations.event(ServiceType.INTERNAL_METHOD.getName(), propagationMarkerMark);
    for (int i = 0; i < numMessages; i++) {
        verifier.verifyDiscreteTrace(channelBasicPublishTrace);
        verifier.verifyDiscreteTrace(rabbitMqConsumerInvocationTrace, Expectations.async(consumerDispatcherHandleDeliveryTrace, asynchronousInvocationTrace, consumerHandleDeliveryTrace, markTrace));
    }
    verifier.verifyTraceCount(0);
}
Also used : ExpectedTrace(com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedTrace) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) TestConsumer(com.navercorp.test.pinpoint.plugin.rabbitmq.TestConsumer) PropagationMarker(com.navercorp.test.pinpoint.plugin.rabbitmq.PropagationMarker) AMQP(com.rabbitmq.client.AMQP)

Aggregations

ExpectedTrace (com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedTrace)1 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)1 PropagationMarker (com.navercorp.test.pinpoint.plugin.rabbitmq.PropagationMarker)1 TestConsumer (com.navercorp.test.pinpoint.plugin.rabbitmq.TestConsumer)1 AMQP (com.rabbitmq.client.AMQP)1 Channel (com.rabbitmq.client.Channel)1 Connection (com.rabbitmq.client.Connection)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1