Major refactoring of AggregatingMessageHandler and related classes.

This commit is contained in:
Mark Fisher
2008-02-27 02:33:44 +00:00
parent 4c0b4f41f9
commit d2580a3b0b
9 changed files with 491 additions and 245 deletions

View File

@@ -68,26 +68,31 @@ public class AggregatingMessageHandlerTests {
}
@Test
public void testShouldFailOnTimeoutByDefault() throws InterruptedException {
public void testShouldNotSendPartialResultOnTimeoutByDefault() throws InterruptedException {
SimpleChannel discardChannel = new SimpleChannel();
AggregatingMessageHandler aggregator = new AggregatingMessageHandler(new TestAggregator());
aggregator.setTimeout(10);
aggregator.setTimeout(50);
aggregator.setReaperInterval(10);
aggregator.setDiscardChannel(discardChannel);
SimpleChannel replyChannel = new SimpleChannel();
Message<?> message1 = createMessage("123", "ABC", 2, 1, replyChannel);
Message<?> message = createMessage("123", "ABC", 2, 1, replyChannel);
CountDownLatch latch = new CountDownLatch(1);
AggregatorTestTask task = new AggregatorTestTask(aggregator, message1, latch);
AggregatorTestTask task = new AggregatorTestTask(aggregator, message, latch);
executor.execute(task);
latch.await(1000, TimeUnit.MILLISECONDS);
Message<?> reply = replyChannel.receive(0);
assertNull(reply);
assertNotNull(task.getException());
assertEquals(MessageHandlingException.class, task.getException().getClass());
Message<?> discardedMessage = discardChannel.receive(500);
assertNotNull(discardedMessage);
assertEquals(message, discardedMessage);
}
@Test
public void testShouldFailOnTimeoutFalse() throws InterruptedException {
public void testShouldSendPartialResultOnTimeoutTrue() throws InterruptedException {
AggregatingMessageHandler aggregator = new AggregatingMessageHandler(new TestAggregator());
aggregator.setTimeout(10);
aggregator.setShouldFailOnTimeout(false);
aggregator.setTimeout(50);
aggregator.setReaperInterval(10);
aggregator.setSendPartialResultOnTimeout(true);
SimpleChannel replyChannel = new SimpleChannel();
Message<?> message1 = createMessage("123", "ABC", 3, 1, replyChannel);
Message<?> message2 = createMessage("456", "ABC", 3, 2, replyChannel);
@@ -131,6 +136,62 @@ public class AggregatingMessageHandlerTests {
assertEquals("abcdefghi", reply2.getPayload());
}
@Test
public void testDiscardChannelForTrackedCorrelationId() {
SimpleChannel replyChannel = new SimpleChannel();
SimpleChannel discardChannel = new SimpleChannel();
AggregatingMessageHandler aggregator = new AggregatingMessageHandler(new TestAggregator());
aggregator.setDiscardChannel(discardChannel);
aggregator.handle(createMessage("test-1a", 1, 1, 1, replyChannel));
assertEquals("test-1a", replyChannel.receive(100).getPayload());
aggregator.handle(createMessage("test-1b", 1, 1, 1, replyChannel));
assertEquals("test-1b", discardChannel.receive(100).getPayload());
}
@Test
public void testTrackedCorrelationIdsCapacityAtLimit() {
SimpleChannel replyChannel = new SimpleChannel();
SimpleChannel discardChannel = new SimpleChannel();
AggregatingMessageHandler aggregator = new AggregatingMessageHandler(new TestAggregator());
aggregator.setTrackedCorrelationIdCapacity(3);
aggregator.setDiscardChannel(discardChannel);
aggregator.handle(createMessage("test-1a", 1, 1, 1, replyChannel));
assertEquals("test-1a", replyChannel.receive(100).getPayload());
aggregator.handle(createMessage("test-2", 2, 1, 1, replyChannel));
assertEquals("test-2", replyChannel.receive(100).getPayload());
aggregator.handle(createMessage("test-3", 3, 1, 1, replyChannel));
assertEquals("test-3", replyChannel.receive(100).getPayload());
aggregator.handle(createMessage("test-1b", 1, 1, 1, replyChannel));
assertEquals("test-1b", discardChannel.receive(100).getPayload());
}
@Test
public void testTrackedCorrelationIdsCapacityPassesLimit() {
SimpleChannel replyChannel = new SimpleChannel();
SimpleChannel discardChannel = new SimpleChannel();
AggregatingMessageHandler aggregator = new AggregatingMessageHandler(new TestAggregator());
aggregator.setTrackedCorrelationIdCapacity(3);
aggregator.setDiscardChannel(discardChannel);
aggregator.handle(createMessage("test-1a", 1, 1, 1, replyChannel));
assertEquals("test-1a", replyChannel.receive(100).getPayload());
aggregator.handle(createMessage("test-2", 2, 1, 1, replyChannel));
assertEquals("test-2", replyChannel.receive(100).getPayload());
aggregator.handle(createMessage("test-3", 3, 1, 1, replyChannel));
assertEquals("test-3", replyChannel.receive(100).getPayload());
aggregator.handle(createMessage("test-4", 4, 1, 1, replyChannel));
assertEquals("test-4", replyChannel.receive(100).getPayload());
aggregator.handle(createMessage("test-1b", 1, 1, 1, replyChannel));
assertEquals("test-1b", replyChannel.receive(100).getPayload());
assertNull(discardChannel.receive(0));
}
@Test(expected=MessageHandlingException.class)
public void testExceptionThrownIfNoCorrelationId() throws InterruptedException {
AggregatingMessageHandler aggregator = new AggregatingMessageHandler(new TestAggregator());
Message<?> message = createMessage("123", null, 2, 1, new SimpleChannel());
aggregator.handle(message);
}
private static Message<?> createMessage(String payload, Object correlationId,
int sequenceSize, int sequenceNumber, MessageChannel replyChannel) {

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed 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.springframework.integration.router;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Test;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class AggregationBarrierTests {
@Test
public void testBasicCompletionCheck() {
AggregationBarrier barrier = new AggregationBarrier(new TwoMessageCompletionStrategy());
assertNull(barrier.addAndRelease(new StringMessage("test1")));
assertNotNull(barrier.addAndRelease(new StringMessage("test2")));
}
@Test
public void testMessageRetrieval() {
AggregationBarrier barrier = new AggregationBarrier(new TwoMessageCompletionStrategy());
barrier.addAndRelease(new StringMessage("test1"));
assertEquals(1, barrier.getMessages().size());
barrier.addAndRelease(new StringMessage("test2"));
assertEquals(2, barrier.getMessages().size());
}
@Test
public void testTimestamp() {
long before = System.currentTimeMillis();
AggregationBarrier barrier = new AggregationBarrier(new TwoMessageCompletionStrategy());
long timestamp = barrier.getTimestamp();
assertTrue(before <= timestamp);
long after = System.currentTimeMillis();
assertTrue(after >= timestamp);
}
@Test
public void testEmptyMessageList() {
AggregationBarrier barrier = new AggregationBarrier(new TwoMessageCompletionStrategy());
assertEquals(0, barrier.getMessages().size());
}
private static class TwoMessageCompletionStrategy implements CompletionStrategy {
public boolean isComplete(List<Message<?>> messages) {
return (messages.size() == 2);
}
}
}

View File

@@ -1,87 +0,0 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed 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.springframework.integration.router;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Test;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class RoutingBarrierTests {
@Test
public void testBasicCompletionCheck() {
RoutingBarrier barrier = new RoutingBarrier(new TwoMessageCompletionStrategy());
barrier.addMessage(new StringMessage("test1"));
assertFalse(barrier.isComplete());
barrier.addMessage(new StringMessage("test2"));
assertTrue(barrier.isComplete());
}
@Test
public void testMessageRetrieval() {
RoutingBarrier barrier = new RoutingBarrier(new TwoMessageCompletionStrategy());
barrier.addMessage(new StringMessage("test1"));
assertEquals(1, barrier.getMessages().size());
barrier.addMessage(new StringMessage("test2"));
assertEquals(2, barrier.getMessages().size());
}
@Test
public void testWaitForCompletionTimesOut() {
RoutingBarrier barrier = new RoutingBarrier(new TwoMessageCompletionStrategy());
barrier.addMessage(new StringMessage("test1"));
assertFalse(barrier.isComplete());
assertFalse(barrier.waitForCompletion(10));
}
@Test
public void testWaitForCompletionReturnsTrueImmediately() {
RoutingBarrier barrier = new RoutingBarrier(new TwoMessageCompletionStrategy());
barrier.addMessage(new StringMessage("test1"));
assertFalse(barrier.isComplete());
barrier.addMessage(new StringMessage("test2"));
assertTrue(barrier.waitForCompletion(0));
assertTrue(barrier.isComplete());
}
@Test
public void testEmptyMessageList() {
RoutingBarrier barrier = new RoutingBarrier(new TwoMessageCompletionStrategy());
assertFalse(barrier.isComplete());
assertFalse(barrier.waitForCompletion(0));
assertEquals(0, barrier.getMessages().size());
}
private static class TwoMessageCompletionStrategy implements RoutingBarrierCompletionStrategy {
public boolean isComplete(List<Message<?>> messages) {
return (messages.size() == 2);
}
}
}