Skip to content

Commit

Permalink
Update to xunit v3
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed Dec 25, 2024
1 parent 44a5978 commit 37d3756
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 30 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PropertyGroup>
<WarningsAsErrors>$(WarningsAsErrors),SYSLIB1045</WarningsAsErrors>
<WarningsAsErrors>$(WarningsAsErrors),IL2026,IL2060,IL2091,IL2095,IL3050</WarningsAsErrors>
<WarningsAsErrors>$(WarningsAsErrors),xUnit1051</WarningsAsErrors>
</PropertyGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Tingle.EventBus/Transports/EventBusTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ protected async Task<EventConsumeResult> ConsumeAsync(IServiceScope scope,
IDictionary<string, string?>? extras = null)
{
var state = new Dictionary<string, string>();

var wrapped = state.ToEventBusWrapper()
.AddIfNotDefault(MetadataNames.Id, id)
.AddIfNotDefault(MetadataNames.CorrelationId, correlationId)
Expand Down
4 changes: 2 additions & 2 deletions tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.4.0" />
<PackageReference Include="MartinCostello.Logging.XUnit.v3" Version="0.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0" />
<PackageReference Include="xunit.v3" Version="1.0.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Tingle.EventBus.Configuration;
using Tingle.EventBus.Serialization;
using Tingle.EventBus.Transports.Azure.EventHubs.IotHub;
using Xunit.Abstractions;

namespace Tingle.EventBus.Transports.Azure.EventHubs.Tests;

Expand Down Expand Up @@ -44,8 +43,15 @@ await TestSerializerAsync(async (provider, _, serializer) =>
{
var ereg = EventRegistration.Create<MyIotHubEvent>();
var stream = TestSamples.GetIotHubTelemetry();

/* Unmerged change from project 'Tingle.EventBus.Transports.Azure.EventHubs.Tests(net9.0)'
Before:
var (ed, ctx) = CreateData(ereg, await BinaryData.FromStreamAsync(stream), "Telemetry");
var envelope = await serializer.DeserializeAsync<MyIotHubEvent>(ctx);
After:
var (ed, ctx) = CreateData(ereg, await BinaryData.FromStreamAsync(stream, TestContext.Current.CancellationToken), "Telemetry");
*/
var (ed, ctx) = CreateData(ereg, await BinaryData.FromStreamAsync(stream, TestContext.Current.CancellationToken), "Telemetry");
var envelope = await serializer.DeserializeAsync<MyIotHubEvent>(ctx, TestContext.Current.CancellationToken);
Assert.NotNull(envelope);
Assert.NotNull(envelope.Event);
Assert.Null(envelope.Event.Event);
Expand All @@ -62,15 +68,22 @@ await TestSerializerAsync(async (provider, _, serializer) =>
{
var ereg = EventRegistration.Create<MyIotHubEvent>();
var stream = TestSamples.GetIotHubTwinChangeEvents();

/* Unmerged change from project 'Tingle.EventBus.Transports.Azure.EventHubs.Tests(net9.0)'
Before:
var (ed, ctx) = CreateData(ereg, await BinaryData.FromStreamAsync(stream), "twinChangeEvents", new Dictionary<string, object>
After:
var (ed, ctx) = CreateData(ereg, await BinaryData.FromStreamAsync(stream, TestContext.Current.CancellationToken), "twinChangeEvents", new Dictionary<string, object>
*/
var (ed, ctx) = CreateData(ereg, await BinaryData.FromStreamAsync(stream, TestContext.Current.CancellationToken), "twinChangeEvents", new Dictionary<string, object>
{
["hubName"] = HubName,
["deviceId"] = DeviceId,
["opType"] = "updateTwin",
["operationTimestamp"] = "2022-01-16T16:36:53.8146535Z",
["iothub-message-schema"] = "twinChangeNotification",
});
var envelope = await serializer.DeserializeAsync<MyIotHubEvent>(ctx);
var envelope = await serializer.DeserializeAsync<MyIotHubEvent>(ctx, TestContext.Current.CancellationToken);
Assert.NotNull(envelope);
Assert.NotNull(envelope.Event);
Assert.Null(envelope.Event.Telemetry);
Expand All @@ -96,15 +109,22 @@ await TestSerializerAsync(async (provider, _, serializer) =>
{
var ereg = EventRegistration.Create<MyIotHubEvent>();
var stream = TestSamples.GetIotHubDeviceLifecycleEvents();

/* Unmerged change from project 'Tingle.EventBus.Transports.Azure.EventHubs.Tests(net9.0)'
Before:
var (ed, ctx) = CreateData(ereg, await BinaryData.FromStreamAsync(stream), "deviceLifecycleEvents", new Dictionary<string, object>
After:
var (ed, ctx) = CreateData(ereg, await BinaryData.FromStreamAsync(stream, TestContext.Current.CancellationToken), "deviceLifecycleEvents", new Dictionary<string, object>
*/
var (ed, ctx) = CreateData(ereg, await BinaryData.FromStreamAsync(stream, TestContext.Current.CancellationToken), "deviceLifecycleEvents", new Dictionary<string, object>
{
["hubName"] = HubName,
["deviceId"] = DeviceId,
["opType"] = "createDeviceIdentity",
["operationTimestamp"] = "2022-01-16T16:36:53.8146535Z",
["iothub-message-schema"] = "deviceLifecycleNotification",
});
var envelope = await serializer.DeserializeAsync<MyIotHubEvent>(ctx);
var envelope = await serializer.DeserializeAsync<MyIotHubEvent>(ctx, TestContext.Current.CancellationToken);
Assert.NotNull(envelope);
Assert.NotNull(envelope.Event);
Assert.Null(envelope.Event.Telemetry);
Expand All @@ -129,15 +149,18 @@ await TestSerializerAsync(async (provider, _, serializer) =>
{
var ereg = EventRegistration.Create<MyIotHubEvent>();
var stream = TestSamples.GetIotHubDeviceConnectionStateEvents();
var (ed, ctx) = CreateData(ereg, await BinaryData.FromStreamAsync(stream), "deviceConnectionStateEvents", new Dictionary<string, object>
{
["hubName"] = HubName,
["deviceId"] = DeviceId,
["opType"] = "deviceConnected",
["operationTimestamp"] = "2022-01-16T16:36:53.8146535Z",
["iothub-message-schema"] = "deviceConnectionStateNotification",
});
var envelope = await serializer.DeserializeAsync<MyIotHubEvent>(ctx);
var (ed, ctx) = CreateData(ereg,
await BinaryData.FromStreamAsync(stream, TestContext.Current.CancellationToken),
"deviceConnectionStateEvents",
new Dictionary<string, object>
{
["hubName"] = HubName,
["deviceId"] = DeviceId,
["opType"] = "deviceConnected",
["operationTimestamp"] = "2022-01-16T16:36:53.8146535Z",
["iothub-message-schema"] = "deviceConnectionStateNotification",
});
var envelope = await serializer.DeserializeAsync<MyIotHubEvent>(ctx, TestContext.Current.CancellationToken);
Assert.NotNull(envelope);
Assert.NotNull(envelope.Event);
Assert.Null(envelope.Event.Telemetry);
Expand All @@ -159,7 +182,8 @@ await TestSerializerAsync(async (provider, _, serializer) =>
{
var ereg = EventRegistration.Create<DummyEvent1>();
var ctx = new DeserializationContext(BinaryData.FromString(""), ereg, false);
var ex = await Assert.ThrowsAsync<NotSupportedException>(() => serializer.DeserializeAsync<DummyEvent1>(ctx));
var ex = await Assert.ThrowsAsync<NotSupportedException>(
() => serializer.DeserializeAsync<DummyEvent1>(ctx, TestContext.Current.CancellationToken));
Assert.Equal("Only events that inherit from 'Tingle.EventBus.Transports.Azure.EventHubs.IotHub.IotHubEvent' are supported for deserialization.", ex.Message);
});
}
Expand All @@ -172,7 +196,7 @@ await TestSerializerAsync(async (provider, publisher, serializer) =>
var ereg = EventRegistration.Create<MyIotHubEvent>();
var context = new EventContext<MyIotHubEvent>(publisher, new());
var ctx = new SerializationContext<MyIotHubEvent>(context, ereg);
var ex = await Assert.ThrowsAsync<NotSupportedException>(() => serializer.SerializeAsync(ctx));
var ex = await Assert.ThrowsAsync<NotSupportedException>(() => serializer.SerializeAsync(ctx, TestContext.Current.CancellationToken));
Assert.Equal("Serialization of IotHub events is not allowed.", ex.Message);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.Extensions.Logging;
using SimpleConsumer;
using Tingle.EventBus.Transports.InMemory;
using Xunit.Abstractions;

namespace Tingle.EventBus.Tests.InMemory;

Expand All @@ -30,7 +29,7 @@ public async Task ConsumerWorksAsync()
var provider = host.Services;

var harness = provider.GetRequiredService<InMemoryTestHarness>();
await harness.StartAsync();
await harness.StartAsync(TestContext.Current.CancellationToken);
try
{
// Ensure we start at 0 for the counter
Expand All @@ -45,13 +44,13 @@ await publisher.PublishAsync(new SampleEvent
Registration = "1234567890",
VIN = "5YJ3E1EA5KF328931",
Year = 2021
});
}, cancellationToken: TestContext.Current.CancellationToken);

// Ensure no faults were published by the consumer
Assert.False(harness.Failed<SampleEvent>().Any());

// Ensure the message was consumed
Assert.NotEmpty(await harness.ConsumedAsync<SampleEvent>(TimeSpan.FromSeconds(0.5f)));
Assert.NotEmpty(await harness.ConsumedAsync<SampleEvent>(TimeSpan.FromSeconds(0.5f), TestContext.Current.CancellationToken));

// Now you can ensure data saved to database correctly

Expand All @@ -60,7 +59,7 @@ await publisher.PublishAsync(new SampleEvent
}
finally
{
await harness.StopAsync();
await harness.StopAsync(TestContext.Current.CancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public async Task Generate_Works()
{
var sng = new SequenceNumberGenerator();
var current = sng.Generate();
await Task.Delay(TimeSpan.FromSeconds(1));
await Task.Delay(TimeSpan.FromSeconds(1), TestContext.Current.CancellationToken);
var next = sng.Generate();
Assert.Equal(1, next - current);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task EventIsPublishedOnBusAsync()

var provider = host.Services;
var harness = provider.GetRequiredService<InMemoryTestHarness>();
await harness.StartAsync();
await harness.StartAsync(TestContext.Current.CancellationToken);
try
{
var publisher = provider.GetRequiredService<IEventPublisher>();
Expand All @@ -34,7 +34,9 @@ public async Task EventIsPublishedOnBusAsync()
VIN = "5YJ3E1EA5KF328931",
Year = 2021,
};
var schedulingId = (string?)await publisher.PublishAsync(@event: evt, scheduled: DateTimeOffset.UtcNow.AddDays(1));
var schedulingId = (string?)await publisher.PublishAsync(@event: evt,
scheduled: DateTimeOffset.UtcNow.AddDays(1),
cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(schedulingId);

// Ensure no failures
Expand All @@ -43,15 +45,15 @@ public async Task EventIsPublishedOnBusAsync()
// Ensure only one was published
Assert.Single(harness.Published<DoorOpenedEvent>());

await publisher.CancelAsync<DoorOpenedEvent>(schedulingId);
await publisher.CancelAsync<DoorOpenedEvent>(schedulingId, TestContext.Current.CancellationToken);

// Ensure only one was cancelled
var sn = Assert.Single(harness.Cancelled());
Assert.Equal(schedulingId, sn.ToString());
}
finally
{
await harness.StopAsync();
await harness.StopAsync(TestContext.Current.CancellationToken);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task EventIsPublishedOnBusAsync(int orderNumber)

var provider = host.Services;
var harness = provider.GetRequiredService<InMemoryTestHarness>();
await harness.StartAsync();
await harness.StartAsync(TestContext.Current.CancellationToken);
try
{
var orderProcessor = provider.GetRequiredService<RandomOrderProcessor>();
Expand All @@ -52,7 +52,7 @@ public async Task EventIsPublishedOnBusAsync(int orderNumber)
}
finally
{
await harness.StopAsync();
await harness.StopAsync(TestContext.Current.CancellationToken);
}
}

Expand Down

0 comments on commit 37d3756

Please sign in to comment.