Skip to content

Commit

Permalink
Fix random failing ServiceSupplierTestCase.testOptionalReferences ecl…
Browse files Browse the repository at this point in the history
…ipse-platform#488

The ServiceSupplierTestCase.testOptionalReferences test case randomly
fails. The test aims to wait for services to be enabled or disabled, but
it only waits until a single value is changed although it asserts that
multiple values are changed afterwards. This is a race condition, as the
order in which the values are changed or in which the changes are
visible is non-deterministic, also due to missing memory barriers.

This change addresses this issue via two means:
1. it makes the fields of the asserted values volatile to ensure that
changed values eventually become visible to the UI thread
2. it waits for all values to be changed before asserting their values
to allow different orders of value changes
  • Loading branch information
HeikoKlare committed Nov 6, 2023
1 parent 99f33c3 commit d60eb44
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public static class TestDisabledBean {
@Inject
@Optional
@Service(filterExpression = "(component=disabled)")
TestService disabledService;
volatile TestService disabledService;

@Inject
@Service(filterExpression = "(component=disabled)")
List<TestService> services;
volatile List<TestService> services;
}

private final List<ServiceRegistration<?>> registrations = new ArrayList<>();
Expand Down Expand Up @@ -250,7 +250,7 @@ public void testOptionalReferences() throws Exception {
try {
enabler.enableDisabledServiceA();
// wait for asynchronous service registry and injection to finish
waitForCondition(() -> bean.services.size() == 1, timeoutInMillis);
waitForCondition(() -> bean.services.size() == 1 && bean.disabledService != null, timeoutInMillis);
assertNotNull(bean.disabledService);
assertEquals(1, bean.services.size());
assertSame(DisabledServiceA.class, bean.disabledService.getClass());
Expand All @@ -271,7 +271,7 @@ public void testOptionalReferences() throws Exception {

enabler.disableDisabledServiceA();
// wait for asynchronous service registry and injection to finish
waitForCondition(() -> bean.services.size() == 0, timeoutInMillis);
waitForCondition(() -> bean.services.size() == 0 && bean.disabledService == null, timeoutInMillis);
assertNull(bean.disabledService);
assertEquals(0, bean.services.size());
} finally {
Expand Down

0 comments on commit d60eb44

Please sign in to comment.