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.

MqttConnectionContextTest.cs 1.8 KiB

há 6 anos
há 6 anos
há 6 anos
há 6 anos
há 6 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. }