You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by da...@apache.org on 2023/08/25 12:18:15 UTC
[causeway] 01/03: CAUSEWAY-3557: adds in FakeScheduler
This is an automated email from the ASF dual-hosted git repository.
danhaywood pushed a commit to branch CAUSEWAY-3557
in repository https://gitbox.apache.org/repos/asf/causeway.git
commit bf5ee961b79aaf82dd1452b49910d6f91d341035
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Fri Aug 25 11:45:48 2023 +0100
CAUSEWAY-3557: adds in FakeScheduler
---
.../commandlog/applib/dom/BackgroundService.java | 1 +
.../applib/fakescheduler/FakeScheduler.java | 121 +++++++++++++++++++++
2 files changed, 122 insertions(+)
diff --git a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/dom/BackgroundService.java b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/dom/BackgroundService.java
index 2496f2a9b7..c9bcfefed7 100644
--- a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/dom/BackgroundService.java
+++ b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/dom/BackgroundService.java
@@ -55,6 +55,7 @@ import lombok.val;
* </p>
*
* @see WrapperFactory
+ * @see org.apache.causeway.extensions.commandlog.applib.fakescheduler.FakeScheduler
* @since 2.0 {@index}
*/
@Service
diff --git a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/fakescheduler/FakeScheduler.java b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/fakescheduler/FakeScheduler.java
new file mode 100644
index 0000000000..0724f6a80c
--- /dev/null
+++ b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/fakescheduler/FakeScheduler.java
@@ -0,0 +1,121 @@
+package org.apache.causeway.extensions.commandlog.applib.fakescheduler;
+
+import java.util.List;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import javax.inject.Inject;
+
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
+
+import org.apache.causeway.applib.services.clock.ClockService;
+import org.apache.causeway.applib.services.command.Command;
+import org.apache.causeway.applib.services.command.CommandExecutorService;
+import org.apache.causeway.applib.services.iactnlayer.InteractionService;
+import org.apache.causeway.applib.services.xactn.TransactionService;
+import org.apache.causeway.extensions.commandlog.applib.dom.CommandLogEntry;
+import org.apache.causeway.extensions.commandlog.applib.dom.CommandLogEntryRepository;
+import org.apache.causeway.schema.cmd.v2.CommandDto;
+
+import lombok.val;
+
+/**
+ * Intended to support integration testing which uses the
+ * {@link org.apache.causeway.extensions.commandlog.applib.dom.BackgroundService} to create background
+ * {@link CommandLogEntry command}s, that the integration test then needs to be executed.
+ *
+ * @see org.apache.causeway.extensions.commandlog.applib.dom.BackgroundService
+ * @since 2.0 {@index}
+ */
+@Service
+public class FakeScheduler {
+
+ public enum NoCommandsPolicy {
+ /**
+ * If no commands are found, simply return
+ */
+ RELAXED,
+ /**
+ * If no commands are found, then throw an exception. The idea here is that this is a utility to support integration testing; in some tests we might want to fail if no background commands were found.
+ */
+ STRICT;
+ }
+
+ /**
+ *
+ * @param waitForMillis how long to wait for the background commands to execute. The commands themselves run in a background thread to this.
+ * @param noCommandsPolicy what to do if there are no commands found to be executed.
+ * @return the number of commands still to be processed. This will generally be 0, but could be non-zero if not enough time was provided to wait.
+ * @throws InterruptedException
+ */
+ public int runBackgroundCommands(
+ final Integer waitForMillis,
+ final NoCommandsPolicy noCommandsPolicy) throws InterruptedException {
+
+ // we obtain the list of Commands first; we use their CommandDto as it is serializable across transactions
+ List<CommandDto> commandDtos = commandLogEntryRepository.findBackgroundAndNotYetStarted()
+ .stream()
+ .map(CommandLogEntry::getCommandDto)
+ .collect(Collectors.toList());
+
+ if(commandDtos.isEmpty()) {
+ switch (noCommandsPolicy) {
+ case STRICT:
+ throw new IllegalStateException("There are no background commands to be started");
+ case RELAXED:
+ default:
+ return 0;
+ }
+ }
+
+ long startAt = nowInMillis();
+
+ transactionService.flushTransaction();
+
+ Thread thread = new Thread(() -> execute(commandDtos));
+ thread.start();
+
+ boolean hitTimeout = false;
+ List<CommandLogEntry> commandsToProcess;
+ while(!(commandsToProcess = commandLogEntryRepository.findBackgroundAndNotYetStarted()).isEmpty() && !hitTimeout) {
+ Thread.sleep(100L);
+ hitTimeout = nowInMillis() > startAt + waitForMillis;
+ }
+
+ return commandsToProcess.size();
+ }
+
+ private long nowInMillis() {
+ return clockService.getClock().nowAsEpochMilli();
+ }
+
+ private void execute(List<CommandDto> commandDtos) {
+
+ for (val commandDto : commandDtos) {
+ interactionService.runAnonymous(() -> {
+ transactionService.runTransactional(Propagation.REQUIRED, () -> {
+ // look up the CommandLogEntry again because we are within a new transaction.
+ val commandLogEntryIfAny = commandLogEntryRepository.findByInteractionId(UUID.fromString(commandDto.getInteractionId()));
+
+ // TODO: this is a hack, really should be in CommandExecutorService
+ val command = interactionService.currentInteraction().map(x -> x.getCommand()).orElseThrow(() -> new IllegalStateException("Cannot obtain current interaction/command"));
+ command.updater().setPublishingPhase(Command.CommandPublishingPhase.READY);
+
+ commandLogEntryIfAny.ifPresent(commandLogEntry ->
+ commandExecutorService.executeCommand(
+ CommandExecutorService.InteractionContextPolicy.NO_SWITCH, commandDto));
+ })
+ .ifFailureFail();
+ }
+ );
+ }
+ }
+
+ @Inject CommandLogEntryRepository<CommandLogEntry> commandLogEntryRepository;
+ @Inject CommandExecutorService commandExecutorService;
+ @Inject TransactionService transactionService;
+ @Inject InteractionService interactionService;
+ @Inject ClockService clockService;
+
+}