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);
}
Aggregations