|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using Microsoft.AspNetCore.Connections;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using MQTTnet.AspNetCore.Tests.Mockups;
- using MQTTnet.Exceptions;
- using MQTTnet.Packets;
- using System;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using MQTTnet.Formatter;
-
- namespace MQTTnet.AspNetCore.Tests
- {
- [TestClass]
- public class MqttConnectionContextTest
- {
- [TestMethod]
- public async Task TestReceivePacketAsyncThrowsWhenReaderCompleted()
- {
- var serializer = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311);
- var pipe = new DuplexPipeMockup();
- var connection = new DefaultConnectionContext();
- connection.Transport = pipe;
- var ctx = new MqttConnectionContext(serializer, connection);
-
- pipe.Receive.Writer.Complete();
-
- await Assert.ThrowsExceptionAsync<MqttCommunicationException>(() => ctx.ReceivePacketAsync(TimeSpan.Zero, CancellationToken.None));
- }
-
- [TestMethod]
- public async Task TestParallelWrites()
- {
- var serializer = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311);
- var pipe = new DuplexPipeMockup();
- var connection = new DefaultConnectionContext();
- connection.Transport = pipe;
- var ctx = new MqttConnectionContext(serializer, connection);
-
- var tasks = Enumerable.Range(1, 10).Select(_ => Task.Run(async () =>
- {
- for (int i = 0; i < 100; i++)
- {
- await ctx.SendPacketAsync(new MqttPublishPacket(), TimeSpan.Zero, CancellationToken.None).ConfigureAwait(false);
- }
- }));
-
- await Task.WhenAll(tasks).ConfigureAwait(false);
- }
-
-
- [TestMethod]
- public async Task TestLargePacket()
- {
- var serializer = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311);
- var pipe = new DuplexPipeMockup();
- var connection = new DefaultConnectionContext();
- connection.Transport = pipe;
- var ctx = new MqttConnectionContext(serializer, connection);
-
- await ctx.SendPacketAsync(new MqttPublishPacket() { Payload = new byte[20_000] }, TimeSpan.Zero, CancellationToken.None).ConfigureAwait(false);
-
- var readResult = await pipe.Send.Reader.ReadAsync();
- Assert.IsTrue(readResult.Buffer.Length > 20000);
- }
- }
- }
|