Skip to content

Use AtomicInteger in JobGroupTest.testShouldCancel_4#2668

Open
trancexpress wants to merge 1 commit into
eclipse-platform:masterfrom
trancexpress:gh2667
Open

Use AtomicInteger in JobGroupTest.testShouldCancel_4#2668
trancexpress wants to merge 1 commit into
eclipse-platform:masterfrom
trancexpress:gh2667

Conversation

@trancexpress
Copy link
Copy Markdown
Contributor

Adjusts JobGroupTest.testShouldCancel_4() to use an atomic data structure, to avoid concurrency issues with incrementing an integer from multiple jobs.

See: #2667

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
@github-actions
Copy link
Copy Markdown
Contributor

Test Results

   36 files   -    18     36 suites   - 18   23m 23s ⏱️ - 10m 24s
4 667 tests ±    0  4 620 ✅  -    25   47 💤 +25  0 ❌ ±0 
7 930 runs   - 3 965  7 825 ✅  - 3 917  105 💤  - 48  0 ❌ ±0 

Results for commit 5968a50. ± Comparison against base commit b885e3b.

This pull request skips 25 tests.
org.eclipse.core.tests.filesystem.SymlinkTest ‑ testSymlinkPutHidden
org.eclipse.core.tests.internal.alias.BasicAliasTest ‑ testBug198571
org.eclipse.core.tests.internal.localstore.MoveTest ‑ testMoveFileAcrossVolumes
org.eclipse.core.tests.internal.localstore.MoveTest ‑ testMoveFolderAcrossVolumes
org.eclipse.core.tests.resources.IProjectTest ‑ testProjectCreationLocationExistsWithDifferentCase
org.eclipse.core.tests.resources.IWorkspaceRootTest ‑ testFindFilesNonCanonicalPath
org.eclipse.core.tests.resources.LinkedResourceTest ‑ testFindFilesForLocationCaseVariant
org.eclipse.core.tests.resources.LinkedResourceWithPathVariableTest ‑ testFindFilesForLocationCaseVariant
org.eclipse.core.tests.resources.ResourceAttributeTest ‑ testAttributeArchive
org.eclipse.core.tests.resources.ResourceAttributeTest ‑ testAttributeHidden
…

Copy link
Copy Markdown
Member

@fedejeanne fedejeanne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants