|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.activemq.transport.vm; |
| 18 | + |
| 19 | +import jakarta.jms.Connection; |
| 20 | +import jakarta.jms.MessageConsumer; |
| 21 | +import jakarta.jms.Session; |
| 22 | +import jakarta.jms.Topic; |
| 23 | +import jakarta.jms.MessageProducer; |
| 24 | +import jakarta.jms.TextMessage; |
| 25 | +import jakarta.jms.JMSException; |
| 26 | + |
| 27 | +import org.apache.activemq.ActiveMQConnectionFactory; |
| 28 | +import org.apache.activemq.broker.BrokerService; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +import java.util.List; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.concurrent.ExecutorService; |
| 36 | +import java.util.concurrent.Executors; |
| 37 | +import java.util.concurrent.CountDownLatch; |
| 38 | +import java.util.concurrent.Future; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | + |
| 41 | +import static org.junit.Assert.assertNotNull; |
| 42 | +import static org.junit.Assert.assertNotSame; |
| 43 | + |
| 44 | + |
| 45 | +public class VMTransportDefensiveCopyTest { |
| 46 | + |
| 47 | + private BrokerService broker; |
| 48 | + private Connection connection; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setUp() throws Exception { |
| 52 | + broker = new BrokerService(); |
| 53 | + broker.setPersistent(false); |
| 54 | + broker.addConnector("vm://localhost"); |
| 55 | + broker.start(); |
| 56 | + |
| 57 | + ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost"); |
| 58 | + connection = cf.createConnection(); |
| 59 | + connection.setClientID("HIGH_CONC_TEST"); // needed for durable subscribers |
| 60 | + connection.start(); |
| 61 | + } |
| 62 | + |
| 63 | + @After |
| 64 | + public void tearDown() throws Exception { |
| 65 | + if (connection != null) connection.close(); |
| 66 | + if (broker != null) broker.stop(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testConcurrentProducersAndConsumers() throws Exception { |
| 71 | + final int MESSAGE_COUNT = 100; |
| 72 | + final int PRODUCERS = 5; |
| 73 | + final int DURABLE_CONSUMERS = 2; |
| 74 | + final int NON_DURABLE_CONSUMERS = 3; |
| 75 | + |
| 76 | + // Topic |
| 77 | + Session tmpSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 78 | + Topic topic = tmpSession.createTopic("HIGH_CONC.TOPIC"); |
| 79 | + |
| 80 | + // Consumers |
| 81 | + List<MessageConsumer> consumers = new ArrayList<>(); |
| 82 | + List<Session> consumerSessions = new ArrayList<>(); |
| 83 | + for (int i = 1; i <= DURABLE_CONSUMERS; i++) { |
| 84 | + Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 85 | + consumers.add(s.createDurableSubscriber(topic, "Durable-" + i)); |
| 86 | + consumerSessions.add(s); |
| 87 | + } |
| 88 | + for (int i = 1; i <= NON_DURABLE_CONSUMERS; i++) { |
| 89 | + Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 90 | + consumers.add(s.createConsumer(topic)); |
| 91 | + consumerSessions.add(s); |
| 92 | + } |
| 93 | + |
| 94 | + ExecutorService executor = Executors.newFixedThreadPool(PRODUCERS + consumers.size()); |
| 95 | + |
| 96 | + // Produce messages concurrently |
| 97 | + CountDownLatch producerLatch = new CountDownLatch(PRODUCERS); |
| 98 | + for (int p = 1; p <= PRODUCERS; p++) { |
| 99 | + final int producerId = p; |
| 100 | + executor.submit(() -> { |
| 101 | + try { |
| 102 | + Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 103 | + MessageProducer producer = s.createProducer(topic); |
| 104 | + for (int m = 1; m <= MESSAGE_COUNT; m++) { |
| 105 | + TextMessage msg = s.createTextMessage("P" + producerId + "-M" + m); |
| 106 | + producer.send(msg); |
| 107 | + } |
| 108 | + } catch (JMSException e) { |
| 109 | + e.printStackTrace(); |
| 110 | + } finally { |
| 111 | + producerLatch.countDown(); |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + // Consume messages concurrently |
| 117 | + List<Future<List<String>>> consumerFutures = new ArrayList<>(); |
| 118 | + for (MessageConsumer consumer : consumers) { |
| 119 | + consumerFutures.add(executor.submit(() -> { |
| 120 | + List<String> received = new ArrayList<>(); |
| 121 | + for (int i = 0; i < MESSAGE_COUNT * PRODUCERS; i++) { |
| 122 | + TextMessage msg = (TextMessage) consumer.receive(5000); |
| 123 | + assertNotNull("Consumer should receive a message", msg); |
| 124 | + received.add(msg.getText()); |
| 125 | + } |
| 126 | + return received; |
| 127 | + })); |
| 128 | + } |
| 129 | + |
| 130 | + // Wait for producers to finish |
| 131 | + producerLatch.await(); |
| 132 | + |
| 133 | + // Collect and validate consumer messages |
| 134 | + List<List<String>> allConsumed = new ArrayList<>(); |
| 135 | + for (Future<List<String>> f : consumerFutures) { |
| 136 | + allConsumed.add(f.get(30, TimeUnit.SECONDS)); |
| 137 | + } |
| 138 | + |
| 139 | + // Check that each consumer received unique message instances |
| 140 | + for (int i = 0; i < allConsumed.size(); i++) { |
| 141 | + List<String> consumerMsgs = allConsumed.get(i); |
| 142 | + for (int j = i + 1; j < allConsumed.size(); j++) { |
| 143 | + List<String> otherMsgs = allConsumed.get(j); |
| 144 | + for (int k = 0; k < consumerMsgs.size(); k++) { |
| 145 | + assertNotSame( |
| 146 | + "Message instances should be independent across consumers", |
| 147 | + consumerMsgs.get(k), |
| 148 | + otherMsgs.get(k) |
| 149 | + ); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + executor.shutdownNow(); |
| 155 | + for (Session s : consumerSessions) s.close(); |
| 156 | + } |
| 157 | +} |
0 commit comments