Class TestSignalEnvironment
SignalEnvironment that records submitted tasks instead of
executing them asynchronously. This allows unit tests to deterministically
drive and observe Signal processing by explicitly flushing the task queue.
How it works:
getEffectDispatcher()returns an executor that enqueues tasks into an internal queue.getResultNotifier()returnsnullso that result notifications fall through to the next environment or run immediately.- Tests call
runPendingTasks(long, TimeUnit)to dequeue and run all pending tasks on the calling thread. - If the current thread holds a
VaadinSessionlock, the lock is temporarily released so that background threads have a chance of locking the session.
Shared signals (SharedValueSignal, SharedListSignal and
friends) back onto a
com.vaadin.flow.signals.shared.impl.LocalAsynchronousSignalTree,
which confirms submitted commands through the effect dispatcher. Their writes
are therefore visible immediately, while the SignalOperation returned
by the write is only completed once the queued confirmation task is executed
by runPendingTasks(long, TimeUnit).
Usage:
TestSignalEnvironment env = TestSignalEnvironment.register();
try {
// trigger signals here
env.waitForTasksCompletion(100, TimeUnit.MILLISECONDS);
} finally {
env.unregister();
}
For internal use only. May be renamed or removed in a future release.
- Since:
- 1.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ExecutorGets an executor to use for running the callback of an effect.protected ExecutorGets an executor to use for asynchronously notifying about operation results.protected booleanisActive()Checks whether this environment is active on the current thread.static TestSignalEnvironmentregister()Registers this test environment as activeSignalEnvironmentand returns the created instance.booleanrunPendingTasks(long maxWaitTime, TimeUnit unit) Executes pending tasks from the queue, continuously polling for new tasks until the timeout expires with no new task arriving.voidRuns the tasks that are still pending on the calling thread, and then unregisters this test environment if it was registered.Methods inherited from class com.vaadin.flow.signals.SignalEnvironment
getCurrentResultNotifier, getDefaultEffectDispatcher, register, registerFirst
-
Constructor Details
-
TestSignalEnvironment
protected TestSignalEnvironment()
-
-
Method Details
-
register
Registers this test environment as activeSignalEnvironmentand returns the created instance.Use together with
unregister()in a try/finally block to ensure proper cleanup.- Returns:
- a new registered
TestSignalEnvironmentinstance
-
unregister
public void unregister()Runs the tasks that are still pending on the calling thread, and then unregisters this test environment if it was registered.A shared signal often outlives the test that wrote to it, for example when it is held by an application-scoped bean or a static field. If the confirmation of such a write were discarded with the queue, the signal would keep the write unconfirmed for good, and every later conditional update of it, such as
SharedValueSignal.update(...), would be rejected and retried forever. Running the pending tasks confirms those writes. Pending effects of UIs that are still open run as well, which is why teardown also runs the pending tasks before it closes the UIs. The environment stays registered while the tasks run, so tasks they submit are queued and run here too, on the calling thread, up to a limit of 10000 tasks; tasks beyond it are discarded with a warning. A task that fails is logged and skipped, so that the remaining tasks still run and teardown completes. Only aVirtualMachineError, such as running out of memory, is passed on, after the environment has been unregistered.The tasks run one after another on the calling thread, so a pending task must not block waiting for another one. For example, an effect that calls
signal.update(...).result().join()waits for a confirmation queued behind it and hangs teardown, which test timeouts do not cover.This method is idempotent and safe to call multiple times. If a cleanup action was provided by
SignalEnvironment.register(SignalEnvironment), it will be invoked. -
isActive
protected boolean isActive()Description copied from class:SignalEnvironmentChecks whether this environment is active on the current thread. No other instance methods will be run while the environment is not active.- Specified by:
isActivein classSignalEnvironment- Returns:
trueif this environment is active,falseif it's inactive
-
getResultNotifier
Description copied from class:SignalEnvironmentGets an executor to use for asynchronously notifying about operation results. This method is run when an operation is submitted and the returned executor is used to notify of the results when the operation has been fully processed. The executor can thus be used to make sure the notification is delivered in the same context as where the operation was initiated. The notification is delivered immediately if no notifier is provided. It is recommended that the executor preserves ordering of submitted tasks within the same context so that notifications are delivered in the order that operations have been applied.- Specified by:
getResultNotifierin classSignalEnvironment- Returns:
- an executor to use for notifying about operation results, or
nullto notify about results immediately
-
getEffectDispatcher
Description copied from class:SignalEnvironmentGets an executor to use for running the callback of an effect. The executor can be used to make effect callback invocations asynchronous rather than blocking the thread that applied the change until all affected effects have been processed. This executor does not need to preserve ordering since the effect callback always uses the latest signal values without concern for in which order values have been changed.- Specified by:
getEffectDispatcherin classSignalEnvironment- Returns:
- the executor to use for invoking affected effects, or
nullto invoke the callbacks immediately
-
runPendingTasks
Executes pending tasks from the queue, continuously polling for new tasks until the timeout expires with no new task arriving.If a
VaadinSessionlock is held by the current thread, it is temporarily released while polling for tasks, allowing background threads to acquire the lock and enqueue tasks. The lock is reacquired before running each task and released again before the next poll.If the current thread is interrupted while waiting for tasks, the method restores the interrupt status and fails with an
AssertionError.- Parameters:
maxWaitTime- the maximum time to wait for the next task to arrive in the given time unit. If <= 0, returns immediately if no tasks are available.unit- the time unit of the timeout value- Returns:
trueif any pending Signals tasks were processed.
-