Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

52 wiersze
1.8 KiB

  1. using Microsoft.AspNetCore.Connections;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using MQTTnet.AspNetCore.Tests.Mockups;
  4. using MQTTnet.Exceptions;
  5. using MQTTnet.Packets;
  6. using MQTTnet.Serializer;
  7. using System;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MQTTnet.AspNetCore.Tests
  12. {
  13. [TestClass]
  14. public class MqttConnectionContextTest
  15. {
  16. [TestMethod]
  17. public async Task TestReceivePacketAsyncThrowsWhenReaderCompleted()
  18. {
  19. var serializer = new MqttPacketSerializerAdapter(MqttProtocolVersion.V311);
  20. var pipe = new DuplexPipeMockup();
  21. var connection = new DefaultConnectionContext();
  22. connection.Transport = pipe;
  23. var ctx = new MqttConnectionContext(serializer, connection);
  24. pipe.Receive.Writer.Complete();
  25. await Assert.ThrowsExceptionAsync<MqttCommunicationException>(() => ctx.ReceivePacketAsync(TimeSpan.FromSeconds(1), CancellationToken.None));
  26. }
  27. [TestMethod]
  28. public async Task TestParallelWrites()
  29. {
  30. var serializer = new MqttPacketSerializerAdapter(MqttProtocolVersion.V311);
  31. var pipe = new DuplexPipeMockup();
  32. var connection = new DefaultConnectionContext();
  33. connection.Transport = pipe;
  34. var ctx = new MqttConnectionContext(serializer, connection);
  35. var tasks = Enumerable.Range(1, 10).Select(_ => Task.Run(async () =>
  36. {
  37. for (int i = 0; i < 100; i++)
  38. {
  39. await ctx.SendPacketAsync(new MqttPublishPacket(), CancellationToken.None).ConfigureAwait(false);
  40. }
  41. }));
  42. await Task.WhenAll(tasks).ConfigureAwait(false);
  43. }
  44. }
  45. }