Fix unit tests that sometimes failed due timeouts (#10698)

Motivation:

We had two unit tests that sometimes failed due timeouts. After insepecting these I noticed these can be improved to run faster while still do the right validation

Modifications:

- Only submit one task for execution per execute
- Cleanup

Result:

No test failures due timeout
This commit is contained in:
Norman Maurer 2020-10-16 21:00:26 +02:00 committed by GitHub
parent fbb42a292f
commit cc79f5f4ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 44 deletions

View File

@ -134,34 +134,22 @@ public class GlobalEventExecutorTest {
//for https://github.com/netty/netty/issues/1614 //for https://github.com/netty/netty/issues/1614
//add scheduled task //add scheduled task
TestRunnable t = new TestRunnable(0); TestRunnable t = new TestRunnable(0);
ScheduledFuture<?> f = e.schedule(t, 1500, TimeUnit.MILLISECONDS); final ScheduledFuture<?> f = e.schedule(t, 1500, TimeUnit.MILLISECONDS);
final Runnable doNothing = new Runnable() {
@Override
public void run() {
//NOOP
}
};
final AtomicBoolean stop = new AtomicBoolean(false);
//ensure always has at least one task in taskQueue //ensure always has at least one task in taskQueue
//check if scheduled tasks are triggered //check if scheduled tasks are triggered
try { e.execute(new Runnable() {
new Thread(new Runnable() { @Override
@Override public void run() {
public void run() { if (!f.isDone()) {
while (!stop.get()) { e.execute(this);
e.execute(doNothing);
}
} }
}).start(); }
});
f.sync(); f.sync();
assertThat(t.ran.get(), is(true)); assertThat(t.ran.get(), is(true));
} finally {
stop.set(true);
}
} }
private static final class TestRunnable implements Runnable { private static final class TestRunnable implements Runnable {

View File

@ -374,34 +374,22 @@ public class SingleThreadEventExecutorTest {
//add scheduled task //add scheduled task
TestRunnable t = new TestRunnable(); TestRunnable t = new TestRunnable();
ScheduledFuture<?> f = executor.schedule(t, 1500, TimeUnit.MILLISECONDS); final ScheduledFuture<?> f = executor.schedule(t, 1500, TimeUnit.MILLISECONDS);
final Runnable doNothing = new Runnable() {
@Override
public void run() {
//NOOP
}
};
final AtomicBoolean stop = new AtomicBoolean(false);
//ensure always has at least one task in taskQueue //ensure always has at least one task in taskQueue
//check if scheduled tasks are triggered //check if scheduled tasks are triggered
try { executor.execute(new Runnable() {
new Thread(new Runnable() { @Override
@Override public void run() {
public void run() { if (!f.isDone()) {
while (!stop.get()) { executor.execute(this);
executor.execute(doNothing);
}
} }
}).start(); }
});
f.sync(); f.sync();
assertThat(t.ran.get(), is(true)); assertThat(t.ran.get(), is(true));
} finally {
stop.set(true);
}
} }
private static final class TestRunnable implements Runnable { private static final class TestRunnable implements Runnable {