|
3 | 3 | // The .NET Foundation licenses this file to you under the MIT license. |
4 | 4 | // See the LICENSE file in the project root for full license information. |
5 | 5 |
|
| 6 | +using System.Collections.Concurrent; |
6 | 7 | using ReactiveUI.Tests.Utilities.Schedulers; |
7 | 8 | using TUnit.Core.Executors; |
8 | 9 |
|
@@ -270,4 +271,117 @@ public async Task UnhandledInteractionsShouldCauseException() |
270 | 271 | await Assert.That(ex.Input).IsEqualTo("bar"); |
271 | 272 | } |
272 | 273 | } |
| 274 | + |
| 275 | + /// |
| 276 | + /// A task-based handler registered while a is installed resumes on that |
| 277 | + /// captured context rather than on a thread-pool thread, so UI handlers run on the UI thread (see issue #4393). |
| 278 | + /// |
| 279 | + /// A representing the asynchronous operation. |
| 280 | + [Test] |
| 281 | + public async Task TaskHandlerResumesOnCapturedSynchronizationContext() |
| 282 | + { |
| 283 | + using var uiContext = new SingleThreadedSynchronizationContext(); |
| 284 | + var observedContext = new TaskCompletionSource<SynchronizationContext?>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 285 | + var completed = new TaskCompletionSource<RxVoid>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 286 | + |
| 287 | + uiContext.Post( |
| 288 | + static state => |
| 289 | + { |
| 290 | + var (observed, done) = ((TaskCompletionSource<SynchronizationContext?>, TaskCompletionSource<RxVoid>))state!; |
| 291 | + var interaction = new Interaction<RxVoid, RxVoid>(); |
| 292 | + _ = interaction.RegisterHandler(async context => |
| 293 | + { |
| 294 | + _ = observed.TrySetResult(SynchronizationContext.Current); |
| 295 | + context.SetOutput(RxVoid.Default); |
| 296 | + await Task.CompletedTask; |
| 297 | + }); |
| 298 | + |
| 299 | + _ = interaction.Handle(RxVoid.Default).Subscribe( |
| 300 | + _ => done.TrySetResult(RxVoid.Default), |
| 301 | + done.SetException); |
| 302 | + }, |
| 303 | + (observedContext, completed)); |
| 304 | + |
| 305 | + var handlerContext = await observedContext.Task; |
| 306 | + _ = await completed.Task; |
| 307 | + |
| 308 | + await Assert.That(handlerContext).IsSameReferenceAs(uiContext); |
| 309 | + } |
| 310 | + |
| 311 | + /// |
| 312 | + /// A task-based handler yields before running, so it is not invoked inside the subscription that triggers the |
| 313 | + /// interaction (see issue #4351); the handler only runs once the triggering call has unwound. |
| 314 | + /// |
| 315 | + /// A representing the asynchronous operation. |
| 316 | + [Test] |
| 317 | + public async Task TaskHandlerDoesNotRunInsideTriggeringSubscription() |
| 318 | + { |
| 319 | + const string afterSubscribeMarker = "after-subscribe"; |
| 320 | + const string handlerMarker = "handler"; |
| 321 | + const int expectedStepCount = 2; |
| 322 | + |
| 323 | + using var uiContext = new SingleThreadedSynchronizationContext(); |
| 324 | + var order = new List<string>(); |
| 325 | + var completed = new TaskCompletionSource<IReadOnlyList<string>>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 326 | + |
| 327 | + uiContext.Post( |
| 328 | + static state => |
| 329 | + { |
| 330 | + var (steps, done) = ((List<string>, TaskCompletionSource<IReadOnlyList<string>>))state!; |
| 331 | + var interaction = new Interaction<RxVoid, string>(); |
| 332 | + _ = interaction.RegisterHandler(async context => |
| 333 | + { |
| 334 | + steps.Add(handlerMarker); |
| 335 | + context.SetOutput(ResultOutput); |
| 336 | + await Task.CompletedTask; |
| 337 | + }); |
| 338 | + |
| 339 | + _ = interaction.Handle(RxVoid.Default).Subscribe( |
| 340 | + _ => done.TrySetResult(steps), |
| 341 | + done.SetException); |
| 342 | + |
| 343 | + // Recorded before the yielded handler continuation is pumped, so it must precede the handler marker. |
| 344 | + steps.Add(afterSubscribeMarker); |
| 345 | + }, |
| 346 | + (order, completed)); |
| 347 | + |
| 348 | + var sequence = await completed.Task; |
| 349 | + |
| 350 | + using (Assert.Multiple()) |
| 351 | + { |
| 352 | + await Assert.That(sequence).Count().IsEqualTo(expectedStepCount); |
| 353 | + await Assert.That(sequence[0]).IsEqualTo(afterSubscribeMarker); |
| 354 | + await Assert.That(sequence[1]).IsEqualTo(handlerMarker); |
| 355 | + } |
| 356 | + } |
| 357 | + |
| 358 | + /// A minimal single-threaded backed by one pumped worker thread. |
| 359 | + private sealed class SingleThreadedSynchronizationContext : SynchronizationContext, IDisposable |
| 360 | + { |
| 361 | + /// The queue of work items pumped on the worker thread in order. |
| 362 | + private readonly BlockingCollection<Action> _queue = new(); |
| 363 | + |
| 364 | + /// Initializes a new instance of the class. |
| 365 | + public SingleThreadedSynchronizationContext() |
| 366 | + { |
| 367 | + var thread = new Thread(Run) { IsBackground = true, Name = "interaction-ui" }; |
| 368 | + thread.Start(); |
| 369 | + } |
| 370 | + |
| 371 | + /// |
| 372 | + public override void Post(SendOrPostCallback d, object? state) => _queue.Add(() => d(state)); |
| 373 | + |
| 374 | + /// |
| 375 | + public void Dispose() => _queue.CompleteAdding(); |
| 376 | + |
| 377 | + /// Pumps queued work on the worker thread with this context installed as current. |
| 378 | + private void Run() |
| 379 | + { |
| 380 | + SynchronizationContext.SetSynchronizationContext(this); |
| 381 | + foreach (var work in _queue.GetConsumingEnumerable()) |
| 382 | + { |
| 383 | + work(); |
| 384 | + } |
| 385 | + } |
| 386 | + } |
273 | 387 | } |
0 commit comments