From 0411242fddb16edba38ead18fa99dd9a79a183bc Mon Sep 17 00:00:00 2001 From: Fabian Gonzalez Date: Thu, 20 Jul 2017 13:06:08 -0300 Subject: [PATCH 1/2] AMQ-6775: Race condition makes messages queued in the session be delivered before messages queued in consumers. --- .../org/apache/activemq/ActiveMQSessionExecutor.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/activemq-client/src/main/java/org/apache/activemq/ActiveMQSessionExecutor.java b/activemq-client/src/main/java/org/apache/activemq/ActiveMQSessionExecutor.java index 357815561c9..9d49f1510e1 100644 --- a/activemq-client/src/main/java/org/apache/activemq/ActiveMQSessionExecutor.java +++ b/activemq-client/src/main/java/org/apache/activemq/ActiveMQSessionExecutor.java @@ -123,15 +123,16 @@ public boolean hasUncomsumedMessages() { return !messageQueue.isClosed() && messageQueue.isRunning() && !messageQueue.isEmpty(); } - void dispatch(MessageDispatch message) { + boolean dispatch(MessageDispatch message) { // TODO - we should use a Map for this indexed by consumerId for (ActiveMQMessageConsumer consumer : this.session.consumers) { ConsumerId consumerId = message.getConsumerId(); - if (consumerId.equals(consumer.getConsumerId())) { + if (consumerId.equals(consumer.getConsumerId()) && consumer.unconsumedMessages.isEmpty()) { consumer.dispatch(message); - break; + return true; } } + return false; } synchronized void start() { @@ -199,7 +200,10 @@ public boolean iterate() { if (message == null) { return false; } else { - dispatch(message); + if (!dispatch(message)) { + messageQueue.enqueue(message); + } + return !messageQueue.isEmpty(); } } From 9fd86cda6827db1308ba006a71d6f1b0a9e71d77 Mon Sep 17 00:00:00 2001 From: Fabian Gonzalez Date: Thu, 24 Aug 2017 12:25:50 -0300 Subject: [PATCH 2/2] Added test for AMQ-6775. --- activemq-client/pom.xml | 5 ++ .../activemq/ActiveMQOrderLostTest.java | 87 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 activemq-client/src/test/java/org/apache/activemq/ActiveMQOrderLostTest.java diff --git a/activemq-client/pom.xml b/activemq-client/pom.xml index 0a6f8e19495..6b7f082aa8b 100644 --- a/activemq-client/pom.xml +++ b/activemq-client/pom.xml @@ -79,6 +79,11 @@ junit test + + org.mockito + mockito-core + test + org.slf4j slf4j-log4j12 diff --git a/activemq-client/src/test/java/org/apache/activemq/ActiveMQOrderLostTest.java b/activemq-client/src/test/java/org/apache/activemq/ActiveMQOrderLostTest.java new file mode 100644 index 00000000000..709c542ef78 --- /dev/null +++ b/activemq-client/src/test/java/org/apache/activemq/ActiveMQOrderLostTest.java @@ -0,0 +1,87 @@ +/** + * 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; + +import java.lang.reflect.Field; + +import org.apache.activemq.command.ConnectionId; +import org.apache.activemq.command.ConnectionInfo; +import org.apache.activemq.command.ConsumerId; +import org.apache.activemq.command.MessageDispatch; +import org.apache.activemq.command.SessionId; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +/** + * This test verifies that when there seems that there are no messages left queued on the listeners, + * no messages are dispatched from the session if the listener queue is not empty. + */ +public class ActiveMQOrderLostTest { + + private ConnectionInfo connectionInfo = Mockito.mock(ConnectionInfo.class); + + private ActiveMQConnection connection = Mockito.mock(ActiveMQConnection.class); + + private ConsumerId consumerId = new ConsumerId(); + + private SessionId sessionId = Mockito.mock(SessionId.class); + + private ActiveMQMessageConsumer consumer = Mockito.mock(ActiveMQMessageConsumer.class); + + private ActiveMQSessionExecutor executor; + + @Before + public void before() throws Exception + { + Mockito.when(connection.getConnectionInfo()).thenReturn(connectionInfo); + Mockito.when(connectionInfo.getConnectionId()).thenReturn(new ConnectionId()); + Mockito.when(connection.isMessagePrioritySupported()).thenReturn(false); + Field unconsumedMessages = ActiveMQMessageConsumer.class.getDeclaredField("unconsumedMessages"); + unconsumedMessages.setAccessible(true); + Mockito.when(consumer.isDurableSubscriber()).thenReturn(false); + Mockito.when(consumer.getConsumerId()).thenReturn(consumerId); + MessageDispatchChannel channel = Mockito.mock(MessageDispatchChannel.class); + unconsumedMessages.set(consumer, channel); + Mockito.when(consumer.unconsumedMessages.isEmpty()).thenReturn(false); + Mockito.doAnswer(new Answer() { + + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + return null; + } + }).when(connection).addDispatcher(Mockito.any(), Mockito.any()); + ActiveMQSession session = new ActiveMQSession(connection, sessionId, 1, true); + session.addConsumer(consumer); + executor = new ActiveMQSessionExecutor(session); + Field field = ActiveMQSessionExecutor.class.getDeclaredField("messageQueue"); + MessageDispatchChannel messageDispatchChannel = Mockito.mock(MessageDispatchChannel.class); + MessageDispatch message = Mockito.mock(MessageDispatch.class); + Mockito.when(message.getConsumerId()).thenReturn(consumerId); + Mockito.when(messageDispatchChannel.dequeueNoWait()).thenReturn(message); + field.setAccessible(true); + field.set(executor, messageDispatchChannel); + } + @Test + public void orderLostWhenUnconsumedMessagesAreNotStarted() throws Exception { + executor.iterate(); + Mockito.verify(consumer, Mockito.never()).dispatch(Mockito.any()); + } +}