Fix sporadic assertion failure in SingleThreadEventLoopTest

Motivation:

SingleThreadEventLoopTest.testScheduleTaskAtFixedRate() fails often due to:

- too little tolerance
- incorrect assertion (it compares only with the previous timestamp)

Modifications:

- Increase the timestamp difference tolerance from 10ms to 20ms
- Improve the timestamp assertion so that the comparison is performed against the first recorded timestamp
- Misc: Fix broken Javadoc tag

Result:

More build stability
This commit is contained in:
Trustin Lee 2015-06-01 14:44:49 +09:00
parent 684bcaa07e
commit add630c957
2 changed files with 19 additions and 12 deletions

View File

@ -67,7 +67,7 @@ public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor im
} }
/** /**
* Marker interface for {@linkRunnable} that will not trigger an {@link #wakeup(boolean)} in all cases. * Marker interface for {@link Runnable} that will not trigger an {@link #wakeup(boolean)} in all cases.
*/ */
interface NonWakeupRunnable extends Runnable { } interface NonWakeupRunnable extends Runnable { }
} }

View File

@ -39,7 +39,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class SingleThreadEventLoopTest { public class SingleThreadEventLoopTest {
@ -146,7 +146,8 @@ public class SingleThreadEventLoopTest {
endTime.set(System.nanoTime()); endTime.set(System.nanoTime());
} }
}, 500, TimeUnit.MILLISECONDS).get(); }, 500, TimeUnit.MILLISECONDS).get();
assertTrue(endTime.get() - startTime >= TimeUnit.MILLISECONDS.toNanos(500)); assertThat(endTime.get() - startTime,
is(greaterThanOrEqualTo(TimeUnit.MILLISECONDS.toNanos(500))));
} }
@Test @Test
@ -177,15 +178,19 @@ public class SingleThreadEventLoopTest {
assertEquals(5, timestamps.size()); assertEquals(5, timestamps.size());
// Check if the task was run without a lag. // Check if the task was run without a lag.
Long previousTimestamp = null; Long firstTimestamp = null;
int cnt = 0;
for (Long t: timestamps) { for (Long t: timestamps) {
if (previousTimestamp == null) { if (firstTimestamp == null) {
previousTimestamp = t; firstTimestamp = t;
continue; continue;
} }
assertTrue(t.longValue() - previousTimestamp.longValue() >= TimeUnit.MILLISECONDS.toNanos(90)); long timepoint = t - firstTimestamp;
previousTimestamp = t; assertThat(timepoint, is(greaterThanOrEqualTo(TimeUnit.MILLISECONDS.toNanos(100 * cnt + 80))));
assertThat(timepoint, is(lessThan(TimeUnit.MILLISECONDS.toNanos(100 * (cnt + 1) + 20))));
cnt ++;
} }
} }
@ -230,9 +235,9 @@ public class SingleThreadEventLoopTest {
long diff = t.longValue() - previousTimestamp.longValue(); long diff = t.longValue() - previousTimestamp.longValue();
if (i == 0) { if (i == 0) {
assertTrue(diff >= TimeUnit.MILLISECONDS.toNanos(400)); assertThat(diff, is(greaterThanOrEqualTo(TimeUnit.MILLISECONDS.toNanos(400))));
} else { } else {
assertTrue(diff <= TimeUnit.MILLISECONDS.toNanos(10)); assertThat(diff, is(lessThanOrEqualTo(TimeUnit.MILLISECONDS.toNanos(10))));
} }
previousTimestamp = t; previousTimestamp = t;
i ++; i ++;
@ -274,7 +279,8 @@ public class SingleThreadEventLoopTest {
continue; continue;
} }
assertTrue(t.longValue() - previousTimestamp.longValue() >= TimeUnit.MILLISECONDS.toNanos(150)); assertThat(t.longValue() - previousTimestamp.longValue(),
is(greaterThanOrEqualTo(TimeUnit.MILLISECONDS.toNanos(150))));
previousTimestamp = t; previousTimestamp = t;
} }
} }
@ -408,7 +414,8 @@ public class SingleThreadEventLoopTest {
loopA.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS); loopA.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
} }
assertTrue(System.nanoTime() - startTime >= TimeUnit.SECONDS.toNanos(1)); assertThat(System.nanoTime() - startTime,
is(greaterThanOrEqualTo(TimeUnit.SECONDS.toNanos(1))));
} }
@Test(timeout = 5000) @Test(timeout = 5000)