Skip to content

Commit 1ba52a2

Browse files
committed
Remove canAcceptMoreWorks(String walletAddress) method
1 parent 589cc16 commit 1ba52a2

File tree

3 files changed

+36
-40
lines changed

3 files changed

+36
-40
lines changed

src/main/java/com/iexec/core/worker/WorkerService.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,6 @@ public List<Worker> getAliveWorkers() {
133133
return workerRepository.findByWalletAddressIn(aliveWorkers);
134134
}
135135

136-
public boolean canAcceptMoreWorks(String walletAddress) {
137-
Optional<Worker> optionalWorker = getWorker(walletAddress);
138-
return optionalWorker.filter(this::canAcceptMoreWorks).isPresent();
139-
140-
}
141-
142136
public boolean canAcceptMoreWorks(Worker worker) {
143137
int workerMaxNbTasks = worker.getMaxNbTasks();
144138
int runningReplicateNb = worker.getComputingChainTaskIds().size();

src/test/java/com/iexec/core/replicate/ReplicateSupplyServiceTests.java

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,14 @@ void init() {
9191
MockitoAnnotations.openMocks(this);
9292
}
9393

94-
void workerCanWorkAndHasGas(String workerAddress) {
95-
when(workerService.canAcceptMoreWorks(workerAddress)).thenReturn(true);
96-
when(web3jService.hasEnoughGas(workerAddress)).thenReturn(true);
97-
}
98-
9994
// Tests on getAuthOfAvailableReplicate()
10095

10196
// If worker does not exist, canAcceptMoreWorks return false
10297
// It is not possible in the current implementation to test workerService.getWorker with an empty Optional
10398
// in getAuthOfAvailableReplicate method
10499
@Test
105100
void shouldNotGetAnyReplicateSinceWorkerDoesNotExist() {
106-
workerCanWorkAndHasGas(WALLET_WORKER_1);
101+
when(web3jService.hasEnoughGas(WALLET_WORKER_1)).thenReturn(true);
107102
when(workerService.getWorker(Mockito.anyString())).thenReturn(Optional.empty());
108103
Optional<ReplicateTaskSummary> replicateTaskSummary =
109104
replicateSupplyService.getAvailableReplicateTaskSummary(workerLastBlock, WALLET_WORKER_1);
@@ -113,7 +108,7 @@ void shouldNotGetAnyReplicateSinceWorkerDoesNotExist() {
113108

114109
@Test
115110
void shouldNotGetReplicateSinceWorkerLastBlockNotAvailable() {
116-
workerCanWorkAndHasGas(WALLET_WORKER_1);
111+
when(web3jService.hasEnoughGas(WALLET_WORKER_1)).thenReturn(true);
117112
Optional<ReplicateTaskSummary> replicateTaskSummary =
118113
replicateSupplyService.getAvailableReplicateTaskSummary(0, WALLET_WORKER_1);
119114
assertThat(replicateTaskSummary).isEmpty();
@@ -122,7 +117,18 @@ void shouldNotGetReplicateSinceWorkerLastBlockNotAvailable() {
122117

123118
@Test
124119
void shouldNotGetReplicateSinceNoRunningTask() {
125-
workerCanWorkAndHasGas(WALLET_WORKER_1);
120+
final Worker worker = Worker.builder()
121+
.id("1")
122+
.walletAddress(WALLET_WORKER_1)
123+
.cpuNb(4)
124+
.maxNbTasks(3)
125+
.teeEnabled(false)
126+
.build();
127+
128+
when(web3jService.hasEnoughGas(WALLET_WORKER_1)).thenReturn(true);
129+
when(workerService.getWorker(WALLET_WORKER_1))
130+
.thenReturn(Optional.ofNullable(worker));
131+
126132
when(taskService.getPrioritizedInitializedOrRunningTask(false, Collections.emptyList())).thenReturn(Optional.empty());
127133
Optional<ReplicateTaskSummary> replicateTaskSummary =
128134
replicateSupplyService.getAvailableReplicateTaskSummary(workerLastBlock, WALLET_WORKER_1);
@@ -148,7 +154,7 @@ void shouldNotGetReplicateSinceNoReplicatesList() {
148154
runningTask.setContributionDeadline(DateTimeUtils.addMinutesToDate(new Date(), 60));
149155
runningTask.setEnclaveChallenge(BytesUtils.EMPTY_ADDRESS);
150156

151-
workerCanWorkAndHasGas(WALLET_WORKER_2);
157+
when(web3jService.hasEnoughGas(WALLET_WORKER_2)).thenReturn(true);
152158
when(taskService.getPrioritizedInitializedOrRunningTask(true, Collections.emptyList()))
153159
.thenReturn(Optional.of(runningTask));
154160
when(workerService.getWorker(WALLET_WORKER_2)).thenReturn(Optional.of(worker));
@@ -185,7 +191,7 @@ void shouldNotGetReplicateSinceConsensusReachedOnChain() {
185191
runningTask.setContributionDeadline(DateTimeUtils.addMinutesToDate(new Date(), 60));
186192
runningTask.setEnclaveChallenge(BytesUtils.EMPTY_ADDRESS);
187193

188-
workerCanWorkAndHasGas(WALLET_WORKER_2);
194+
when(web3jService.hasEnoughGas(WALLET_WORKER_2)).thenReturn(true);
189195
when(taskService.getPrioritizedInitializedOrRunningTask(true, Collections.emptyList()))
190196
.thenReturn(Optional.of(runningTask));
191197
when(workerService.getWorker(WALLET_WORKER_2)).thenReturn(Optional.of(worker));
@@ -206,7 +212,14 @@ void shouldNotGetReplicateSinceConsensusReachedOnChain() {
206212

207213
@Test
208214
void shouldNotGetAnyReplicateSinceWorkerIsFull() {
209-
when(workerService.canAcceptMoreWorks(WALLET_WORKER_1)).thenReturn(false);
215+
final Worker worker = Worker.builder()
216+
.walletAddress(WALLET_WORKER_1)
217+
.cpuNb(2)
218+
.maxNbTasks(1)
219+
.build();
220+
when(workerService.getWorker(WALLET_WORKER_1))
221+
.thenReturn(Optional.of(worker));
222+
when(workerService.canAcceptMoreWorks(worker)).thenReturn(false);
210223
Optional<ReplicateTaskSummary> replicateTaskSummary =
211224
replicateSupplyService.getAvailableReplicateTaskSummary(workerLastBlock, WALLET_WORKER_1);
212225
assertThat(replicateTaskSummary).isEmpty();
@@ -215,7 +228,6 @@ void shouldNotGetAnyReplicateSinceWorkerIsFull() {
215228

216229
@Test
217230
void shouldNotGetAnyReplicateSinceWorkerDoesNotHaveEnoughGas() {
218-
when(workerService.canAcceptMoreWorks(WALLET_WORKER_1)).thenReturn(true);
219231
when(web3jService.hasEnoughGas(WALLET_WORKER_1)).thenReturn(false);
220232
Optional<ReplicateTaskSummary> replicateTaskSummary =
221233
replicateSupplyService.getAvailableReplicateTaskSummary(workerLastBlock, WALLET_WORKER_1);
@@ -245,7 +257,8 @@ void shouldNotGetAnyReplicateSinceWorkerAlreadyParticipated() {
245257
Collections.singletonList(new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID))
246258
));
247259

248-
workerCanWorkAndHasGas(WALLET_WORKER_1);
260+
when(web3jService.hasEnoughGas(WALLET_WORKER_1)).thenReturn(true);
261+
when(web3jService.hasEnoughGas(WALLET_WORKER_1)).thenReturn(true);
249262
when(taskService.getPrioritizedInitializedOrRunningTask(true, Collections.emptyList()))
250263
.thenReturn(Optional.of(runningTask));
251264
when(workerService.getWorker(WALLET_WORKER_1)).thenReturn(Optional.of(existingWorker));
@@ -312,6 +325,7 @@ void shouldNotGetReplicateSinceEnclaveChallengeNeededButNotGenerated() {
312325
.id("1")
313326
.walletAddress(WALLET_WORKER_1)
314327
.cpuNb(2)
328+
.maxNbTasks(1)
315329
.teeEnabled(true)
316330
.build();
317331

@@ -326,7 +340,7 @@ void shouldNotGetReplicateSinceEnclaveChallengeNeededButNotGenerated() {
326340
new ReplicatesList(CHAIN_TASK_ID, Collections.emptyList())
327341
);
328342

329-
workerCanWorkAndHasGas(WALLET_WORKER_1);
343+
when(web3jService.hasEnoughGas(WALLET_WORKER_1)).thenReturn(true);
330344
when(taskService.getPrioritizedInitializedOrRunningTask(false, Collections.emptyList()))
331345
.thenReturn(Optional.of(runningTask));
332346
when(workerService.getWorker(WALLET_WORKER_1)).thenReturn(Optional.of(existingWorker));
@@ -371,7 +385,7 @@ void shouldGetOnlyOneReplicateSinceOtherOneReachedConsensusDeadline() {
371385

372386
ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.emptyList());
373387

374-
workerCanWorkAndHasGas(WALLET_WORKER_1);
388+
when(web3jService.hasEnoughGas(WALLET_WORKER_1)).thenReturn(true);
375389
when(taskService.getPrioritizedInitializedOrRunningTask(true, List.of(CHAIN_TASK_ID)))
376390
.thenReturn(Optional.of(taskDeadlineReached));
377391
when(taskService.getPrioritizedInitializedOrRunningTask(true, Collections.emptyList()))
@@ -414,7 +428,7 @@ void shouldNotGetReplicateWhenTaskAlreadyAccessed() {
414428
runningTask.setContributionDeadline(DateTimeUtils.addMinutesToDate(new Date(), 60));
415429
runningTask.setEnclaveChallenge(BytesUtils.EMPTY_ADDRESS);
416430

417-
workerCanWorkAndHasGas(WALLET_WORKER_1);
431+
when(web3jService.hasEnoughGas(WALLET_WORKER_1)).thenReturn(true);
418432
when(taskService.getPrioritizedInitializedOrRunningTask(true, Collections.emptyList()))
419433
.thenReturn(Optional.of(runningTask));
420434
when(workerService.getWorker(WALLET_WORKER_1)).thenReturn(Optional.of(existingWorker));
@@ -450,7 +464,7 @@ void shouldGetReplicateWithNoTee() {
450464
new ReplicatesList(CHAIN_TASK_ID, Collections.emptyList())
451465
);
452466

453-
workerCanWorkAndHasGas(WALLET_WORKER_1);
467+
when(web3jService.hasEnoughGas(WALLET_WORKER_1)).thenReturn(true);
454468
when(taskService.getPrioritizedInitializedOrRunningTask(true, Collections.emptyList()))
455469
.thenReturn(Optional.of(runningTask));
456470
when(workerService.getWorker(WALLET_WORKER_1)).thenReturn(Optional.of(existingWorker));
@@ -495,7 +509,7 @@ void shouldGetReplicateWithTee() {
495509
new ReplicatesList(CHAIN_TASK_ID, Collections.emptyList())
496510
);
497511

498-
workerCanWorkAndHasGas(WALLET_WORKER_1);
512+
when(web3jService.hasEnoughGas(WALLET_WORKER_1)).thenReturn(true);
499513
when(taskService.getPrioritizedInitializedOrRunningTask(false, Collections.emptyList()))
500514
.thenReturn(Optional.of(runningTask));
501515
when(workerService.getWorker(WALLET_WORKER_1)).thenReturn(Optional.of(existingWorker));
@@ -535,7 +549,7 @@ void shouldTeeNeededTaskNotBeGivenToTeeDisabledWorker() {
535549
runningTask.setTag(TEE_TAG);
536550
runningTask.setContributionDeadline(DateTimeUtils.addMinutesToDate(new Date(), 60));
537551

538-
workerCanWorkAndHasGas(WALLET_WORKER_1);
552+
when(web3jService.hasEnoughGas(WALLET_WORKER_1)).thenReturn(true);
539553
when(taskService.getPrioritizedInitializedOrRunningTask(true, Collections.emptyList()))
540554
.thenReturn(Optional.empty());
541555
when(workerService.getWorker(WALLET_WORKER_1)).thenReturn(Optional.of(existingWorker));
@@ -569,7 +583,7 @@ void shouldTeeNeededTaskBeGivenToTeeEnabledWorker() {
569583
new ReplicatesList(CHAIN_TASK_ID, Collections.emptyList())
570584
);
571585

572-
workerCanWorkAndHasGas(WALLET_WORKER_1);
586+
when(web3jService.hasEnoughGas(WALLET_WORKER_1)).thenReturn(true);
573587
when(taskService.getPrioritizedInitializedOrRunningTask(false, Collections.emptyList()))
574588
.thenReturn(Optional.of(runningTask));
575589
when(workerService.getWorker(WALLET_WORKER_1)).thenReturn(Optional.of(existingWorker));

src/test/java/com/iexec/core/worker/WorkerServiceTests.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -522,19 +522,8 @@ void shouldAcceptMoreWorks() {
522522
3,
523523
Arrays.asList("task1", "task2", "task3", "task4", "task5"),
524524
Arrays.asList("task1", "task3"));
525-
when(workerRepository.findByWalletAddress(walletAddress)).thenReturn(Optional.of(worker));
526-
527-
assertThat(workerService.canAcceptMoreWorks(walletAddress)).isTrue();
528-
}
529-
530-
@Test
531-
void shouldNotAcceptMoreWorksSinceWorkerNotFound() {
532-
String walletAddress = "0x1a69b2eb604db8eba185df03ea4f5288dcbbd248";
533-
534-
when(workerRepository.findByWalletAddress(Mockito.any())).thenReturn(Optional.empty());
535525

536-
boolean canAccept = workerService.canAcceptMoreWorks(walletAddress);
537-
assertThat(canAccept).isFalse();
526+
assertThat(workerService.canAcceptMoreWorks(worker)).isTrue();
538527
}
539528

540529
@Test
@@ -545,9 +534,8 @@ void shouldNotAcceptMoreWorksSinceSaturatedCpus() {
545534
2,
546535
Arrays.asList("task1", "task2", "task3", "task4"),
547536
Arrays.asList("task1", "task3"));
548-
when(workerRepository.findByWalletAddress(Mockito.anyString())).thenReturn(Optional.of(worker));
549537

550-
assertThat(workerService.canAcceptMoreWorks(walletAddress)).isFalse();
538+
assertThat(workerService.canAcceptMoreWorks(worker)).isFalse();
551539
}
552540

553541
List<Worker> getDummyWorkers() {

0 commit comments

Comments
 (0)