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.
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.
-
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.voidUnregisters 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()Unregisters this test environment if it was registered.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.
-