Added ResponseCorrelator and RetrievalBlockingMessageStore (INT-143).
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.handler;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ResponseCorrelatorTests {
|
||||
|
||||
@Test
|
||||
public void testReceiversPrecedeResponse() throws InterruptedException {
|
||||
final ResponseCorrelator correlator = new ResponseCorrelator(10);
|
||||
final AtomicInteger responseCounter = new AtomicInteger();
|
||||
CountDownLatch latch = startReceivers(correlator, responseCounter, 5, 500);
|
||||
Message<?> message = new StringMessage("test");
|
||||
message.getHeader().setCorrelationId("123");
|
||||
correlator.handle(message);
|
||||
latch.await(1000, TimeUnit.MILLISECONDS);
|
||||
assertEquals(0, latch.getCount());
|
||||
assertEquals(1, responseCounter.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResponsePrecedeReceivers() throws InterruptedException {
|
||||
final ResponseCorrelator correlator = new ResponseCorrelator(10);
|
||||
Message<?> message = new StringMessage("test");
|
||||
message.getHeader().setCorrelationId("123");
|
||||
correlator.handle(message);
|
||||
final AtomicInteger responseCounter = new AtomicInteger();
|
||||
CountDownLatch latch = startReceivers(correlator, responseCounter, 5, 50);
|
||||
latch.await(1000, TimeUnit.MILLISECONDS);
|
||||
assertEquals(0, latch.getCount());
|
||||
assertEquals(1, responseCounter.get());
|
||||
}
|
||||
|
||||
|
||||
private static CountDownLatch startReceivers(final ResponseCorrelator correlator,
|
||||
final AtomicInteger responseCounter, int numReceivers, final long timeout) {
|
||||
final CountDownLatch latch = new CountDownLatch(numReceivers);
|
||||
Executor executor = Executors.newFixedThreadPool(numReceivers);
|
||||
for (int i = 0; i < numReceivers; i++) {
|
||||
executor.execute(new Runnable() {
|
||||
public void run() {
|
||||
Message<?> response = correlator.getResponse("123", timeout);
|
||||
if (response != null) {
|
||||
responseCounter.incrementAndGet();
|
||||
}
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
return latch;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.message;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class RetrievalBlockingMessageStoreTests {
|
||||
|
||||
@Test
|
||||
public void testGetWithElapsedTimeout() {
|
||||
final RetrievalBlockingMessageStore store = new RetrievalBlockingMessageStore(10);
|
||||
publishWithDelay(store, "foo", "bar", 100);
|
||||
Message<?> message = store.get("foo", 5);
|
||||
assertNull(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetWithinTimeout() {
|
||||
final RetrievalBlockingMessageStore store = new RetrievalBlockingMessageStore(10);
|
||||
publishWithDelay(store, "foo", "bar", 50);
|
||||
Message<?> message = store.get("foo", 500);
|
||||
assertNotNull(message);
|
||||
assertEquals("bar", message.getPayload());
|
||||
assertNotNull(store.get("foo", 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveWithElapsedTimeout() {
|
||||
final RetrievalBlockingMessageStore store = new RetrievalBlockingMessageStore(10);
|
||||
publishWithDelay(store, "foo", "bar", 100);
|
||||
Message<?> message = store.remove("foo", 5);
|
||||
assertNull(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveWithinTimeout() {
|
||||
final RetrievalBlockingMessageStore store = new RetrievalBlockingMessageStore(10);
|
||||
publishWithDelay(store, "foo", "bar", 50);
|
||||
Message<?> message = store.remove("foo", 500);
|
||||
assertNotNull(message);
|
||||
assertEquals("bar", message.getPayload());
|
||||
assertNull(store.get("foo", 0));
|
||||
}
|
||||
|
||||
|
||||
private static void publishWithDelay(final MessageStore store, final String key, final String value, final long delay) {
|
||||
Executors.newSingleThreadExecutor().execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
catch (InterruptedException e) {}
|
||||
store.put(key, new StringMessage(value));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,7 +29,7 @@ public class SimpleMessageStoreTests {
|
||||
|
||||
@Test
|
||||
public void testPut() {
|
||||
SimpleMessageStore store = new SimpleMessageStore();
|
||||
SimpleMessageStore store = new SimpleMessageStore(5);
|
||||
Message<?> message1 = new StringMessage("message-1");
|
||||
Message<?> previous = store.put(1, message1);
|
||||
assertNull(previous);
|
||||
@@ -39,7 +39,7 @@ public class SimpleMessageStoreTests {
|
||||
|
||||
@Test
|
||||
public void testReplace() {
|
||||
SimpleMessageStore store = new SimpleMessageStore();
|
||||
SimpleMessageStore store = new SimpleMessageStore(5);
|
||||
Message<?> messageA = new StringMessage("message-a");
|
||||
Message<?> messageB = new StringMessage("message-b");
|
||||
store.put(1, messageA);
|
||||
@@ -50,7 +50,7 @@ public class SimpleMessageStoreTests {
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
SimpleMessageStore store = new SimpleMessageStore();
|
||||
SimpleMessageStore store = new SimpleMessageStore(5);
|
||||
Message<?> message = new StringMessage("message");
|
||||
assertNull(store.remove(1));
|
||||
store.put(1, message);
|
||||
|
||||
Reference in New Issue
Block a user