Skip to content

Commit 855316d

Browse files
authored
Dispose should close the server connection (#1960)
* Dispose should close the server connection * Fix tests * Disable flacky tests
1 parent 53f7d05 commit 855316d

File tree

5 files changed

+17
-12
lines changed

5 files changed

+17
-12
lines changed

src/Microsoft.Azure.SignalR.AspNet/ServerConnections/ServiceConnectionManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public Task StopGetServersPing()
134134

135135
public void Dispose()
136136
{
137+
StopAsync().GetAwaiter().GetResult();
137138
}
138139

139140
private async Task ConnectionInitializedAsync()

src/Microsoft.Azure.SignalR.Common/ServiceConnections/ServiceConnectionContainerBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ public Task StopGetServersPing()
261261
// Ready for scalable containers
262262
public void Dispose()
263263
{
264+
StopAsync().GetAwaiter().GetResult();
264265
_statusPing.Dispose();
265266
_serversPing.Dispose();
266267
Dispose(true);
@@ -726,7 +727,7 @@ private static class Log
726727
LoggerMessage.Define<ServiceEndpoint, string>(LogLevel.Debug, new EventId(9, "ReceivedServersTagPing"), "Received a servers tag ping from {endpoint} for hub {hub}.");
727728

728729
private static readonly Action<ILogger, string, Exception> _timerAlreadyStopped =
729-
LoggerMessage.Define<string>(LogLevel.Warning, new EventId(10, "TimerAlreadyStopped"), "Failed to stop {pingName} timer as it's not started");
730+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(10, "TimerAlreadyStopped"), "Failed to stop {pingName} timer as it's not started");
730731

731732
public static void EndpointOnline(ILogger logger, HubServiceEndpoint endpoint)
732733
{

src/Microsoft.Azure.SignalR/ServerConnections/ServiceConnectionManager.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using Microsoft.AspNetCore.SignalR;
@@ -9,7 +10,7 @@
910

1011
namespace Microsoft.Azure.SignalR
1112
{
12-
internal class ServiceConnectionManager<THub> : IServiceConnectionManager<THub> where THub : Hub
13+
internal class ServiceConnectionManager<THub> : IDisposable, IServiceConnectionManager<THub> where THub : Hub
1314
{
1415
private IServiceConnectionContainer _serviceConnection = null;
1516

@@ -52,5 +53,10 @@ public Task<bool> WriteAckableMessageAsync(ServiceMessage seviceMessage, Cancell
5253

5354
return _serviceConnection.WriteAckableMessageAsync(seviceMessage, cancellationToken);
5455
}
56+
57+
public void Dispose()
58+
{
59+
StopAsync().GetAwaiter().GetResult();
60+
}
5561
}
5662
}

test/Microsoft.Azure.SignalR.Common.Tests/CustomizedTimerTests.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Testing.xunit;
89
using Microsoft.Azure.SignalR.Tests.Common;
910
using Microsoft.Extensions.Logging;
1011
using Microsoft.Extensions.Logging.Abstractions;
@@ -62,6 +63,7 @@ await RetryWhenExceptionThrows(async () =>
6263
}
6364

6465
[Fact]
66+
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX, "Flacky tests due to slow machine")]
6567
public async Task StartStopStartStop()
6668
{
6769
using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning))
@@ -84,14 +86,9 @@ public async Task StartStopStartStop()
8486
}
8587

8688
[Fact]
89+
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX, "Flacky tests due to slow machine")]
8790
public async Task StartStopDispose_StartDisposeStop()
8891
{
89-
if (Environment.OSVersion.Platform == PlatformID.Unix)
90-
{
91-
// it will fail in osx in github action, due to slow machine.
92-
// skip it for now.
93-
return;
94-
}
9592
using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning))
9693
{
9794
var callbackCount = 0;

test/Microsoft.Azure.SignalR.Tests/ServiceConnectionContainerBaseTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public ServiceConnectionContainerBaseTests(ITestOutputHelper helper) : base(help
2323
[InlineData(3, 1, 0)]
2424
public async Task TestServersPing(int startCount, int stopCount, int expectedWarn)
2525
{
26-
using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning, logChecker: logs =>
26+
using (StartVerifiableLog(out var loggerFactory, LogLevel.Debug, logChecker: logs =>
2727
{
28-
var warns = logs.Where(s => s.Write.LogLevel == LogLevel.Warning).ToList();
28+
var warns = logs.Where(s => s.Write.EventId.Name == "TimerAlreadyStopped").ToList();
2929
Assert.Equal(expectedWarn, warns.Count);
3030
if (expectedWarn > 0)
3131
{
@@ -84,9 +84,9 @@ await Task.WhenAny(connections.Select(c =>
8484
[InlineData(1, 3, 2, 2, 2)] // first time error stop won't break second time write.
8585
public async Task TestServersPingWorkSecondTime(int firstStart, int firstStop, int secondStart, int secondStop, int expectedWarn)
8686
{
87-
using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning, logChecker: logs =>
87+
using (StartVerifiableLog(out var loggerFactory, LogLevel.Debug, logChecker: logs =>
8888
{
89-
var warns = logs.Where(s => s.Write.LogLevel == LogLevel.Warning).ToList();
89+
var warns = logs.Where(s => s.Write.EventId.Name == "TimerAlreadyStopped").ToList();
9090
Assert.Equal(expectedWarn, warns.Count);
9191
if (expectedWarn > 0)
9292
{

0 commit comments

Comments
 (0)