|
| 1 | +package com.rabbitmq.client.test.server; |
| 2 | + |
| 3 | +import com.rabbitmq.client.Channel; |
| 4 | +import com.rabbitmq.client.Connection; |
| 5 | +import com.rabbitmq.client.ConnectionFactory; |
| 6 | +import com.rabbitmq.client.MessageProperties; |
| 7 | +import com.rabbitmq.client.QueueingConsumer; |
| 8 | +import com.rabbitmq.client.test.BrokerTestCase; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +/** |
| 13 | + * From bug 19844 - we want to be sure that publish vs everything else can't |
| 14 | + * happen out of order |
| 15 | + */ |
| 16 | +public class EffectVisibilityCrossNodeTest extends BrokerTestCase { |
| 17 | + private static final String exchange = "exchange"; |
| 18 | + |
| 19 | + private Channel secondaryChannel; |
| 20 | + private Connection secondaryConnection; |
| 21 | + |
| 22 | + private String[] queues = new String[QUEUES]; |
| 23 | + |
| 24 | + @Override |
| 25 | + public void openChannel() throws IOException { |
| 26 | + super.openChannel(); |
| 27 | + secondaryChannel = secondaryConnection.createChannel(); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void openConnection() throws IOException { |
| 32 | + super.openConnection(); |
| 33 | + if (secondaryConnection == null) { |
| 34 | + ConnectionFactory cf2 = connectionFactory.clone(); |
| 35 | + cf2.setHost("localhost"); |
| 36 | + cf2.setPort(5673); |
| 37 | + secondaryConnection = cf2.newConnection(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void closeChannel() throws IOException { |
| 43 | + if (secondaryChannel != null) { |
| 44 | + secondaryChannel.abort(); |
| 45 | + secondaryChannel = null; |
| 46 | + } |
| 47 | + super.closeChannel(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void closeConnection() throws IOException { |
| 52 | + if (secondaryConnection != null) { |
| 53 | + secondaryConnection.abort(); |
| 54 | + secondaryConnection = null; |
| 55 | + } |
| 56 | + super.closeConnection(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected void createResources() throws IOException { |
| 61 | + channel.exchangeDeclare(exchange, "fanout"); |
| 62 | + for (int i = 0; i < queues.length ; i++) { |
| 63 | + queues[i] = secondaryChannel.queueDeclare().getQueue(); |
| 64 | + secondaryChannel.queueBind(queues[i], exchange, ""); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void releaseResources() throws IOException { |
| 70 | + channel.exchangeDelete(exchange); |
| 71 | + } |
| 72 | + |
| 73 | + private static final int QUEUES = 5; |
| 74 | + private static final int COMMITS = 500; |
| 75 | + private static final int MESSAGES_PER_COMMIT = 10; |
| 76 | + |
| 77 | + public void testEffectVisibility() throws Exception { |
| 78 | + channel.txSelect(); |
| 79 | + |
| 80 | + for (int i = 0; i < COMMITS; i++) { |
| 81 | + for (int j = 0; j < MESSAGES_PER_COMMIT; j++) { |
| 82 | + channel.basicPublish(exchange, "", MessageProperties.MINIMAL_BASIC, ("" + (i * MESSAGES_PER_COMMIT + j)).getBytes()); |
| 83 | + } |
| 84 | + channel.txCommit(); |
| 85 | + |
| 86 | + for (int j = 0; j < MESSAGES_PER_COMMIT; j++) { |
| 87 | + channel.basicPublish(exchange, "", MessageProperties.MINIMAL_BASIC, "bad".getBytes()); |
| 88 | + } |
| 89 | + channel.txRollback(); |
| 90 | + } |
| 91 | + |
| 92 | + for (int i = 0; i < queues.length ; i++) { |
| 93 | + QueueingConsumer consumer = new QueueingConsumer(secondaryChannel); |
| 94 | + secondaryChannel.basicConsume(queues[i], true, consumer); |
| 95 | + |
| 96 | + for (int j = 0; j < MESSAGES_PER_COMMIT * COMMITS; j++) { |
| 97 | + QueueingConsumer.Delivery delivery = consumer.nextDelivery(1000); |
| 98 | + assertNotNull(delivery); |
| 99 | + int sequence = Integer.parseInt(new String(delivery.getBody())); |
| 100 | + |
| 101 | + assertEquals(j, sequence); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments