Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

95 linhas
3.1 KiB

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