diff options
author | Joakim Erdfelt | 2013-02-14 23:24:07 +0000 |
---|---|---|
committer | Joakim Erdfelt | 2013-02-14 23:24:07 +0000 |
commit | 2faba0bf4be87e81f81ca12e376dee137f78d649 (patch) | |
tree | 0d45f459d0e693c0a60019d58dd98dc494d49d50 /jetty-websocket/javax-websocket-client-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/endpoints/TrackingSocket.java | |
parent | e27ad8ef5c0dd2f4772810133e83c6b9f49d25f1 (diff) | |
download | org.eclipse.jetty.project-2faba0bf4be87e81f81ca12e376dee137f78d649.tar.gz org.eclipse.jetty.project-2faba0bf4be87e81f81ca12e376dee137f78d649.tar.xz org.eclipse.jetty.project-2faba0bf4be87e81f81ca12e376dee137f78d649.zip |
393473 - Add support for JSR-356 (javax.websocket) draft
+ Start of @WebSocketClient class/method scanning with tests
Diffstat (limited to 'jetty-websocket/javax-websocket-client-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/endpoints/TrackingSocket.java')
-rw-r--r-- | jetty-websocket/javax-websocket-client-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/endpoints/TrackingSocket.java | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/jetty-websocket/javax-websocket-client-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/endpoints/TrackingSocket.java b/jetty-websocket/javax-websocket-client-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/endpoints/TrackingSocket.java new file mode 100644 index 0000000000..64db37e578 --- /dev/null +++ b/jetty-websocket/javax-websocket-client-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/endpoints/TrackingSocket.java @@ -0,0 +1,125 @@ +// +// ======================================================================== +// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.jsr356.endpoints; + +import static org.hamcrest.Matchers.*; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import javax.websocket.CloseReason; +import javax.websocket.CloseReason.CloseCode; + +import org.eclipse.jetty.util.BlockingArrayQueue; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.log.Logger; +import org.junit.Assert; + +/** + * Abstract base socket used for tracking state and events within the socket for testing reasons. + */ +public abstract class TrackingSocket +{ + private static final Logger LOG = Log.getLogger(TrackingSocket.class); + + public CloseReason closeReason; + public BlockingQueue<String> eventQueue = new BlockingArrayQueue<String>(); + public BlockingQueue<Throwable> errorQueue = new BlockingArrayQueue<>(); + public CountDownLatch openLatch = new CountDownLatch(1); + public CountDownLatch closeLatch = new CountDownLatch(1); + public CountDownLatch dataLatch = new CountDownLatch(1); + + public void assertClose(CloseCode expectedCode, String expectedReason) throws InterruptedException + { + assertCloseCode(expectedCode); + assertCloseReason(expectedReason); + } + + public void assertCloseCode(CloseCode expectedCode) throws InterruptedException + { + Assert.assertThat("Was Closed",closeLatch.await(50,TimeUnit.MILLISECONDS),is(true)); + Assert.assertThat("CloseReason",closeReason,notNullValue()); + Assert.assertThat("Close Code",closeReason.getCloseCode(),is(expectedCode)); + } + + private void assertCloseReason(String expectedReason) + { + Assert.assertThat("Close Reason",closeReason.getReasonPhrase(),is(expectedReason)); + } + + protected void addEvent(String format, Object... args) + { + eventQueue.add(String.format(format,args)); + } + + protected void addError(Throwable t) + { + errorQueue.add(t); + } + + public void assertIsOpen() throws InterruptedException + { + assertWasOpened(); + assertNotClosed(); + } + + public void assertEvent(String expected) + { + String actual = eventQueue.poll(); + Assert.assertEquals("Event",expected,actual); + } + + public void assertNotClosed() + { + Assert.assertThat("Closed Latch",closeLatch.getCount(),greaterThanOrEqualTo(1L)); + } + + public void assertNotOpened() + { + Assert.assertThat("Open Latch",openLatch.getCount(),greaterThanOrEqualTo(1L)); + } + + public void assertWasOpened() throws InterruptedException + { + Assert.assertThat("Was Opened",openLatch.await(500,TimeUnit.MILLISECONDS),is(true)); + } + + public void clear() + { + eventQueue.clear(); + errorQueue.clear(); + } + + public void waitForClose(int timeoutDuration, TimeUnit timeoutUnit) throws InterruptedException + { + Assert.assertThat("Client Socket Closed",closeLatch.await(timeoutDuration,timeoutUnit),is(true)); + } + + public void waitForConnected(int timeoutDuration, TimeUnit timeoutUnit) throws InterruptedException + { + Assert.assertThat("Client Socket Connected",openLatch.await(timeoutDuration,timeoutUnit),is(true)); + } + + public void waitForData(int timeoutDuration, TimeUnit timeoutUnit) throws InterruptedException + { + LOG.debug("Waiting for message"); + Assert.assertThat("Data Received",dataLatch.await(timeoutDuration,timeoutUnit),is(true)); + } +} |