瀏覽代碼

fixed #691

release/3.x.x
Jan Eggers 5 年之前
父節點
當前提交
4f66614cdd
共有 5 個檔案被更改,包括 82 行新增6 行删除
  1. +5
    -4
      Source/MQTTnet.AspnetCore/MqttConnectionContext.cs
  2. +10
    -2
      Tests/MQTTnet.AspNetCore.Tests/Mockups/DuplexPipeMockup.cs
  3. +18
    -0
      Tests/MQTTnet.AspNetCore.Tests/Mockups/LimitedMemoryPool.cs
  4. +33
    -0
      Tests/MQTTnet.AspNetCore.Tests/Mockups/MemoryOwner.cs
  5. +16
    -0
      Tests/MQTTnet.AspNetCore.Tests/MqttConnectionContextTest.cs

+ 5
- 4
Source/MQTTnet.AspnetCore/MqttConnectionContext.cs 查看文件

@@ -162,11 +162,12 @@ namespace MQTTnet.AspNetCore
var buffer = formatter.Encode(packet);
var msg = buffer.AsMemory();
var output = _output;
msg.CopyTo(output.GetMemory(msg.Length));
BytesSent += msg.Length;
var result = await output.WriteAsync(msg, cancellationToken).ConfigureAwait(false);
if (result.IsCompleted)
{
BytesSent += msg.Length;
}
PacketFormatterAdapter.FreeBuffer();
output.Advance(msg.Length);
await output.FlushAsync().ConfigureAwait(false);
}
finally
{


+ 10
- 2
Tests/MQTTnet.AspNetCore.Tests/Mockups/DuplexPipeMockup.cs 查看文件

@@ -4,13 +4,21 @@ namespace MQTTnet.AspNetCore.Tests.Mockups
{
public class DuplexPipeMockup : IDuplexPipe
{
public DuplexPipeMockup()
{
var pool = new LimitedMemoryPool();
var pipeOptions = new PipeOptions(pool);
Receive = new Pipe(pipeOptions);
Send = new Pipe(pipeOptions);
}

PipeReader IDuplexPipe.Input => Receive.Reader;

PipeWriter IDuplexPipe.Output => Send.Writer;


public Pipe Receive { get; set; } = new Pipe();
public Pipe Receive { get; set; }

public Pipe Send { get; set; } = new Pipe();
public Pipe Send { get; set; }
}
}

+ 18
- 0
Tests/MQTTnet.AspNetCore.Tests/Mockups/LimitedMemoryPool.cs 查看文件

@@ -0,0 +1,18 @@
using System.Buffers;

namespace MQTTnet.AspNetCore.Tests.Mockups
{
public class LimitedMemoryPool : MemoryPool<byte>
{
protected override void Dispose(bool disposing)
{
}

public override IMemoryOwner<byte> Rent(int minBufferSize = -1)
{
return new MemoryOwner(minBufferSize);
}

public override int MaxBufferSize { get; }
}
}

+ 33
- 0
Tests/MQTTnet.AspNetCore.Tests/Mockups/MemoryOwner.cs 查看文件

@@ -0,0 +1,33 @@
using System;
using System.Buffers;

namespace MQTTnet.AspNetCore.Tests.Mockups
{
public class MemoryOwner : IMemoryOwner<byte>
{
private readonly byte[] _raw;

public MemoryOwner(int size)
{
if (size <= 0)
{
size = 1024;
}

if (size > 4096)
{
size = 4096;
}

_raw = ArrayPool<byte>.Shared.Rent(size);
Memory = _raw;
}

public void Dispose()
{
ArrayPool<byte>.Shared.Return(_raw);
}

public Memory<byte> Memory { get; }
}
}

+ 16
- 0
Tests/MQTTnet.AspNetCore.Tests/MqttConnectionContextTest.cs 查看文件

@@ -47,5 +47,21 @@ namespace MQTTnet.AspNetCore.Tests

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);
}
}
}

Loading…
取消
儲存