diff --git a/projects/Test/Common/IntegrationFixture.cs b/projects/Test/Common/IntegrationFixture.cs index b742ba659..a62bf9266 100644 --- a/projects/Test/Common/IntegrationFixture.cs +++ b/projects/Test/Common/IntegrationFixture.cs @@ -84,16 +84,9 @@ public abstract class IntegrationFixture : IAsyncLifetime public static readonly TimeSpan RecoveryInterval = TimeSpan.FromSeconds(2); public static readonly TimeSpan TestTimeout = TimeSpan.FromSeconds(5); public static readonly TimeSpan RequestedConnectionTimeout = TimeSpan.FromSeconds(1); - public static readonly Random S_Random; static IntegrationFixture() { - -#if NET - S_Random = Random.Shared; -#else - S_Random = new Random(); -#endif s_isRunningInCI = InitIsRunningInCI(); s_isVerbose = InitIsVerbose(); @@ -450,12 +443,12 @@ protected async Task WithTemporaryChannelAsync(Func action) protected string GenerateExchangeName() { - return $"{_testDisplayName}-exchange-{Util.Now}-{GenerateShortUuid()}"; + return $"{_testDisplayName}-exchange-{Util.Now}-{Util.GenerateShortUuid()}"; } protected string GenerateQueueName() { - return $"{_testDisplayName}-queue-{Util.Now}-{GenerateShortUuid()}"; + return $"{_testDisplayName}-queue-{Util.Now}-{Util.GenerateShortUuid()}"; } protected Task WithTemporaryNonExclusiveQueueAsync(Func action) @@ -631,10 +624,15 @@ protected static string GetUniqueString(ushort length) protected static byte[] GetRandomBody(ushort size = 1024) { byte[] body = new byte[size]; - S_Random.NextBytes(body); + Util.S_Random.NextBytes(body); return body; } + protected static int RandomNext(int min, int max) + { + return Util.S_Random.Next(min, max); + } + protected static Task WaitForRecoveryAsync(IConnection conn) { TaskCompletionSource tcs = PrepareForRecovery((AutorecoveringConnection)conn); @@ -664,7 +662,5 @@ protected void LogWarning(string text, _output.WriteLine("::warning file={0},line={1}::{2} {3}", Path.GetFileName(file), line, member, text); } - - protected static string GenerateShortUuid() => S_Random.Next().ToString("x"); } } diff --git a/projects/Test/Common/Util.cs b/projects/Test/Common/Util.cs index 052084910..ec5542d33 100644 --- a/projects/Test/Common/Util.cs +++ b/projects/Test/Common/Util.cs @@ -10,6 +10,13 @@ namespace Test { public class Util : IDisposable { +#if NET + private static readonly Random s_random = Random.Shared; +#else + [ThreadStatic] + private static Random s_random; +#endif + private readonly ITestOutputHelper _output; private readonly ManagementClient _managementClient; private static readonly bool s_isWindows = false; @@ -41,6 +48,21 @@ public Util(ITestOutputHelper output, string managementUsername, string manageme _managementClient = new ManagementClient(managementUri, managementUsername, managementPassword); } + public static Random S_Random + { + get + { +#if NET + return s_random; +#else + s_random ??= new Random(); + return s_random; +#endif + } + } + + public static string GenerateShortUuid() => S_Random.Next().ToString("x"); + public static string Now => DateTime.UtcNow.ToString("s", CultureInfo.InvariantCulture); public static bool IsWindows => s_isWindows; diff --git a/projects/Test/Integration/TestAsyncConsumer.cs b/projects/Test/Integration/TestAsyncConsumer.cs index 7dd4deebb..2b658de6c 100644 --- a/projects/Test/Integration/TestAsyncConsumer.cs +++ b/projects/Test/Integration/TestAsyncConsumer.cs @@ -703,7 +703,7 @@ await _channel.BasicPublishAsync(exchange: string.Empty, routingKey: queueName, private async Task ValidateConsumerDispatchConcurrency() { - ushort expectedConsumerDispatchConcurrency = (ushort)S_Random.Next(3, 10); + ushort expectedConsumerDispatchConcurrency = (ushort)RandomNext(3, 10); AutorecoveringChannel autorecoveringChannel = (AutorecoveringChannel)_channel; Assert.Equal(ConsumerDispatchConcurrency, autorecoveringChannel.ConsumerDispatcher.Concurrency); Assert.Equal(_consumerDispatchConcurrency, autorecoveringChannel.ConsumerDispatcher.Concurrency); diff --git a/projects/Test/Integration/TestConcurrentAccessBase.cs.cs b/projects/Test/Integration/TestConcurrentAccessBase.cs.cs index 21d862e82..0aa538c99 100644 --- a/projects/Test/Integration/TestConcurrentAccessBase.cs.cs +++ b/projects/Test/Integration/TestConcurrentAccessBase.cs.cs @@ -54,7 +54,7 @@ protected async Task TestConcurrentOperationsAsync(Func action, int iterat { for (int j = 0; j < iterations; j++) { - await Task.Delay(S_Random.Next(1, 10)); + await Task.Delay(RandomNext(1, 10)); tasks.Add(action()); } } diff --git a/projects/Test/Integration/TestExchangeDeclare.cs b/projects/Test/Integration/TestExchangeDeclare.cs index ecff7b267..ef21255f2 100644 --- a/projects/Test/Integration/TestExchangeDeclare.cs +++ b/projects/Test/Integration/TestExchangeDeclare.cs @@ -62,7 +62,7 @@ async Task f() { try { - await Task.Delay(S_Random.Next(5, 50)); + await Task.Delay(RandomNext(5, 50)); string exchangeName = GenerateExchangeName(); await _channel.ExchangeDeclareAsync(exchange: exchangeName, type: "fanout", false, false); await _channel.ExchangeBindAsync(destination: ex_destination, source: exchangeName, routingKey: "unused"); @@ -87,7 +87,7 @@ async Task f() { try { - await Task.Delay(S_Random.Next(5, 50)); + await Task.Delay(RandomNext(5, 50)); await _channel.ExchangeUnbindAsync(destination: ex_destination, source: exchangeName, routingKey: "unused", noWait: false, arguments: null); await _channel.ExchangeDeleteAsync(exchange: exchangeName, ifUnused: false); diff --git a/projects/Test/Integration/TestQueueDeclare.cs b/projects/Test/Integration/TestQueueDeclare.cs index 8c1cffff5..79d0c9106 100644 --- a/projects/Test/Integration/TestQueueDeclare.cs +++ b/projects/Test/Integration/TestQueueDeclare.cs @@ -103,7 +103,7 @@ async Task f() { // sleep for a random amount of time to increase the chances // of thread interleaving. MK. - await Task.Delay(S_Random.Next(5, 50)); + await Task.Delay(RandomNext(5, 50)); string queueName = GenerateQueueName(); QueueDeclareOk r = await _channel.QueueDeclareAsync(queue: queueName, durable: false, exclusive: true, autoDelete: false); @@ -136,7 +136,7 @@ async Task f() string qname = q; try { - await Task.Delay(S_Random.Next(5, 50)); + await Task.Delay(RandomNext(5, 50)); QueueDeclareOk r = await _channel.QueueDeclarePassiveAsync(qname); Assert.Equal(qname, r.QueueName); @@ -176,7 +176,7 @@ public async Task TestConcurrentQueueDeclare() { // sleep for a random amount of time to increase the chances // of thread interleaving. MK. - await Task.Delay(S_Random.Next(5, 50)); + await Task.Delay(RandomNext(5, 50)); string q = GenerateQueueName(); await _channel.QueueDeclareAsync(q, false, false, false); queueNames.Add(q); @@ -201,7 +201,7 @@ public async Task TestConcurrentQueueDeclare() { try { - await Task.Delay(S_Random.Next(5, 50)); + await Task.Delay(RandomNext(5, 50)); await _channel.QueueDeleteAsync(queueName); } catch (NotSupportedException e) diff --git a/projects/Test/Integration/TestToxiproxy.cs b/projects/Test/Integration/TestToxiproxy.cs index 25ba534c6..86ce35774 100644 --- a/projects/Test/Integration/TestToxiproxy.cs +++ b/projects/Test/Integration/TestToxiproxy.cs @@ -221,7 +221,7 @@ public async Task TestThatStoppedSocketResultsInHeartbeatTimeout() Assert.True(await tcs.Task); - string toxicName = $"rmq-localhost-timeout-{Util.Now}-{GenerateShortUuid()}"; + string toxicName = $"rmq-localhost-timeout-{Util.Now}-{Util.GenerateShortUuid()}"; var timeoutToxic = new TimeoutToxic { Name = toxicName @@ -275,7 +275,7 @@ public async Task TestTcpReset_GH1464() Assert.True(await channelCreatedTcs.Task); - string toxicName = $"rmq-localhost-reset_peer-{Util.Now}-{GenerateShortUuid()}"; + string toxicName = $"rmq-localhost-reset_peer-{Util.Now}-{Util.GenerateShortUuid()}"; var resetPeerToxic = new ResetPeerToxic { Name = toxicName @@ -360,7 +360,7 @@ public async Task TestPublisherConfirmationThrottling() await channelCreatedTcs.Task; - string toxicName = $"rmq-localhost-bandwidth-{Util.Now}-{GenerateShortUuid()}"; + string toxicName = $"rmq-localhost-bandwidth-{Util.Now}-{Util.GenerateShortUuid()}"; var bandwidthToxic = new BandwidthToxic { Name = toxicName diff --git a/projects/Test/Integration/ToxiproxyManager.cs b/projects/Test/Integration/ToxiproxyManager.cs index 53fcfb92b..ee88c203f 100644 --- a/projects/Test/Integration/ToxiproxyManager.cs +++ b/projects/Test/Integration/ToxiproxyManager.cs @@ -69,7 +69,7 @@ public ToxiproxyManager(string testDisplayName, bool isRunningInCI, bool isWindo public async Task InitializeAsync() { - _proxy.Name = $"{ProxyNamePrefix}-{_testDisplayName}-{Util.Now}"; + _proxy.Name = $"{ProxyNamePrefix}-{_testDisplayName}-{Util.Now}-{Util.GenerateShortUuid()}"; try {