Index: webrtc/examples/androidjunit/src/org/appspot/apprtc/TCPChannelClientTest.java |
diff --git a/webrtc/examples/androidjunit/src/org/appspot/apprtc/TCPChannelClientTest.java b/webrtc/examples/androidjunit/src/org/appspot/apprtc/TCPChannelClientTest.java |
index e0e29630d1e173d53be4ec2d17ea98110778d88e..b93ba5ff1efe40c6cf2754693f126339e9fcf975 100644 |
--- a/webrtc/examples/androidjunit/src/org/appspot/apprtc/TCPChannelClientTest.java |
+++ b/webrtc/examples/androidjunit/src/org/appspot/apprtc/TCPChannelClientTest.java |
@@ -10,7 +10,7 @@ |
package org.appspot.apprtc; |
-import org.appspot.apprtc.util.RobolectricLooperExecutor; |
+ |
import org.junit.After; |
import org.junit.Before; |
import org.junit.Test; |
@@ -21,6 +21,10 @@ import org.robolectric.RobolectricTestRunner; |
import org.robolectric.annotation.Config; |
import org.robolectric.shadows.ShadowLog; |
+import java.util.concurrent.ExecutorService; |
+import java.util.concurrent.Executors; |
+import java.util.concurrent.TimeUnit; |
+ |
import static org.junit.Assert.fail; |
import static org.mockito.Mockito.timeout; |
import static org.mockito.Mockito.verify; |
@@ -38,13 +42,14 @@ public class TCPChannelClientTest { |
private static final int CONNECT_TIMEOUT = 100; |
private static final int SEND_TIMEOUT = 100; |
private static final int DISCONNECT_TIMEOUT = 100; |
+ private static final int TERMINATION_TIMEOUT = 1000; |
private static final String TEST_MESSAGE_SERVER = "Hello, Server!"; |
private static final String TEST_MESSAGE_CLIENT = "Hello, Client!"; |
@Mock TCPChannelClient.TCPChannelEvents serverEvents; |
@Mock TCPChannelClient.TCPChannelEvents clientEvents; |
- private RobolectricLooperExecutor executor; |
+ private ExecutorService executor; |
private TCPChannelClient server; |
private TCPChannelClient client; |
@@ -55,15 +60,14 @@ public class TCPChannelClientTest { |
MockitoAnnotations.initMocks(this); |
- executor = new RobolectricLooperExecutor(); |
- executor.requestStart(); |
+ executor = Executors.newSingleThreadExecutor(); |
} |
@After |
public void tearDown() { |
verifyNoMoreEvents(); |
- executor.executeAndWait(new Runnable() { |
+ executeAndWait(new Runnable() { |
@Override |
public void run() { |
client.disconnect(); |
@@ -72,9 +76,9 @@ public class TCPChannelClientTest { |
}); |
// Stop the executor thread |
- executor.requestStop(); |
+ executor.shutdown(); |
try { |
- executor.join(); |
+ executor.awaitTermination(TERMINATION_TIMEOUT, TimeUnit.MILLISECONDS); |
} catch (InterruptedException e) { |
fail(e.getMessage()); |
} |
@@ -112,7 +116,7 @@ public class TCPChannelClientTest { |
public void testSendData() { |
testConnectIPv4(); |
- executor.executeAndWait(new Runnable() { |
+ executeAndWait(new Runnable() { |
@Override |
public void run() { |
client.send(TEST_MESSAGE_SERVER); |
@@ -127,7 +131,7 @@ public class TCPChannelClientTest { |
@Test |
public void testDisconnectServer() { |
testConnectIPv4(); |
- executor.executeAndWait(new Runnable() { |
+ executeAndWait(new Runnable() { |
@Override |
public void run() { |
server.disconnect(); |
@@ -141,7 +145,7 @@ public class TCPChannelClientTest { |
@Test |
public void testDisconnectClient() { |
testConnectIPv4(); |
- executor.executeAndWait(new Runnable() { |
+ executeAndWait(new Runnable() { |
@Override |
public void run() { |
client.disconnect(); |
@@ -183,4 +187,40 @@ public class TCPChannelClientTest { |
verifyNoMoreInteractions(serverEvents); |
verifyNoMoreInteractions(clientEvents); |
} |
+ |
+ /** |
+ * Executes the runnable passed to the constructor and sets isDone flag afterwards. |
+ */ |
+ private static class ExecuteAndWaitRunnable implements Runnable { |
+ public boolean isDone = false; |
+ private final Runnable runnable; |
+ ExecuteAndWaitRunnable(Runnable runnable) { |
+ this.runnable = runnable; |
+ } |
+ @Override |
+ public void run() { |
+ runnable.run(); |
+ synchronized (this) { |
+ isDone = true; |
+ notifyAll(); |
+ } |
+ } |
+ } |
+ |
+ /** |
+ * Queues runnable to be run and waits for it to be executed by the executor thread |
+ */ |
+ public void executeAndWait(Runnable runnable) { |
magjed_webrtc
2016/05/19 12:07:54
Can you implement it like this?
public void execut
sakal
2016/05/19 12:39:03
Done.
|
+ ExecuteAndWaitRunnable executeAndWaitRunnable = new ExecuteAndWaitRunnable(runnable); |
+ executor.execute(executeAndWaitRunnable); |
+ synchronized (executeAndWaitRunnable) { |
+ while (!executeAndWaitRunnable.isDone) { |
+ try { |
+ executeAndWaitRunnable.wait(); |
+ } catch (InterruptedException e) { |
+ fail(e.getMessage()); |
+ } |
+ } |
+ } |
+ } |
} |