-
Notifications
You must be signed in to change notification settings - Fork 95
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
Add Test cases for EndpointUtilty, LifetimeManager and ServiceConnection #133
Changes from 15 commits
e8ee9d3
29fa032
ba2c6c4
a49b27b
3c4bfd0
333a351
21cc5b0
4d6dbc0
b22ab60
1996838
5ce9885
e5562aa
f2320d1
ba520a2
0cc5010
ca24f3b
19c3d2b
7a323e1
fc11bfa
2350479
eaf9791
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.SignalR.Protocol; | ||
|
||
namespace Microsoft.Azure.SignalR.Tests.Infrastructure | ||
{ | ||
interface IHandshackMessageFactory | ||
{ | ||
ServiceMessage GetHandshackResposeMessage(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Connections; | ||
using Microsoft.Azure.SignalR.Protocol; | ||
using Microsoft.Azure.SignalR.Tests.Infrastructure; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using HandshakeRequestMessage = Microsoft.Azure.SignalR.Protocol.HandshakeRequestMessage; | ||
using HandshakeResponseMessage = Microsoft.Azure.SignalR.Protocol.HandshakeResponseMessage; | ||
|
@@ -21,25 +22,36 @@ internal class ServiceConnectionProxy : IClientConnectionManager, IClientConnect | |
private static readonly IServiceProtocol _serviceProtocol = new ServiceProtocol(); | ||
private readonly PipeOptions _clientPipeOptions; | ||
|
||
private IConnectionFactory ConnectionFactory { get; } | ||
public IHandshackMessageFactory HandshackMessageFactory { get; } | ||
|
||
public IConnectionFactory ConnectionFactory { get; } | ||
|
||
public IClientConnectionManager ClientConnectionManager { get; } | ||
|
||
public TestConnection ConnectionContext { get; } | ||
|
||
private ServiceConnection ServiceConnection { get; } | ||
public ServiceConnection ServiceConnection { get; } | ||
|
||
public ConcurrentDictionary<string, ServiceConnectionContext> ClientConnections => ClientConnectionManager.ClientConnections; | ||
|
||
private readonly ConcurrentDictionary<string, TaskCompletionSource<ConnectionContext>> _waitForConnectionOpen = new ConcurrentDictionary<string, TaskCompletionSource<ConnectionContext>>(); | ||
private readonly ConcurrentDictionary<string, TaskCompletionSource<object>> _waitForConnectionClose = new ConcurrentDictionary<string, TaskCompletionSource<object>>(); | ||
private readonly ConcurrentDictionary<int, TaskCompletionSource<ConnectionContext>> _waitForServerConnection = new ConcurrentDictionary<int, TaskCompletionSource<ConnectionContext>>(); | ||
private readonly ConcurrentDictionary<Type, TaskCompletionSource<ServiceMessage>> _waitForMessage = new ConcurrentDictionary<Type, TaskCompletionSource<ServiceMessage>>(); | ||
|
||
private int _serverConnectionCount = 0; | ||
|
||
public ServiceConnectionProxy(ConnectionDelegate callback = null, PipeOptions clientPipeOptions = null) | ||
public ServiceConnectionProxy(ConnectionDelegate callback = null, | ||
PipeOptions clientPipeOptions = null, | ||
TestConnection connectionContext = null, | ||
IConnectionFactory connectionFactory = null, | ||
IHandshackMessageFactory handshackMessageFactory = null) | ||
{ | ||
ConnectionContext = new TestConnection(); | ||
ConnectionFactory = new TestConnectionFactory(ConnectionContext); | ||
ConnectionContext = connectionContext ?? new TestConnection(); | ||
ConnectionFactory = connectionFactory ?? new TestConnectionFactory(ConnectionContext, this); | ||
ClientConnectionManager = new ClientConnectionManager(); | ||
_clientPipeOptions = clientPipeOptions; | ||
HandshackMessageFactory = handshackMessageFactory ?? new TestHandshackMessageFactory(); | ||
|
||
ServiceConnection = new ServiceConnection( | ||
_serviceProtocol, | ||
|
@@ -53,8 +65,15 @@ public ServiceConnectionProxy(ConnectionDelegate callback = null, PipeOptions cl | |
|
||
public Task StartAsync() | ||
{ | ||
_ = ServiceConnection.StartAsync(); | ||
return HandshakeAsync(); | ||
return ServiceConnection.StartAsync(); | ||
} | ||
|
||
public Task ProcessIncomingAsync() | ||
{ | ||
using (var processIncomingCts = new CancellationTokenSource(DefaultHandshakeTimeout)) | ||
{ | ||
return ProcessIncomingCoreAsync(ConnectionContext.Application.Input, processIncomingCts.Token); | ||
} | ||
} | ||
|
||
public void Stop() | ||
|
@@ -78,6 +97,17 @@ public Task WaitForConnectionCloseAsync(string connectionId) | |
return _waitForConnectionClose.GetOrAdd(connectionId, key => new TaskCompletionSource<object>()).Task; | ||
} | ||
|
||
public Task<ServiceMessage> WaitForMessageAsync(Type type) | ||
{ | ||
return _waitForMessage.GetOrAdd(type, key => new TaskCompletionSource<ServiceMessage>()).Task; | ||
} | ||
|
||
public Task<ConnectionContext> WaitForServerConnectionAsync(int connectionCount) | ||
{ | ||
return _waitForServerConnection.GetOrAdd(connectionCount, key => new TaskCompletionSource<ConnectionContext>()) | ||
.Task; | ||
} | ||
|
||
private Task OnConnectionAsync(ConnectionContext connection) | ||
{ | ||
var tcs = new TaskCompletionSource<object>(); | ||
|
@@ -112,14 +142,33 @@ public void RemoveClientConnection(string connectionId) | |
} | ||
} | ||
|
||
private async Task HandshakeAsync() | ||
public void AddServerConnection() | ||
{ | ||
Interlocked.Increment(ref _serverConnectionCount); | ||
|
||
if (_waitForServerConnection.TryGetValue(_serverConnectionCount, out var tcs)) | ||
{ | ||
tcs.TrySetResult(null); | ||
} | ||
} | ||
|
||
public void AddMessage(Type type, ServiceMessage message) | ||
{ | ||
if (_waitForMessage.TryGetValue(type, out var tcs)) | ||
{ | ||
tcs.TrySetResult(message); | ||
} | ||
} | ||
|
||
public async Task HandshakeAsync() | ||
{ | ||
using (var handshakeCts = new CancellationTokenSource(DefaultHandshakeTimeout)) | ||
{ | ||
await ReceiveHandshakeRequestAsync(ConnectionContext.Application.Input, handshakeCts.Token); | ||
} | ||
|
||
await WriteMessageAsync(new HandshakeResponseMessage()); | ||
await WriteMessageAsync(HandshackMessageFactory.GetHandshackResposeMessage()); | ||
AddServerConnection(); | ||
} | ||
|
||
private async Task ReceiveHandshakeRequestAsync(PipeReader input, CancellationToken cancellationToken) | ||
|
@@ -170,6 +219,41 @@ private async Task ReceiveHandshakeRequestAsync(PipeReader input, CancellationTo | |
} | ||
} | ||
|
||
private async Task ProcessIncomingCoreAsync(PipeReader input, CancellationToken cancellationToken) | ||
{ | ||
while (true) | ||
{ | ||
var result = await input.ReadAsync(cancellationToken); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. break when |
||
var buffer = result.Buffer; | ||
|
||
var consumed = buffer.Start; | ||
var examined = buffer.End; | ||
|
||
try | ||
{ | ||
if (!buffer.IsEmpty) | ||
{ | ||
if (_serviceProtocol.TryParseMessage(ref buffer, out var message)) | ||
{ | ||
consumed = buffer.Start; | ||
examined = consumed; | ||
|
||
AddMessage(message.GetType(), message); | ||
} | ||
} | ||
|
||
if (result.IsCompleted) | ||
{ | ||
break; | ||
} | ||
} | ||
finally | ||
{ | ||
input.AdvanceTo(consumed, examined); | ||
} | ||
} | ||
} | ||
|
||
public ServiceConnectionContext CreateConnection(OpenConnectionMessage message) | ||
{ | ||
return new ServiceConnectionContext(message, _clientPipeOptions, _clientPipeOptions); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Microsoft.Azure.SignalR.Tests.Infrastructure | ||
{ | ||
class TestClientConnectionManager : IClientConnectionManager | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can just use |
||
{ | ||
public void AddClientConnection(ServiceConnectionContext clientConnection) | ||
{ | ||
} | ||
|
||
public void RemoveClientConnection(string connectionId) | ||
{ | ||
} | ||
|
||
public ConcurrentDictionary<string, ServiceConnectionContext> ClientConnections { get; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.Azure.SignalR.Protocol; | ||
|
||
namespace Microsoft.Azure.SignalR.Tests.Infrastructure | ||
{ | ||
class TestHandshackMessageFactory : IHandshackMessageFactory | ||
{ | ||
private readonly string _errorMessage; | ||
|
||
public TestHandshackMessageFactory(string errorMessage = null) | ||
{ | ||
_errorMessage = errorMessage ?? string.Empty; | ||
} | ||
|
||
public ServiceMessage GetHandshackResposeMessage() | ||
{ | ||
return new HandshakeResponseMessage(_errorMessage); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.AspNetCore.SignalR; | ||
|
||
namespace Microsoft.Azure.SignalR.Tests.Infrastructure | ||
{ | ||
public class TestHub : Hub | ||
{ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.SignalR; | ||
using Microsoft.Azure.SignalR.Protocol; | ||
|
||
namespace Microsoft.Azure.SignalR.Tests.Infrastructure | ||
{ | ||
class TestServiceConnectionManager<THub> : IServiceConnectionManager<THub> where THub : Hub | ||
{ | ||
private readonly ConcurrentDictionary<Type, int> _writeAsyncCallCount = new ConcurrentDictionary<Type, int>(); | ||
|
||
public ServiceMessage ServiceMessage { get; private set; } | ||
|
||
public void AddServiceConnection(ServiceConnection serviceConnection) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line between fields and method. |
||
{ | ||
} | ||
|
||
public async Task StartAsync() | ||
{ | ||
await Task.CompletedTask; | ||
} | ||
|
||
public async Task WriteAsync(ServiceMessage serviceMessage) | ||
{ | ||
_writeAsyncCallCount.AddOrUpdate(serviceMessage.GetType(), 1, (_, value) => value + 1); | ||
ServiceMessage = serviceMessage; | ||
await Task.CompletedTask; | ||
} | ||
|
||
public int GetCallCount(Type type) | ||
{ | ||
if (_writeAsyncCallCount.TryGetValue(type, out var count)) | ||
{ | ||
return count; | ||
} | ||
|
||
return 0; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handshack
->Handshake
. Update all of them in changed files.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Respose
->Response