Skip to content

Commit 912ea8a

Browse files
author
Ugo Plouviez
committed
Update unit tests following refactoring
1 parent 0c043e9 commit 912ea8a

File tree

2 files changed

+33
-139
lines changed

2 files changed

+33
-139
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,14 @@ public List<String> recoverInterruptedReplicates() {
7676
}
7777

7878
TaskDescription taskDescription = optionalTaskDescription.get();
79-
recoverTask(missedTaskNotification, taskDescription);
79+
80+
subscriptionService.subscribeToTopic(chainTaskId);
81+
resultService.saveResultInfo(chainTaskId, taskDescription);
82+
subscriptionService.handleTaskNotification(missedTaskNotification);
83+
8084
recoveredChainTaskIds.add(chainTaskId);
8185
}
8286

8387
return recoveredChainTaskIds;
8488
}
85-
86-
public void recoverTask(TaskNotification taskNotification,
87-
TaskDescription taskDescription) {
88-
String chainTaskId = taskNotification.getChainTaskId();
89-
90-
subscriptionService.subscribeToTopic(chainTaskId);
91-
resultService.saveResultInfo(chainTaskId, taskDescription);
92-
subscriptionService.handleTaskNotification(taskNotification);
93-
}
94-
9589
}

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

Lines changed: 28 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ public class AmnesiaRecoveryServiceTests {
3131
@Mock
3232
private CustomFeignClient customFeignClient;
3333
@Mock
34-
private SubscriptionService subscriptionService;
35-
@Mock
3634
private ResultService resultService;
3735
@Mock
38-
private TaskExecutorService taskExecutorService;
39-
@Mock
4036
private IexecHubService iexecHubService;
37+
@Mock
38+
private SubscriptionService subscriptionService;
4139

4240
@InjectMocks
4341
AmnesiaRecoveryService amnesiaRecoveryService;
@@ -63,191 +61,93 @@ public void shouldNotRecoverSinceNothingToRecover() {
6361
}
6462

6563
@Test
66-
public void shouldRecoverByWaiting() {
67-
when(iexecHubService.getLatestBlockNumber()).thenReturn(blockNumber);
68-
when(resultService.isResultAvailable(CHAIN_TASK_ID)).thenReturn(true);
69-
when(iexecHubService.getTaskDescriptionFromChain(any())).thenReturn(getStubModel());
70-
when(customFeignClient.getMissedTaskNotifications(blockNumber))
71-
.thenReturn(getStubInterruptedTasks(TaskNotificationType.PLEASE_WAIT));
72-
73-
List<String> recovered = amnesiaRecoveryService.recoverInterruptedReplicates();
74-
75-
assertThat(recovered).isNotEmpty();
76-
assertThat(recovered.get(0)).isEqualTo(CHAIN_TASK_ID);
77-
}
78-
79-
@Test
80-
public void shouldRecoverByComputingAgainWhenResultNotFound() {
64+
public void shouldNotRecoverSinceCannotGetTaskDescriptionFromChain() {
8165
when(iexecHubService.getLatestBlockNumber()).thenReturn(blockNumber);
66+
TaskNotification notif = getStubInterruptedTask(TaskNotificationType.PLEASE_REVEAL);
8267
when(customFeignClient.getMissedTaskNotifications(blockNumber))
83-
.thenReturn(getStubInterruptedTasks(TaskNotificationType.PLEASE_CONTRIBUTE));
84-
when(iexecHubService.getTaskDescriptionFromChain(any())).thenReturn(getStubModel());
85-
when(resultService.isResultFolderFound(CHAIN_TASK_ID)).thenReturn(false);
86-
87-
List<String> recovered = amnesiaRecoveryService.recoverInterruptedReplicates();
88-
89-
assertThat(recovered).isNotEmpty();
90-
assertThat(recovered.get(0)).isEqualTo(CHAIN_TASK_ID);
91-
92-
Mockito.verify(taskExecutorService, Mockito.times(1))
93-
.addReplicate(any(ContributionAuthorization.class));
94-
}
95-
96-
@Test
97-
public void shouldRecoverByContributingWhenResultFound() {
98-
when(iexecHubService.getLatestBlockNumber()).thenReturn(blockNumber);
99-
when(customFeignClient.getMissedTaskNotifications(blockNumber))
100-
.thenReturn(getStubInterruptedTasks(TaskNotificationType.PLEASE_CONTRIBUTE));
101-
when(iexecHubService.getTaskDescriptionFromChain(any())).thenReturn(getStubModel());
68+
.thenReturn(Collections.singletonList(notif));
69+
when(iexecHubService.getTaskDescriptionFromChain(CHAIN_TASK_ID)).thenReturn(Optional.empty());
10270
when(resultService.isResultAvailable(CHAIN_TASK_ID)).thenReturn(true);
10371

10472
List<String> recovered = amnesiaRecoveryService.recoverInterruptedReplicates();
10573

106-
assertThat(recovered).isNotEmpty();
107-
assertThat(recovered.get(0)).isEqualTo(CHAIN_TASK_ID);
108-
109-
Mockito.verify(taskExecutorService, Mockito.times(1))
110-
.contribute(getStubAuth());
111-
}
112-
113-
@Test
114-
public void shouldAbortSinceConsensusReached() {
115-
when(iexecHubService.getLatestBlockNumber()).thenReturn(blockNumber);
116-
when(customFeignClient.getMissedTaskNotifications(blockNumber))
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-
122-
List<String> recovered = amnesiaRecoveryService.recoverInterruptedReplicates();
123-
124-
assertThat(recovered).isNotEmpty();
125-
assertThat(recovered.get(0)).isEqualTo(CHAIN_TASK_ID);
126-
127-
Mockito.verify(taskExecutorService, Mockito.times(1))
128-
.abortConsensusReached(CHAIN_TASK_ID);
129-
}
130-
131-
@Test
132-
public void shouldAbortSinceContributionTimeout() {
133-
when(iexecHubService.getLatestBlockNumber()).thenReturn(blockNumber);
134-
when(customFeignClient.getMissedTaskNotifications(blockNumber))
135-
.thenReturn(getStubInterruptedTasks(TaskNotificationType.PLEASE_ABORT_CONTRIBUTION_TIMEOUT));
136-
when(resultService.isResultZipFound(CHAIN_TASK_ID)).thenReturn(true);
137-
when(iexecHubService.getTaskDescriptionFromChain(any())).thenReturn(getStubModel());
138-
139-
List<String> recovered = amnesiaRecoveryService.recoverInterruptedReplicates();
140-
141-
assertThat(recovered).isNotEmpty();
142-
assertThat(recovered.get(0)).isEqualTo(CHAIN_TASK_ID);
74+
assertThat(recovered).isEmpty();
14375

144-
Mockito.verify(taskExecutorService, Mockito.times(1))
145-
.abortContributionTimeout(CHAIN_TASK_ID);
76+
Mockito.verify(subscriptionService, Mockito.times(0))
77+
.handleTaskNotification(notif);
14678
}
14779

14880
@Test
14981
public void shouldNotRecoverByRevealingWhenResultNotFound() {
15082
when(iexecHubService.getLatestBlockNumber()).thenReturn(blockNumber);
83+
TaskNotification notif = getStubInterruptedTask(TaskNotificationType.PLEASE_REVEAL);
15184
when(customFeignClient.getMissedTaskNotifications(blockNumber))
152-
.thenReturn(getStubInterruptedTasks(TaskNotificationType.PLEASE_REVEAL));
85+
.thenReturn(Collections.singletonList(notif));
15386
when(iexecHubService.getTaskDescriptionFromChain(any())).thenReturn(getStubModel());
15487
when(resultService.isResultFolderFound(CHAIN_TASK_ID)).thenReturn(false);
15588

15689
List<String> recovered = amnesiaRecoveryService.recoverInterruptedReplicates();
15790

15891
assertThat(recovered).isEmpty();
15992

160-
Mockito.verify(taskExecutorService, Mockito.times(0))
161-
.reveal(CHAIN_TASK_ID, blockNumber);
162-
}
163-
164-
@Test
165-
public void shouldRecoverByRevealingWhenResultFound() {
166-
when(iexecHubService.getLatestBlockNumber()).thenReturn(blockNumber);
167-
when(customFeignClient.getMissedTaskNotifications(blockNumber))
168-
.thenReturn(getStubInterruptedTasks(TaskNotificationType.PLEASE_REVEAL));
169-
when(iexecHubService.getTaskDescriptionFromChain(any())).thenReturn(getStubModel());
170-
when(resultService.isResultFolderFound(CHAIN_TASK_ID)).thenReturn(true);
171-
172-
List<String> recovered = amnesiaRecoveryService.recoverInterruptedReplicates();
173-
174-
assertThat(recovered).isNotEmpty();
175-
assertThat(recovered.get(0)).isEqualTo(CHAIN_TASK_ID);
176-
177-
Mockito.verify(taskExecutorService, Mockito.times(1))
178-
.reveal(CHAIN_TASK_ID, blockNumber);
93+
Mockito.verify(subscriptionService, Mockito.times(0))
94+
.handleTaskNotification(notif);
17995
}
18096

18197
@Test
18298
public void shouldNotRecoverByUploadingWhenResultNotFound() {
18399
when(iexecHubService.getLatestBlockNumber()).thenReturn(blockNumber);
100+
TaskNotification notif = getStubInterruptedTask(TaskNotificationType.PLEASE_UPLOAD);
184101
when(customFeignClient.getMissedTaskNotifications(blockNumber))
185-
.thenReturn(getStubInterruptedTasks(TaskNotificationType.PLEASE_UPLOAD));
102+
.thenReturn(Collections.singletonList(notif));
186103
when(iexecHubService.getTaskDescriptionFromChain(any())).thenReturn(getStubModel());
187104
when(resultService.isResultFolderFound(CHAIN_TASK_ID)).thenReturn(false);
188105

189106
List<String> recovered = amnesiaRecoveryService.recoverInterruptedReplicates();
190107

191108
assertThat(recovered).isEmpty();
192109

193-
Mockito.verify(taskExecutorService, Mockito.times(0))
194-
.uploadResult(CHAIN_TASK_ID);
110+
Mockito.verify(subscriptionService, Mockito.times(0))
111+
.handleTaskNotification(notif);
195112
}
196113

114+
// The notification type does not matter here since it is handled on the subscription service
197115
@Test
198-
public void shouldRecoverByUploadingWhenResultFound() {
116+
public void shouldNotificationPassedToSubscriptionService() {
199117
when(iexecHubService.getLatestBlockNumber()).thenReturn(blockNumber);
118+
TaskNotification notif = getStubInterruptedTask(TaskNotificationType.PLEASE_COMPLETE);
200119
when(customFeignClient.getMissedTaskNotifications(blockNumber))
201-
.thenReturn(getStubInterruptedTasks(TaskNotificationType.PLEASE_UPLOAD));
202-
when(iexecHubService.getTaskDescriptionFromChain(any())).thenReturn(getStubModel());
203-
when(resultService.isResultFolderFound(CHAIN_TASK_ID)).thenReturn(true);
204-
205-
List<String> recovered = amnesiaRecoveryService.recoverInterruptedReplicates();
206-
207-
assertThat(recovered).isNotEmpty();
208-
assertThat(recovered.get(0)).isEqualTo(CHAIN_TASK_ID);
209-
210-
Mockito.verify(taskExecutorService, Mockito.times(1))
211-
.uploadResult(CHAIN_TASK_ID);
212-
}
213-
214-
@Test
215-
public void shouldCompleteTask() {
216-
when(iexecHubService.getLatestBlockNumber()).thenReturn(blockNumber);
217-
when(customFeignClient.getMissedTaskNotifications(blockNumber))
218-
.thenReturn(getStubInterruptedTasks(TaskNotificationType.PLEASE_COMPLETE));
219-
220-
when(resultService.isResultZipFound(CHAIN_TASK_ID)).thenReturn(true);
120+
.thenReturn(Collections.singletonList(notif));
121+
when(resultService.isResultAvailable(CHAIN_TASK_ID)).thenReturn(true);
221122
when(iexecHubService.getTaskDescriptionFromChain(any())).thenReturn(getStubModel());
222123

223124
List<String> recovered = amnesiaRecoveryService.recoverInterruptedReplicates();
224125

225126
assertThat(recovered).isNotEmpty();
226127
assertThat(recovered.get(0)).isEqualTo(CHAIN_TASK_ID);
227128

228-
Mockito.verify(taskExecutorService, Mockito.times(1))
229-
.completeTask(CHAIN_TASK_ID);
129+
Mockito.verify(subscriptionService, Mockito.times(1))
130+
.handleTaskNotification(notif);
230131
}
231132

232-
List<TaskNotification> getStubInterruptedTasks(TaskNotificationType notificationType) {
233-
TaskNotification interruptedReplicate = TaskNotification.builder()
133+
private TaskNotification getStubInterruptedTask(TaskNotificationType notificationType) {
134+
return TaskNotification.builder()
234135
.chainTaskId(CHAIN_TASK_ID)
235136
.taskNotificationType(notificationType)
236137
.taskNotificationExtra(TaskNotificationExtra.builder()
237138
.contributionAuthorization(getStubAuth())
238139
.build())
239140
.build();
240141

241-
return Collections.singletonList(interruptedReplicate);
242142
}
243143

244-
ContributionAuthorization getStubAuth() {
144+
private ContributionAuthorization getStubAuth() {
245145
return ContributionAuthorization.builder()
246146
.chainTaskId(CHAIN_TASK_ID)
247147
.build();
248148
}
249149

250-
Optional<TaskDescription> getStubModel() {
150+
private Optional<TaskDescription> getStubModel() {
251151
return Optional.of(TaskDescription.builder()
252152
.chainTaskId(CHAIN_TASK_ID)
253153
.build());

0 commit comments

Comments
 (0)