You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExtensionTests.cs 3.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using MQTTnet.Exceptions;
  7. using MQTTnet.Internal;
  8. namespace MQTTnet.Tests
  9. {
  10. [TestClass]
  11. public class ExtensionTests
  12. {
  13. [ExpectedException(typeof(MqttCommunicationTimedOutException))]
  14. [TestMethod]
  15. public async Task TimeoutAfter()
  16. {
  17. await MqttTaskTimeout.WaitAsync(ct => Task.Delay(TimeSpan.FromMilliseconds(500), ct), TimeSpan.FromMilliseconds(100), CancellationToken.None);
  18. }
  19. [ExpectedException(typeof(MqttCommunicationTimedOutException))]
  20. [TestMethod]
  21. public async Task TimeoutAfterWithResult()
  22. {
  23. await MqttTaskTimeout.WaitAsync(ct => Task.Delay(TimeSpan.FromMilliseconds(500), ct).ContinueWith(t => 5, ct), TimeSpan.FromMilliseconds(100), CancellationToken.None);
  24. }
  25. [TestMethod]
  26. public async Task TimeoutAfterCompleteInTime()
  27. {
  28. var result = await MqttTaskTimeout.WaitAsync(ct => Task.Delay(TimeSpan.FromMilliseconds(100), ct).ContinueWith(t => 5, ct), TimeSpan.FromMilliseconds(500), CancellationToken.None);
  29. Assert.AreEqual(5, result);
  30. }
  31. [TestMethod]
  32. public async Task TimeoutAfterWithInnerException()
  33. {
  34. try
  35. {
  36. await MqttTaskTimeout.WaitAsync(ct => Task.Run(() =>
  37. {
  38. var iis = new int[0];
  39. iis[1] = 0;
  40. }, ct), TimeSpan.FromSeconds(1), CancellationToken.None);
  41. Assert.Fail();
  42. }
  43. catch (Exception e)
  44. {
  45. Assert.IsTrue(e is IndexOutOfRangeException);
  46. }
  47. }
  48. [TestMethod]
  49. public async Task TimeoutAfterWithInnerExceptionWithResult()
  50. {
  51. try
  52. {
  53. await MqttTaskTimeout.WaitAsync(ct => Task.Run(() =>
  54. {
  55. var iis = new int[0];
  56. iis[1] = 0;
  57. return iis[0];
  58. }, ct), TimeSpan.FromSeconds(1), CancellationToken.None);
  59. Assert.Fail();
  60. }
  61. catch (Exception e)
  62. {
  63. Assert.IsTrue(e is IndexOutOfRangeException);
  64. }
  65. }
  66. [TestMethod]
  67. public async Task TimeoutAfterMemoryUsage()
  68. {
  69. var tasks = Enumerable.Range(0, 100000)
  70. .Select(i =>
  71. {
  72. return MqttTaskTimeout.WaitAsync(ct => Task.Delay(TimeSpan.FromMilliseconds(1), ct), TimeSpan.FromMinutes(1), CancellationToken.None);
  73. });
  74. await Task.WhenAll(tasks);
  75. AssertIsLess(3_000_000, GC.GetTotalMemory(true));
  76. }
  77. private static void AssertIsLess(long bound, long actual)
  78. {
  79. if (bound < actual)
  80. {
  81. Assert.Fail($"value must be less than {bound:N0} but is {actual:N0}");
  82. }
  83. }
  84. }
  85. }