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

View File

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