fix websocket tests to not use complete() TMO for success completion Project: http://git-wip-us.apache.org/repos/asf/incubator-edgent/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-edgent/commit/a6db4b8f Tree: http://git-wip-us.apache.org/repos/asf/incubator-edgent/tree/a6db4b8f Diff: http://git-wip-us.apache.org/repos/asf/incubator-edgent/diff/a6db4b8f
Branch: refs/heads/develop Commit: a6db4b8f9d2d49ba62f815c51fb4fd1cd3edc9b1 Parents: 719fe6d Author: Dale LaBossiere <dlab...@us.ibm.com> Authored: Wed Oct 25 16:01:43 2017 -0400 Committer: Dale LaBossiere <dlab...@us.ibm.com> Committed: Wed Oct 25 16:01:43 2017 -0400 ---------------------------------------------------------------------- .../javax/websocket/WebSocketClientTest.java | 78 ++++++++++++++++++-- 1 file changed, 70 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-edgent/blob/a6db4b8f/connectors/websocket/src/test/java/org/apache/edgent/test/connectors/wsclient/javax/websocket/WebSocketClientTest.java ---------------------------------------------------------------------- diff --git a/connectors/websocket/src/test/java/org/apache/edgent/test/connectors/wsclient/javax/websocket/WebSocketClientTest.java b/connectors/websocket/src/test/java/org/apache/edgent/test/connectors/wsclient/javax/websocket/WebSocketClientTest.java index 58e7531..fd16084 100644 --- a/connectors/websocket/src/test/java/org/apache/edgent/test/connectors/wsclient/javax/websocket/WebSocketClientTest.java +++ b/connectors/websocket/src/test/java/org/apache/edgent/test/connectors/wsclient/javax/websocket/WebSocketClientTest.java @@ -20,6 +20,7 @@ package org.apache.edgent.test.connectors.wsclient.javax.websocket; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; import java.net.InetSocketAddress; @@ -27,6 +28,7 @@ import java.net.Socket; import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Properties; import java.util.concurrent.CountDownLatch; @@ -42,6 +44,7 @@ import org.apache.edgent.topology.TStream; import org.apache.edgent.topology.Topology; import org.apache.edgent.topology.json.JsonFunctions; import org.apache.edgent.topology.plumbing.PlumbingStreams; +import org.apache.edgent.topology.tester.Condition; import org.junit.After; import org.junit.Test; @@ -596,9 +599,38 @@ public class WebSocketClientTest extends ConnectorTestBase { s = PlumbingStreams.blockingOneShotDelay(s, 2, TimeUnit.SECONDS); wsClient.sendString(s); - TStream<String> rcvd = wsClient.receiveString(); + TStream<String> rcvd = wsClient.receiveString(); // rcv nothing + + // in this case there's no useful condition that we can check for + // to validate this is behaving properly other than the connector doesn't + // blow up and that nothing is rcvd, so just wait a short time + // before verifying nothing was rcvd. + // Don't use the complete() TMO for successful termination. + + Condition<List<String>> rcvdContent = t.getTester().streamContents(rcvd, new String[0]); + Condition<Object> tc = newWaitTimeCondition(3); - completeAndValidate("", t, rcvd, SEC_TMO, new String[0]); //rcv nothing + complete(t, tc, SEC_TMO, TimeUnit.SECONDS); + assertTrue("rcvd: "+rcvdContent.getResult(), rcvdContent.valid()); + } + + private Condition<Object> newWaitTimeCondition(int seconds) { + return new Condition<Object>() { + private long startTime = 0; + private long endTime = 0; + private volatile boolean done = false; + public boolean valid() { + if (startTime==0) { + startTime = System.currentTimeMillis(); + endTime = startTime + TimeUnit.SECONDS.toMillis(seconds); + } + long now = System.currentTimeMillis(); + done = now >= endTime; + return done; + } + public Object getResult() { return done; } + }; + } @Test @@ -675,9 +707,19 @@ public class WebSocketClientTest extends ConnectorTestBase { s = PlumbingStreams.blockingOneShotDelay(s, 2, TimeUnit.SECONDS); wsClient.sendString(s); - TStream<String> rcvd = wsClient.receiveString(); + TStream<String> rcvd = wsClient.receiveString(); // rcv nothing + + // in this case there's no useful condition that we can check for + // to validate this is behaving properly other than the connector doesn't + // blow up and that nothing is rcvd, so just wait a short time + // before verifying nothing was rcvd. + // Don't use the complete() TMO for successful termination. + + Condition<List<String>> rcvdContent = t.getTester().streamContents(rcvd, new String[0]); + Condition<Object> tc = newWaitTimeCondition(3); - completeAndValidate("", t, rcvd, SEC_TMO, new String[0]); // rcv nothing + complete(t, tc, SEC_TMO, TimeUnit.SECONDS); + assertTrue("rcvd: "+rcvdContent.getResult(), rcvdContent.valid()); } @Test @@ -733,9 +775,19 @@ public class WebSocketClientTest extends ConnectorTestBase { s = PlumbingStreams.blockingOneShotDelay(s, 2, TimeUnit.SECONDS); wsClient.sendString(s); - TStream<String> rcvd = wsClient.receiveString(); + TStream<String> rcvd = wsClient.receiveString(); // rcv nothing + + // in this case there's no useful condition that we can check for + // to validate this is behaving properly other than the connector doesn't + // blow up and that nothing is rcvd, so just wait a short time + // before verifying nothing was rcvd. + // Don't use the complete() TMO for successful termination. - completeAndValidate("", t, rcvd, SEC_TMO, new String[0]); //rcv nothing + Condition<List<String>> rcvdContent = t.getTester().streamContents(rcvd, new String[0]); + Condition<Object> tc = newWaitTimeCondition(3); + + complete(t, tc, SEC_TMO, TimeUnit.SECONDS); + assertTrue("rcvd: "+rcvdContent.getResult(), rcvdContent.valid()); } private void skipTestIfCantConnect(Properties config) throws Exception { @@ -847,9 +899,19 @@ public class WebSocketClientTest extends ConnectorTestBase { s = PlumbingStreams.blockingOneShotDelay(s, 2, TimeUnit.SECONDS); wsClient.sendString(s); - TStream<String> rcvd = wsClient.receiveString(); + TStream<String> rcvd = wsClient.receiveString(); // rcv nothing + + // in this case there's no useful condition that we can check for + // to validate this is behaving properly other than the connector doesn't + // blow up and that nothing is rcvd, so just wait a short time + // before verifying nothing was rcvd. + // Don't use the complete() TMO for successful termination. + + Condition<List<String>> rcvdContent = t.getTester().streamContents(rcvd, new String[0]); + Condition<Object> tc = newWaitTimeCondition(3); - completeAndValidate("", t, rcvd, SEC_TMO, new String[0]); //rcv nothing + complete(t, tc, SEC_TMO, TimeUnit.SECONDS); + assertTrue("rcvd: "+rcvdContent.getResult(), rcvdContent.valid()); } finally { sslProps.restore();