Skip to content

Commit

Permalink
Fix race condition in MetricsSchedulingAspectTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed May 23, 2017
1 parent edd579c commit d44191a
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
@SpringBootTest
class MetricsSchedulingAspectTest {

static CountDownLatch observeLongTaskLatch = new CountDownLatch(1);
static CountDownLatch longTaskStarted = new CountDownLatch(1);
static CountDownLatch longTaskShouldComplete = new CountDownLatch(1);

@Autowired
MeterRegistry registry;
Expand All @@ -53,11 +54,13 @@ void scheduledIsInstrumented() throws InterruptedException {
assertThat(registry.findMeter(Timer.class, "beeper"))
.hasValueSatisfying(t -> assertThat(t.count()).isEqualTo(1));

longTaskStarted.await();

assertThat(registry.findMeter(LongTaskTimer.class, "longBeep"))
.hasValueSatisfying(t -> assertThat(t.activeTasks()).isEqualTo(1));

// make sure longBeep continues running until we have a chance to observe it in the active state
observeLongTaskLatch.countDown();
longTaskShouldComplete.countDown();

while(scheduler.getActiveCount() > 0) {}

Expand Down Expand Up @@ -87,7 +90,8 @@ ThreadPoolTaskScheduler scheduler() {
@Timed(value = "longBeep", longTask = true)
@Scheduled(fixedRate = 1000)
void longBeep() throws InterruptedException {
observeLongTaskLatch.await();
longTaskStarted.countDown();
longTaskShouldComplete.await();
System.out.println("beep");
}

Expand Down

0 comments on commit d44191a

Please sign in to comment.