diff --git a/playlet-lib/src/components/JobSystem/tests/SimpleTestJob.bs b/playlet-lib/src/components/JobSystem/tests/SimpleTestJob.bs index 6c4913fe..58a14df9 100644 --- a/playlet-lib/src/components/JobSystem/tests/SimpleTestJob.bs +++ b/playlet-lib/src/components/JobSystem/tests/SimpleTestJob.bs @@ -5,29 +5,23 @@ import "pkg:/source/utils/Logging.bs" function ExecuteJob() as void input = JobGetInput() - if JobIsCancelled() - JobCancel() - LogInfo("Job cancelled before execution") - return - end if + ' Register before reading: a cancel between the read and the registration would reach no observer + cancellation = JobGetCancellation() + messagePort = CreateObject("roMessagePort") + CancellationUtils.ObserveCancellationScoped(cancellation, messagePort) + cancelled = CancellationUtils.IsCancelled(cancellation) sleepTimeMs = ValidInt(input.sleep) - if sleepTimeMs > 0 - callback = JobGetCallbackNode() - if callback <> invalid - messagePort = CreateObject("roMessagePort") - cancellation = CancellationUtils.CreateCancellation(callback, "cancel", true) - CancellationUtils.ObserveCancellationScoped(cancellation, messagePort) - message = wait(sleepTimeMs, messagePort) - CancellationUtils.UnobserveCancellationScoped(cancellation) - if CancellationUtils.IsCancellationEvent(cancellation, message) - LogInfo("Job cancelled during execution") - JobCancel() - return - end if - else - sleep(sleepTimeMs) - end if + if not cancelled and sleepTimeMs > 0 + message = wait(sleepTimeMs, messagePort) + cancelled = CancellationUtils.IsCancellationEvent(cancellation, message) + end if + CancellationUtils.UnobserveCancellationScoped(cancellation) + + if cancelled + LogInfo("Simulated job cancellation for testing purposes") + JobCancel() + return end if if not input.success