Skip to content

Commit 9a8ef47

Browse files
committed
Fix random failing ServiceSupplierTestCase.testOptionalReferences eclipse-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
1 parent 9bf98d6 commit 9a8ef47

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

runtime/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/ServiceSupplierTestCase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ public static class TestDisabledBean {
7373
@Inject
7474
@Optional
7575
@Service(filterExpression = "(component=disabled)")
76-
TestService disabledService;
76+
volatile TestService disabledService;
7777

7878
@Inject
7979
@Service(filterExpression = "(component=disabled)")
80-
List<TestService> services;
80+
volatile List<TestService> services;
8181
}
8282

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

272272
enabler.disableDisabledServiceA();
273273
// wait for asynchronous service registry and injection to finish
274-
waitForCondition(() -> bean.services.size() == 0, timeoutInMillis);
274+
waitForCondition(() -> bean.services.size() == 0 && bean.disabledService == null, timeoutInMillis);
275275
assertNull(bean.disabledService);
276276
assertEquals(0, bean.services.size());
277277
} finally {

0 commit comments

Comments
 (0)