Files
natsnet/dotnet/tests/ZB.MOM.NatsNet.Server.Tests/ImplBacklog/RouteHandlerTests.Impltests.cs

103 lines
3.7 KiB
C#

using System.IO;
using Shouldly;
using ZB.MOM.NatsNet.Server;
using ZB.MOM.NatsNet.Server.Internal;
namespace ZB.MOM.NatsNet.Server.Tests.ImplBacklog;
public sealed partial class RouteHandlerTests
{
[Fact] // T:2854
public void RouteCompressionAuto_ShouldSucceed()
{
var errors = new List<Exception>();
var warnings = new List<Exception>();
var options = new ServerOptions();
var parseError = ServerOptions.ParseCluster(
new Dictionary<string, object?>
{
["name"] = "local",
["compression"] = new Dictionary<string, object?>
{
["mode"] = CompressionModes.S2Auto,
["rtt_thresholds"] = new List<object?> { "100ms", "200ms", "300ms" },
},
},
options,
errors,
warnings);
parseError.ShouldBeNull();
errors.ShouldBeEmpty();
options.Cluster.Compression.Mode.ShouldBe(CompressionModes.S2Auto);
options.Cluster.Compression.RttThresholds.Count.ShouldBe(3);
options.Cluster.Compression.RttThresholds[0].ShouldBe(TimeSpan.FromMilliseconds(100));
options.Cluster.Compression.RttThresholds[1].ShouldBe(TimeSpan.FromMilliseconds(200));
options.Cluster.Compression.RttThresholds[2].ShouldBe(TimeSpan.FromMilliseconds(300));
options = new ServerOptions();
errors.Clear();
warnings.Clear();
parseError = ServerOptions.ParseCluster(
new Dictionary<string, object?>
{
["compression"] = new Dictionary<string, object?>
{
["mode"] = CompressionModes.S2Auto,
["rtt_thresholds"] = new List<object?> { "0ms", "100ms", "0ms", "300ms" },
},
},
options,
errors,
warnings);
parseError.ShouldBeNull();
errors.ShouldBeEmpty();
options.Cluster.Compression.RttThresholds.Count.ShouldBe(4);
options.Cluster.Compression.RttThresholds[0].ShouldBe(TimeSpan.Zero);
options.Cluster.Compression.RttThresholds[1].ShouldBe(TimeSpan.FromMilliseconds(100));
options.Cluster.Compression.RttThresholds[2].ShouldBe(TimeSpan.Zero);
options.Cluster.Compression.RttThresholds[3].ShouldBe(TimeSpan.FromMilliseconds(300));
options = new ServerOptions();
errors.Clear();
warnings.Clear();
parseError = ServerOptions.ParseCluster(
new Dictionary<string, object?>
{
["compression"] = false,
},
options,
errors,
warnings);
parseError.ShouldBeNull();
errors.ShouldBeEmpty();
options.Cluster.Compression.Mode.ShouldBe(CompressionModes.Off);
}
[Fact] // T:2859
public void RouteSlowConsumerRecover_ShouldSucceed()
{
var (server, err) = NatsServer.NewServer(new ServerOptions());
err.ShouldBeNull();
using var outStream = new MemoryStream();
var route = new ClientConnection(ClientKind.Router, server, outStream)
{
OutWtp = WriteTimeoutPolicy.Retry,
OutMp = 1024 * 1024,
};
// Detect slow consumer state from write-timeout path.
route.HandleWriteTimeout(written: 1, attempted: 1024, numChunks: 2).ShouldBeFalse();
route.Flags.IsSet(ClientFlags.IsSlowConsumer).ShouldBeTrue();
// A successful flush should clear slow-consumer marker (recovered).
route.QueueOutbound("MSG test 1 5\r\nhello\r\n"u8.ToArray());
route.FlushOutbound().ShouldBeTrue();
route.Flags.IsSet(ClientFlags.IsSlowConsumer).ShouldBeFalse();
}
}