Use AtomicInteger in JobGroupTest.testShouldCancel_4#2668
Conversation
Adjusts JobGroupTest.testShouldCancel_4() to use an atomic data structure, to avoid concurrency issues with incrementing an integer from multiple jobs. See: eclipse-platform#2667
Test Results 36 files - 18 36 suites - 18 23m 23s ⏱️ - 10m 24s Results for commit 5968a50. ± Comparison against base commit b885e3b. This pull request skips 25 tests. |
fedejeanne
left a comment
There was a problem hiding this comment.
Nice catch, thank you for the contribution!
Just a nit: would it be possible to use AssertJ for more expressive assertions?
Other than that, the PR is fine.
| waitForCompletion(jobGroup); | ||
| assertTrue(numShouldCancelCalled[0] >= NUM_JOBS_LIMIT); | ||
| int called = numShouldCancelCalled.get(); | ||
| assertTrue(called >= NUM_JOBS_LIMIT); |
There was a problem hiding this comment.
| assertTrue(called >= NUM_JOBS_LIMIT); | |
| assertThat(called).isGreaterThanOrEqualTo(NUM_JOBS_LIMIT); |
| // Verify that the group is canceled in a reasonable time, | ||
| // i.e only 10 jobs are allowed to run after the shouldCancel method returned true. | ||
| assertTrue(numShouldCancelCalled[0] < NUM_JOBS_LIMIT + 10); | ||
| assertTrue(called < NUM_JOBS_LIMIT + 10, "Expected fewer jobs to run but got: " + called); |
There was a problem hiding this comment.
| assertTrue(called < NUM_JOBS_LIMIT + 10, "Expected fewer jobs to run but got: " + called); | |
| assertThat(called).isLessThan(NUM_JOBS_LIMIT + 10); |
| return true; | ||
| } | ||
| return false; | ||
| return numShouldCancelCalled.incrementAndGet() >= NUM_JOBS_LIMIT; |
There was a problem hiding this comment.
Nice catch, checking for == was wrong because of the race condition (2 threads executing numShouldCancelCalled[0]++; before one of them reached the if below would skip numbers).
FTR it is now safe to simply do:
return numShouldCancelCalled.incrementAndGet() == NUM_JOBS_LIMIT;... because the comparison (==) uses the now stable value that comes from incrementAndGet() (no race condition there).
Adjusts
JobGroupTest.testShouldCancel_4()to use an atomic data structure, to avoid concurrency issues with incrementing an integer from multiple jobs.See: #2667