Skip to content

Commit 4b41870

Browse files
authored
chore: Update xunit (#141)
1 parent 3638441 commit 4b41870

27 files changed

+229
-151
lines changed

src/EventSourcingDb.Tests/CustomHttpClientTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task UsesCustomHttpClientSuccessfully()
2626

2727
var client = new Client(httpClient);
2828

29-
await client.PingAsync();
29+
await client.PingAsync(TestContext.Current.CancellationToken);
3030
}
3131

3232
[Fact]
@@ -44,7 +44,7 @@ public async Task CustomHttpClientWithCustomHeadersWorks()
4444

4545
var client = new Client(httpClient);
4646

47-
await client.PingAsync();
47+
await client.PingAsync(TestContext.Current.CancellationToken);
4848
}
4949

5050
[Fact]
@@ -62,6 +62,6 @@ public async Task ContainerCanProvideClientWithCustomHttpClient()
6262

6363
var client = Container.GetClient(httpClient);
6464

65-
await client.PingAsync();
65+
await client.PingAsync(TestContext.Current.CancellationToken);
6666
}
6767
}

src/EventSourcingDb.Tests/CustomSerializationOptionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async Task UsesCustomSerializationOptionsToControlSerialization()
2525
Data: new EventData(MyEnum.Foo)
2626
);
2727

28-
var writtenEvents = await client.WriteEventsAsync([eventCandidate]);
28+
var writtenEvents = await client.WriteEventsAsync([eventCandidate], token: TestContext.Current.CancellationToken);
2929

3030
var writtenEvent = writtenEvents[0];
3131
var eventDataJsonString = writtenEvent.Data.ToString();

src/EventSourcingDb.Tests/DependencyInjectionTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,23 @@ public async Task HttpClientConfigurationViaDependencyInjectionIsWorking()
4545
throw new Exception("IClient is not registered.");
4646
}
4747

48-
await Container.PauseAsync();
48+
await Container.PauseAsync(TestContext.Current.CancellationToken);
4949

5050
// With the container paused, ping fails after one retry.
5151
// The task is canceled due to the configured timeout, thus we expect a TaskCanceledException.
52-
await Assert.ThrowsAsync<TaskCanceledException>(async () => await client.PingAsync());
52+
await Assert.ThrowsAsync<TaskCanceledException>(async () => await client.PingAsync(TestContext.Current.CancellationToken));
5353

54-
_ = Task.Run(async () =>
54+
_ = Task.Run(
55+
async () =>
5556
{
5657
await Task.Delay(simulatedNetworkOutageDurationInMs);
5758
await Container.UnpauseAsync();
5859
}
60+
, TestContext.Current.CancellationToken
5961
);
6062

6163
// With the container unpaused within the retry delay, ping succeeds.
62-
await client.PingAsync();
64+
await client.PingAsync(TestContext.Current.CancellationToken);
6365
}
6466
}
6567

src/EventSourcingDb.Tests/EventSourcingDb.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<PackageReference Include="coverlet.collector" Version="6.0.4" />
1616
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
1717
<PackageReference Include="System.Linq.Async" Version="7.0.0" Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &lt; 10.0" />
18-
<PackageReference Include="xunit" Version="2.9.3" />
18+
<PackageReference Include="xunit.v3" Version="3.2.0" />
1919
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.2"/>
2020
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.2"/>
2121
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />

src/EventSourcingDb.Tests/EventSourcingDbTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
using System.Threading.Tasks;
22
using Microsoft.Extensions.Logging;
33
using Xunit;
4-
using Xunit.Abstractions;
54

65
namespace EventSourcingDb.Tests;
76

87
public class EventSourcingDbTests : IAsyncLifetime
98
{
109
protected Container? Container { get; private set; }
1110

12-
public async Task InitializeAsync()
11+
public async ValueTask InitializeAsync()
1312
{
1413
var imageVersion = DockerfileHelper.GetImageVersionFromDockerfile();
1514
Container = new Container().WithImageTag(imageVersion);
1615
await Container.StartAsync();
1716
}
1817

19-
public ILogger<Client> GetClientLogger(ITestOutputHelper testOutputHelper)
18+
protected ILogger<Client> GetClientLogger(ITestOutputHelper testOutputHelper)
2019
{
2120
return new XUnitLogger<Client>(testOutputHelper);
2221
}
2322

24-
public async Task DisposeAsync()
23+
public async ValueTask DisposeAsync()
2524
{
2625
if (Container is not null)
2726
{
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text.Json;
4+
5+
namespace EventSourcingDb.Tests;
6+
7+
public static class JsonElementComparer
8+
{
9+
public static bool Equals(JsonElement x, JsonElement y)
10+
{
11+
if (x.ValueKind != y.ValueKind)
12+
{
13+
return false;
14+
}
15+
16+
switch (x.ValueKind)
17+
{
18+
case JsonValueKind.Object:
19+
var xProps = x.EnumerateObject().OrderBy(p => p.Name).ToList();
20+
var yProps = y.EnumerateObject().OrderBy(p => p.Name).ToList();
21+
if (xProps.Count != yProps.Count)
22+
return false;
23+
for (int i = 0; i < xProps.Count; i++)
24+
{
25+
if (xProps[i].Name != yProps[i].Name)
26+
return false;
27+
if (!Equals(xProps[i].Value, yProps[i].Value))
28+
return false;
29+
}
30+
return true;
31+
case JsonValueKind.Array:
32+
var xArr = x.EnumerateArray().ToList();
33+
var yArr = y.EnumerateArray().ToList();
34+
if (xArr.Count != yArr.Count)
35+
return false;
36+
for (int i = 0; i < xArr.Count; i++)
37+
{
38+
if (!Equals(xArr[i], yArr[i]))
39+
return false;
40+
}
41+
return true;
42+
case JsonValueKind.String:
43+
return x.GetString() == y.GetString();
44+
case JsonValueKind.Number:
45+
return x.GetRawText() == y.GetRawText();
46+
case JsonValueKind.True:
47+
case JsonValueKind.False:
48+
return x.GetBoolean() == y.GetBoolean();
49+
case JsonValueKind.Null:
50+
case JsonValueKind.Undefined:
51+
return true;
52+
default:
53+
return x.GetRawText() == y.GetRawText();
54+
}
55+
}
56+
}

src/EventSourcingDb.Tests/ObserveEventsTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Threading.Tasks;
55
using EventSourcingDb.Types;
66
using Xunit;
7-
using Xunit.Abstractions;
87

98
namespace EventSourcingDb.Tests;
109

@@ -55,7 +54,7 @@ public async Task ObserveAllEventsFromTheGivenSubject()
5554
Data: secondData
5655
);
5756

58-
await client.WriteEventsAsync([firstEvent, secondEvent]);
57+
await client.WriteEventsAsync([firstEvent, secondEvent], token: TestContext.Current.CancellationToken);
5958

6059
var observedEvents = new List<Event>();
6160
var options = new ObserveEventsOptions(Recursive: true);
@@ -97,7 +96,7 @@ public async Task ObservesWithLowerBound()
9796
Data: secondData
9897
);
9998

100-
await client.WriteEventsAsync([firstEvent, secondEvent]);
99+
await client.WriteEventsAsync([firstEvent, secondEvent], token: TestContext.Current.CancellationToken);
101100

102101
var observedEvents = new List<Event>();
103102
var options = new ObserveEventsOptions(Recursive: true, LowerBound: new Bound("1", BoundType.Inclusive));
@@ -146,7 +145,7 @@ public async Task ObservesFromLatestEvent()
146145
Data: secondData
147146
);
148147

149-
await client.WriteEventsAsync([firstEvent, secondEvent]);
148+
await client.WriteEventsAsync([firstEvent, secondEvent], token: TestContext.Current.CancellationToken);
150149

151150
var observedEvents = new List<Event>();
152151
var options = new ObserveEventsOptions(

src/EventSourcingDb.Tests/PingTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public async Task DoesNotThrowIfServerIsReachable()
1414
var client = Container!.GetClient();
1515

1616
// Should not throw.
17-
await client.PingAsync();
17+
await client.PingAsync(TestContext.Current.CancellationToken);
1818
}
1919

2020
[Fact]
@@ -28,7 +28,7 @@ public async Task ThrowsIfServerIsNotReachable()
2828

2929
await Assert.ThrowsAsync<HttpRequestException>(async () =>
3030
{
31-
await client.PingAsync();
31+
await client.PingAsync(TestContext.Current.CancellationToken);
3232
});
3333
}
3434
}

src/EventSourcingDb.Tests/ReadEventTypeTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public async Task FailsIfTheEventTypeDoesNotExist()
1616

1717
try
1818
{
19-
await client.ReadEventTypeAsync("io.eventsourcingdb.nonexistent");
19+
await client.ReadEventTypeAsync("io.eventsourcingdb.nonexistent", TestContext.Current.CancellationToken);
2020
}
2121
catch (HttpRequestException ex)
2222
{
@@ -36,7 +36,7 @@ public async Task FailsIfTheEventTypeIsMalformed()
3636

3737
try
3838
{
39-
await client.ReadEventTypeAsync("io.eventsourcingdb.malformed.");
39+
await client.ReadEventTypeAsync("io.eventsourcingdb.malformed.", TestContext.Current.CancellationToken);
4040
}
4141
catch (HttpRequestException ex)
4242
{
@@ -63,9 +63,9 @@ public async Task ReadsAnExistingEventType()
6363
Data: firstData
6464
);
6565

66-
await client.WriteEventsAsync([firstEvent]);
66+
await client.WriteEventsAsync([firstEvent], token: TestContext.Current.CancellationToken);
6767

68-
var eventType = await client.ReadEventTypeAsync("io.eventsourcingdb.test");
68+
var eventType = await client.ReadEventTypeAsync("io.eventsourcingdb.test", TestContext.Current.CancellationToken);
6969
Assert.Equal("io.eventsourcingdb.test", eventType.Type);
7070
Assert.False(eventType.IsPhantom);
7171
Assert.Null(eventType.Schema);

src/EventSourcingDb.Tests/ReadEventTypesTest.cs

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Linq;
33
using System.Net.Http;
4-
using System.Net.Http.Headers;
5-
using System.Text;
64
using System.Text.Json;
75
using System.Text.Json.Serialization;
86
using System.Threading.Tasks;
@@ -17,7 +15,9 @@ public class ReadEventTypesTest : EventSourcingDbTests
1715
public async Task ReadsNoEventTypesIfTheDatabaseIsEmpty()
1816
{
1917
var client = Container!.GetClient();
20-
var eventTypesRead = await client.ReadEventTypesAsync().ToListAsync();
18+
var eventTypesRead = await client
19+
.ReadEventTypesAsync(TestContext.Current.CancellationToken)
20+
.ToListAsync(TestContext.Current.CancellationToken);
2121

2222
Assert.Empty(eventTypesRead);
2323
}
@@ -43,9 +43,11 @@ public async Task ReadsAllEventTypes()
4343
Data: secondData
4444
);
4545

46-
await client.WriteEventsAsync([firstEvent, secondEvent]);
46+
await client.WriteEventsAsync([firstEvent, secondEvent], token: TestContext.Current.CancellationToken);
4747

48-
var eventTypesRead = await client.ReadEventTypesAsync().ToListAsync();
48+
var eventTypesRead = await client
49+
.ReadEventTypesAsync(TestContext.Current.CancellationToken)
50+
.ToListAsync(TestContext.Current.CancellationToken);
4951

5052
Assert.Equal(2, eventTypesRead.Count);
5153
Assert.Equal(eventTypesRead[0], new EventType(
@@ -65,46 +67,35 @@ public async Task SupportsReadingEventSchemas()
6567
{
6668
var client = Container!.GetClient();
6769

68-
// TODO: simplify this once schema registration is supported by the client.
69-
var registerEventSchemaUrl = new Uri(Container.GetBaseUrl(), "/api/v1/register-event-schema");
70-
var eventSchema = new
71-
{
72-
Type = "object",
73-
Properties = new { value = new { type = "integer" } },
74-
Required = new[] { "value" }
75-
};
76-
77-
using var request = new HttpRequestMessage(HttpMethod.Post, registerEventSchemaUrl);
78-
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Container.GetApiToken());
79-
request.Content = new StringContent(
80-
JsonSerializer.Serialize(new
70+
const string schemaJson =
71+
"""
8172
{
82-
eventType = "io.eventsourcingdb.v1.test",
83-
schema = eventSchema
84-
}, _defaultSerializerOptions),
85-
Encoding.UTF8,
86-
"application/json"
87-
);
88-
using var response = await _httpClient.SendAsync(request);
89-
response.EnsureSuccessStatusCode();
90-
91-
var eventTypesRead = await client.ReadEventTypesAsync().ToListAsync();
73+
"type": "object",
74+
"properties": {
75+
"value": {
76+
"type": "number"
77+
}
78+
},
79+
"required": [
80+
"value"
81+
]
82+
}
83+
""";
84+
var schema = JsonDocument.Parse(schemaJson).RootElement;
85+
const string eventType = "io.eventsourcingdb.v1.test";
86+
await client.RegisterEventSchemaAsync(eventType, schema, TestContext.Current.CancellationToken);
87+
88+
var eventTypesRead = await client
89+
.ReadEventTypesAsync(TestContext.Current.CancellationToken)
90+
.ToListAsync(TestContext.Current.CancellationToken);
9291

9392
Assert.Single(eventTypesRead);
94-
Assert.Collection(eventTypesRead, eventType =>
93+
Assert.Collection(eventTypesRead, type =>
9594
{
96-
Assert.Equal("io.eventsourcingdb.v1.test", eventType.Type);
97-
Assert.True(eventType.IsPhantom);
98-
Assert.NotNull(eventType.Schema);
99-
100-
var schema = eventType.Schema.Value;
101-
Assert.Equal(JsonValueKind.Object, schema.ValueKind);
102-
Assert.Equal("object", schema.GetProperty("type").GetString());
103-
Assert.Equal("integer", schema.GetProperty("properties").GetProperty("value").GetProperty("type").GetString());
104-
Assert.True(schema.TryGetProperty("required", out var required));
105-
Assert.Equal(JsonValueKind.Array, required.ValueKind);
106-
Assert.Equal(1, required.GetArrayLength());
107-
Assert.Equal("value", required[0].GetString());
95+
Assert.Equal(eventType, type.Type);
96+
Assert.True(type.IsPhantom);
97+
Assert.NotNull(type.Schema);
98+
Assert.True(JsonElementComparer.Equals(type.Schema.Value, schema));
10899
});
109100
}
110101

0 commit comments

Comments
 (0)