11package com .iexec .worker .amnesia ;
22
33import com .iexec .common .chain .ContributionAuthorization ;
4- import com .iexec .common .disconnection .InterruptedReplicateModel ;
5- import com .iexec .common .disconnection .RecoveryAction ;
6- import com .iexec .common .replicate .AvailableReplicateModel ;
4+ import com .iexec .common .notification .TaskNotification ;
5+ import com .iexec .common .notification .TaskNotificationExtra ;
6+ import com .iexec .common .notification .TaskNotificationType ;
7+ import com .iexec .common .task .TaskDescription ;
78import com .iexec .worker .chain .IexecHubService ;
89import com .iexec .worker .executor .TaskExecutorService ;
910import com .iexec .worker .feign .CustomFeignClient ;
1617import org .mockito .Mockito ;
1718import org .mockito .MockitoAnnotations ;
1819
19- import java .util .Arrays ;
2020import java .util .Collections ;
2121import java .util .List ;
2222import java .util .Optional ;
2828
2929public class AmnesiaRecoveryServiceTests {
3030
31- @ Mock private CustomFeignClient customFeignClient ;
32- @ Mock private SubscriptionService subscriptionService ;
33- @ Mock private ResultService resultService ;
34- @ Mock private TaskExecutorService taskExecutorService ;
35- @ Mock private IexecHubService iexecHubService ;
31+ @ Mock
32+ private CustomFeignClient customFeignClient ;
33+ @ Mock
34+ private SubscriptionService subscriptionService ;
35+ @ Mock
36+ private ResultService resultService ;
37+ @ Mock
38+ private TaskExecutorService taskExecutorService ;
39+ @ Mock
40+ private IexecHubService iexecHubService ;
3641
3742 @ InjectMocks
3843 AmnesiaRecoveryService amnesiaRecoveryService ;
@@ -60,11 +65,10 @@ public void shouldNotRecoverSinceNothingToRecover() {
6065 @ Test
6166 public void shouldRecoverByWaiting () {
6267 when (iexecHubService .getLatestBlockNumber ()).thenReturn (blockNumber );
63- when (resultService .isResultZipFound (CHAIN_TASK_ID )).thenReturn (true );
64- when (replicateService .retrieveAvailableReplicateModelFromContribAuth (any ()))
65- .thenReturn (getStubModel ());
68+ when (resultService .isResultAvailable (CHAIN_TASK_ID )).thenReturn (true );
69+ when (iexecHubService .getTaskDescriptionFromChain (any ())).thenReturn (getStubModel ());
6670 when (customFeignClient .getMissedTaskNotifications (blockNumber ))
67- .thenReturn (getStubInterruptedReplicateList ( RecoveryAction . WAIT ));
71+ .thenReturn (getStubInterruptedTasks ( TaskNotificationType . PLEASE_WAIT ));
6872
6973 List <String > recovered = amnesiaRecoveryService .recoverInterruptedReplicates ();
7074
@@ -76,29 +80,27 @@ public void shouldRecoverByWaiting() {
7680 public void shouldRecoverByComputingAgainWhenResultNotFound () {
7781 when (iexecHubService .getLatestBlockNumber ()).thenReturn (blockNumber );
7882 when (customFeignClient .getMissedTaskNotifications (blockNumber ))
79- .thenReturn (getStubInterruptedReplicateList (RecoveryAction .CONTRIBUTE ));
80- when (replicateService .retrieveAvailableReplicateModelFromContribAuth (any ()))
81- .thenReturn (getStubModel ());
83+ .thenReturn (getStubInterruptedTasks (TaskNotificationType .PLEASE_CONTRIBUTE ));
84+ when (iexecHubService .getTaskDescriptionFromChain (any ())).thenReturn (getStubModel ());
8285 when (resultService .isResultFolderFound (CHAIN_TASK_ID )).thenReturn (false );
83-
86+
8487 List <String > recovered = amnesiaRecoveryService .recoverInterruptedReplicates ();
8588
8689 assertThat (recovered ).isNotEmpty ();
8790 assertThat (recovered .get (0 )).isEqualTo (CHAIN_TASK_ID );
8891
8992 Mockito .verify (taskExecutorService , Mockito .times (1 ))
90- .addReplicate (getStubModel (). get ( ));
93+ .addReplicate (any ( ContributionAuthorization . class ));
9194 }
9295
9396 @ Test
9497 public void shouldRecoverByContributingWhenResultFound () {
9598 when (iexecHubService .getLatestBlockNumber ()).thenReturn (blockNumber );
9699 when (customFeignClient .getMissedTaskNotifications (blockNumber ))
97- .thenReturn (getStubInterruptedReplicateList (RecoveryAction .CONTRIBUTE ));
98- when (replicateService .retrieveAvailableReplicateModelFromContribAuth (any ()))
99- .thenReturn (getStubModel ());
100- when (resultService .isResultFolderFound (CHAIN_TASK_ID )).thenReturn (true );
101-
100+ .thenReturn (getStubInterruptedTasks (TaskNotificationType .PLEASE_CONTRIBUTE ));
101+ when (iexecHubService .getTaskDescriptionFromChain (any ())).thenReturn (getStubModel ());
102+ when (resultService .isResultAvailable (CHAIN_TASK_ID )).thenReturn (true );
103+
102104 List <String > recovered = amnesiaRecoveryService .recoverInterruptedReplicates ();
103105
104106 assertThat (recovered ).isNotEmpty ();
@@ -112,11 +114,11 @@ public void shouldRecoverByContributingWhenResultFound() {
112114 public void shouldAbortSinceConsensusReached () {
113115 when (iexecHubService .getLatestBlockNumber ()).thenReturn (blockNumber );
114116 when (customFeignClient .getMissedTaskNotifications (blockNumber ))
115- .thenReturn (getStubInterruptedReplicateList ( RecoveryAction . ABORT_CONSENSUS_REACHED ));
116- when (resultService .isResultZipFound (CHAIN_TASK_ID )).thenReturn (true );
117- when (replicateService . retrieveAvailableReplicateModelFromContribAuth (any ()))
118- . thenReturn ( getStubModel ());
119-
117+ .thenReturn (getStubInterruptedTasks ( TaskNotificationType . PLEASE_ABORT_CONSENSUS_REACHED ));
118+ when (resultService .isResultAvailable (CHAIN_TASK_ID )).thenReturn (true );
119+ when (iexecHubService . getTaskDescriptionFromChain (any ())). thenReturn ( getStubModel ());
120+ // when(subscriptionService.handleTaskNotification(any ())).
121+
120122 List <String > recovered = amnesiaRecoveryService .recoverInterruptedReplicates ();
121123
122124 assertThat (recovered ).isNotEmpty ();
@@ -130,10 +132,9 @@ public void shouldAbortSinceConsensusReached() {
130132 public void shouldAbortSinceContributionTimeout () {
131133 when (iexecHubService .getLatestBlockNumber ()).thenReturn (blockNumber );
132134 when (customFeignClient .getMissedTaskNotifications (blockNumber ))
133- .thenReturn (getStubInterruptedReplicateList ( RecoveryAction . ABORT_CONTRIBUTION_TIMEOUT ));
135+ .thenReturn (getStubInterruptedTasks ( TaskNotificationType . PLEASE_ABORT_CONTRIBUTION_TIMEOUT ));
134136 when (resultService .isResultZipFound (CHAIN_TASK_ID )).thenReturn (true );
135- when (replicateService .retrieveAvailableReplicateModelFromContribAuth (any ()))
136- .thenReturn (getStubModel ());
137+ when (iexecHubService .getTaskDescriptionFromChain (any ())).thenReturn (getStubModel ());
137138
138139 List <String > recovered = amnesiaRecoveryService .recoverInterruptedReplicates ();
139140
@@ -148,11 +149,10 @@ public void shouldAbortSinceContributionTimeout() {
148149 public void shouldNotRecoverByRevealingWhenResultNotFound () {
149150 when (iexecHubService .getLatestBlockNumber ()).thenReturn (blockNumber );
150151 when (customFeignClient .getMissedTaskNotifications (blockNumber ))
151- .thenReturn (getStubInterruptedReplicateList (RecoveryAction .REVEAL ));
152- when (replicateService .retrieveAvailableReplicateModelFromContribAuth (any ()))
153- .thenReturn (getStubModel ());
152+ .thenReturn (getStubInterruptedTasks (TaskNotificationType .PLEASE_REVEAL ));
153+ when (iexecHubService .getTaskDescriptionFromChain (any ())).thenReturn (getStubModel ());
154154 when (resultService .isResultFolderFound (CHAIN_TASK_ID )).thenReturn (false );
155-
155+
156156 List <String > recovered = amnesiaRecoveryService .recoverInterruptedReplicates ();
157157
158158 assertThat (recovered ).isEmpty ();
@@ -165,11 +165,10 @@ public void shouldNotRecoverByRevealingWhenResultNotFound() {
165165 public void shouldRecoverByRevealingWhenResultFound () {
166166 when (iexecHubService .getLatestBlockNumber ()).thenReturn (blockNumber );
167167 when (customFeignClient .getMissedTaskNotifications (blockNumber ))
168- .thenReturn (getStubInterruptedReplicateList (RecoveryAction .REVEAL ));
169- when (replicateService .retrieveAvailableReplicateModelFromContribAuth (any ()))
170- .thenReturn (getStubModel ());
168+ .thenReturn (getStubInterruptedTasks (TaskNotificationType .PLEASE_REVEAL ));
169+ when (iexecHubService .getTaskDescriptionFromChain (any ())).thenReturn (getStubModel ());
171170 when (resultService .isResultFolderFound (CHAIN_TASK_ID )).thenReturn (true );
172-
171+
173172 List <String > recovered = amnesiaRecoveryService .recoverInterruptedReplicates ();
174173
175174 assertThat (recovered ).isNotEmpty ();
@@ -183,11 +182,10 @@ public void shouldRecoverByRevealingWhenResultFound() {
183182 public void shouldNotRecoverByUploadingWhenResultNotFound () {
184183 when (iexecHubService .getLatestBlockNumber ()).thenReturn (blockNumber );
185184 when (customFeignClient .getMissedTaskNotifications (blockNumber ))
186- .thenReturn (getStubInterruptedReplicateList (RecoveryAction .UPLOAD_RESULT ));
187- when (replicateService .retrieveAvailableReplicateModelFromContribAuth (any ()))
188- .thenReturn (getStubModel ());
185+ .thenReturn (getStubInterruptedTasks (TaskNotificationType .PLEASE_UPLOAD ));
186+ when (iexecHubService .getTaskDescriptionFromChain (any ())).thenReturn (getStubModel ());
189187 when (resultService .isResultFolderFound (CHAIN_TASK_ID )).thenReturn (false );
190-
188+
191189 List <String > recovered = amnesiaRecoveryService .recoverInterruptedReplicates ();
192190
193191 assertThat (recovered ).isEmpty ();
@@ -200,11 +198,10 @@ public void shouldNotRecoverByUploadingWhenResultNotFound() {
200198 public void shouldRecoverByUploadingWhenResultFound () {
201199 when (iexecHubService .getLatestBlockNumber ()).thenReturn (blockNumber );
202200 when (customFeignClient .getMissedTaskNotifications (blockNumber ))
203- .thenReturn (getStubInterruptedReplicateList (RecoveryAction .UPLOAD_RESULT ));
204- when (replicateService .retrieveAvailableReplicateModelFromContribAuth (any ()))
205- .thenReturn (getStubModel ());
201+ .thenReturn (getStubInterruptedTasks (TaskNotificationType .PLEASE_UPLOAD ));
202+ when (iexecHubService .getTaskDescriptionFromChain (any ())).thenReturn (getStubModel ());
206203 when (resultService .isResultFolderFound (CHAIN_TASK_ID )).thenReturn (true );
207-
204+
208205 List <String > recovered = amnesiaRecoveryService .recoverInterruptedReplicates ();
209206
210207 assertThat (recovered ).isNotEmpty ();
@@ -218,11 +215,10 @@ public void shouldRecoverByUploadingWhenResultFound() {
218215 public void shouldCompleteTask () {
219216 when (iexecHubService .getLatestBlockNumber ()).thenReturn (blockNumber );
220217 when (customFeignClient .getMissedTaskNotifications (blockNumber ))
221- .thenReturn (getStubInterruptedReplicateList ( RecoveryAction . COMPLETE ));
218+ .thenReturn (getStubInterruptedTasks ( TaskNotificationType . PLEASE_COMPLETE ));
222219
223220 when (resultService .isResultZipFound (CHAIN_TASK_ID )).thenReturn (true );
224- when (replicateService .retrieveAvailableReplicateModelFromContribAuth (any ()))
225- .thenReturn (getStubModel ());
221+ when (iexecHubService .getTaskDescriptionFromChain (any ())).thenReturn (getStubModel ());
226222
227223 List <String > recovered = amnesiaRecoveryService .recoverInterruptedReplicates ();
228224
@@ -233,13 +229,16 @@ public void shouldCompleteTask() {
233229 .completeTask (CHAIN_TASK_ID );
234230 }
235231
236- List <InterruptedReplicateModel > getStubInterruptedReplicateList (RecoveryAction action ) {
237- InterruptedReplicateModel interruptedReplicate = InterruptedReplicateModel .builder ()
238- .contributionAuthorization (getStubAuth ())
239- .recoveryAction (action )
232+ List <TaskNotification > getStubInterruptedTasks (TaskNotificationType notificationType ) {
233+ TaskNotification interruptedReplicate = TaskNotification .builder ()
234+ .chainTaskId (CHAIN_TASK_ID )
235+ .taskNotificationType (notificationType )
236+ .taskNotificationExtra (TaskNotificationExtra .builder ()
237+ .contributionAuthorization (getStubAuth ())
238+ .build ())
240239 .build ();
241240
242- return Arrays . asList (interruptedReplicate );
241+ return Collections . singletonList (interruptedReplicate );
243242 }
244243
245244 ContributionAuthorization getStubAuth () {
@@ -248,9 +247,9 @@ ContributionAuthorization getStubAuth() {
248247 .build ();
249248 }
250249
251- Optional <AvailableReplicateModel > getStubModel () {
252- return Optional .of (AvailableReplicateModel .builder ()
253- .contributionAuthorization ( getStubAuth () )
250+ Optional <TaskDescription > getStubModel () {
251+ return Optional .of (TaskDescription .builder ()
252+ .chainTaskId ( CHAIN_TASK_ID )
254253 .build ());
255254 }
256255
0 commit comments