From 44252cdce509066e114622fadf4c5c4021aa493a Mon Sep 17 00:00:00 2001 From: Jeremy Bernard Date: Thu, 8 Feb 2024 15:56:24 +0100 Subject: [PATCH 1/3] Use `@DataMongoTest` and `@Testcontainers` annotations in replicates and compute logs tests --- CHANGELOG.md | 5 + build.gradle | 1 + .../iexec/core/logs/TaskLogsServiceTests.java | 59 +++-- .../core/replicate/ReplicateServiceTests.java | 206 +++++++++--------- .../iexec/core/task/TaskRepositoryTest.java | 2 +- .../task/TaskServiceRealRepositoryTest.java | 2 +- .../WorkerServiceRealRepositoryTests.java | 2 +- 7 files changed, 146 insertions(+), 131 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fa99757..8563664f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ All notable changes to this project will be documented in this file. - Filter out `CONTRIBUTE_AND_FINALIZE` tasks when detecting missed `REVEALED` status update. (#658) - Fetch `results` on-chain when updating a replicate status in `CONTRIBUTE_AND_FINALIZE` workflow. (#659 #660) +### Quality + +- Use `@DataMongoTest` and `@Testcontainers` annotations in replicates and compute logs tests. (#662) + + ## [[8.3.0]](https://github.com/iExecBlockchainComputing/iexec-core/releases/tag/v8.3.0) 2024-01-11 ### New Features diff --git a/build.gradle b/build.gradle index 9072640b..0f29ab3e 100644 --- a/build.gradle +++ b/build.gradle @@ -130,6 +130,7 @@ tasks.named("bootJar") { test { useJUnitPlatform() + systemProperty "mongo.image", "mongo:4.4.28-focal" } tasks.register('itest') { diff --git a/src/test/java/com/iexec/core/logs/TaskLogsServiceTests.java b/src/test/java/com/iexec/core/logs/TaskLogsServiceTests.java index a483e0fb..c6b92737 100644 --- a/src/test/java/com/iexec/core/logs/TaskLogsServiceTests.java +++ b/src/test/java/com/iexec/core/logs/TaskLogsServiceTests.java @@ -19,10 +19,14 @@ import com.iexec.common.replicate.ComputeLogs; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; import java.util.Collections; import java.util.List; @@ -31,32 +35,46 @@ import static com.iexec.commons.poco.utils.TestUtils.CHAIN_TASK_ID; import static com.iexec.commons.poco.utils.TestUtils.WORKER_ADDRESS; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.spy; +@DataMongoTest +@Testcontainers class TaskLogsServiceTests { private static final String STDOUT = "This is an stdout string"; private static final String STDERR = "This is an stderr string"; - @Mock - private TaskLogsRepository taskLogsRepository; - @InjectMocks - private TaskLogsService taskLogsService; + @Container + private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse(System.getProperty("mongo.image"))); + + @DynamicPropertySource + static void registerProperties(DynamicPropertyRegistry registry) { + registry.add("spring.data.mongodb.host", mongoDBContainer::getHost); + registry.add("spring.data.mongodb.port", () -> mongoDBContainer.getMappedPort(27017)); + } + + private final TaskLogsRepository taskLogsRepository; + private final TaskLogsService taskLogsService; + + @Autowired + public TaskLogsServiceTests(TaskLogsRepository taskLogsRepository) { + this.taskLogsRepository = taskLogsRepository; + this.taskLogsService = new TaskLogsService(taskLogsRepository); + spy(taskLogsRepository); + } @BeforeEach void init() { - MockitoAnnotations.openMocks(this); + taskLogsRepository.deleteAll(); } //region addComputeLogs @Test void shouldAddComputeLogs() { final ComputeLogs computeLogs = new ComputeLogs(WORKER_ADDRESS, STDOUT, STDERR); - - ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(TaskLogs.class); taskLogsService.addComputeLogs(CHAIN_TASK_ID, computeLogs); - verify(taskLogsRepository, times(1)).save(argumentCaptor.capture()); - TaskLogs capturedEvent = argumentCaptor.getAllValues().get(0); + assertThat(taskLogsRepository.count()).isOne(); + TaskLogs capturedEvent = taskLogsRepository.findOneByChainTaskId(CHAIN_TASK_ID).orElseThrow(); assertThat(capturedEvent.getComputeLogsList().get(0).getStdout()).isEqualTo(STDOUT); assertThat(capturedEvent.getComputeLogsList().get(0).getStderr()).isEqualTo(STDERR); assertThat(capturedEvent.getComputeLogsList().get(0).getWalletAddress()).isEqualTo(WORKER_ADDRESS); @@ -65,7 +83,7 @@ void shouldAddComputeLogs() { @Test void shouldNotAddComputeLogsSinceNull() { taskLogsService.addComputeLogs(CHAIN_TASK_ID, null); - verifyNoInteractions(taskLogsRepository); + assertThat(taskLogsRepository.count()).isZero(); } @Test @@ -75,14 +93,10 @@ void shouldNotAddComputeLogsSinceLogsAlreadyKnown() { .chainTaskId(CHAIN_TASK_ID) .computeLogsList(Collections.singletonList(computeLogs)) .build(); - - when(taskLogsService.getTaskLogs(CHAIN_TASK_ID)) - .thenReturn(Optional.of(taskLogs)); + taskLogsRepository.save(taskLogs); taskLogsService.addComputeLogs(CHAIN_TASK_ID, computeLogs); - - verify(taskLogsRepository).findOneByChainTaskId(CHAIN_TASK_ID); - verify(taskLogsRepository, times(0)).save(any()); + assertThat(taskLogsRepository.count()).isOne(); } //endregion @@ -94,8 +108,7 @@ void shouldGetComputeLogs() { .chainTaskId(CHAIN_TASK_ID) .computeLogsList(List.of(computeLogs)) .build(); - when(taskLogsRepository.findByChainTaskIdAndWalletAddress(CHAIN_TASK_ID, WORKER_ADDRESS)) - .thenReturn(Optional.of(taskLogs)); + taskLogsRepository.save(taskLogs); Optional optional = taskLogsService.getComputeLogs(CHAIN_TASK_ID, WORKER_ADDRESS); assertThat(optional).isPresent(); final ComputeLogs actualLogs = optional.get(); diff --git a/src/test/java/com/iexec/core/replicate/ReplicateServiceTests.java b/src/test/java/com/iexec/core/replicate/ReplicateServiceTests.java index ab17a8ef..12cabf82 100644 --- a/src/test/java/com/iexec/core/replicate/ReplicateServiceTests.java +++ b/src/test/java/com/iexec/core/replicate/ReplicateServiceTests.java @@ -24,15 +24,26 @@ import com.iexec.commons.poco.utils.BytesUtils; import com.iexec.core.chain.IexecHubService; import com.iexec.core.chain.Web3jService; +import com.iexec.core.logs.TaskLogsRepository; import com.iexec.core.logs.TaskLogsService; import com.iexec.core.result.ResultService; import io.vavr.control.Either; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.*; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; import org.web3j.utils.Numeric; import java.nio.charset.StandardCharsets; @@ -45,15 +56,28 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.*; +@DataMongoTest +@Testcontainers class ReplicateServiceTests { private static final UpdateReplicateStatusArgs UPDATE_ARGS = UpdateReplicateStatusArgs.builder() .workerWeight(1) .build(); - @Mock + @Container + private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse(System.getProperty("mongo.image"))); + + @DynamicPropertySource + static void registerProperties(DynamicPropertyRegistry registry) { + registry.add("spring.data.mongodb.host", mongoDBContainer::getHost); + registry.add("spring.data.mongodb.port", () -> mongoDBContainer.getMappedPort(27017)); + } + + @Autowired private MongoTemplate mongoTemplate; - @Mock + @Autowired + private TaskLogsRepository taskLogsRepository; + @Autowired private ReplicatesRepository replicatesRepository; @Mock private IexecHubService iexecHubService; @@ -63,15 +87,17 @@ class ReplicateServiceTests { private Web3jService web3jService; @Mock private ResultService resultService; - @Mock - private TaskLogsService taskLogsService; - @InjectMocks private ReplicatesService replicatesService; @BeforeEach void init() { MockitoAnnotations.openMocks(this); + TaskLogsService taskLogsService = new TaskLogsService(taskLogsRepository); + replicatesService = new ReplicatesService(mongoTemplate, replicatesRepository, iexecHubService, + applicationEventPublisher, web3jService, resultService, taskLogsService); + taskLogsRepository.deleteAll(); + replicatesRepository.deleteAll(); } @Test @@ -86,11 +112,10 @@ void shouldCreateNewReplicate() { list.add(replicate1); list.add(replicate2); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, list); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); - when(replicatesRepository.save(any())).thenReturn(replicatesList); + replicatesRepository.save(replicatesList); replicatesService.addNewReplicate(replicatesList, WALLET_WORKER_3); - Mockito.verify(replicatesRepository, Mockito.times(1)) - .save(any()); + final ReplicatesList result = replicatesRepository.findByChainTaskId(CHAIN_TASK_ID).orElseThrow(); + assertThat(result.getReplicates()).hasSize(3); } @Test @@ -105,35 +130,29 @@ void shouldNotCreateNewReplicate() { list.add(replicate1); list.add(replicate2); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, list); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); - when(replicatesRepository.save(any())).thenReturn(replicatesList); + replicatesRepository.save(replicatesList); replicatesService.addNewReplicate(replicatesList, WALLET_WORKER_1); - Mockito.verify(replicatesRepository, Mockito.times(0)) - .save(any()); replicatesService.addNewReplicate(replicatesList, WALLET_WORKER_2); - Mockito.verify(replicatesRepository, Mockito.times(0)) - .save(any()); + assertThat(replicatesService.getReplicates(CHAIN_TASK_ID)).hasSize(2); } @Test void shouldCreateEmptyReplicateList() { replicatesService.createEmptyReplicateList(CHAIN_TASK_ID); - - Mockito.verify(replicatesRepository, Mockito.times(1)).save(new ReplicatesList(CHAIN_TASK_ID)); + assertThat(replicatesRepository.count()).isOne(); } @Test void shouldHaveReplicates() { - when(replicatesRepository.countByChainTaskId(CHAIN_TASK_ID)).thenReturn(Long.valueOf(1)); - Assertions.assertTrue(replicatesService.hasReplicatesList(CHAIN_TASK_ID)); + replicatesService.createEmptyReplicateList(CHAIN_TASK_ID); + assertThat(replicatesService.hasReplicatesList(CHAIN_TASK_ID)).isTrue(); } @Test void shouldNotHaveReplicates() { - when(replicatesRepository.countByChainTaskId(CHAIN_TASK_ID)).thenReturn(Long.valueOf(0)); - Assertions.assertFalse(replicatesService.hasReplicatesList(CHAIN_TASK_ID)); + assertThat(replicatesService.hasReplicatesList(CHAIN_TASK_ID)).isFalse(); } @Test @@ -143,15 +162,14 @@ void shouldGetReplicates() { replicate1.updateStatus(COMPUTED, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate1)); + replicatesRepository.save(replicatesList); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); assertThat(replicatesService.getReplicates(CHAIN_TASK_ID)).isNotNull(); assertThat(replicatesService.getReplicates(CHAIN_TASK_ID)).hasSize(1); } @Test void shouldNotGetReplicates() { - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.empty()); assertThat(replicatesService.getReplicates(CHAIN_TASK_ID)).isEmpty(); } @@ -164,15 +182,14 @@ void shouldGetReplicate() { replicate2.updateStatus(STARTING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Arrays.asList(replicate1, replicate2)); + replicatesRepository.save(replicatesList); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); assertThat(replicatesService.getReplicate(CHAIN_TASK_ID, WALLET_WORKER_1)).isEqualTo(Optional.of(replicate1)); assertThat(replicatesService.getReplicate(CHAIN_TASK_ID, WALLET_WORKER_2)).isEqualTo(Optional.of(replicate2)); } @Test void shouldNotGetReplicate1() { - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.empty()); assertThat(replicatesService.getReplicate(CHAIN_TASK_ID, WALLET_WORKER_1)).isEmpty(); } @@ -185,8 +202,8 @@ void shouldNotGetReplicate2() { replicate2.updateStatus(STARTING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Arrays.asList(replicate1, replicate2)); + replicatesRepository.save(replicatesList); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); assertThat(replicatesService.getReplicate(CHAIN_TASK_ID, WALLET_WORKER_3)).isEmpty(); } @@ -201,11 +218,10 @@ void shouldGetCorrectNbReplicatesWithOneStatus() { replicate3.updateStatus(STARTING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Arrays.asList(replicate1, replicate2, replicate3)); - - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); + replicatesRepository.save(replicatesList); assertThat(replicatesService.getNbReplicatesWithCurrentStatus(CHAIN_TASK_ID, STARTING)).isEqualTo(2); - assertThat(replicatesService.getNbReplicatesWithCurrentStatus(CHAIN_TASK_ID, COMPUTED)).isEqualTo(1); + assertThat(replicatesService.getNbReplicatesWithCurrentStatus(CHAIN_TASK_ID, COMPUTED)).isOne(); assertThat(replicatesService.getNbReplicatesWithCurrentStatus(CHAIN_TASK_ID, CONTRIBUTED)).isZero(); } @@ -224,8 +240,7 @@ void shouldGetCorrectNbReplicatesWithMultipleStatus() { replicate4.updateStatus(CONTRIBUTED, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Arrays.asList(replicate1, replicate2, replicate3, replicate4)); - - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); + replicatesRepository.save(replicatesList); int shouldBe2 = replicatesService.getNbReplicatesWithCurrentStatus(CHAIN_TASK_ID, COMPUTED, CONTRIBUTED); assertThat(shouldBe2).isEqualTo(2); @@ -253,8 +268,7 @@ void shouldGetCorrectNbReplicatesWithOneLastRelevantStatus() { replicate4.updateStatus(WORKER_LOST, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Arrays.asList(replicate1, replicate2, replicate3, replicate4)); - - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); + replicatesRepository.save(replicatesList); assertThat(replicatesService.getNbReplicatesWithLastRelevantStatus(CHAIN_TASK_ID, STARTING)).isEqualTo(3); assertThat(replicatesService.getNbReplicatesWithLastRelevantStatus(CHAIN_TASK_ID, COMPUTED)).isEqualTo(1); @@ -279,8 +293,7 @@ void shouldGetCorrectNbReplicatesWithMultipleLastReleveantStatus() { replicate5.updateStatus(RECOVERING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Arrays.asList(replicate1, replicate2, replicate3, replicate4, replicate5)); - - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); + replicatesRepository.save(replicatesList); int shouldBe2 = replicatesService.getNbReplicatesWithLastRelevantStatus(CHAIN_TASK_ID, COMPUTED, CONTRIBUTED); assertThat(shouldBe2).isEqualTo(2); @@ -305,8 +318,7 @@ void shouldGetCorrectNbReplicatesContainingOneStatus() { replicate3.updateStatus(STARTING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Arrays.asList(replicate1, replicate2, replicate3)); - - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); + replicatesRepository.save(replicatesList); assertThat(replicatesService.getNbReplicatesContainingStatus(CHAIN_TASK_ID, STARTING)).isEqualTo(3); assertThat(replicatesService.getNbReplicatesContainingStatus(CHAIN_TASK_ID, COMPUTED)).isEqualTo(1); @@ -328,8 +340,7 @@ void shouldGetCorrectNbReplicatesContainingMultipleStatus() { replicate4.updateStatus(CONTRIBUTED, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Arrays.asList(replicate1, replicate2, replicate3, replicate4)); - - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); + replicatesRepository.save(replicatesList); int shouldBe2 = replicatesService.getNbReplicatesContainingStatus(CHAIN_TASK_ID, COMPUTED, CONTRIBUTED); assertThat(shouldBe2).isEqualTo(2); @@ -358,7 +369,7 @@ void shouldGetReplicateWithRevealStatus() { replicate2.updateStatus(CONTRIBUTED, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Arrays.asList(replicate, replicate2)); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); + replicatesRepository.save(replicatesList); Optional optional = replicatesService.getRandomReplicateWithRevealStatus(CHAIN_TASK_ID); assertThat(optional).contains(replicate); @@ -366,8 +377,6 @@ void shouldGetReplicateWithRevealStatus() { @Test void shouldNotGetReplicateWithRevealStatusSinceEmptyReplicatesList() { - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.empty()); - Optional optional = replicatesService.getRandomReplicateWithRevealStatus(CHAIN_TASK_ID); assertThat(optional).isEmpty(); } @@ -386,7 +395,7 @@ void shouldNotGetReplicateWithRevealStatusWithNonEmptyList() { replicate2.updateStatus(CONTRIBUTED, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Arrays.asList(replicate, replicate2)); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); + replicatesRepository.save(replicatesList); Optional optional = replicatesService.getRandomReplicateWithRevealStatus(CHAIN_TASK_ID); assertThat(optional).isEmpty(); @@ -399,11 +408,10 @@ void shouldUpdateReplicateStatusWithoutStdout() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(CONTRIBUTING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); + replicatesRepository.save(replicatesList); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); when(web3jService.isBlockAvailable(anyLong())).thenReturn(true); when(iexecHubService.repeatIsContributedTrue(anyString(), anyString())).thenReturn(true); - when(replicatesRepository.save(replicatesList)).thenReturn(replicatesList); String resultHash = "hash"; when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_WORKER_1)).thenReturn(Optional.of(ChainContribution.builder() .resultHash(resultHash) @@ -428,8 +436,9 @@ void shouldUpdateReplicateStatusWithoutStdout() { assertThat(capturedEvent.getReplicateStatusUpdate().getStatus()).isEqualTo(CONTRIBUTED); assertThat(capturedEvent.getReplicateStatusUpdate().getDetails().getChainReceipt().getBlockNumber()) .isEqualTo(10); - assertThat(replicatesList.getReplicates().get(0).getContributionHash()).isEqualTo(resultHash); - Mockito.verify(taskLogsService, never()).addComputeLogs(anyString(), any()); + final ReplicatesList result = replicatesRepository.findByChainTaskId(CHAIN_TASK_ID).orElseThrow(); + assertThat(result.getReplicates().get(0).getContributionHash()).isEqualTo(resultHash); + assertThat(taskLogsRepository.count()).isZero(); assertThat(capturedEvent.getReplicateStatusUpdate().getDetails().getComputeLogs()).isNull(); } @@ -439,6 +448,7 @@ void shouldUpdateReplicateStatusWithStdoutIfComputed() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(COMPUTING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); + replicatesRepository.save(replicatesList); final ComputeLogs computeLogs = ComputeLogs.builder().walletAddress(WALLET_WORKER_1).stdout(stdout).build(); ReplicateStatusDetails details = ReplicateStatusDetails.builder().computeLogs(computeLogs).build(); ReplicateStatusUpdate statusUpdate = ReplicateStatusUpdate.builder() @@ -447,8 +457,6 @@ void shouldUpdateReplicateStatusWithStdoutIfComputed() { .details(details) .build(); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(ReplicateUpdatedEvent.class); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); - when(replicatesRepository.save(replicatesList)).thenReturn(replicatesList); when(iexecHubService.getTaskDescription(CHAIN_TASK_ID)) .thenReturn(TaskDescription.builder().isTeeTask(true).build()); @@ -459,7 +467,7 @@ void shouldUpdateReplicateStatusWithStdoutIfComputed() { assertThat(capturedEvent.getChainTaskId()).isEqualTo(replicate.getChainTaskId()); assertThat(capturedEvent.getWalletAddress()).isEqualTo(WALLET_WORKER_1); assertThat(capturedEvent.getReplicateStatusUpdate().getStatus()).isEqualTo(COMPUTED); - Mockito.verify(taskLogsService, times(1)).addComputeLogs(CHAIN_TASK_ID, computeLogs); + assertThat(taskLogsRepository.count()).isOne(); assertThat(capturedEvent.getReplicateStatusUpdate().getDetails().getComputeLogs()).isNull(); } @@ -469,6 +477,7 @@ void shouldUpdateReplicateStatusWithStdoutIfAppComputeFailed() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(COMPUTING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); + replicatesRepository.save(replicatesList); ComputeLogs computeLogs = ComputeLogs.builder().walletAddress(WALLET_WORKER_1).stdout(stdout).build(); ReplicateStatusDetails details = ReplicateStatusDetails.builder() .cause(ReplicateStatusCause.APP_COMPUTE_FAILED) @@ -480,8 +489,6 @@ void shouldUpdateReplicateStatusWithStdoutIfAppComputeFailed() { .details(details) .build(); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(ReplicateUpdatedEvent.class); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); - when(replicatesRepository.save(replicatesList)).thenReturn(replicatesList); replicatesService.updateReplicateStatus(CHAIN_TASK_ID, WALLET_WORKER_1, statusUpdate); Mockito.verify(applicationEventPublisher, Mockito.times(1)) @@ -492,19 +499,15 @@ void shouldUpdateReplicateStatusWithStdoutIfAppComputeFailed() { assertThat(capturedEvent.getReplicateStatusUpdate().getStatus()).isEqualTo(COMPUTE_FAILED); assertThat(capturedEvent.getReplicateStatusUpdate().getDetails().getCause()) .isEqualTo(ReplicateStatusCause.APP_COMPUTE_FAILED); - Mockito.verify(taskLogsService, times(1)).addComputeLogs(CHAIN_TASK_ID, computeLogs); + assertThat(taskLogsRepository.count()).isOne(); assertThat(capturedEvent.getReplicateStatusUpdate().getDetails().getComputeLogs()).isNull(); } @Test void shouldNotUpdateReplicateStatusSinceNoReplicateList() { - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.empty()); - replicatesService.updateReplicateStatus(CHAIN_TASK_ID, WALLET_WORKER_1, new ReplicateStatusUpdate(REVEALING)); - Mockito.verify(replicatesRepository, Mockito.times(0)) - .save(any()); - Mockito.verify(applicationEventPublisher, Mockito.times(0)) - .publishEvent(any()); + assertThat(replicatesRepository.count()).isZero(); + verifyNoInteractions(applicationEventPublisher); } @Test @@ -512,8 +515,7 @@ void shouldNotUpdateReplicateStatusSinceNoMatchingReplicate() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(CONTRIBUTED, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); - - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); + replicatesRepository.save(replicatesList); // Call on a different worker ReplicateStatusUpdate statusUpdate = ReplicateStatusUpdate.builder() @@ -522,10 +524,7 @@ void shouldNotUpdateReplicateStatusSinceNoMatchingReplicate() { .build(); replicatesService.updateReplicateStatus(CHAIN_TASK_ID, WALLET_WORKER_2, statusUpdate); - Mockito.verify(replicatesRepository, Mockito.times(0)) - .save(any()); - Mockito.verify(applicationEventPublisher, Mockito.times(0)) - .publishEvent(any()); + verifyNoInteractions(applicationEventPublisher); } @Test @@ -533,8 +532,7 @@ void shouldNotUpdateReplicateStatusSinceInvalidWorkflowTransition() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(CONTRIBUTED, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); - - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); + replicatesRepository.save(replicatesList); ReplicateStatusUpdate statusUpdate = ReplicateStatusUpdate.builder() .modifier(WORKER) @@ -542,10 +540,7 @@ void shouldNotUpdateReplicateStatusSinceInvalidWorkflowTransition() { .build(); replicatesService.updateReplicateStatus(CHAIN_TASK_ID, WALLET_WORKER_1, statusUpdate); - Mockito.verify(replicatesRepository, Mockito.times(0)) - .save(any()); - Mockito.verify(applicationEventPublisher, Mockito.times(0)) - .publishEvent(any()); + verifyNoInteractions(applicationEventPublisher); } @Test @@ -553,8 +548,8 @@ void shouldNotUpdateReplicateStatusSinceWrongOnChainStatus() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(CONTRIBUTING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); + replicatesRepository.save(replicatesList); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); when(web3jService.isBlockAvailable(anyLong())).thenReturn(true); when(iexecHubService.repeatIsContributedTrue(anyString(), anyString())).thenReturn(false); @@ -564,10 +559,8 @@ void shouldNotUpdateReplicateStatusSinceWrongOnChainStatus() { .build(); replicatesService.updateReplicateStatus(CHAIN_TASK_ID, WALLET_WORKER_1, statusUpdate); - Mockito.verify(replicatesRepository, Mockito.times(0)) - .save(any()); - Mockito.verify(applicationEventPublisher, Mockito.times(0)) - .publishEvent(any()); + assertThat(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).contains(replicatesList); + verifyNoInteractions(applicationEventPublisher); } @Test @@ -575,12 +568,11 @@ void shouldNotUpdateReplicateStatusToContributedSinceGetContributionFailed() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(CONTRIBUTING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); + replicatesRepository.save(replicatesList); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); when(web3jService.isBlockAvailable(anyLong())).thenReturn(true); when(iexecHubService.repeatIsContributedTrue(anyString(), anyString())).thenReturn(false); when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_WORKER_1)).thenReturn(Optional.empty()); - when(replicatesRepository.save(replicatesList)).thenReturn(replicatesList); ReplicateStatusDetails details = new ReplicateStatusDetails(10L); ReplicateStatusUpdate statusUpdate = ReplicateStatusUpdate.builder() @@ -591,8 +583,7 @@ void shouldNotUpdateReplicateStatusToContributedSinceGetContributionFailed() { replicatesService.updateReplicateStatus(CHAIN_TASK_ID, WALLET_WORKER_1, statusUpdate); - Mockito.verify(applicationEventPublisher, Mockito.times(0)) - .publishEvent(any()); + verifyNoInteractions(applicationEventPublisher); } @Test @@ -600,8 +591,8 @@ void shouldNotUpdateReplicateStatusToContributedSinceCannotGetWorkerWeight() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(CONTRIBUTING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); + replicatesRepository.save(replicatesList); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); when(web3jService.isBlockAvailable(anyLong())).thenReturn(true); when(iexecHubService.repeatIsContributedTrue(anyString(), anyString())).thenReturn(false); String resultHash = "hash"; @@ -610,8 +601,6 @@ void shouldNotUpdateReplicateStatusToContributedSinceCannotGetWorkerWeight() { .build())); when(iexecHubService.getWorkerWeight(WALLET_WORKER_1)).thenReturn(0); - when(replicatesRepository.save(replicatesList)).thenReturn(replicatesList); - ReplicateStatusDetails details = new ReplicateStatusDetails(10L); ReplicateStatusUpdate statusUpdate = ReplicateStatusUpdate.builder() .modifier(WORKER) @@ -621,8 +610,7 @@ void shouldNotUpdateReplicateStatusToContributedSinceCannotGetWorkerWeight() { replicatesService.updateReplicateStatus(CHAIN_TASK_ID, WALLET_WORKER_1, statusUpdate); - Mockito.verify(applicationEventPublisher, Mockito.times(0)) - .publishEvent(any()); + verifyNoInteractions(applicationEventPublisher); } @Test @@ -630,8 +618,7 @@ void shouldNotUpdateReplicateStatusSinceAlreadyReported() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(CONTRIBUTED, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); - - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); + replicatesRepository.save(replicatesList); ReplicateStatusUpdate statusUpdate = ReplicateStatusUpdate.builder() .modifier(WORKER) @@ -648,11 +635,10 @@ void shouldNotEncounterRaceConditionOnReplicateUpdate() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(REVEALING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); + replicatesRepository.save(replicatesList); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); when(web3jService.isBlockAvailable(anyLong())).thenReturn(true); when(iexecHubService.repeatIsRevealedTrue(anyString(), anyString())).thenReturn(true); - when(replicatesRepository.save(replicatesList)).thenReturn(replicatesList); ReplicateStatusUpdate statusUpdate = ReplicateStatusUpdate.builder() .modifier(WORKER) @@ -665,7 +651,13 @@ void shouldNotEncounterRaceConditionOnReplicateUpdate() { .parallel() .forEach(i -> replicatesService.updateReplicateStatus(CHAIN_TASK_ID, WALLET_WORKER_1, statusUpdate)); - assertThat(replicate.getStatusUpdateList().stream().filter(update -> REVEALED.equals(update.getStatus())).count()).isOne(); + replicate = replicatesRepository.findByChainTaskId(CHAIN_TASK_ID) + .map(ReplicatesList::getReplicates) + .stream() + .flatMap(List::stream) + .findFirst() + .orElseThrow(); + assertThat(replicate.getStatusUpdateList().stream().filter(update -> REVEALED == update.getStatus()).count()).isOne(); } @Test @@ -673,11 +665,10 @@ void shouldEncounterRaceConditionOnReplicateUpdateWithoutThreadSafety() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(REVEALING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); + replicatesRepository.save(replicatesList); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); when(web3jService.isBlockAvailable(anyLong())).thenReturn(true); when(iexecHubService.repeatIsRevealedTrue(anyString(), anyString())).thenReturn(true); - when(replicatesRepository.save(replicatesList)).thenReturn(replicatesList); ReplicateStatusUpdate statusUpdate = ReplicateStatusUpdate.builder() .modifier(WORKER) @@ -698,6 +689,12 @@ void shouldEncounterRaceConditionOnReplicateUpdateWithoutThreadSafety() { return; } + replicate = replicatesRepository.findByChainTaskId(CHAIN_TASK_ID) + .map(ReplicatesList::getReplicates) + .stream() + .flatMap(List::stream) + .findFirst() + .orElseThrow(); final long revealedUpdateCount = replicate.getStatusUpdateList() .stream() .filter(update -> REVEALED.equals(update.getStatus())) @@ -715,14 +712,13 @@ void shouldNotSetContributionHashSinceRevealing() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(REVEALING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); + replicatesRepository.save(replicatesList); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); when(web3jService.isBlockAvailable(anyLong())).thenReturn(true); when(iexecHubService.repeatIsRevealedTrue(anyString(), anyString())).thenReturn(true); when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_WORKER_1)).thenReturn(Optional.of(ChainContribution.builder() .resultHash("hash") .build())); - when(replicatesRepository.save(replicatesList)).thenReturn(replicatesList); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(ReplicateUpdatedEvent.class); ReplicateStatusDetails details = new ReplicateStatusDetails(10L); @@ -752,13 +748,12 @@ void shouldUpdateToResultUploaded() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(RESULT_UPLOADING, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); + replicatesRepository.save(replicatesList); ReplicateStatusUpdate statusUpdate = ReplicateStatusUpdate.builder() .modifier(WORKER) .status(RESULT_UPLOADED) .build(); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(ReplicateUpdatedEvent.class); - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).thenReturn(Optional.of(replicatesList)); - when(replicatesRepository.save(replicatesList)).thenReturn(replicatesList); final UpdateReplicateStatusArgs updateArgs = UpdateReplicateStatusArgs .builder() @@ -779,16 +774,14 @@ void shouldUpdateToResultUploaded() { // region getReplicateWithResultUploadedStatus @Test - void should() { + void shouldGetReplicateWithResultUploadedStatus() { Replicate replicate1 = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate1.updateStatus(CONTRIBUTED, ReplicateStatusModifier.WORKER); Replicate replicate2 = new Replicate(WALLET_WORKER_2, CHAIN_TASK_ID); replicate2.updateStatus(RESULT_UPLOADED, ReplicateStatusModifier.WORKER); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Arrays.asList(replicate1, replicate2)); - - when(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)) - .thenReturn(Optional.of(replicatesList)); + replicatesRepository.save(replicatesList); assertThat(replicatesService .getReplicateWithResultUploadedStatus(CHAIN_TASK_ID) @@ -905,10 +898,13 @@ void shouldFindReplicatedRevealedOnchain() { void shouldSetTriggerReplicateUpdateIfRevealTimeout() { Replicate replicate = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate.updateStatus(CONTRIBUTED, ReplicateStatusModifier.WORKER); - + ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, Collections.singletonList(replicate)); + replicatesRepository.save(replicatesList); replicatesService.setRevealTimeoutStatusIfNeeded(CHAIN_TASK_ID, replicate); - - verify(replicatesRepository).findByChainTaskId(CHAIN_TASK_ID); + final ReplicatesList result = replicatesRepository.findByChainTaskId(CHAIN_TASK_ID).orElseThrow(); + assertThat(result.getReplicates()).hasSize(1); + assertThat(result.getReplicates().get(0).getStatusUpdateList()).hasSize(3); + //verify(replicatesRepository).findByChainTaskId(CHAIN_TASK_ID); } // region canUpdateReplicateStatus diff --git a/src/test/java/com/iexec/core/task/TaskRepositoryTest.java b/src/test/java/com/iexec/core/task/TaskRepositoryTest.java index f18ad4f7..ced32126 100644 --- a/src/test/java/com/iexec/core/task/TaskRepositoryTest.java +++ b/src/test/java/com/iexec/core/task/TaskRepositoryTest.java @@ -32,7 +32,7 @@ class TaskRepositoryTest { private final long maxExecutionTime = 60000; @Container - private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.4")); + private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse(System.getProperty("mongo.image"))); @DynamicPropertySource static void registerProperties(DynamicPropertyRegistry registry) { diff --git a/src/test/java/com/iexec/core/task/TaskServiceRealRepositoryTest.java b/src/test/java/com/iexec/core/task/TaskServiceRealRepositoryTest.java index 369c1cc7..551a8f4c 100644 --- a/src/test/java/com/iexec/core/task/TaskServiceRealRepositoryTest.java +++ b/src/test/java/com/iexec/core/task/TaskServiceRealRepositoryTest.java @@ -52,7 +52,7 @@ class TaskServiceRealRepositoryTest { private final Date finalDeadline = new Date(); @Container - private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.4")); + private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse(System.getProperty("mongo.image"))); @DynamicPropertySource static void registerProperties(DynamicPropertyRegistry registry) { diff --git a/src/test/java/com/iexec/core/worker/WorkerServiceRealRepositoryTests.java b/src/test/java/com/iexec/core/worker/WorkerServiceRealRepositoryTests.java index 930f7593..821538b2 100644 --- a/src/test/java/com/iexec/core/worker/WorkerServiceRealRepositoryTests.java +++ b/src/test/java/com/iexec/core/worker/WorkerServiceRealRepositoryTests.java @@ -54,7 +54,7 @@ class WorkerServiceRealRepositoryTests { private static final String WALLET_ADDRESS = "0x1a69b2eb604db8eba185df03ea4f5288dcbbd248"; @Container - private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.4")); + private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse(System.getProperty("mongo.image"))); @DynamicPropertySource static void registerProperties(DynamicPropertyRegistry registry) { From 76e03688533210e953f72527dcad41f386d8fa62 Mon Sep 17 00:00:00 2001 From: Jeremy Bernard Date: Fri, 9 Feb 2024 09:36:17 +0100 Subject: [PATCH 2/3] Rework compute logs tests --- CHANGELOG.md | 1 - .../iexec/core/logs/TaskLogsServiceTests.java | 41 +++++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8563664f..4793d6b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,6 @@ All notable changes to this project will be documented in this file. - Use `@DataMongoTest` and `@Testcontainers` annotations in replicates and compute logs tests. (#662) - ## [[8.3.0]](https://github.com/iExecBlockchainComputing/iexec-core/releases/tag/v8.3.0) 2024-01-11 ### New Features diff --git a/src/test/java/com/iexec/core/logs/TaskLogsServiceTests.java b/src/test/java/com/iexec/core/logs/TaskLogsServiceTests.java index c6b92737..da44241f 100644 --- a/src/test/java/com/iexec/core/logs/TaskLogsServiceTests.java +++ b/src/test/java/com/iexec/core/logs/TaskLogsServiceTests.java @@ -43,6 +43,7 @@ class TaskLogsServiceTests { private static final String STDOUT = "This is an stdout string"; private static final String STDERR = "This is an stderr string"; + private static final ComputeLogs COMPUTE_LOGS = new ComputeLogs(WORKER_ADDRESS, STDOUT, STDERR); @Container private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse(System.getProperty("mongo.image"))); @@ -71,8 +72,7 @@ void init() { //region addComputeLogs @Test void shouldAddComputeLogs() { - final ComputeLogs computeLogs = new ComputeLogs(WORKER_ADDRESS, STDOUT, STDERR); - taskLogsService.addComputeLogs(CHAIN_TASK_ID, computeLogs); + taskLogsService.addComputeLogs(CHAIN_TASK_ID, COMPUTE_LOGS); assertThat(taskLogsRepository.count()).isOne(); TaskLogs capturedEvent = taskLogsRepository.findOneByChainTaskId(CHAIN_TASK_ID).orElseThrow(); assertThat(capturedEvent.getComputeLogsList().get(0).getStdout()).isEqualTo(STDOUT); @@ -88,14 +88,40 @@ void shouldNotAddComputeLogsSinceNull() { @Test void shouldNotAddComputeLogsSinceLogsAlreadyKnown() { - final ComputeLogs computeLogs = new ComputeLogs(WORKER_ADDRESS, STDOUT, STDERR); final TaskLogs taskLogs = TaskLogs.builder() .chainTaskId(CHAIN_TASK_ID) - .computeLogsList(Collections.singletonList(computeLogs)) + .computeLogsList(Collections.singletonList(COMPUTE_LOGS)) .build(); taskLogsRepository.save(taskLogs); + assertThat(taskLogs.containsWalletAddress(WORKER_ADDRESS)).isTrue(); + taskLogsService.addComputeLogs(CHAIN_TASK_ID, COMPUTE_LOGS); + assertThat(taskLogsRepository.count()).isOne(); + assertThat(taskLogsRepository.findOneByChainTaskId(CHAIN_TASK_ID)).contains(taskLogs); + } + //endregion - taskLogsService.addComputeLogs(CHAIN_TASK_ID, computeLogs); + //region delete + @Test + void shouldDeleteKnownTask() { + final TaskLogs taskLogs = TaskLogs.builder() + .chainTaskId(CHAIN_TASK_ID) + .computeLogsList(List.of(COMPUTE_LOGS)) + .build(); + taskLogsRepository.save(taskLogs); + assertThat(taskLogsRepository.count()).isOne(); + taskLogsService.delete(List.of(CHAIN_TASK_ID)); + assertThat(taskLogsRepository.count()).isZero(); + } + + @Test + void shouldNotDeleteUnknownTask() { + final TaskLogs taskLogs = TaskLogs.builder() + .chainTaskId(CHAIN_TASK_ID) + .computeLogsList(List.of(COMPUTE_LOGS)) + .build(); + taskLogsRepository.save(taskLogs); + assertThat(taskLogsRepository.count()).isOne(); + taskLogsService.delete(List.of("0x00")); assertThat(taskLogsRepository.count()).isOne(); } //endregion @@ -103,10 +129,9 @@ void shouldNotAddComputeLogsSinceLogsAlreadyKnown() { //region getComputeLogs @Test void shouldGetComputeLogs() { - ComputeLogs computeLogs = new ComputeLogs(WORKER_ADDRESS, STDOUT, STDERR); - TaskLogs taskLogs = TaskLogs.builder() + final TaskLogs taskLogs = TaskLogs.builder() .chainTaskId(CHAIN_TASK_ID) - .computeLogsList(List.of(computeLogs)) + .computeLogsList(List.of(COMPUTE_LOGS)) .build(); taskLogsRepository.save(taskLogs); Optional optional = taskLogsService.getComputeLogs(CHAIN_TASK_ID, WORKER_ADDRESS); From 830959419f016dee2e81197ed227b544c18d38b1 Mon Sep 17 00:00:00 2001 From: Jeremy Bernard Date: Fri, 9 Feb 2024 12:02:40 +0100 Subject: [PATCH 3/3] Rework replicates tests --- .../core/replicate/ReplicateServiceTests.java | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/test/java/com/iexec/core/replicate/ReplicateServiceTests.java b/src/test/java/com/iexec/core/replicate/ReplicateServiceTests.java index 12cabf82..a65dbd24 100644 --- a/src/test/java/com/iexec/core/replicate/ReplicateServiceTests.java +++ b/src/test/java/com/iexec/core/replicate/ReplicateServiceTests.java @@ -112,7 +112,8 @@ void shouldCreateNewReplicate() { list.add(replicate1); list.add(replicate2); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, list); - replicatesRepository.save(replicatesList); + final ReplicatesList savedReplicatesList = replicatesRepository.save(replicatesList); + assertThat(savedReplicatesList.getReplicates()).hasSize(2); replicatesService.addNewReplicate(replicatesList, WALLET_WORKER_3); final ReplicatesList result = replicatesRepository.findByChainTaskId(CHAIN_TASK_ID).orElseThrow(); assertThat(result.getReplicates()).hasSize(3); @@ -130,10 +131,10 @@ void shouldNotCreateNewReplicate() { list.add(replicate1); list.add(replicate2); ReplicatesList replicatesList = new ReplicatesList(CHAIN_TASK_ID, list); - replicatesRepository.save(replicatesList); + final ReplicatesList savedReplicatesList = replicatesRepository.save(replicatesList); + assertThat(savedReplicatesList.getReplicates()).hasSize(2); replicatesService.addNewReplicate(replicatesList, WALLET_WORKER_1); - replicatesService.addNewReplicate(replicatesList, WALLET_WORKER_2); assertThat(replicatesService.getReplicates(CHAIN_TASK_ID)).hasSize(2); } @@ -165,7 +166,9 @@ void shouldGetReplicates() { replicatesRepository.save(replicatesList); assertThat(replicatesService.getReplicates(CHAIN_TASK_ID)).isNotNull(); - assertThat(replicatesService.getReplicates(CHAIN_TASK_ID)).hasSize(1); + assertThat(replicatesService.getReplicates(CHAIN_TASK_ID)) + .hasSize(1) + .contains(replicate1); } @Test @@ -248,10 +251,8 @@ void shouldGetCorrectNbReplicatesWithMultipleStatus() { int shouldBe3 = replicatesService.getNbReplicatesWithCurrentStatus(CHAIN_TASK_ID, STARTING, COMPUTED); assertThat(shouldBe3).isEqualTo(3); - int shouldBe4 = replicatesService.getNbReplicatesWithCurrentStatus(CHAIN_TASK_ID, STARTING, COMPUTED, - CONTRIBUTED); + int shouldBe4 = replicatesService.getNbReplicatesWithCurrentStatus(CHAIN_TASK_ID, STARTING, COMPUTED, CONTRIBUTED); assertThat(shouldBe4).isEqualTo(4); - } @Test @@ -276,7 +277,7 @@ void shouldGetCorrectNbReplicatesWithOneLastRelevantStatus() { } @Test - void shouldGetCorrectNbReplicatesWithMultipleLastReleveantStatus() { + void shouldGetCorrectNbReplicatesWithMultipleLastRelevantStatus() { Replicate replicate1 = new Replicate(WALLET_WORKER_1, CHAIN_TASK_ID); replicate1.updateStatus(STARTING, ReplicateStatusModifier.WORKER); replicate1.updateStatus(COMPUTED, ReplicateStatusModifier.WORKER); @@ -298,13 +299,11 @@ void shouldGetCorrectNbReplicatesWithMultipleLastReleveantStatus() { int shouldBe2 = replicatesService.getNbReplicatesWithLastRelevantStatus(CHAIN_TASK_ID, COMPUTED, CONTRIBUTED); assertThat(shouldBe2).isEqualTo(2); - int shouldBe3 = replicatesService.getNbReplicatesWithLastRelevantStatus(CHAIN_TASK_ID, STARTING, COMPUTED); - assertThat(shouldBe3).isEqualTo(4); - - int shouldBe4 = replicatesService.getNbReplicatesWithLastRelevantStatus(CHAIN_TASK_ID, STARTING, COMPUTED, - CONTRIBUTED); - assertThat(shouldBe4).isEqualTo(5); + int shouldBe4 = replicatesService.getNbReplicatesWithLastRelevantStatus(CHAIN_TASK_ID, STARTING, COMPUTED); + assertThat(shouldBe4).isEqualTo(4); + int shouldBe5 = replicatesService.getNbReplicatesWithLastRelevantStatus(CHAIN_TASK_ID, STARTING, COMPUTED, CONTRIBUTED); + assertThat(shouldBe5).isEqualTo(5); } @Test @@ -348,8 +347,7 @@ void shouldGetCorrectNbReplicatesContainingMultipleStatus() { int shouldBe4 = replicatesService.getNbReplicatesContainingStatus(CHAIN_TASK_ID, STARTING, COMPUTED); assertThat(shouldBe4).isEqualTo(4); - int shouldBe0 = replicatesService.getNbReplicatesContainingStatus(CHAIN_TASK_ID, COMPLETED, FAILED, - RESULT_UPLOADING); + int shouldBe0 = replicatesService.getNbReplicatesContainingStatus(CHAIN_TASK_ID, COMPLETED, FAILED, RESULT_UPLOADING); assertThat(shouldBe0).isZero(); } @@ -524,6 +522,9 @@ void shouldNotUpdateReplicateStatusSinceNoMatchingReplicate() { .build(); replicatesService.updateReplicateStatus(CHAIN_TASK_ID, WALLET_WORKER_2, statusUpdate); + assertThat(replicatesService.getReplicate(CHAIN_TASK_ID, WALLET_WORKER_2)).isEmpty(); + assertThat(replicatesService.getReplicate(CHAIN_TASK_ID, WALLET_WORKER_1)).map(Replicate::getCurrentStatus) + .contains(CONTRIBUTED); verifyNoInteractions(applicationEventPublisher); } @@ -540,6 +541,8 @@ void shouldNotUpdateReplicateStatusSinceInvalidWorkflowTransition() { .build(); replicatesService.updateReplicateStatus(CHAIN_TASK_ID, WALLET_WORKER_1, statusUpdate); + assertThat(replicatesService.getReplicate(CHAIN_TASK_ID, WALLET_WORKER_1)).map(Replicate::getCurrentStatus) + .contains(CONTRIBUTED); verifyNoInteractions(applicationEventPublisher); } @@ -559,7 +562,8 @@ void shouldNotUpdateReplicateStatusSinceWrongOnChainStatus() { .build(); replicatesService.updateReplicateStatus(CHAIN_TASK_ID, WALLET_WORKER_1, statusUpdate); - assertThat(replicatesRepository.findByChainTaskId(CHAIN_TASK_ID)).contains(replicatesList); + assertThat(replicatesService.getReplicate(CHAIN_TASK_ID, WALLET_WORKER_1)).map(Replicate::getCurrentStatus) + .contains(CONTRIBUTING); verifyNoInteractions(applicationEventPublisher); } @@ -582,7 +586,8 @@ void shouldNotUpdateReplicateStatusToContributedSinceGetContributionFailed() { .build(); replicatesService.updateReplicateStatus(CHAIN_TASK_ID, WALLET_WORKER_1, statusUpdate); - + assertThat(replicatesService.getReplicate(CHAIN_TASK_ID, WALLET_WORKER_1)).map(Replicate::getCurrentStatus) + .contains(CONTRIBUTING); verifyNoInteractions(applicationEventPublisher); } @@ -609,7 +614,8 @@ void shouldNotUpdateReplicateStatusToContributedSinceCannotGetWorkerWeight() { .build(); replicatesService.updateReplicateStatus(CHAIN_TASK_ID, WALLET_WORKER_1, statusUpdate); - + assertThat(replicatesService.getReplicate(CHAIN_TASK_ID, WALLET_WORKER_1)).map(Replicate::getCurrentStatus) + .contains(CONTRIBUTING); verifyNoInteractions(applicationEventPublisher); } @@ -783,11 +789,9 @@ void shouldGetReplicateWithResultUploadedStatus() { Arrays.asList(replicate1, replicate2)); replicatesRepository.save(replicatesList); - assertThat(replicatesService - .getReplicateWithResultUploadedStatus(CHAIN_TASK_ID) - .get() - .getWalletAddress()) - .isEqualTo(WALLET_WORKER_2); + assertThat(replicatesService.getReplicateWithResultUploadedStatus(CHAIN_TASK_ID) + .map(Replicate::getWalletAddress)) + .contains(WALLET_WORKER_2); } // endregion @@ -904,7 +908,6 @@ void shouldSetTriggerReplicateUpdateIfRevealTimeout() { final ReplicatesList result = replicatesRepository.findByChainTaskId(CHAIN_TASK_ID).orElseThrow(); assertThat(result.getReplicates()).hasSize(1); assertThat(result.getReplicates().get(0).getStatusUpdateList()).hasSize(3); - //verify(replicatesRepository).findByChainTaskId(CHAIN_TASK_ID); } // region canUpdateReplicateStatus