Skip to content

Commit 7de9f93

Browse files
authored
refactor CI (#77)
* change the way to install the dotnet versions * tests refactor avoiding code duplication --------- Signed-off-by: Gabriele Santomaggio <[email protected]>
1 parent 8672d5f commit 7de9f93

File tree

4 files changed

+61
-55
lines changed

4 files changed

+61
-55
lines changed

.github/workflows/wf_build-and-test.yaml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,12 @@ jobs:
4848
runs-on: ubuntu-latest
4949
steps:
5050
- uses: actions/checkout@v4
51-
- uses: actions/cache@v4
51+
- name: Setup dotnet
52+
uses: actions/setup-dotnet@v4
5253
with:
53-
path: |
54-
~/.nuget/packages
55-
~/.local/share/NuGet/v3-cache
56-
key: ${{ runner.os }}-v0-nuget-${{ hashFiles('**/*.csproj') }}
57-
restore-keys: |
58-
${{ runner.os }}-v0-nuget-
54+
dotnet-version: |
55+
6.0.x
56+
8.0.x
5957
- name: Build (Debug)
6058
run: dotnet build ${{ github.workspace }}/Build.csproj
6159
- name: Verify

Tests/ConnectionRecoveryTests.cs

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ public void Reset()
4444
public int CurrentAttempt => 1;
4545
}
4646

47-
public class ConnectionRecoveryTests(ITestOutputHelper testOutputHelper)
47+
public class ConnectionRecoveryTests(ITestOutputHelper testOutputHelper) : IntegrationTest(testOutputHelper)
4848
{
49-
private readonly ITestOutputHelper _testOutputHelper = testOutputHelper;
50-
5149
/// <summary>
5250
/// The normal close the status should be correct and error null
5351
/// The test records the status change when the connection is closed normally.
@@ -59,12 +57,13 @@ public class ConnectionRecoveryTests(ITestOutputHelper testOutputHelper)
5957
[InlineData(false)]
6058
public async Task NormalCloseTheStatusShouldBeCorrectAndErrorNull(bool activeRecovery)
6159
{
62-
string containerId = Guid.NewGuid().ToString();
60+
string localContainerId = $"{_containerId}_normal-close-connection-name";
61+
6362
IConnection connection = await AmqpConnection.CreateAsync(
64-
ConnectionSettingBuilder.Create().ContainerId(containerId).RecoveryConfiguration(
63+
ConnectionSettingBuilder.Create().ContainerId(localContainerId).RecoveryConfiguration(
6564
RecoveryConfiguration.Create().Activated(activeRecovery).Topology(false)).Build());
6665

67-
TaskCompletionSource<bool> connectionClosedStateTcs = CreateTaskCompletionSource();
66+
TaskCompletionSource<bool> connectionClosedStateTcs = CreateTaskCompletionSource<bool>();
6867
var listFromStatus = new List<State>();
6968
var listToStatus = new List<State>();
7069
var listError = new List<Error?>();
@@ -105,14 +104,16 @@ public async Task NormalCloseTheStatusShouldBeCorrectAndErrorNull(bool activeRec
105104
[Fact]
106105
public async Task UnexpectedCloseTheStatusShouldBeCorrectAndErrorNotNull()
107106
{
108-
const string containerId = "unexpected-close-connection-name";
107+
108+
string localContainerId = $"{_containerId}_unexpected-close-connection-name";
109+
109110
IConnection connection = await AmqpConnection.CreateAsync(
110-
ConnectionSettingBuilder.Create().ContainerId(containerId).RecoveryConfiguration(
111+
ConnectionSettingBuilder.Create().ContainerId(localContainerId).RecoveryConfiguration(
111112
RecoveryConfiguration.Create().Activated(true).Topology(false)
112113
.BackOffDelayPolicy(new FakeFastBackOffDelay())).Build());
113114

114-
TaskCompletionSource<bool> listErrorCountGreaterThanOrEqualToTwoTcs = CreateTaskCompletionSource();
115-
TaskCompletionSource<bool> listErrorCountGreaterThanOrEqualToFourTcs = CreateTaskCompletionSource();
115+
TaskCompletionSource<bool> listErrorCountGreaterThanOrEqualToTwoTcs = CreateTaskCompletionSource<bool>();
116+
TaskCompletionSource<bool> listErrorCountGreaterThanOrEqualToFourTcs = CreateTaskCompletionSource<bool>();
116117

117118
var listFromStatus = new List<State>();
118119
var listToStatus = new List<State>();
@@ -129,6 +130,7 @@ public async Task UnexpectedCloseTheStatusShouldBeCorrectAndErrorNotNull()
129130
// Note: must use try since it'll be called again
130131
listErrorCountGreaterThanOrEqualToTwoTcs.TrySetResult(true);
131132
}
133+
132134
if (listError.Count >= 4)
133135
{
134136
listErrorCountGreaterThanOrEqualToFourTcs.SetResult(true);
@@ -142,7 +144,7 @@ public async Task UnexpectedCloseTheStatusShouldBeCorrectAndErrorNotNull()
142144
};
143145

144146
Assert.Equal(State.Open, connection.State);
145-
await SystemUtils.WaitUntilConnectionIsKilled(containerId);
147+
await SystemUtils.WaitUntilConnectionIsKilled(localContainerId);
146148
await listErrorCountGreaterThanOrEqualToTwoTcs.Task.WaitAsync(TimeSpan.FromSeconds(5));
147149

148150
Assert.Equal(State.Open, listFromStatus[0]);
@@ -175,15 +177,16 @@ public async Task UnexpectedCloseTheStatusShouldBeCorrectAndErrorNotNull()
175177
[Fact]
176178
public async Task OverrideTheBackOffWithBackOffDisabled()
177179
{
178-
string containerId = Guid.NewGuid().ToString();
180+
181+
string localContainerId = $"{_containerId}_override-backoff-disabled-connection-name";
179182
IConnection connection = await AmqpConnection.CreateAsync(
180-
ConnectionSettingBuilder.Create().ContainerId(containerId).RecoveryConfiguration(
183+
ConnectionSettingBuilder.Create().ContainerId(localContainerId).RecoveryConfiguration(
181184
RecoveryConfiguration.Create().Activated(true).Topology(false).BackOffDelayPolicy(
182185
new FakeBackOffDelayPolicyDisabled())).Build());
183186

184187
var listFromStatus = new List<State>();
185-
TaskCompletionSource<bool> listFromStatusCountGreaterOrEqualToTwo = CreateTaskCompletionSource();
186-
TaskCompletionSource<bool> listErrorCountGreaterOrEqualToTwo = CreateTaskCompletionSource();
188+
TaskCompletionSource<bool> listFromStatusCountGreaterOrEqualToTwo = CreateTaskCompletionSource<bool>();
189+
TaskCompletionSource<bool> listErrorCountGreaterOrEqualToTwo = CreateTaskCompletionSource<bool>();
187190

188191
var listToStatus = new List<State>();
189192
var listError = new List<Error>();
@@ -195,18 +198,20 @@ public async Task OverrideTheBackOffWithBackOffDisabled()
195198
{
196199
listError.Add(error);
197200
}
201+
198202
if (listFromStatus.Count >= 2)
199203
{
200204
listFromStatusCountGreaterOrEqualToTwo.TrySetResult(true);
201205
}
206+
202207
if (listError.Count >= 2)
203208
{
204209
listErrorCountGreaterOrEqualToTwo.SetResult(true);
205210
}
206211
};
207212

208213
Assert.Equal(State.Open, connection.State);
209-
await SystemUtils.WaitUntilConnectionIsKilled(containerId);
214+
await SystemUtils.WaitUntilConnectionIsKilled(localContainerId);
210215

211216
await listFromStatusCountGreaterOrEqualToTwo.Task.WaitAsync(TimeSpan.FromSeconds(5));
212217

@@ -234,16 +239,16 @@ public async Task OverrideTheBackOffWithBackOffDisabled()
234239
[Fact]
235240
public async Task RecoveryTopologyShouldRecoverTheTempQueues()
236241
{
242+
string localContainerId = $"{_containerId}_temp-queue-should-recover-connection-name";
237243
string queueName = $"temp-queue-should-recover-{true}";
238-
const string containerId = "temp-queue-should-recover-connection-name";
239244
var connection = await AmqpConnection.CreateAsync(
240245
ConnectionSettingBuilder.Create()
241246
.RecoveryConfiguration(RecoveryConfiguration.Create()
242247
.BackOffDelayPolicy(new FakeFastBackOffDelay())
243248
.Topology(true))
244-
.ContainerId(containerId)
249+
.ContainerId(localContainerId)
245250
.Build());
246-
TaskCompletionSource<bool> twoRecoveryEventsSeenTcs = CreateTaskCompletionSource();
251+
TaskCompletionSource<bool> twoRecoveryEventsSeenTcs = CreateTaskCompletionSource<bool>();
247252
int recoveryEvents = 0;
248253
connection.ChangeState += (sender, from, to, error) =>
249254
{
@@ -257,7 +262,7 @@ public async Task RecoveryTopologyShouldRecoverTheTempQueues()
257262
await management.Queue().Name(queueName).AutoDelete(true).Exclusive(true).DeclareAsync();
258263
Assert.Equal(1, topologyListener.QueueCount());
259264

260-
await SystemUtils.WaitUntilConnectionIsKilled(containerId);
265+
await SystemUtils.WaitUntilConnectionIsKilled(localContainerId);
261266
await twoRecoveryEventsSeenTcs.Task.WaitAsync(TimeSpan.FromSeconds(10));
262267
await SystemUtils.WaitUntilFuncAsync(() => recoveryEvents == 2);
263268

@@ -281,15 +286,15 @@ public async Task RecoveryTopologyShouldRecoverTheTempQueues()
281286
public async Task RecoveryTopologyShouldNotRecoverTheTempQueues()
282287
{
283288
string queueName = $"temp-queue-should-recover-{false}";
284-
const string containerId = "temp-queue-should-not-recover-connection-name";
289+
string localContainerId = $"{_containerId}_temp-queue-should-not-recover-connection-name";
285290
var connection = await AmqpConnection.CreateAsync(
286291
ConnectionSettingBuilder.Create()
287292
.RecoveryConfiguration(RecoveryConfiguration.Create()
288293
.BackOffDelayPolicy(new FakeFastBackOffDelay())
289294
.Topology(false))
290-
.ContainerId(containerId)
295+
.ContainerId(localContainerId)
291296
.Build());
292-
TaskCompletionSource<bool> oneRecoveryEventSeenTcs = CreateTaskCompletionSource();
297+
TaskCompletionSource<bool> oneRecoveryEventSeenTcs = CreateTaskCompletionSource<bool>();
293298
int recoveryEvents = 0;
294299
connection.ChangeState += (sender, from, to, error) =>
295300
{
@@ -303,7 +308,7 @@ public async Task RecoveryTopologyShouldNotRecoverTheTempQueues()
303308
await management.Queue().Name(queueName).AutoDelete(true).Exclusive(true).DeclareAsync();
304309
Assert.Equal(1, topologyListener.QueueCount());
305310

306-
await SystemUtils.WaitUntilConnectionIsKilled(containerId);
311+
await SystemUtils.WaitUntilConnectionIsKilled(localContainerId);
307312
await oneRecoveryEventSeenTcs.Task.WaitAsync(TimeSpan.FromSeconds(10));
308313

309314
await SystemUtils.WaitUntilQueueDeletedAsync(queueName);
@@ -318,15 +323,15 @@ public async Task RecoveryTopologyShouldNotRecoverTheTempQueues()
318323
public async Task RecoveryTopologyShouldRecoverExchanges(bool topologyEnabled)
319324
{
320325
const string exchangeName = "exchange-should-recover";
321-
const string containerId = nameof(RecoveryTopologyShouldRecoverExchanges);
326+
string localContainerId = $"{_containerId}_exchange-should-recover-connection-name";
322327
IConnection connection = await AmqpConnection.CreateAsync(
323328
ConnectionSettingBuilder.Create()
324329
.RecoveryConfiguration(RecoveryConfiguration.Create()
325330
.BackOffDelayPolicy(new FakeFastBackOffDelay())
326331
.Topology(topologyEnabled))
327-
.ContainerId(containerId)
332+
.ContainerId(localContainerId)
328333
.Build());
329-
TaskCompletionSource<bool> twoRecoveryEventsSeenTcs = CreateTaskCompletionSource();
334+
TaskCompletionSource<bool> twoRecoveryEventsSeenTcs = CreateTaskCompletionSource<bool>();
330335
int recoveryEvents = 0;
331336
connection.ChangeState += (sender, from, to, error) =>
332337
{
@@ -346,7 +351,7 @@ public async Task RecoveryTopologyShouldRecoverExchanges(bool topologyEnabled)
346351
// the exchange is recovered.
347352
await SystemUtils.DeleteExchangeAsync("exchange-should-recover");
348353

349-
await SystemUtils.WaitUntilConnectionIsKilled(containerId);
354+
await SystemUtils.WaitUntilConnectionIsKilled(localContainerId);
350355

351356
await twoRecoveryEventsSeenTcs.Task.WaitAsync(TimeSpan.FromSeconds(10));
352357

@@ -374,15 +379,15 @@ public async Task RecoveryTopologyShouldRecoverExchanges(bool topologyEnabled)
374379
[InlineData(false)]
375380
public async Task RecoveryTopologyShouldRecoverBindings(bool topologyEnabled)
376381
{
377-
const string containerId = "binding-should-recover-connection-name";
382+
string localContainerId = $"{_containerId}_binding-should-recover-connection-name";
378383
var connection = await AmqpConnection.CreateAsync(
379384
ConnectionSettingBuilder.Create()
380385
.RecoveryConfiguration(RecoveryConfiguration.Create()
381386
.BackOffDelayPolicy(new FakeFastBackOffDelay())
382387
.Topology(topologyEnabled))
383-
.ContainerId(containerId)
388+
.ContainerId(localContainerId)
384389
.Build());
385-
TaskCompletionSource<bool> twoRecoveryEventsSeenTcs = CreateTaskCompletionSource();
390+
TaskCompletionSource<bool> twoRecoveryEventsSeenTcs = CreateTaskCompletionSource<bool>();
386391
int recoveryEvents = 0;
387392
connection.ChangeState += (sender, from, to, error) =>
388393
{
@@ -410,7 +415,7 @@ public async Task RecoveryTopologyShouldRecoverBindings(bool topologyEnabled)
410415
await SystemUtils.DeleteExchangeAsync("exchange-should-recover-binding");
411416

412417
// The queue will be deleted due of the auto-delete flag
413-
await SystemUtils.WaitUntilConnectionIsKilled(containerId);
418+
await SystemUtils.WaitUntilConnectionIsKilled(localContainerId);
414419
await twoRecoveryEventsSeenTcs.Task.WaitAsync(TimeSpan.FromSeconds(10));
415420

416421
if (topologyEnabled)
@@ -453,13 +458,14 @@ await SystemUtils.WaitUntilBindingsBetweenExchangeAndQueueDontExistAsync("exchan
453458
[Fact]
454459
public async Task RemoveAQueueShouldRemoveTheBindings()
455460
{
456-
const string containerId = nameof(RemoveAQueueShouldRemoveTheBindings);
461+
string localContainerId = $"{_containerId}_remove-queue-should-remove-binding-connection-name";
462+
457463
IConnection connection = await AmqpConnection.CreateAsync(
458464
ConnectionSettingBuilder.Create()
459465
.RecoveryConfiguration(RecoveryConfiguration.Create()
460466
.BackOffDelayPolicy(new FakeFastBackOffDelay())
461467
.Topology(true))
462-
.ContainerId(containerId)
468+
.ContainerId(localContainerId)
463469
.Build());
464470

465471
IManagement management = connection.Management();
@@ -513,13 +519,13 @@ await SystemUtils.WaitUntilBindingsBetweenExchangeAndQueueDontExistAsync("e-remo
513519
[Fact]
514520
public async Task RemoveAnExchangeShouldRemoveTheBindings()
515521
{
516-
const string containerId = "remove-exchange-should-remove-binding-connection-name";
522+
string localContainerId = $"{_containerId}_remove-exchange-should-remove-binding-connection-name";
517523
var connection = await AmqpConnection.CreateAsync(
518524
ConnectionSettingBuilder.Create()
519525
.RecoveryConfiguration(RecoveryConfiguration.Create()
520526
.BackOffDelayPolicy(new FakeFastBackOffDelay())
521527
.Topology(true))
522-
.ContainerId(containerId)
528+
.ContainerId(localContainerId)
523529
.Build());
524530

525531
IManagement management = connection.Management();
@@ -578,13 +584,13 @@ await SystemUtils.WaitUntilBindingsBetweenExchangeAndQueueDontExistAsync(
578584
[Fact]
579585
public async Task RemoveAnExchangeBoundToAnotherExchangeShouldRemoveTheBindings()
580586
{
581-
const string containerId = nameof(RemoveAnExchangeBoundToAnotherExchangeShouldRemoveTheBindings);
587+
string localContainerId = $"{_containerId}_remove-exchange-bound-to-another-exchange-should-remove-binding-connection-name";
582588
var connection = await AmqpConnection.CreateAsync(
583589
ConnectionSettingBuilder.Create()
584590
.RecoveryConfiguration(RecoveryConfiguration.Create()
585591
.BackOffDelayPolicy(new FakeFastBackOffDelay())
586592
.Topology(true))
587-
.ContainerId(containerId)
593+
.ContainerId(localContainerId)
588594
.Build());
589595

590596
IManagement management = connection.Management();
@@ -595,7 +601,8 @@ public async Task RemoveAnExchangeBoundToAnotherExchangeShouldRemoveTheBindings(
595601

596602
await exSpec.DeclareAsync();
597603

598-
var exSpecDestination = management.Exchange().Name("e-remove-exchange-bound-to-another-exchange-should-remove-binding-destination")
604+
var exSpecDestination = management.Exchange()
605+
.Name("e-remove-exchange-bound-to-another-exchange-should-remove-binding-destination")
599606
.Type(ExchangeType.DIRECT);
600607

601608
await exSpecDestination.DeclareAsync();
@@ -606,7 +613,8 @@ await management.Binding().SourceExchange(exSpec)
606613
.DestinationExchange(exSpecDestination).Key($"key_{i}").BindAsync();
607614
}
608615

609-
await SystemUtils.WaitUntilBindingsBetweenExchangeAndExchangeExistAsync("e-remove-exchange-bound-to-another-exchange-should-remove-binding",
616+
await SystemUtils.WaitUntilBindingsBetweenExchangeAndExchangeExistAsync(
617+
"e-remove-exchange-bound-to-another-exchange-should-remove-binding",
610618
"e-remove-exchange-bound-to-another-exchange-should-remove-binding-destination");
611619

612620
Assert.Equal(10, topologyListener.BindingCount());
@@ -616,16 +624,12 @@ await SystemUtils.WaitUntilBindingsBetweenExchangeAndExchangeExistAsync("e-remov
616624

617625
await exSpec.DeleteAsync();
618626

619-
await SystemUtils.WaitUntilBindingsBetweenExchangeAndExchangeDontExistAsync("e-remove-exchange-bound-to-another-exchange-should-remove-binding",
627+
await SystemUtils.WaitUntilBindingsBetweenExchangeAndExchangeDontExistAsync(
628+
"e-remove-exchange-bound-to-another-exchange-should-remove-binding",
620629
"e-remove-exchange-bound-to-another-exchange-should-remove-binding-destination");
621630

622631
Assert.Equal(0, topologyListener.ExchangeCount());
623632

624633
await connection.CloseAsync();
625634
}
626-
627-
private static TaskCompletionSource<bool> CreateTaskCompletionSource()
628-
{
629-
return new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
630-
}
631635
}

Tests/IntegrationTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ public IntegrationTest(ITestOutputHelper testOutputHelper,
3434
{
3535
_testOutputHelper = testOutputHelper;
3636
_setupConnectionAndManagement = setupConnectionAndManagement;
37-
3837
_queueName = $"{_testDisplayName}-queue-{Now}";
3938
_exchangeName = $"{_testDisplayName}-exchange-{Now}";
4039

4140
_testDisplayName = InitTestDisplayName();
4241
_containerId = $"{_testDisplayName}:{Now}";
4342

43+
testOutputHelper.WriteLine($"Running test: {_testDisplayName}");
4444
_connectionSettingBuilder = InitConnectionSettingsBuilder();
4545
}
4646

@@ -63,6 +63,7 @@ public virtual async Task InitializeAsync()
6363

6464
public virtual async Task DisposeAsync()
6565
{
66+
_testOutputHelper.WriteLine($"Disposing test: {_testDisplayName}");
6667
if (_management is not null && _management.State == State.Open)
6768
{
6869
try
@@ -72,6 +73,7 @@ public virtual async Task DisposeAsync()
7273
}
7374
catch
7475
{
76+
7577
}
7678

7779
try

Tests/SystemUtils.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ public static Task WaitUntilConnectionIsClosed(string containerId)
9595
public static async Task WaitUntilConnectionIsKilled(string containerId)
9696
{
9797
await WaitUntilConnectionIsOpen(containerId);
98-
await WaitUntilAsync(async () => await s_httpApiClient.KillConnectionAsync(containerId) == 1);
98+
await WaitUntilAsync(async () =>
99+
100+
await s_httpApiClient.KillConnectionAsync(containerId) == 1);
99101
}
100102

101103
public static async Task WaitUntilConnectionIsKilledAndOpen(string containerId)

0 commit comments

Comments
 (0)