From b7c178b85c827e0c9d63973cc5d4571d7a01e062 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Sat, 14 Nov 2020 15:30:57 +0100 Subject: [PATCH] Fix Unit Tests. --- .../Mockups/TestEnvironment.cs | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/Tests/MQTTnet.Core.Tests/Mockups/TestEnvironment.cs b/Tests/MQTTnet.Core.Tests/Mockups/TestEnvironment.cs index 8a36cd1..1fc2508 100644 --- a/Tests/MQTTnet.Core.Tests/Mockups/TestEnvironment.cs +++ b/Tests/MQTTnet.Core.Tests/Mockups/TestEnvironment.cs @@ -105,7 +105,7 @@ namespace MQTTnet.Tests.Mockups Server = new TestServerWrapper(_mqttFactory.CreateMqttServer(ServerLogger), TestContext, this); options.WithDefaultEndpointPort(ServerPort); - + await Server.StartAsync(options.Build()).ConfigureAwait(false); return Server; @@ -118,7 +118,7 @@ namespace MQTTnet.Tests.Mockups public Task ConnectLowLevelClientAsync() { - return ConnectLowLevelClientAsync(o => {}); + return ConnectLowLevelClientAsync(o => { }); } public async Task ConnectLowLevelClientAsync(Action optionsBuilder) @@ -160,7 +160,7 @@ namespace MQTTnet.Tests.Mockups return client; } - + public async Task ConnectClientAsync(IMqttClientOptions options) { if (options == null) throw new ArgumentNullException(nameof(options)); @@ -170,7 +170,7 @@ namespace MQTTnet.Tests.Mockups return client; } - + public void ThrowIfLogErrors() { lock (_serverErrors) @@ -198,7 +198,7 @@ namespace MQTTnet.Tests.Mockups { mqttClient.DisconnectAsync().GetAwaiter().GetResult(); } - catch (ObjectDisposedException) + catch { // This can happen when the test already disconnected the client. } @@ -208,8 +208,18 @@ namespace MQTTnet.Tests.Mockups } } - Server?.StopAsync().GetAwaiter().GetResult(); - Server?.Dispose(); + try + { + Server?.StopAsync().GetAwaiter().GetResult(); + } + catch + { + // This can happen when the test already stopped the server. + } + finally + { + Server?.Dispose(); + } ThrowIfLogErrors();