cshannon commented on code in PR #1659: URL: https://github.com/apache/activemq/pull/1659#discussion_r2806195369
########## activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQTextMessageStressTest.java: ########## @@ -0,0 +1,176 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.command; + +import jakarta.jms.Connection; +import jakarta.jms.MessageConsumer; +import jakarta.jms.Session; +import jakarta.jms.Topic; +import jakarta.jms.MessageProducer; +import jakarta.jms.TextMessage; +import jakarta.jms.JMSException; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.broker.BrokerService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; +import java.util.ArrayList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertEquals; + +public class ActiveMQTextMessageStressTest { + + private BrokerService broker; + private Connection connection; + + @Before + public void setUp() throws Exception { + broker = new BrokerService(); + broker.setPersistent(false); + broker.addConnector("vm://localhost"); + broker.start(); + + ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost"); + connection = cf.createConnection(); + connection.setClientID("HIGH_CONC_TEST"); // needed for durable subscribers + connection.start(); + } + + @After + public void tearDown() throws Exception { + if (connection != null) connection.close(); + if (broker != null) broker.stop(); + } + + @Test + public void testConcurrentProducersAndConsumers() throws Exception { + final int MESSAGE_COUNT = 100; + final int PRODUCERS = 5; + final int DURABLE_CONSUMERS = 0; + final int NON_DURABLE_CONSUMERS = 5; + + // Topic + Session tmpSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + Topic topic = tmpSession.createTopic("HIGH_CONC.TOPIC"); + + // Consumers + List<MessageConsumer> consumers = new ArrayList<>(); + List<Session> consumerSessions = new ArrayList<>(); + for (int i = 1; i <= DURABLE_CONSUMERS; i++) { + Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + consumers.add(s.createDurableSubscriber(topic, "Durable-" + i)); + consumerSessions.add(s); + } + for (int i = 1; i <= NON_DURABLE_CONSUMERS; i++) { + Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + consumers.add(s.createConsumer(topic)); + consumerSessions.add(s); + } + + ExecutorService executor = Executors.newFixedThreadPool(PRODUCERS + consumers.size()); + + // Produce messages concurrently + CountDownLatch producerLatch = new CountDownLatch(PRODUCERS); + for (int p = 1; p <= PRODUCERS; p++) { + final int producerId = p; + executor.submit(() -> { + try { + Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer producer = s.createProducer(topic); + for (int m = 1; m <= MESSAGE_COUNT; m++) { + TextMessage msg = s.createTextMessage("P" + producerId + "-M" + m); + producer.send(msg); + } + } catch (JMSException e) { + e.printStackTrace(); Review Comment: I wouldn't use print stacktrace here, the exception should be handled better. We should either fail the test or in this case maybe just log it at debug level if we don't care. ########## activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQTextMessageStressTest.java: ########## @@ -0,0 +1,176 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.command; + +import jakarta.jms.Connection; +import jakarta.jms.MessageConsumer; +import jakarta.jms.Session; +import jakarta.jms.Topic; +import jakarta.jms.MessageProducer; +import jakarta.jms.TextMessage; +import jakarta.jms.JMSException; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.broker.BrokerService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; +import java.util.ArrayList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertEquals; + +public class ActiveMQTextMessageStressTest { + + private BrokerService broker; + private Connection connection; + + @Before + public void setUp() throws Exception { + broker = new BrokerService(); + broker.setPersistent(false); + broker.addConnector("vm://localhost"); + broker.start(); + + ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost"); + connection = cf.createConnection(); + connection.setClientID("HIGH_CONC_TEST"); // needed for durable subscribers + connection.start(); + } + + @After + public void tearDown() throws Exception { + if (connection != null) connection.close(); + if (broker != null) broker.stop(); + } + + @Test + public void testConcurrentProducersAndConsumers() throws Exception { + final int MESSAGE_COUNT = 100; + final int PRODUCERS = 5; + final int DURABLE_CONSUMERS = 0; + final int NON_DURABLE_CONSUMERS = 5; + + // Topic + Session tmpSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + Topic topic = tmpSession.createTopic("HIGH_CONC.TOPIC"); + + // Consumers + List<MessageConsumer> consumers = new ArrayList<>(); + List<Session> consumerSessions = new ArrayList<>(); + for (int i = 1; i <= DURABLE_CONSUMERS; i++) { Review Comment: DURABLE_CONSUMERS is always 0, so this should either be changed or removed. ########## activemq-client/src/main/java/org/apache/activemq/command/ActiveMQTextMessage.java: ########## @@ -54,9 +54,11 @@ public Message copy() { return copy; } - private void copy(ActiveMQTextMessage copy) { - super.copy(copy); - copy.text = text; + protected void copy(ActiveMQTextMessage copy) { Review Comment: I agree, leaving it private here makes sense unless we need to change it (we can always change it later) ########## activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQTextMessageStressTest.java: ########## @@ -0,0 +1,176 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.command; + +import jakarta.jms.Connection; +import jakarta.jms.MessageConsumer; +import jakarta.jms.Session; +import jakarta.jms.Topic; +import jakarta.jms.MessageProducer; +import jakarta.jms.TextMessage; +import jakarta.jms.JMSException; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.broker.BrokerService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; +import java.util.ArrayList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertEquals; + +public class ActiveMQTextMessageStressTest { + + private BrokerService broker; + private Connection connection; + + @Before + public void setUp() throws Exception { + broker = new BrokerService(); + broker.setPersistent(false); + broker.addConnector("vm://localhost"); + broker.start(); + + ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost"); + connection = cf.createConnection(); + connection.setClientID("HIGH_CONC_TEST"); // needed for durable subscribers + connection.start(); + } + + @After + public void tearDown() throws Exception { + if (connection != null) connection.close(); + if (broker != null) broker.stop(); + } + + @Test + public void testConcurrentProducersAndConsumers() throws Exception { + final int MESSAGE_COUNT = 100; + final int PRODUCERS = 5; + final int DURABLE_CONSUMERS = 0; + final int NON_DURABLE_CONSUMERS = 5; + + // Topic + Session tmpSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + Topic topic = tmpSession.createTopic("HIGH_CONC.TOPIC"); + + // Consumers + List<MessageConsumer> consumers = new ArrayList<>(); + List<Session> consumerSessions = new ArrayList<>(); + for (int i = 1; i <= DURABLE_CONSUMERS; i++) { + Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + consumers.add(s.createDurableSubscriber(topic, "Durable-" + i)); + consumerSessions.add(s); + } + for (int i = 1; i <= NON_DURABLE_CONSUMERS; i++) { + Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + consumers.add(s.createConsumer(topic)); + consumerSessions.add(s); + } + + ExecutorService executor = Executors.newFixedThreadPool(PRODUCERS + consumers.size()); + + // Produce messages concurrently + CountDownLatch producerLatch = new CountDownLatch(PRODUCERS); + for (int p = 1; p <= PRODUCERS; p++) { + final int producerId = p; + executor.submit(() -> { + try { + Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer producer = s.createProducer(topic); + for (int m = 1; m <= MESSAGE_COUNT; m++) { + TextMessage msg = s.createTextMessage("P" + producerId + "-M" + m); + producer.send(msg); + } + } catch (JMSException e) { + e.printStackTrace(); + } finally { + producerLatch.countDown(); + } + }); + } + +// Consume messages concurrently + List<Future<List<TextMessage>>> consumerFutures = new ArrayList<>(); + for (MessageConsumer consumer : consumers) { + consumerFutures.add(executor.submit(() -> { + List<TextMessage> received = new ArrayList<>(); + for (int i = 0; i < MESSAGE_COUNT * PRODUCERS; i++) { + TextMessage msg = (TextMessage) consumer.receive(5000); + assertNotNull("Consumer should receive a message", msg); + + // loop to increase the chance of hitting the race condition + // while other consumers are doing the same to their copies. + for (int j = 0; j < 50; j++) { + String txt = msg.getText(); + assertNotNull("Text should never be null during stress", txt); + + // It clears the 'text' field and forces the next getText() + ((org.apache.activemq.command.ActiveMQTextMessage)msg).clearUnMarshalledState(); + } + + received.add(msg); + } + return received; + })); + } + +// Wait for producers and consumers Review Comment: Indentation here is off and needs to be fixed ########## activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQTextMessageStressTest.java: ########## @@ -0,0 +1,176 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.command; + +import jakarta.jms.Connection; +import jakarta.jms.MessageConsumer; +import jakarta.jms.Session; +import jakarta.jms.Topic; +import jakarta.jms.MessageProducer; +import jakarta.jms.TextMessage; +import jakarta.jms.JMSException; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.broker.BrokerService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; +import java.util.ArrayList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertEquals; + +public class ActiveMQTextMessageStressTest { + + private BrokerService broker; + private Connection connection; + + @Before + public void setUp() throws Exception { + broker = new BrokerService(); + broker.setPersistent(false); + broker.addConnector("vm://localhost"); + broker.start(); + + ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost"); + connection = cf.createConnection(); + connection.setClientID("HIGH_CONC_TEST"); // needed for durable subscribers + connection.start(); + } + + @After + public void tearDown() throws Exception { + if (connection != null) connection.close(); Review Comment: Nitpicking but you should use braces even for one line statements. ########## activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQTextMessageStressTest.java: ########## @@ -0,0 +1,176 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.command; + +import jakarta.jms.Connection; +import jakarta.jms.MessageConsumer; +import jakarta.jms.Session; +import jakarta.jms.Topic; +import jakarta.jms.MessageProducer; +import jakarta.jms.TextMessage; +import jakarta.jms.JMSException; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.broker.BrokerService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; +import java.util.ArrayList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertEquals; + +public class ActiveMQTextMessageStressTest { + + private BrokerService broker; + private Connection connection; + + @Before + public void setUp() throws Exception { + broker = new BrokerService(); + broker.setPersistent(false); + broker.addConnector("vm://localhost"); + broker.start(); + + ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost"); + connection = cf.createConnection(); + connection.setClientID("HIGH_CONC_TEST"); // needed for durable subscribers + connection.start(); + } + + @After + public void tearDown() throws Exception { + if (connection != null) connection.close(); + if (broker != null) broker.stop(); + } + + @Test + public void testConcurrentProducersAndConsumers() throws Exception { + final int MESSAGE_COUNT = 100; + final int PRODUCERS = 5; + final int DURABLE_CONSUMERS = 0; + final int NON_DURABLE_CONSUMERS = 5; + + // Topic + Session tmpSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + Topic topic = tmpSession.createTopic("HIGH_CONC.TOPIC"); + + // Consumers + List<MessageConsumer> consumers = new ArrayList<>(); + List<Session> consumerSessions = new ArrayList<>(); + for (int i = 1; i <= DURABLE_CONSUMERS; i++) { + Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + consumers.add(s.createDurableSubscriber(topic, "Durable-" + i)); + consumerSessions.add(s); + } + for (int i = 1; i <= NON_DURABLE_CONSUMERS; i++) { + Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + consumers.add(s.createConsumer(topic)); + consumerSessions.add(s); + } + + ExecutorService executor = Executors.newFixedThreadPool(PRODUCERS + consumers.size()); + + // Produce messages concurrently + CountDownLatch producerLatch = new CountDownLatch(PRODUCERS); + for (int p = 1; p <= PRODUCERS; p++) { + final int producerId = p; + executor.submit(() -> { + try { + Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer producer = s.createProducer(topic); + for (int m = 1; m <= MESSAGE_COUNT; m++) { + TextMessage msg = s.createTextMessage("P" + producerId + "-M" + m); + producer.send(msg); + } + } catch (JMSException e) { + e.printStackTrace(); + } finally { + producerLatch.countDown(); + } + }); + } + +// Consume messages concurrently + List<Future<List<TextMessage>>> consumerFutures = new ArrayList<>(); + for (MessageConsumer consumer : consumers) { + consumerFutures.add(executor.submit(() -> { + List<TextMessage> received = new ArrayList<>(); + for (int i = 0; i < MESSAGE_COUNT * PRODUCERS; i++) { + TextMessage msg = (TextMessage) consumer.receive(5000); + assertNotNull("Consumer should receive a message", msg); + + // loop to increase the chance of hitting the race condition + // while other consumers are doing the same to their copies. + for (int j = 0; j < 50; j++) { + String txt = msg.getText(); + assertNotNull("Text should never be null during stress", txt); + + // It clears the 'text' field and forces the next getText() + ((org.apache.activemq.command.ActiveMQTextMessage)msg).clearUnMarshalledState(); + } + + received.add(msg); + } + return received; + })); + } + +// Wait for producers and consumers + producerLatch.await(); + List<List<TextMessage>> allConsumed = new ArrayList<>(); + for (Future<List<TextMessage>> f : consumerFutures) { + allConsumed.add(f.get(30, TimeUnit.SECONDS)); + } + +// VALIDATION LOGIC Review Comment: Indentation here is off and needs to be fixed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information, visit: https://activemq.apache.org/contact
