-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Test cases for EndpointUtilty, LifetimeManager and ServiceConnect…
…ion (#133) * Add EndpointUtilty Test * Add tests for lifetimeManager * Add LifetimeManager IT * Format the codes * Format codes * Remove mock and use TestClass instead. * Add a few reconnection test * Add message fieds verification * Add reconnect test * Update reconnection test * Update reconnection when connection throws exception * Update reconnection logic * Format codes * Add keepalive reconnect * Fix typo * fix merge conflict * clean up test infrastructure * PR comments * fix typo
- Loading branch information
Showing
11 changed files
with
742 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
test/Microsoft.Azure.SignalR.Tests/Infrastructure/HandshakeUtils.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// 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.IO; | ||
using System.IO.Pipelines; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.SignalR.Protocol; | ||
|
||
namespace Microsoft.Azure.SignalR.Tests | ||
{ | ||
public static class HandshakeUtils | ||
{ | ||
private static readonly TimeSpan DefaultHandshakeTimeout = TimeSpan.FromSeconds(5); | ||
private static readonly IServiceProtocol ServiceProtocol = new ServiceProtocol(); | ||
|
||
public static async Task ReceiveHandshakeRequestAsync(PipeReader input) | ||
{ | ||
using (var cts = new CancellationTokenSource(DefaultHandshakeTimeout)) | ||
{ | ||
while (true) | ||
{ | ||
var result = await input.ReadAsync(cts.Token); | ||
|
||
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; | ||
|
||
if (!(message is HandshakeRequestMessage handshakeRequest)) | ||
{ | ||
throw new InvalidDataException( | ||
$"{message.GetType().Name} received when waiting for handshake request."); | ||
} | ||
|
||
if (handshakeRequest.Version != ServiceProtocol.Version) | ||
{ | ||
throw new InvalidDataException("Protocol version not supported."); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
if (result.IsCompleted) | ||
{ | ||
// Not enough data, and we won't be getting any more data. | ||
throw new InvalidOperationException( | ||
"Service connectioned disconnected before sending a handshake request"); | ||
} | ||
} | ||
finally | ||
{ | ||
input.AdvanceTo(consumed, examined); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static Task SendHandshakeResponseAsync(PipeWriter output, HandshakeResponseMessage response = null) | ||
{ | ||
ServiceProtocol.WriteMessage(response ?? new HandshakeResponseMessage(), output); | ||
return output.FlushAsync().AsTask(); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
test/Microsoft.Azure.SignalR.Tests/Infrastructure/PipeReaderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.