Skip to content

Commit 0d98e4e

Browse files
author
Ugo Plouviez
committed
Fix unit tests
1 parent a31b6c2 commit 0d98e4e

File tree

5 files changed

+125
-146
lines changed

5 files changed

+125
-146
lines changed

src/main/java/com/iexec/worker/amnesia/AmnesiaRecoveryService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public AmnesiaRecoveryService(CustomFeignClient customFeignClient,
4141
}
4242

4343
public List<String> recoverInterruptedReplicates() {
44-
long lasAvailableBlockNumber = iexecHubService.getLatestBlockNumber();
44+
long latestAvailableBlockNumber = iexecHubService.getLatestBlockNumber();
4545
List<TaskNotification> missedTaskNotifications = customFeignClient.getMissedTaskNotifications(
46-
lasAvailableBlockNumber);
46+
latestAvailableBlockNumber);
4747
List<String> recoveredChainTaskIds = new ArrayList<>();
4848

4949
if (missedTaskNotifications == null || missedTaskNotifications.isEmpty()) {

src/main/java/com/iexec/worker/executor/TaskExecutorService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.scheduling.annotation.Async;
2525
import org.springframework.stereotype.Service;
2626

27+
import javax.annotation.PostConstruct;
2728
import java.util.Optional;
2829
import java.util.concurrent.CompletableFuture;
2930
import java.util.concurrent.Executors;
@@ -80,6 +81,10 @@ public TaskExecutorService(DatasetService datasetService,
8081

8182
maxNbExecutions = Runtime.getRuntime().availableProcessors() - 1;
8283
executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(maxNbExecutions);
84+
}
85+
86+
@PostConstruct
87+
public void initIt() {
8388
corePublicAddress = customFeignClient.getPublicConfiguration().getSchedulerPublicAddress();
8489
}
8590

src/test/java/com/iexec/worker/amnesia/AmnesiaRecoveryServiceTests.java

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.iexec.worker.amnesia;
22

33
import 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;
78
import com.iexec.worker.chain.IexecHubService;
89
import com.iexec.worker.executor.TaskExecutorService;
910
import com.iexec.worker.feign.CustomFeignClient;
@@ -16,7 +17,6 @@
1617
import org.mockito.Mockito;
1718
import org.mockito.MockitoAnnotations;
1819

19-
import java.util.Arrays;
2020
import java.util.Collections;
2121
import java.util.List;
2222
import java.util.Optional;
@@ -28,11 +28,16 @@
2828

2929
public 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

Comments
 (0)