You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: explain how shared signal writes are confirmed in tests
A write to a shared signal is applied optimistically and is visible
through peek() right away, but the SignalOperation it returns completes
only after the queued confirmation task has been run by
runPendingSignalsTasks(). Document that, including why blocking on
operation.result().get(...) before draining the queue always times out.
Refs vaadin/browserless-test#202
[methodname]`runPendingSignalsTasks()` waits up to 100 milliseconds for the first pending task to arrive and then drains the queue; the [methodname]`runPendingSignalsTasks(long, TimeUnit)` overload accepts a custom wait time. The method returns [code]`true` if any tasks were processed. If the calling thread holds the window's [classname]`VaadinSession` lock, the lock is temporarily released during the wait so that background threads can enqueue tasks.
206
206
207
+
The same call also confirms shared-signal writes made through the window: the [classname]`SignalOperation` returned by a write completes only after the queue has been drained. Write the signal while the window's thread-locals are active -- either from within a DSL call or after [methodname]`window.activate()` -- so that the confirmation is dispatched through the window's UI. See <<testing-signals#shared-signal-writes, Confirming a Write>> for details.
Copy file name to clipboardExpand all lines: articles/flow/testing/browserless/testing-signals.adoc
+79Lines changed: 79 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,6 +196,85 @@ The same method is available on the JUnit extension as [methodname]`ext.runPendi
196
196
As a result, a change that an observer should react to needs the same treatment as any other off-thread mutation. After triggering the change, call [methodname]`runPendingSignalsTasks()` before asserting on the observing side. A change made and observed on the same test thread -- such as mutating a shared signal and asserting a binding on the same view -- still propagates synchronously and needs no flush. For tests that drive several sessions or windows observing one shared signal, see <<multi-user#signals, Signals in Multi-User Tests>>.
197
197
198
198
199
+
[#shared-signal-writes]
200
+
==== Confirming a Write
201
+
202
+
A write to a shared signal returns a <<{articles}/flow/ui-state/transactions#operation-results, [classname]`SignalOperation`>> that completes once the underlying signal tree has confirmed the command. The write itself is applied optimistically, so the new value is visible through [methodname]`peek()` as soon as the call returns -- while the confirmation travels through the same queue as the effects.
203
+
204
+
This view inserts a ticket into a [classname]`SharedListSignal` and updates a status label when the write is confirmed. The result callback is delivered in the context that started the operation, so it can touch components directly:
205
+
206
+
[source,java]
207
+
----
208
+
@Route("tickets")
209
+
public class TicketView extends Div {
210
+
final SharedListSignal<String> tickets =
211
+
new SharedListSignal<>(String.class);
212
+
final TextField title = new TextField("Title");
213
+
final Span status = new Span();
214
+
final NativeButton submit = new NativeButton("Submit");
The entry is in the list right after the click, but the status label still reads [code]`Saving...` -- the callback runs only once the queued confirmation task has been executed:
A test that gets hold of the operation itself -- because the code under test returns it, as [methodname]`submitTicket()` does -- can assert on the confirmation directly instead of going through the UI:
Don't block on the operation before draining the queue. A call such as [code]`operation.result().get(5, TimeUnit.SECONDS)` always times out, because the confirmation task can only run on the very thread that's blocked waiting for it. A timeout there means the queue hasn't been drained -- not that the write was lost.
276
+
277
+
199
278
.Quick Start and Collaborative Scenarios
200
279
[TIP]
201
280
For a brief introduction to testing signal-based views, see <<getting-started#signals, Testing Signal-Based UIs>>. For collaborative features where several windows or users observe the same shared signal, see <<multi-user#signals, Signals in Multi-User Tests>>.
0 commit comments