summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpauljensen <pauljensen@chromium.org>2015-04-22 12:50:54 -0700
committerCommit bot <commit-bot@chromium.org>2015-04-22 19:51:09 +0000
commit660c1a3051cb4c2ba50f1e4b5ee88c9bb87c0d59 (patch)
treea5789035ec59a0ef6b4a81a071be0657971448a2
parent43f3b0d25752d924390a4d6fc868d6e9469edc4f (diff)
downloadchromium_src-660c1a3051cb4c2ba50f1e4b5ee88c9bb87c0d59.zip
chromium_src-660c1a3051cb4c2ba50f1e4b5ee88c9bb87c0d59.tar.gz
chromium_src-660c1a3051cb4c2ba50f1e4b5ee88c9bb87c0d59.tar.bz2
[cronet] Run MessageLoops on single thread in tests
This better matches real usage and avoids asserts. Review URL: https://codereview.chromium.org/1052233006 Cr-Commit-Position: refs/heads/master@{#326361}
-rw-r--r--components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/MessageLoopTest.java88
1 files changed, 49 insertions, 39 deletions
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/MessageLoopTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/MessageLoopTest.java
index b2e5fd9..b480967 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/MessageLoopTest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/MessageLoopTest.java
@@ -10,18 +10,32 @@ import org.chromium.base.test.util.Feature;
import org.chromium.net.CronetTestBase;
import java.io.IOException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.ThreadFactory;
/**
* Tests the MessageLoop implementation.
*/
public class MessageLoopTest extends CronetTestBase {
+ private Thread mTestThread;
+ private final ExecutorService mExecutorService =
+ Executors.newSingleThreadExecutor(new ExecutorThreadFactory());
+ private class ExecutorThreadFactory implements ThreadFactory {
+ public Thread newThread(Runnable r) {
+ mTestThread = new Thread(r);
+ return mTestThread;
+ }
+ }
+ private boolean mFailed = false;
@SmallTest
@Feature({"Cronet"})
public void testInterrupt() throws Exception {
final MessageLoop loop = new MessageLoop();
assertFalse(loop.isRunning());
- TestThread thread = new TestThread() {
+ Future future = mExecutorService.submit(new Runnable() {
@Override
public void run() {
try {
@@ -31,23 +45,29 @@ public class MessageLoopTest extends CronetTestBase {
// Expected interrupt.
}
}
- };
- thread.start();
+ });
Thread.sleep(1000);
assertTrue(loop.isRunning());
assertFalse(loop.hasLoopFailed());
- thread.interrupt();
- Thread.sleep(1000);
+ mTestThread.interrupt();
+ future.get();
assertFalse(loop.isRunning());
assertTrue(loop.hasLoopFailed());
- assertFalse(thread.mFailed);
+ assertFalse(mFailed);
// Re-spinning the message loop is not allowed after interrupt.
- try {
- loop.loop();
- fail();
- } catch (IllegalStateException e) {
- // Expected.
- }
+ mExecutorService.submit(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ loop.loop();
+ fail();
+ } catch (Exception e) {
+ if (!(e instanceof IllegalStateException)) {
+ fail();
+ }
+ }
+ }
+ }).get();
}
@SmallTest
@@ -55,7 +75,7 @@ public class MessageLoopTest extends CronetTestBase {
public void testTaskFailed() throws Exception {
final MessageLoop loop = new MessageLoop();
assertFalse(loop.isRunning());
- TestThread thread = new TestThread() {
+ Future future = mExecutorService.submit(new Runnable() {
@Override
public void run() {
try {
@@ -67,44 +87,34 @@ public class MessageLoopTest extends CronetTestBase {
}
}
}
- };
+ });
Runnable failedTask = new Runnable() {
@Override
public void run() {
throw new NullPointerException();
}
};
- thread.start();
Thread.sleep(1000);
assertTrue(loop.isRunning());
assertFalse(loop.hasLoopFailed());
loop.execute(failedTask);
- Thread.sleep(1000);
+ future.get();
assertFalse(loop.isRunning());
assertTrue(loop.hasLoopFailed());
- assertFalse(thread.mFailed);
+ assertFalse(mFailed);
// Re-spinning the message loop is not allowed after exception.
- try {
- loop.loop();
- fail();
- } catch (IllegalStateException e) {
- // Expected.
- }
- }
-
- /**
- * A Thread class to move assertion to the main thread, so findbug
- * won't complain.
- */
- private class TestThread extends Thread {
- boolean mFailed = false;
-
- public TestThread() {
- }
-
- @Override
- public void run() {
- throw new IllegalStateException();
- }
+ mExecutorService.submit(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ loop.loop();
+ fail();
+ } catch (Exception e) {
+ if (!(e instanceof IllegalStateException)) {
+ fail();
+ }
+ }
+ }
+ }).get();
}
}