Class TestSignalEnvironment

java.lang.Object
com.vaadin.flow.signals.SignalEnvironment
com.vaadin.browserless.TestSignalEnvironment

public class TestSignalEnvironment extends SignalEnvironment
Test-only 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() returns null so 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 VaadinSession lock, 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 Details

    • TestSignalEnvironment

      protected TestSignalEnvironment()
  • Method Details

    • register

      public static TestSignalEnvironment register()
      Registers this test environment as active SignalEnvironment and returns the created instance.

      Use together with unregister() in a try/finally block to ensure proper cleanup.

      Returns:
      a new registered TestSignalEnvironment instance
    • 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: SignalEnvironment
      Checks whether this environment is active on the current thread. No other instance methods will be run while the environment is not active.
      Specified by:
      isActive in class SignalEnvironment
      Returns:
      true if this environment is active, false if it's inactive
    • getResultNotifier

      protected Executor getResultNotifier()
      Description copied from class: SignalEnvironment
      Gets 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:
      getResultNotifier in class SignalEnvironment
      Returns:
      an executor to use for notifying about operation results, or null to notify about results immediately
    • getEffectDispatcher

      protected Executor getEffectDispatcher()
      Description copied from class: SignalEnvironment
      Gets 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:
      getEffectDispatcher in class SignalEnvironment
      Returns:
      the executor to use for invoking affected effects, or null to invoke the callbacks immediately
    • runPendingTasks

      public boolean runPendingTasks(long maxWaitTime, TimeUnit unit)
      Executes pending tasks from the queue, continuously polling for new tasks until the timeout expires with no new task arriving.

      If a VaadinSession lock 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:
      true if any pending Signals tasks were processed.