4646import java .time .Duration ;
4747import java .util .List ;
4848import java .util .Optional ;
49+ import java .util .concurrent .CompletableFuture ;
50+ import java .util .concurrent .ExecutionException ;
4951
5052import static org .assertj .core .api .Assertions .assertThat ;
5153import static org .mockito .ArgumentMatchers .any ;
@@ -73,6 +75,8 @@ class IexecHubServiceTests {
7375 private Web3j web3jClient ;
7476 private IexecHubService iexecHubService ;
7577 private Credentials credentials ;
78+ @ Mock
79+ private CompletableFuture <?> completableFuture ;
7680
7781 @ BeforeEach
7882 void init () throws Exception {
@@ -127,6 +131,43 @@ void shouldContribute() throws Exception {
127131 IexecHubContract .TaskContributeEventResponse response = iexecHubService .contribute (contribution );
128132 assertThat (response ).isNotNull ();
129133 }
134+
135+ @ Test
136+ void shouldNotContributeOnExecutionException () throws ExecutionException , InterruptedException {
137+ final Contribution contribution = Contribution .builder ()
138+ .chainTaskId (CHAIN_TASK_ID )
139+ .enclaveChallenge ("enclaveChallenge" )
140+ .enclaveSignature ("enclaveSignature" )
141+ .resultHash ("resultHash" )
142+ .resultSeal ("resultSeal" )
143+ .workerPoolSignature ("workerPoolSignature" )
144+ .build ();
145+ try (MockedStatic <CompletableFuture > mockedStatic = mockStatic (CompletableFuture .class )) {
146+ mockedStatic .when (() -> CompletableFuture .supplyAsync (any (), any ())).thenReturn (completableFuture );
147+ when (completableFuture .get ()).thenThrow (ExecutionException .class );
148+ IexecHubContract .TaskContributeEventResponse response = iexecHubService .contribute (contribution );
149+ assertThat (response ).isNull ();
150+ }
151+ }
152+
153+ @ Test
154+ void shouldNotContributeWhenInterrupted () throws ExecutionException , InterruptedException {
155+ final Contribution contribution = Contribution .builder ()
156+ .chainTaskId (CHAIN_TASK_ID )
157+ .enclaveChallenge ("enclaveChallenge" )
158+ .enclaveSignature ("enclaveSignature" )
159+ .resultHash ("resultHash" )
160+ .resultSeal ("resultSeal" )
161+ .workerPoolSignature ("workerPoolSignature" )
162+ .build ();
163+ try (MockedStatic <CompletableFuture > mockedStatic = mockStatic (CompletableFuture .class )) {
164+ mockedStatic .when (() -> CompletableFuture .supplyAsync (any (), any ())).thenReturn (completableFuture );
165+ when (completableFuture .get ()).thenThrow (InterruptedException .class );
166+ IexecHubContract .TaskContributeEventResponse response = iexecHubService .contribute (contribution );
167+ assertThat (response ).isNull ();
168+ assertThat (Thread .currentThread ().isInterrupted ()).isTrue ();
169+ }
170+ }
130171 // endregion
131172
132173 // region reveal
@@ -145,6 +186,27 @@ void shouldReveal() throws Exception {
145186 IexecHubContract .TaskRevealEventResponse response = iexecHubService .reveal (CHAIN_TASK_ID , "resultDigest" );
146187 assertThat (response ).isNotNull ();
147188 }
189+
190+ @ Test
191+ void shouldNotRevealOnExecutionException () throws ExecutionException , InterruptedException {
192+ try (MockedStatic <CompletableFuture > mockedStatic = mockStatic (CompletableFuture .class )) {
193+ mockedStatic .when (() -> CompletableFuture .supplyAsync (any (), any ())).thenReturn (completableFuture );
194+ when (completableFuture .get ()).thenThrow (ExecutionException .class );
195+ IexecHubContract .TaskRevealEventResponse response = iexecHubService .reveal (CHAIN_TASK_ID , "resultDigest" );
196+ assertThat (response ).isNull ();
197+ }
198+ }
199+
200+ @ Test
201+ void shouldNotContributeRevealWhenInterrupted () throws ExecutionException , InterruptedException {
202+ try (MockedStatic <CompletableFuture > mockedStatic = mockStatic (CompletableFuture .class )) {
203+ mockedStatic .when (() -> CompletableFuture .supplyAsync (any (), any ())).thenReturn (completableFuture );
204+ when (completableFuture .get ()).thenThrow (InterruptedException .class );
205+ IexecHubContract .TaskRevealEventResponse response = iexecHubService .reveal (CHAIN_TASK_ID , "resultDigest" );
206+ assertThat (response ).isNull ();
207+ assertThat (Thread .currentThread ().isInterrupted ()).isTrue ();
208+ }
209+ }
148210 // end region
149211
150212 // region contributeAndFinalize
@@ -168,6 +230,43 @@ void shouldContributeAndFinalize() throws Exception {
168230 Optional <ChainReceipt > chainReceipt = iexecHubService .contributeAndFinalize (contribution , "resultLink" , "callbackData" );
169231 assertThat (chainReceipt ).isNotEmpty ();
170232 }
233+
234+ @ Test
235+ void shouldNotContributeAndFinalizeOnExecutionException () throws ExecutionException , InterruptedException {
236+ final Contribution contribution = Contribution .builder ()
237+ .chainTaskId (CHAIN_TASK_ID )
238+ .enclaveChallenge ("enclaveChallenge" )
239+ .enclaveSignature ("enclaveSignature" )
240+ .resultHash ("resultHash" )
241+ .resultSeal ("resultSeal" )
242+ .workerPoolSignature ("workerPoolSignature" )
243+ .build ();
244+ try (MockedStatic <CompletableFuture > mockedStatic = mockStatic (CompletableFuture .class )) {
245+ mockedStatic .when (() -> CompletableFuture .supplyAsync (any (), any ())).thenReturn (completableFuture );
246+ when (completableFuture .get ()).thenThrow (ExecutionException .class );
247+ Optional <ChainReceipt > chainReceipt = iexecHubService .contributeAndFinalize (contribution , "resultLink" , "callbackData" );
248+ assertThat (chainReceipt ).isEmpty ();
249+ }
250+ }
251+
252+ @ Test
253+ void shouldNotContributeAndFinalizeWhenInterrupted () throws ExecutionException , InterruptedException {
254+ final Contribution contribution = Contribution .builder ()
255+ .chainTaskId (CHAIN_TASK_ID )
256+ .enclaveChallenge ("enclaveChallenge" )
257+ .enclaveSignature ("enclaveSignature" )
258+ .resultHash ("resultHash" )
259+ .resultSeal ("resultSeal" )
260+ .workerPoolSignature ("workerPoolSignature" )
261+ .build ();
262+ try (MockedStatic <CompletableFuture > mockedStatic = mockStatic (CompletableFuture .class )) {
263+ mockedStatic .when (() -> CompletableFuture .supplyAsync (any (), any ())).thenReturn (completableFuture );
264+ when (completableFuture .get ()).thenThrow (InterruptedException .class );
265+ Optional <ChainReceipt > chainReceipt = iexecHubService .contributeAndFinalize (contribution , "resultLink" , "callbackData" );
266+ assertThat (chainReceipt ).isEmpty ();
267+ assertThat (Thread .currentThread ().isInterrupted ()).isTrue ();
268+ }
269+ }
171270 // endregion
172271
173272 // region ChainTask status
0 commit comments