Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

134 lignes
5.1 KiB

  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Connections;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using System;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using MQTTnet.Adapter;
  11. using MQTTnet.AspNetCore.Tests.Mockups;
  12. using MQTTnet.Client.Options;
  13. using MQTTnet.Exceptions;
  14. using MQTTnet.Formatter;
  15. using MQTTnet.Packets;
  16. using System.Net;
  17. using MQTTnet.AspNetCore.Extensions;
  18. using MQTTnet.Protocol;
  19. using MQTTnet.Tests.Extensions;
  20. namespace MQTTnet.AspNetCore.Tests
  21. {
  22. [TestClass]
  23. public class MqttConnectionContextTest
  24. {
  25. [TestMethod]
  26. public async Task TestReceivePacketAsyncThrowsWhenReaderCompleted()
  27. {
  28. var serializer = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311);
  29. var pipe = new DuplexPipeMockup();
  30. var connection = new DefaultConnectionContext();
  31. connection.Transport = pipe;
  32. var ctx = new MqttConnectionContext(serializer, connection);
  33. pipe.Receive.Writer.Complete();
  34. await Assert.ThrowsExceptionAsync<MqttCommunicationException>(() => ctx.ReceivePacketAsync(CancellationToken.None));
  35. }
  36. [TestMethod]
  37. public async Task TestCorruptedConnectPacket()
  38. {
  39. var writer = new MqttPacketWriter();
  40. var serializer = new MqttPacketFormatterAdapter(writer);
  41. var pipe = new DuplexPipeMockup();
  42. var connection = new DefaultConnectionContext();
  43. connection.Transport = pipe;
  44. var ctx = new MqttConnectionContext(serializer, connection);
  45. await pipe.Receive.Writer.WriteAsync(writer.AddMqttHeader(MqttControlPacketType.Connect, new byte[0]));
  46. await Assert.ThrowsExceptionAsync<MqttProtocolViolationException>(() => ctx.ReceivePacketAsync(CancellationToken.None));
  47. // the first exception should complete the pipes so if someone tries to use the connection after that it should throw immidiatly
  48. await Assert.ThrowsExceptionAsync<InvalidOperationException>(() => ctx.ReceivePacketAsync(CancellationToken.None));
  49. }
  50. // COMMENTED OUT DUE TO DEAD LOCK? OR VERY VERY SLOW PERFORMANCE ON LOCAL DEV MACHINE. TEST WAS STILL RUNNING AFTER SEVERAL MINUTES!
  51. //[TestMethod]
  52. //public async Task TestParallelWrites()
  53. //{
  54. // var serializer = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311);
  55. // var pipe = new DuplexPipeMockup();
  56. // var connection = new DefaultConnectionContext();
  57. // connection.Transport = pipe;
  58. // var ctx = new MqttConnectionContext(serializer, connection);
  59. // var tasks = Enumerable.Range(1, 100).Select(_ => Task.Run(async () =>
  60. // {
  61. // for (int i = 0; i < 100; i++)
  62. // {
  63. // await ctx.SendPacketAsync(new MqttPublishPacket(), TimeSpan.Zero, CancellationToken.None).ConfigureAwait(false);
  64. // }
  65. // }));
  66. // await Task.WhenAll(tasks).ConfigureAwait(false);
  67. //}
  68. [TestMethod]
  69. public async Task TestLargePacket()
  70. {
  71. var serializer = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311);
  72. var pipe = new DuplexPipeMockup();
  73. var connection = new DefaultConnectionContext();
  74. connection.Transport = pipe;
  75. var ctx = new MqttConnectionContext(serializer, connection);
  76. await ctx.SendPacketAsync(new MqttPublishPacket { Payload = new byte[20_000] }, CancellationToken.None).ConfigureAwait(false);
  77. var readResult = await pipe.Send.Reader.ReadAsync();
  78. Assert.IsTrue(readResult.Buffer.Length > 20000);
  79. }
  80. private class Startup
  81. {
  82. public void Configure(IApplicationBuilder app)
  83. {
  84. }
  85. }
  86. [TestMethod]
  87. public async Task TestEndpoint()
  88. {
  89. var mockup = new ConnectionHandlerMockup();
  90. using (var host = new WebHostBuilder()
  91. .UseKestrel(kestrel => kestrel.ListenLocalhost(1883, listener => listener.Use((ctx, next) => mockup.OnConnectedAsync(ctx))))
  92. .UseStartup<Startup>()
  93. .ConfigureServices((hostContext, services) =>
  94. {
  95. services.AddHostedMqttServer(o => o.WithoutDefaultEndpoint());
  96. services.AddSingleton<IMqttServerAdapter>(mockup);
  97. })
  98. .Build())
  99. using (var client = new MqttFactory().CreateMqttClient())
  100. {
  101. host.Start();
  102. await client.ConnectAsync(new MqttClientOptionsBuilder()
  103. .WithTcpServer("localhost")
  104. .Build(), CancellationToken.None);
  105. var ctx = await mockup.Context.Task;
  106. #if NETCOREAPP3_1
  107. var ep = IPEndPoint.Parse(ctx.Endpoint);
  108. Assert.IsNotNull(ep);
  109. #endif
  110. Assert.IsNotNull(ctx);
  111. }
  112. }
  113. }
  114. }