ThreadingTests.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Avalonia.Threading;
  5. namespace Avalonia.Headless.UnitTests;
  6. public class ThreadingTests
  7. {
  8. #if NUNIT
  9. [AvaloniaTest, Timeout(10000)]
  10. #elif XUNIT
  11. [AvaloniaFact(Timeout = 10000)]
  12. #endif
  13. public void Should_Be_On_Dispatcher_Thread()
  14. {
  15. Dispatcher.UIThread.VerifyAccess();
  16. }
  17. #if NUNIT
  18. [AvaloniaTest, Ignore("This test should always fail, enable to test if it fails")]
  19. #elif XUNIT
  20. [AvaloniaFact(Skip = "This test should always fail, enable to test if it fails")]
  21. #endif
  22. public void Should_Fail_Test_On_Delayed_Post_When_FlushDispatcher()
  23. {
  24. Dispatcher.UIThread.Post(() => throw new InvalidOperationException(), DispatcherPriority.Default);
  25. }
  26. #if NUNIT
  27. [AvaloniaTheory, Timeout(10000), TestCase(1), TestCase(10), TestCase(100)]
  28. #elif XUNIT
  29. [AvaloniaTheory(Timeout = 10000), InlineData(1), InlineData(10), InlineData(100)]
  30. #endif
  31. public async Task DispatcherTimer_Works_On_The_Same_Thread(int interval)
  32. {
  33. await Task.Delay(100);
  34. var currentThread = Thread.CurrentThread;
  35. var tcs = new TaskCompletionSource();
  36. var hasCompleted = false;
  37. DispatcherTimer.RunOnce(() =>
  38. {
  39. hasCompleted = currentThread == Thread.CurrentThread;
  40. tcs.SetResult();
  41. }, TimeSpan.FromTicks(interval));
  42. await tcs.Task;
  43. Assert.True(hasCompleted);
  44. }
  45. }