Search in sources :

Example 1 with TestMessagePuller

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

the class RabbitMQTestRunner method runPullTest.

void runPullTest() 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_PULL, false, false, false, null);
    producerChannel.queueBind(RabbitMQTestConstants.QUEUE_PULL, RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PULL);
    AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
    producerChannel.basicPublish(RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PULL, false, false, builder.appId("test").build(), message.getBytes());
    producerChannel.close();
    producerConnection.close();
    // comsumer side
    final Connection consumerConnection = connectionFactory.newConnection();
    final Channel consumerChannel = consumerConnection.createChannel();
    final String remoteAddress = consumerConnection.getAddress().getHostAddress() + ":" + consumerConnection.getPort();
    TestMessagePuller messagePuller = new TestMessagePuller(consumerChannel);
    Assert.assertEquals(message, messagePuller.pullMessage(MessageConverter.FOR_TEST, RabbitMQTestConstants.QUEUE_PULL, true));
    consumerChannel.close();
    consumerConnection.close();
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    // Wait till all traces are recorded (consumer traces are recorded from another thread)
    awaitAndVerifyTraceCount(verifier, 5, 5000L);
    verifier.printCache();
    // verify producer traces
    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_PULL));
    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_PULL));
    Class<?> amqChannelClass = Class.forName("com.rabbitmq.client.impl.AMQChannel");
    Method handleCompleteInboundCommand = amqChannelClass.getDeclaredMethod("handleCompleteInboundCommand", AMQCommand.class);
    ExpectedTrace handleCompleteInboundCommandTrace = Expectations.event(// serviceType
    RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL, // method
    handleCompleteInboundCommand);
    ExpectedTrace[] producerTraces = { channelBasicPublishTrace };
    ExpectedTrace[] consumerTraces = { rabbitMqConsumerInvocationTrace, handleCompleteInboundCommandTrace };
    verifier.verifyDiscreteTrace(producerTraces);
    verifier.verifyDiscreteTrace(consumerTraces);
    // verify consumer traces
    Class<?> consumerChannelClass = consumerChannel.getClass();
    Method channelBasicGet = consumerChannelClass.getDeclaredMethod("basicGet", String.class, boolean.class);
    ExpectedTrace channelBasicGetTrace = Expectations.event(RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL, channelBasicGet);
    Class<?> propagationMarkerClass = PropagationMarker.class;
    Method propagationMarkerMark = propagationMarkerClass.getDeclaredMethod("mark");
    ExpectedTrace markTrace = Expectations.event(ServiceType.INTERNAL_METHOD.getName(), propagationMarkerMark);
    verifier.verifyDiscreteTrace(channelBasicGetTrace, markTrace);
    verifier.verifyTraceCount(0);
}
Also used : ExpectedTrace(com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedTrace) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) Method(java.lang.reflect.Method) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) PropagationMarker(com.navercorp.test.pinpoint.plugin.rabbitmq.PropagationMarker) TestMessagePuller(com.navercorp.test.pinpoint.plugin.rabbitmq.TestMessagePuller) 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 TestMessagePuller (com.navercorp.test.pinpoint.plugin.rabbitmq.TestMessagePuller)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