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] }, CancellationToken.None).ConfigureAwait(false);
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(CancellationToken.None)); }
public async Task TestReceivePacketAsyncThrowsWhenReaderCompleted() { var serializer = new MqttPacketSerializer { }; 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.FromSeconds(1), CancellationToken.None)); }
public async Task TestCorruptedConnectPacket() { var writer = new MqttPacketWriter(); var serializer = new MqttPacketFormatterAdapter(writer); var pipe = new DuplexPipeMockup(); var connection = new DefaultConnectionContext(); connection.Transport = pipe; var ctx = new MqttConnectionContext(serializer, connection); await pipe.Receive.Writer.WriteAsync(writer.AddMqttHeader(MqttControlPacketType.Connect, new byte[0])); await Assert.ThrowsExceptionAsync <MqttProtocolViolationException>(() => ctx.ReceivePacketAsync(CancellationToken.None)); // the first exception should complete the pipes so if someone tries to use the connection after that it should throw immidiatly await Assert.ThrowsExceptionAsync <InvalidOperationException>(() => ctx.ReceivePacketAsync(CancellationToken.None)); }
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); }