CAMEL-10883: correct tests and ensure that reader's wait for data
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4196654f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4196654f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4196654f Branch: refs/heads/master Commit: 4196654f91f540bc32465f24cc04c190a233f8b8 Parents: de3afdd Author: rohan <rohan.h...@fronde.com> Authored: Tue Feb 21 15:44:46 2017 +1300 Committer: Andrea Cosentino <anco...@gmail.com> Committed: Thu Feb 23 10:00:25 2017 +0100 ---------------------------------------------------------------------- .../DefaultUndertowHttpBindingTest.java | 41 +++++++++++++------- 1 file changed, 28 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/4196654f/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/DefaultUndertowHttpBindingTest.java ---------------------------------------------------------------------- diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/DefaultUndertowHttpBindingTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/DefaultUndertowHttpBindingTest.java index 532ee9d..4a4884b 100644 --- a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/DefaultUndertowHttpBindingTest.java +++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/DefaultUndertowHttpBindingTest.java @@ -15,12 +15,14 @@ import java.util.stream.Stream; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; public class DefaultUndertowHttpBindingTest { @Test(timeout = 1000) public void readEntireDelayedPayload() throws Exception { String[] delayedPayloads = new String[] { + "", "chunk", }; @@ -29,12 +31,13 @@ public class DefaultUndertowHttpBindingTest { DefaultUndertowHttpBinding binding = new DefaultUndertowHttpBinding(); String result = new String(binding.readFromChannel(source)); - assertThat(result, is(delayedPayloads[0])); + checkResult(result, delayedPayloads); } @Test(timeout = 1000) public void readEntireMultiDelayedPayload() throws Exception { String[] delayedPayloads = new String[] { + "", "first ", "second", }; @@ -44,6 +47,10 @@ public class DefaultUndertowHttpBindingTest { DefaultUndertowHttpBinding binding = new DefaultUndertowHttpBinding(); String result = new String(binding.readFromChannel(source)); + checkResult(result, delayedPayloads); + } + + private void checkResult(String result, String[] delayedPayloads) { assertThat(result, is( Stream.of(delayedPayloads) .collect(Collectors.joining()))); @@ -52,6 +59,7 @@ public class DefaultUndertowHttpBindingTest { @Test(timeout = 1000) public void readEntireMultiDelayedWithPausePayload() throws Exception { String[] delayedPayloads = new String[] { + "", "first ", "", "second", @@ -62,9 +70,7 @@ public class DefaultUndertowHttpBindingTest { DefaultUndertowHttpBinding binding = new DefaultUndertowHttpBinding(); String result = new String(binding.readFromChannel(source)); - assertThat(result, is( - Stream.of(delayedPayloads) - .collect(Collectors.joining()))); + checkResult(result, delayedPayloads); } private StreamSourceChannel source(final String[] delayedPayloads) { @@ -72,20 +78,23 @@ public class DefaultUndertowHttpBindingTest { return new EmptyStreamSourceChannel(thread()) { int chunk = 0; + boolean mustWait = false; // make sure that the caller is not spinning on read==0 @Override public int read(ByteBuffer dst) throws IOException { - // can only read payloads in the reader thread - if (sourceThread != Thread.currentThread()) { - if (chunk < delayedPayloads.length) { - byte[] delayedPayload = delayedPayloads[chunk].getBytes(); - dst.put(delayedPayload); - chunk++; - return delayedPayload.length; + if (mustWait) { + fail("must wait before reading"); + } + if (chunk < delayedPayloads.length) { + byte[] delayedPayload = delayedPayloads[chunk].getBytes(); + dst.put(delayedPayload); + chunk++; + if (delayedPayload.length == 0) { + mustWait = true; } - return -1; + return delayedPayload.length; } - return 0; + return -1; } @Override @@ -97,6 +106,12 @@ public class DefaultUndertowHttpBindingTest { super.resumeReads(); } } + + @Override + public void awaitReadable() throws IOException { + mustWait = false; + super.awaitReadable(); + } }; }