Skip to content

Commit

Permalink
Merge pull request #1025 from stebet/xunit
Browse files Browse the repository at this point in the history
Replacing nUnit with xUnit and parallelizing tests where possible.
  • Loading branch information
michaelklishin authored Mar 10, 2021
2 parents b643a26 + 272be01 commit d6a11d9
Show file tree
Hide file tree
Showing 65 changed files with 1,115 additions and 1,211 deletions.
3 changes: 1 addition & 2 deletions projects/Benchmarks/Networking/RabbitMqBroker.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Diagnostics;
using System.Threading;

using Ductus.FluentDocker.Builders;
using Ductus.FluentDocker.Services;
using Ductus.FluentDocker.Services.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Benchmarks.Networking
{
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/RabbitMQ.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

<PropertyGroup>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
</PropertyGroup>

<PropertyGroup Condition="'$(CONCOURSE_CI_BUILD)' == 'true'">
Expand Down Expand Up @@ -67,7 +68,6 @@
<PackageReference Include="MinVer" Version="2.4.0" PrivateAssets="All" />
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="System.Threading.Channels" Version="5.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Threading.Channels;
using System.Threading.Tasks;

using RabbitMQ.Client.Impl;

namespace RabbitMQ.Client.ConsumerDispatching
{
#nullable enable
#nullable enable
internal abstract class ConsumerDispatcherChannelBase : ConsumerDispatcherBase, IConsumerDispatcher
{
protected readonly ModelBase _model;
Expand Down Expand Up @@ -48,7 +49,7 @@ public void HandleBasicConsumeOk(IBasicConsumer consumer, string consumerTag)
if (!IsShutdown)
{
AddConsumer(consumer, consumerTag);
_writer.TryWrite(new WorkStruct(WorkType.ConsumeOk, consumer, consumerTag));
_writer.TryWrite(new WorkStruct(WorkType.ConsumeOk, consumer, consumerTag));
}
}

Expand Down Expand Up @@ -104,12 +105,12 @@ protected readonly struct WorkStruct
{
public readonly IBasicConsumer Consumer;
public IAsyncBasicConsumer AsyncConsumer => (IAsyncBasicConsumer)Consumer;
public readonly string ConsumerTag;
public readonly string? ConsumerTag;
public readonly ulong DeliveryTag;
public readonly bool Redelivered;
public readonly string Exchange;
public readonly string RoutingKey;
public readonly IBasicProperties BasicProperties;
public readonly string? Exchange;
public readonly string? RoutingKey;
public readonly IBasicProperties? BasicProperties;
public readonly ReadOnlyMemory<byte> Body;
public readonly byte[]? RentedArray;
public readonly ShutdownEventArgs? Reason;
Expand Down
2 changes: 1 addition & 1 deletion projects/TestApplications/CreateChannel/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace CreateChannel
{
Expand Down
14 changes: 9 additions & 5 deletions projects/Unit/APIApproval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@
//---------------------------------------------------------------------------

using System.Threading.Tasks;
using NUnit.Framework;

using PublicApiGenerator;
using VerifyNUnit;

using VerifyTests;

using VerifyXunit;

using Xunit;

namespace RabbitMQ.Client.Unit
{
[TestFixture]
[Platform(Exclude="Mono")]

[UsesVerify]
public class APIApproval
{
[Test]
[Fact]
public Task Approve()
{
string publicApi = typeof(ConnectionFactory).Assembly.GeneratePublicApi(new ApiGeneratorOptions
Expand Down
141 changes: 30 additions & 111 deletions projects/Unit/Fixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,42 @@
using System.Text;
using System.Threading;

using NUnit.Framework;

using RabbitMQ.Client.Framing.Impl;

using Xunit;

using static RabbitMQ.Client.Unit.RabbitMQCtl;

namespace RabbitMQ.Client.Unit
{

public class IntegrationFixture
public class IntegrationFixture : IDisposable
{
internal IConnectionFactory _connFactory;
internal IConnection _conn;
internal IModel _model;
internal Encoding _encoding = new UTF8Encoding();
public static TimeSpan RECOVERY_INTERVAL = TimeSpan.FromSeconds(2);

[SetUp]
public virtual void Init()
protected IntegrationFixture()
{
SetUp();
}

protected virtual void SetUp()
{
_connFactory = new ConnectionFactory();
_conn = _connFactory.CreateConnection();
_model = _conn.CreateModel();
}

[TearDown]
public void Dispose()
public virtual void Dispose()
{
if(_model.IsOpen)
if (_model.IsOpen)
{
_model.Close();
}
if(_conn.IsOpen)
if (_conn.IsOpen)
{
_conn.Close();
}
Expand Down Expand Up @@ -141,16 +145,6 @@ internal AutorecoveringConnection CreateAutorecoveringConnectionWithTopologyReco
return (AutorecoveringConnection)cf.CreateConnection($"UNIT_CONN:{Guid.NewGuid()}");
}

internal IConnection CreateNonRecoveringConnection()
{
var cf = new ConnectionFactory
{
AutomaticRecoveryEnabled = false,
TopologyRecoveryEnabled = false
};
return cf.CreateConnection($"UNIT_CONN:{Guid.NewGuid()}");
}

internal IConnection CreateConnectionWithContinuationTimeout(bool automaticRecoveryEnabled, TimeSpan continuationTimeout)
{
var cf = new ConnectionFactory
Expand All @@ -165,38 +159,6 @@ internal IConnection CreateConnectionWithContinuationTimeout(bool automaticRecov
// Channels
//

internal void WithTemporaryAutorecoveringConnection(Action<AutorecoveringConnection> action)
{
var factory = new ConnectionFactory
{
AutomaticRecoveryEnabled = true
};

var connection = (AutorecoveringConnection)factory.CreateConnection($"UNIT_CONN:{Guid.NewGuid()}");
try
{
action(connection);
}
finally
{
connection.Abort();
}
}

internal void WithTemporaryModel(IConnection connection, Action<IModel> action)
{
IModel model = connection.CreateModel();

try
{
action(model);
}
finally
{
model.Abort();
}
}

internal void WithTemporaryModel(Action<IModel> action)
{
IModel model = _conn.CreateModel();
Expand Down Expand Up @@ -260,50 +222,24 @@ internal string GenerateQueueName()
return $"queue{Guid.NewGuid()}";
}

internal void WithTemporaryQueue(Action<IModel, string> action)
{
WithTemporaryQueue(_model, action);
}

internal void WithTemporaryNonExclusiveQueue(Action<IModel, string> action)
{
WithTemporaryNonExclusiveQueue(_model, action);
}

internal void WithTemporaryQueue(IModel model, Action<IModel, string> action)
{
WithTemporaryQueue(model, action, GenerateQueueName());
}

internal void WithTemporaryNonExclusiveQueue(IModel model, Action<IModel, string> action)
{
WithTemporaryNonExclusiveQueue(model, action, GenerateQueueName());
}

internal void WithTemporaryQueue(Action<IModel, string> action, string q)
{
WithTemporaryQueue(_model, action, q);
}

internal void WithTemporaryQueue(IModel model, Action<IModel, string> action, string queue)
{
try
{
model.QueueDeclare(queue, false, true, false, null);
action(model, queue);
} finally
{
WithTemporaryModel(x => x.QueueDelete(queue));
}
}

internal void WithTemporaryNonExclusiveQueue(IModel model, Action<IModel, string> action, string queue)
{
try
{
model.QueueDeclare(queue, false, false, false, null);
action(model, queue);
} finally
}
finally
{
WithTemporaryModel(tm => tm.QueueDelete(queue));
}
Expand All @@ -315,17 +251,13 @@ internal void WithTemporaryQueueNoWait(IModel model, Action<IModel, string> acti
{
model.QueueDeclareNoWait(queue, false, true, false, null);
action(model, queue);
} finally
}
finally
{
WithTemporaryModel(x => x.QueueDelete(queue));
}
}

internal void EnsureNotEmpty(string q)
{
EnsureNotEmpty(q, "msg");
}

internal void EnsureNotEmpty(string q, string body)
{
WithTemporaryModel(x => x.BasicPublish("", q, null, _encoding.GetBytes(body)));
Expand Down Expand Up @@ -354,26 +286,28 @@ internal void WithEmptyQueue(Action<IModel, string> action)
});
}

internal void AssertMessageCount(string q, int count)
internal void AssertMessageCount(string q, uint count)
{
WithTemporaryModel((m) => {
WithTemporaryModel((m) =>
{
QueueDeclareOk ok = m.QueueDeclarePassive(q);
Assert.AreEqual(count, ok.MessageCount);
Assert.Equal(count, ok.MessageCount);
});
}

internal void AssertConsumerCount(string q, int count)
{
WithTemporaryModel((m) => {
WithTemporaryModel((m) =>
{
QueueDeclareOk ok = m.QueueDeclarePassive(q);
Assert.AreEqual(count, ok.ConsumerCount);
Assert.Equal((uint)count, ok.ConsumerCount);
});
}

internal void AssertConsumerCount(IModel m, string q, int count)
internal void AssertConsumerCount(IModel m, string q, uint count)
{
QueueDeclareOk ok = m.QueueDeclarePassive(q);
Assert.AreEqual(count, ok.ConsumerCount);
Assert.Equal(count, ok.ConsumerCount);
}

//
Expand All @@ -382,7 +316,7 @@ internal void AssertConsumerCount(IModel m, string q, int count)

internal void AssertShutdownError(ShutdownEventArgs args, int code)
{
Assert.AreEqual(args.ReplyCode, code);
Assert.Equal(args.ReplyCode, code);
}

internal void AssertPreconditionFailed(ShutdownEventArgs args)
Expand All @@ -401,7 +335,7 @@ internal bool InitiatedByPeerOrLibrary(ShutdownEventArgs evt)

internal void WaitOn(object o)
{
lock(o)
lock (o)
{
Monitor.Wait(o, TimingFixture.TestTimeout);
}
Expand All @@ -421,20 +355,10 @@ internal void Unblock()
RabbitMQCtl.Unblock();
}

internal void Publish(IConnection conn)
{
RabbitMQCtl.Publish(conn, _encoding);
}

//
// Connection Closure
//

internal List<ConnectionInfo> ListConnections()
{
return RabbitMQCtl.ListConnections();
}

internal void CloseConnection(IConnection conn)
{
RabbitMQCtl.CloseConnection(conn);
Expand All @@ -445,11 +369,6 @@ internal void CloseAllConnections()
RabbitMQCtl.CloseAllConnections();
}

internal void CloseConnection(string pid)
{
RabbitMQCtl.CloseConnection(pid);
}

internal void RestartRabbitMQ()
{
RabbitMQCtl.RestartRabbitMQ();
Expand All @@ -471,12 +390,12 @@ internal void StartRabbitMQ()

internal void Wait(ManualResetEventSlim latch)
{
Assert.IsTrue(latch.Wait(TimeSpan.FromSeconds(10)), "waiting on a latch timed out");
Assert.True(latch.Wait(TimeSpan.FromSeconds(10)), "waiting on a latch timed out");
}

internal void Wait(ManualResetEventSlim latch, TimeSpan timeSpan)
{
Assert.IsTrue(latch.Wait(timeSpan), "waiting on a latch timed out");
Assert.True(latch.Wait(timeSpan), "waiting on a latch timed out");
}

//
Expand Down
Loading

0 comments on commit d6a11d9

Please sign in to comment.