174 lines
5.6 KiB
C#
174 lines
5.6 KiB
C#
using System.Collections.Concurrent;
|
|
using Shouldly;
|
|
using ZB.MOM.NatsNet.Server;
|
|
|
|
namespace ZB.MOM.NatsNet.Server.Tests.ImplBacklog;
|
|
|
|
public sealed partial class ConcurrencyTests1
|
|
{
|
|
[Fact] // T:2452
|
|
public void NoRaceFileStoreStreamMaxAgePerformance_ShouldSucceed()
|
|
{
|
|
WithStore((fs, _) =>
|
|
{
|
|
Parallel.For(0, 200, i => fs.StoreMsg($"age.{i % 4}", null, new[] { (byte)(i % 255) }, 0));
|
|
|
|
var state = fs.State();
|
|
state.Msgs.ShouldBeGreaterThan(0UL);
|
|
state.LastSeq.ShouldBeGreaterThanOrEqualTo(state.Msgs);
|
|
|
|
var (total, validThrough, err) = fs.NumPending(1, ">", false);
|
|
err.ShouldBeNull();
|
|
total.ShouldBeGreaterThan(0UL);
|
|
validThrough.ShouldBeGreaterThan(0UL);
|
|
}, DefaultStreamConfig(maxAge: TimeSpan.FromMilliseconds(20)));
|
|
}
|
|
|
|
[Fact] // T:2453
|
|
public void NoRaceFileStoreFilteredStateWithLargeDeletes_ShouldSucceed()
|
|
{
|
|
WithStore((fs, _) =>
|
|
{
|
|
for (var i = 0; i < 240; i++)
|
|
fs.StoreMsg("fd", null, new[] { (byte)(i % 255) }, 0);
|
|
|
|
Parallel.For(1L, 240L, i =>
|
|
{
|
|
if (i % 3 == 0)
|
|
fs.RemoveMsg((ulong)i);
|
|
});
|
|
|
|
var filtered = fs.FilteredState(1, "fd");
|
|
filtered.Msgs.ShouldBeGreaterThan(0UL);
|
|
filtered.Last.ShouldBeGreaterThanOrEqualTo(filtered.First);
|
|
|
|
fs.SubjectsTotals(">")["fd"].ShouldBeGreaterThan(0UL);
|
|
});
|
|
}
|
|
|
|
[Fact] // T:2462
|
|
public void NoRaceFileStoreNumPending_ShouldSucceed()
|
|
{
|
|
WithStore((fs, _) =>
|
|
{
|
|
for (var i = 0; i < 100; i++)
|
|
fs.StoreMsg($"np.{i % 5}", null, "x"u8.ToArray(), 0);
|
|
|
|
var errors = new ConcurrentQueue<Exception>();
|
|
var workers = Enumerable.Range(0, 8).Select(_ => Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
for (var i = 0; i < 40; i++)
|
|
{
|
|
var (_, _, err1) = fs.NumPending(1, ">", false);
|
|
if (err1 != null)
|
|
throw err1;
|
|
|
|
var (_, _, err2) = fs.NumPendingMulti(1, new[] { "np.1", "np.*" }, false);
|
|
if (err2 != null)
|
|
throw err2;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
errors.Enqueue(ex);
|
|
}
|
|
})).ToArray();
|
|
|
|
Task.WaitAll(workers);
|
|
errors.ShouldBeEmpty();
|
|
});
|
|
}
|
|
|
|
[Fact] // T:2427
|
|
public void NoRaceJetStreamFileStoreKeyFileCleanup_ShouldSucceed()
|
|
{
|
|
WithStore((_, root) =>
|
|
{
|
|
var msgDir = Path.Combine(root, FileStoreDefaults.MsgDir);
|
|
Directory.CreateDirectory(msgDir);
|
|
var perm = UnixFileMode.UserRead | UnixFileMode.UserWrite;
|
|
|
|
var errors = new ConcurrentQueue<Exception>();
|
|
Parallel.For(0, 300, i =>
|
|
{
|
|
var payload = BitConverter.GetBytes(i);
|
|
var keyFile = Path.Combine(msgDir, string.Format(FileStoreDefaults.KeyScan, (uint)(i + 1)));
|
|
var err = JetStreamFileStore.WriteAtomically(keyFile, payload, perm, sync: true);
|
|
if (err != null)
|
|
errors.Enqueue(err);
|
|
});
|
|
|
|
errors.ShouldBeEmpty();
|
|
var keyFiles = Directory.GetFiles(msgDir, "*.key");
|
|
keyFiles.Length.ShouldBe(300);
|
|
|
|
foreach (var key in keyFiles.Skip(1))
|
|
File.Delete(key);
|
|
|
|
Directory.GetFiles(msgDir, "*.key").Length.ShouldBe(1);
|
|
Directory.GetFiles(msgDir, "*.tmp").ShouldBeEmpty();
|
|
});
|
|
}
|
|
|
|
[Fact] // T:2447
|
|
public void NoRaceEncodeConsumerStateBug_ShouldSucceed()
|
|
{
|
|
for (var i = 0; i < 5_000; i++)
|
|
{
|
|
var pending = new Pending
|
|
{
|
|
Sequence = 1,
|
|
Timestamp = DateTimeOffset.UtcNow.AddSeconds(1).ToUnixTimeSeconds() * 1_000_000_000L,
|
|
};
|
|
var state = new ConsumerState
|
|
{
|
|
Delivered = new SequencePair { Consumer = 1, Stream = 1 },
|
|
Pending = new Dictionary<ulong, Pending> { [1] = pending },
|
|
};
|
|
|
|
var encoded = StoreParity.EncodeConsumerState(state);
|
|
var (_, err) = JetStreamFileStore.DecodeConsumerState(encoded);
|
|
err.ShouldBeNull();
|
|
}
|
|
}
|
|
|
|
private static void WithStore(Action<JetStreamFileStore, string> action, StreamConfig? cfg = null)
|
|
{
|
|
var root = NewRoot();
|
|
Directory.CreateDirectory(root);
|
|
JetStreamFileStore? fs = null;
|
|
|
|
try
|
|
{
|
|
fs = JetStreamFileStore.NewFileStore(new FileStoreConfig { StoreDir = root }, cfg ?? DefaultStreamConfig());
|
|
action(fs, root);
|
|
}
|
|
finally
|
|
{
|
|
fs?.Stop();
|
|
if (Directory.Exists(root))
|
|
Directory.Delete(root, recursive: true);
|
|
}
|
|
}
|
|
|
|
private static StreamConfig DefaultStreamConfig(TimeSpan? maxAge = null)
|
|
{
|
|
return new StreamConfig
|
|
{
|
|
Name = "TEST",
|
|
Storage = StorageType.FileStorage,
|
|
Subjects = ["test.>"],
|
|
MaxMsgs = -1,
|
|
MaxBytes = -1,
|
|
MaxAge = maxAge ?? TimeSpan.Zero,
|
|
MaxMsgsPer = -1,
|
|
Discard = DiscardPolicy.DiscardOld,
|
|
Retention = RetentionPolicy.LimitsPolicy,
|
|
};
|
|
}
|
|
|
|
private static string NewRoot() => Path.Combine(Path.GetTempPath(), $"impl-fs-c1-{Guid.NewGuid():N}");
|
|
}
|