Elffuss BraidHub

kikocis/EDC public

cc2a9ff178c4 · 3.6 KiBRenderedRawPermalink
1# Unify asynchronous code testing practices
2
3## Decision
4
5Currently, the testing of async code is performed in two different ways:
6
7* using the `CoundDownLatch` object
8* using the `Awaitility` library
9
10The goal of `CountDownLatch` objects and the `Awaitility` library is to block a thread until other threads have
11completed a particular task. This is useful for testing asynchronous threads.
12
13The idea is to avoid using `CoundDownLatch` and start only using `Awaitility`.
14
15## Rationale
16
17Testing asynchronous systems is difficult. It requires not only handling threads, timeouts and concurrency issues, but
18also the intent of the test code can be obscured by all these details. Awaitility is a DSL that allows to express
19expectations of an asynchronous system in a conciser and easier to read manner than CountdownLatch.
20
21## Approach
22
23Awaitility permits to check multiple asynchronous behaviors at the same time with assertions that are included in the
24untilAsserted block that will by default poll the assertions every 100 ms until either the assertions are valid or the
25timeout expires which is by default 10 seconds.
26
27The following is a comparative of the current code with CountDownLatch and the proposal with Awaitility in one test:
28
29### class `AssetEventDispatchTest`
30
31#### with CountDownLatch
32
33```java
34public class AssetEventDispatchTest {
35 //...
36 @Test
37 void shouldDispatchEventsOnAssetCreationAndDeletion(AssetService service, EventRouter eventRouter) throws InterruptedException {
38 var createdLatch = onDispatchLatch(AssetCreated.class);
39 var deletedLatch = onDispatchLatch(AssetDeleted.class);
40 eventRouter.register(eventSubscriber);
41 var asset = Asset.Builder.newInstance().id("assetId").build();
42 var dataAddress = DataAddress.Builder.newInstance().type("any").build();
43
44 service.create(asset, dataAddress);
45
46 assertThat(createdLatch.await(10, SECONDS)).isTrue();
47 verify(eventSubscriber).on(isA(AssetCreated.class));
48
49 service.delete(asset.getId());
50
51 assertThat(deletedLatch.await(10, SECONDS)).isTrue();
52 verify(eventSubscriber).on(isA(AssetDeleted.class));
53 }
54
55 private CountDownLatch onDispatchLatch(Class<? extends Event> eventType) {
56 var latch = new CountDownLatch(1);
57
58 doAnswer(i -> {
59 latch.countDown();
60 return null;
61 }).when(eventSubscriber).on(isA(eventType));
62
63 return latch;
64 }
65}
66```
67
68#### with awaitility
69
70```java
71public class AssetEventDispatchTest {
72 //...
73 @Test
74 void shouldDispatchEventsOnAssetCreationAndDeletion(AssetService service, EventRouter eventRouter) {
75
76 doAnswer(i -> {
77 return null;
78 }).when(eventSubscriber).on(isA(AssetCreated.class));
79
80 doAnswer(i -> {
81 return null;
82 }).when(eventSubscriber).on(isA(AssetDeleted.class));
83
84 eventRouter.register(eventSubscriber);
85 var asset = Asset.Builder.newInstance().id("assetId").build();
86 var dataAddress = DataAddress.Builder.newInstance().type("any").build();
87
88 service.create(asset, dataAddress);
89 await().untilAsserted(() -> {
90 verify(eventSubscriber).on(isA(AssetCreated.class));
91 });
92
93 service.delete(asset.getId());
94 await().untilAsserted(() -> {
95 verify(eventSubscriber).on(isA(AssetDeleted.class));
96 });
97 }
98}
99```
100
101### Additional Note:
102
103class `TransferProcessManagerImplTest`
104(core\transfer\src\test\java\org\eclipse\dataspaceconnector\transfer\core\transfer\TransferProcessManagerImplTest.java)
105*** In method verifyCompleted_noCheckerForManaged(): A latch was only instantiated but not used ***