Skip to content

Commit

Permalink
MINOR: fix flaky testRecordThreadIdleRatioTwoThreads test (#15937)
Browse files Browse the repository at this point in the history
Reviewers: David Jacot <djacot@confluent.io>, Chia-Ping Tsai <chia7712@gmail.com>
  • Loading branch information
jeffkbkim committed May 14, 2024
1 parent 2958dcb commit df5735d
Showing 1 changed file with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,14 @@ public void testMetrics() throws Exception {
}

@Test
public void testRecordThreadIdleRatioTwoThreads() throws Exception {
public void testRecordThreadIdleRatio() throws Exception {
GroupCoordinatorRuntimeMetrics mockRuntimeMetrics = mock(GroupCoordinatorRuntimeMetrics.class);
Time time = Time.SYSTEM;
Time time = new MockTime();

try (CoordinatorEventProcessor eventProcessor = new MultiThreadedEventProcessor(
new LogContext(),
"event-processor-",
2,
1,
time,
mockRuntimeMetrics,
new DelayEventAccumulator(time, 100L)
Expand All @@ -474,10 +474,7 @@ public void testRecordThreadIdleRatioTwoThreads() throws Exception {
ArgumentCaptor<Long> idleTimeCaptured = ArgumentCaptor.forClass(Long.class);
doAnswer(invocation -> {
long threadIdleTime = idleTimeCaptured.getValue();

// As each thread sleeps for 100ms before returning from take(),
// we know that each recorded idle time must be at least 50 (100/2)ms.
assertTrue(threadIdleTime >= 50);
assertEquals(100, threadIdleTime);
synchronized (recordedIdleTimesMs) {
recordedIdleTimesMs.add(threadIdleTime);
}
Expand Down Expand Up @@ -508,15 +505,15 @@ public void testRecordThreadIdleRatioTwoThreads() throws Exception {
assertFalse(event.future.isCompletedExceptionally());
});

long diff = time.milliseconds() - startMs;

assertEquals(events.size(), numEventsExecuted.get());
verify(mockRuntimeMetrics, times(8)).recordThreadIdleTime(anyLong());
assertEquals(8, recordedIdleTimesMs.size());

long diff = time.milliseconds() - startMs;
long sum = recordedIdleTimesMs.stream().mapToLong(Long::longValue).sum();
double idleRatio = (double) sum / diff;
assertTrue(idleRatio >= 0.5 && idleRatio <= 1.0);

assertEquals(1.0, idleRatio, "idle ratio should be 1.0 but was: " + idleRatio);
}
}
}

0 comments on commit df5735d

Please sign in to comment.