Skip to content

Commit 384e527

Browse files
committed
Merge pull request androidannotations#1191 from WonderCsabo/1182_cleanUpThreadActivityTest
Reset Executor after each test in ThreadActivityTest
2 parents 277baca + a35671a commit 384e527

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

‎AndroidAnnotations/functional-test-1-5/src/test/java/org/androidannotations/test15/ThreadActivityTest.java‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,28 @@
1717

1818
import static org.mockito.Mockito.mock;
1919
import static org.mockito.Mockito.verify;
20+
import static org.fest.reflect.core.Reflection.staticField;
2021

2122
import java.lang.reflect.Field;
2223
import java.util.ArrayList;
2324
import java.util.Collections;
2425
import java.util.List;
2526
import java.util.Random;
2627
import java.util.concurrent.Executor;
28+
import java.util.concurrent.ExecutorService;
2729
import java.util.concurrent.Executors;
2830
import java.util.concurrent.Semaphore;
2931
import java.util.concurrent.TimeUnit;
3032

3133
import org.androidannotations.api.BackgroundExecutor;
34+
import org.fest.reflect.reference.TypeRef;
3235
import org.junit.After;
3336
import org.junit.Assert;
3437
import org.junit.Before;
3538
import org.junit.Test;
3639
import org.junit.runner.RunWith;
3740
import org.mockito.Mockito;
41+
import org.mockito.internal.util.MockUtil;
3842
import org.robolectric.Robolectric;
3943
import org.robolectric.RobolectricTestRunner;
4044

@@ -59,8 +63,41 @@ public void setup() {
5963
}
6064

6165
@After
62-
public void after() {
66+
public void after() throws InterruptedException {
6367
Thread.setDefaultUncaughtExceptionHandler(defaultExceptionHandler);
68+
69+
List<String> tasks = staticField("tasks") //
70+
.ofType(new TypeRef<List<String>>() {
71+
}) //
72+
.in(BackgroundExecutor.class) //
73+
.get();
74+
75+
tasks.clear();
76+
77+
ThreadLocal<String> currentSerial = staticField("currentSerial") //
78+
.ofType(new TypeRef<ThreadLocal<String>>() {
79+
}) //
80+
.in(BackgroundExecutor.class) //
81+
.get();
82+
83+
currentSerial.set(null);
84+
85+
Executor executor = staticField("executor") //
86+
.ofType(Executor.class) //
87+
.in(BackgroundExecutor.class) //
88+
.get();
89+
90+
if (new MockUtil().isMock(executor) || !(executor instanceof ExecutorService)) {
91+
return;
92+
}
93+
94+
ExecutorService service = (ExecutorService) executor;
95+
96+
service.shutdownNow();
97+
boolean termination = service.awaitTermination(MAX_WAITING_TIME, TimeUnit.MILLISECONDS);
98+
if (!termination) {
99+
throw new IllegalStateException("The Executor could not terminate after the test!");
100+
}
64101
}
65102

66103
@Test

0 commit comments

Comments
 (0)