Skip to content

Commit 9ce2745

Browse files
authored
[tests] add graceful shutdown to mock collectors listener (#4847)
add graceful shutdown in test server, fix received log records count logging
1 parent 53c0769 commit 9ce2745

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

test/IntegrationTests/Helpers/MockLogsCollector.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal sealed class MockLogsCollector : IDisposable
2121
private readonly TestHttpServer _listener;
2222
private readonly BlockingCollection<LogRecord> _logs = new(100); // bounded to avoid memory leak
2323
private readonly List<Expectation> _expectations = new();
24+
private int _logsReceived;
2425

2526
private CollectedExpectation? _collectedExpectation;
2627

@@ -44,10 +45,10 @@ public MockLogsCollector(ITestOutputHelper output, string host = "localhost")
4445

4546
public void Dispose()
4647
{
47-
WriteOutput($"Shutting down. Total logs requests received: '{_logs.Count}'");
48+
_listener.Dispose();
49+
WriteOutput($"Shutting down. Total logs received: '{_logsReceived}'");
4850
ResourceExpector.Dispose();
4951
_logs.Dispose();
50-
_listener.Dispose();
5152
}
5253

5354
public void Expect(Func<LogRecord, bool> predicate, string? description = null)
@@ -213,6 +214,7 @@ private void HandleLogsMessage(ExportLogsServiceRequest logsMessage)
213214
{
214215
foreach (var logRecord in scopeLogs.LogRecords ?? Enumerable.Empty<LogRecord>())
215216
{
217+
Interlocked.Increment(ref _logsReceived);
216218
_logs.Add(logRecord);
217219
}
218220
}

test/IntegrationTests/Helpers/TestHttpServer.AspNetCore.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public TestHttpServer(ITestOutputHelper output, string name, params PathHandler[
4242
x.Run(pathHandler.Delegate);
4343
});
4444
}
45-
});
45+
})
46+
.UseShutdownTimeout(TimeSpan.FromSeconds(5));
4647
})
4748
.Build();
4849

@@ -69,7 +70,8 @@ public static TestHttpServer CreateDefaultTestServer(ITestOutputHelper output)
6970

7071
public void Dispose()
7172
{
72-
WriteOutput($"Shutting down");
73+
WriteOutput("Shutting down");
74+
_listener.StopAsync().GetAwaiter().GetResult();
7375
_listener.Dispose();
7476
}
7577

0 commit comments

Comments
 (0)