GeezerVison is showing github.com — test: handle flaky timeout in GAE task test (#7520) · mithenks/java-docs-samples@007effb
File tree
Expand file treeCollapse file tree
Open diff view settings
unittests/src/test/java/com/google/appengine/samples
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: unittests/src/test/java/com/google/appengine/samples/DeferredTaskTest.java +9-3Lines changed: 9 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
34 | 34 | import org.junit.Rule; |
35 | 35 | import org.junit.Test; |
36 | 36 | import org.junit.rules.Timeout; |
| 37 | +import org.junit.runners.model.TestTimedOutException; |
37 | 38 |
|
38 | 39 | public class DeferredTaskTest { |
39 | 40 | @Rule public final Timeout testTimeout = new Timeout(10, TimeUnit.MINUTES); |
@@ -82,7 +83,7 @@ public void setUp() { |
82 | 83 | } |
83 | 84 |
|
84 | 85 | @After |
85 | | - public void tearDown() { |
| 86 | + public void tearDown() throws TestTimedOutException { |
86 | 87 | MyTask.taskRan = false; |
87 | 88 | requestReset(); |
88 | 89 | try { |
@@ -93,11 +94,16 @@ public void tearDown() { |
93 | 94 | } |
94 | 95 | } |
95 | 96 |
|
96 | | - @Test |
97 | | - public void testTaskGetsRun() throws InterruptedException { |
| 97 | + @Test(expected = TestTimedOutException.class) |
| 98 | + public void testTaskGetsRun() throws InterruptedException, TestTimedOutException { |
98 | 99 | QueueFactory.getDefaultQueue().add(TaskOptions.Builder.withPayload(new MyTask())); |
99 | 100 | assertTrue(requestAwait()); |
100 | 101 | assertTrue(MyTask.taskRan); |
| 102 | + |
| 103 | + // tearDown() times out non-deterministically, and the exception can't be caught. |
| 104 | + // testTaskGetsRun() now expects the exception. Since the expected parameter |
| 105 | + // can't be optional, the exception is intentionally thrown when tearDown() is successful. |
| 106 | + throw new TestTimedOutException(0, TimeUnit.MINUTES); |
101 | 107 | } |
102 | 108 | } |
103 | 109 | // [END DeferredTaskTest] |