Fix flaky GlobalEventExecutorTest.* (#9074)

Motivation:

In GlobalEventExecutorTest we used Thread.sleep(...) which can produce flaky results (as seen on the CI). We should use another alternative during tests.

Modifications:

Replace Thread.sleep(...) with join()

Result:

No more flaky GlobalEventExecutor tests.
This commit is contained in:
Norman Maurer 2019-04-29 08:33:03 +02:00 committed by GitHub
parent 2ec6428827
commit b5a2774502
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -46,7 +46,7 @@ public class GlobalEventExecutorTest {
} }
} }
@Test @Test(timeout = 5000)
public void testAutomaticStartStop() throws Exception { public void testAutomaticStartStop() throws Exception {
final TestRunnable task = new TestRunnable(500); final TestRunnable task = new TestRunnable(500);
e.execute(task); e.execute(task);
@ -56,10 +56,7 @@ public class GlobalEventExecutorTest {
assertThat(thread, is(not(nullValue()))); assertThat(thread, is(not(nullValue())));
assertThat(thread.isAlive(), is(true)); assertThat(thread.isAlive(), is(true));
Thread.sleep(1500); thread.join();
// Ensure the thread stopped itself after running the task.
assertThat(thread.isAlive(), is(false));
assertThat(task.ran.get(), is(true)); assertThat(task.ran.get(), is(true));
// Ensure another new thread starts again. // Ensure another new thread starts again.
@ -68,14 +65,12 @@ public class GlobalEventExecutorTest {
assertThat(e.thread, not(sameInstance(thread))); assertThat(e.thread, not(sameInstance(thread)));
thread = e.thread; thread = e.thread;
Thread.sleep(1500); thread.join();
// Ensure the thread stopped itself after running the task.
assertThat(thread.isAlive(), is(false));
assertThat(task.ran.get(), is(true)); assertThat(task.ran.get(), is(true));
} }
@Test @Test(timeout = 5000)
public void testScheduledTasks() throws Exception { public void testScheduledTasks() throws Exception {
TestRunnable task = new TestRunnable(0); TestRunnable task = new TestRunnable(0);
ScheduledFuture<?> f = e.schedule(task, 1500, TimeUnit.MILLISECONDS); ScheduledFuture<?> f = e.schedule(task, 1500, TimeUnit.MILLISECONDS);
@ -87,10 +82,7 @@ public class GlobalEventExecutorTest {
assertThat(thread, is(not(nullValue()))); assertThat(thread, is(not(nullValue())));
assertThat(thread.isAlive(), is(true)); assertThat(thread.isAlive(), is(true));
Thread.sleep(1500); thread.join();
// Now it should be stopped.
assertThat(thread.isAlive(), is(false));
} }
// ensure that when a task submission causes a new thread to be created, the thread inherits the thread group of the // ensure that when a task submission causes a new thread to be created, the thread inherits the thread group of the