Skip to content

Plumb a clock interface through SignalR for testing #19311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ public HttpConnectionContext(string connectionId, string connectionToken, ILogge
Features.Set<IConnectionInherentKeepAliveFeature>(this);
}

internal HttpConnectionContext(string id, IDuplexPipe transport, IDuplexPipe application, ILogger logger = null)
: this(id, null, logger)
{
Transport = transport;
Application = application;
}

public CancellationTokenSource Cancellation { get; set; }

public HttpTransportType TransportType { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Net.WebSockets;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Internal;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Internal;
Expand All @@ -31,24 +30,14 @@ internal partial class HttpConnectionManager
private readonly TimerAwaitable _nextHeartbeat;
private readonly ILogger<HttpConnectionManager> _logger;
private readonly ILogger<HttpConnectionContext> _connectionLogger;
private readonly bool _useSendTimeout = true;
private readonly TimeSpan _disconnectTimeout;

public HttpConnectionManager(ILoggerFactory loggerFactory, IHostApplicationLifetime appLifetime)
: this(loggerFactory, appLifetime, Options.Create(new ConnectionOptions() { DisconnectTimeout = ConnectionOptionsSetup.DefaultDisconectTimeout }))
{
}

public HttpConnectionManager(ILoggerFactory loggerFactory, IHostApplicationLifetime appLifetime, IOptions<ConnectionOptions> connectionOptions)
{
_logger = loggerFactory.CreateLogger<HttpConnectionManager>();
_connectionLogger = loggerFactory.CreateLogger<HttpConnectionContext>();
_nextHeartbeat = new TimerAwaitable(_heartbeatTickRate, _heartbeatTickRate);
_disconnectTimeout = connectionOptions.Value.DisconnectTimeout ?? ConnectionOptionsSetup.DefaultDisconectTimeout;
if (AppContext.TryGetSwitch("Microsoft.AspNetCore.Http.Connections.DoNotUseSendTimeout", out var timeoutDisabled))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove the switch?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was only for patch branches

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we do this in other places too. Like MVC

{
_useSendTimeout = !timeoutDisabled;
}

// Register these last as the callbacks could run immediately
appLifetime.ApplicationStarted.Register(() => Start());
Expand Down Expand Up @@ -176,7 +165,7 @@ public void Scan()
}
else
{
if (!Debugger.IsAttached && _useSendTimeout)
if (!Debugger.IsAttached)
{
connection.TryCancelSend(utcNow.Ticks);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2347,10 +2347,10 @@ private static void SetTransport(HttpContext context, HttpTransportType transpor

private static HttpConnectionManager CreateConnectionManager(ILoggerFactory loggerFactory)
{
return new HttpConnectionManager(loggerFactory ?? new LoggerFactory(), new EmptyApplicationLifetime());
return CreateConnectionManager(loggerFactory, null);
}

private static HttpConnectionManager CreateConnectionManager(ILoggerFactory loggerFactory, TimeSpan disconnectTimeout)
private static HttpConnectionManager CreateConnectionManager(ILoggerFactory loggerFactory, TimeSpan? disconnectTimeout)
{
var connectionOptions = new ConnectionOptions();
connectionOptions.DisconnectTimeout = disconnectTimeout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Connections.Internal;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.SignalR.Tests;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -411,7 +412,7 @@ public async Task ApplicationLifetimeCanStartBeforeHttpConnectionManagerInitiali
private static HttpConnectionManager CreateConnectionManager(ILoggerFactory loggerFactory, IHostApplicationLifetime lifetime = null)
{
lifetime = lifetime ?? new EmptyApplicationLifetime();
return new HttpConnectionManager(loggerFactory, lifetime);
return new HttpConnectionManager(loggerFactory, lifetime, Options.Create(new ConnectionOptions()));
}

[Flags]
Expand Down
60 changes: 48 additions & 12 deletions src/SignalR/common/Http.Connections/test/WebSocketsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ public async Task ReceivedFramesAreWrittenToChannel(string webSocketMessageType)
using (StartVerifiableLog())
{
var pair = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);
var connection = new HttpConnectionContext("foo", pair.Transport, pair.Application, LoggerFactory.CreateLogger("HttpConnectionContext1"));
var connection = new HttpConnectionContext("foo", connectionToken: null, LoggerFactory.CreateLogger("HttpConnectionContext1"))
{
Transport = pair.Transport,
Application = pair.Application,
};

using (var feature = new TestWebSocketConnectionFeature())
{
var connectionContext = new HttpConnectionContext(string.Empty, null, null, LoggerFactory.CreateLogger("HttpConnectionContext2"));
var connectionContext = new HttpConnectionContext(string.Empty, connectionToken: null, LoggerFactory.CreateLogger("HttpConnectionContext2"));
var ws = new WebSocketsServerTransport(new WebSocketOptions(), connection.Application, connectionContext, LoggerFactory);

// Give the server socket to the transport and run it
Expand Down Expand Up @@ -79,11 +83,15 @@ public async Task WebSocketTransportSetsMessageTypeBasedOnTransferFormatFeature(
using (StartVerifiableLog())
{
var pair = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);
var connection = new HttpConnectionContext("foo", pair.Transport, pair.Application, LoggerFactory.CreateLogger("HttpConnectionContext1"));
var connection = new HttpConnectionContext("foo", connectionToken: null, LoggerFactory.CreateLogger("HttpConnectionContext1"))
{
Transport = pair.Transport,
Application = pair.Application,
};

using (var feature = new TestWebSocketConnectionFeature())
{
var connectionContext = new HttpConnectionContext(string.Empty, null, null, LoggerFactory.CreateLogger("HttpConnectionContext2"));
var connectionContext = new HttpConnectionContext(string.Empty, connectionToken: null, LoggerFactory.CreateLogger("HttpConnectionContext2"));
connectionContext.ActiveFormat = transferFormat;
var ws = new WebSocketsServerTransport(new WebSocketOptions(), connection.Application, connectionContext, LoggerFactory);

Expand Down Expand Up @@ -116,7 +124,11 @@ public async Task TransportCommunicatesErrorToApplicationWhenClientDisconnectsAb
using (StartVerifiableLog())
{
var pair = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);
var connection = new HttpConnectionContext("foo", pair.Transport, pair.Application, LoggerFactory.CreateLogger("HttpConnectionContext1"));
var connection = new HttpConnectionContext("foo", connectionToken: null, LoggerFactory.CreateLogger("HttpConnectionContext1"))
{
Transport = pair.Transport,
Application = pair.Application,
};

using (var feature = new TestWebSocketConnectionFeature())
{
Expand All @@ -139,7 +151,7 @@ async Task CompleteApplicationAfterTransportCompletes()
}
}

var connectionContext = new HttpConnectionContext(string.Empty, null, null, LoggerFactory.CreateLogger("HttpConnectionContext2"));
var connectionContext = new HttpConnectionContext(string.Empty, connectionToken: null, LoggerFactory.CreateLogger("HttpConnectionContext2"));
var ws = new WebSocketsServerTransport(new WebSocketOptions(), connection.Application, connectionContext, LoggerFactory);

// Give the server socket to the transport and run it
Expand Down Expand Up @@ -169,7 +181,11 @@ public async Task ClientReceivesInternalServerErrorWhenTheApplicationFails()
using (StartVerifiableLog())
{
var pair = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);
var connection = new HttpConnectionContext("foo", pair.Transport, pair.Application);
var connection = new HttpConnectionContext("foo", connectionToken: null, LoggerFactory.CreateLogger(nameof(HttpConnectionContext)))
{
Transport = pair.Transport,
Application = pair.Application,
};

using (var feature = new TestWebSocketConnectionFeature())
{
Expand Down Expand Up @@ -201,7 +217,11 @@ public async Task TransportClosesOnCloseTimeoutIfClientDoesNotSendCloseFrame()
using (StartVerifiableLog())
{
var pair = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);
var connection = new HttpConnectionContext("foo", pair.Transport, pair.Application);
var connection = new HttpConnectionContext("foo", connectionToken: null, LoggerFactory.CreateLogger(nameof(HttpConnectionContext)))
{
Transport = pair.Transport,
Application = pair.Application,
};

using (var feature = new TestWebSocketConnectionFeature())
{
Expand Down Expand Up @@ -236,7 +256,11 @@ public async Task TransportFailsOnTimeoutWithErrorWhenApplicationFailsAndClientD
using (StartVerifiableLog())
{
var pair = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);
var connection = new HttpConnectionContext("foo", pair.Transport, pair.Application);
var connection = new HttpConnectionContext("foo", connectionToken: null, LoggerFactory.CreateLogger(nameof(HttpConnectionContext)))
{
Transport = pair.Transport,
Application = pair.Application,
};

using (var feature = new TestWebSocketConnectionFeature())
{
Expand Down Expand Up @@ -271,7 +295,11 @@ public async Task ServerGracefullyClosesWhenApplicationEndsThenClientSendsCloseF
using (StartVerifiableLog())
{
var pair = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);
var connection = new HttpConnectionContext("foo", pair.Transport, pair.Application);
var connection = new HttpConnectionContext("foo", connectionToken: null, LoggerFactory.CreateLogger(nameof(HttpConnectionContext)))
{
Transport = pair.Transport,
Application = pair.Application,
};

using (var feature = new TestWebSocketConnectionFeature())
{
Expand Down Expand Up @@ -311,7 +339,11 @@ public async Task ServerGracefullyClosesWhenClientSendsCloseFrameThenApplication
using (StartVerifiableLog())
{
var pair = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);
var connection = new HttpConnectionContext("foo", pair.Transport, pair.Application);
var connection = new HttpConnectionContext("foo", connectionToken: null, LoggerFactory.CreateLogger(nameof(HttpConnectionContext)))
{
Transport = pair.Transport,
Application = pair.Application,
};

using (var feature = new TestWebSocketConnectionFeature())
{
Expand Down Expand Up @@ -354,7 +386,11 @@ public async Task SubProtocolSelectorIsUsedToSelectSubProtocol()
using (StartVerifiableLog())
{
var pair = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);
var connection = new HttpConnectionContext("foo", pair.Transport, pair.Application);
var connection = new HttpConnectionContext("foo", connectionToken: null, LoggerFactory.CreateLogger(nameof(HttpConnectionContext)))
{
Transport = pair.Transport,
Application = pair.Application,
};

using (var feature = new TestWebSocketConnectionFeature())
{
Expand Down
26 changes: 26 additions & 0 deletions src/SignalR/common/Shared/ISystemClock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.AspNetCore.Internal
{
internal interface ISystemClock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we share this stuff with Kestrel?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, I did just copy it from Kestrel.

{
/// <summary>
/// Retrieves the current UTC system time.
/// </summary>
DateTimeOffset UtcNow { get; }

/// <summary>
/// Retrieves ticks for the current UTC system time.
/// </summary>
long UtcNowTicks { get; }

/// <summary>
/// Retrieves the current UTC system time.
/// This is only safe to use from code called by the Heartbeat.
/// </summary>
DateTimeOffset UtcNowUnsynchronized { get; }
}
}
28 changes: 28 additions & 0 deletions src/SignalR/common/Shared/SystemClock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.AspNetCore.Internal
{
/// <summary>
/// Provides access to the normal system clock.
/// </summary>
internal class SystemClock : ISystemClock
{
/// <summary>
/// Retrieves the current UTC system time.
/// </summary>
public DateTimeOffset UtcNow => DateTimeOffset.UtcNow;

/// <summary>
/// Retrieves ticks for the current UTC system time.
/// </summary>
public long UtcNowTicks => DateTimeOffset.UtcNow.Ticks;

/// <summary>
/// Retrieves the current UTC system time.
/// </summary>
public DateTimeOffset UtcNowUnsynchronized => DateTimeOffset.UtcNow;
}
}
Loading