From 5def499323bf94b320ccafbbcbd4898c1e3c67ab Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Tue, 31 Jan 2017 14:59:21 -0800 Subject: [PATCH] Added Sockets.Common and connection level testing (#173) *Moved IChannelConnection, ChannelConnection, Format and Message to the new Sockets.Common project. * Adding Connection level tests. --- SignalR.sln | 7 +++ samples/ClientSample/RawSample.cs | 2 +- .../Connection.cs | 2 +- .../Message.cs | 25 ---------- .../project.json | 10 +--- .../Format.cs | 2 +- .../IChannelConnection.cs | 0 .../Internal/ChannelConnection.cs | 3 +- .../Message.cs | 6 +++ .../Microsoft.AspNetCore.Sockets.Common.xproj | 21 +++++++++ .../Properties/AssemblyInfo.cs | 12 +++++ .../project.json | 31 +++++++++++++ src/Microsoft.AspNetCore.Sockets/project.json | 11 ++--- .../EndToEndTests.cs | 46 +++++++++++++++++++ .../project.json | 1 + .../LongPollingTransportTests.cs | 2 +- .../project.json | 1 + 17 files changed, 137 insertions(+), 45 deletions(-) delete mode 100644 src/Microsoft.AspNetCore.Sockets.Client/Message.cs rename src/{Microsoft.AspNetCore.Sockets => Microsoft.AspNetCore.Sockets.Common}/Format.cs (79%) rename src/{Microsoft.AspNetCore.Sockets => Microsoft.AspNetCore.Sockets.Common}/IChannelConnection.cs (100%) rename src/{Microsoft.AspNetCore.Sockets => Microsoft.AspNetCore.Sockets.Common}/Internal/ChannelConnection.cs (93%) rename src/{Microsoft.AspNetCore.Sockets => Microsoft.AspNetCore.Sockets.Common}/Message.cs (82%) create mode 100644 src/Microsoft.AspNetCore.Sockets.Common/Microsoft.AspNetCore.Sockets.Common.xproj create mode 100644 src/Microsoft.AspNetCore.Sockets.Common/Properties/AssemblyInfo.cs create mode 100644 src/Microsoft.AspNetCore.Sockets.Common/project.json diff --git a/SignalR.sln b/SignalR.sln index b060d43624..888c50319d 100644 --- a/SignalR.sln +++ b/SignalR.sln @@ -57,6 +57,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Signal EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.SignalR.Client.FunctionalTests", "test\Microsoft.AspNetCore.SignalR.Client.FunctionalTests\Microsoft.AspNetCore.SignalR.Client.FunctionalTests.xproj", "{455B68D2-C5B6-4BF4-A685-964B07AFAAF8}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Sockets.Common", "src\Microsoft.AspNetCore.Sockets.Common\Microsoft.AspNetCore.Sockets.Common.xproj", "{F3EFFD9F-DD85-48A2-9B11-83A133ECC099}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -147,6 +149,10 @@ Global {455B68D2-C5B6-4BF4-A685-964B07AFAAF8}.Debug|Any CPU.Build.0 = Debug|Any CPU {455B68D2-C5B6-4BF4-A685-964B07AFAAF8}.Release|Any CPU.ActiveCfg = Release|Any CPU {455B68D2-C5B6-4BF4-A685-964B07AFAAF8}.Release|Any CPU.Build.0 = Release|Any CPU + {F3EFFD9F-DD85-48A2-9B11-83A133ECC099}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F3EFFD9F-DD85-48A2-9B11-83A133ECC099}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F3EFFD9F-DD85-48A2-9B11-83A133ECC099}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F3EFFD9F-DD85-48A2-9B11-83A133ECC099}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -173,5 +179,6 @@ Global {E37324FF-6BAF-4243-BA80-7C024CF5F29D} = {DA69F624-5398-4884-87E4-B816698CDE65} {354335AB-CEE9-4434-A641-78058F6EFE56} = {DA69F624-5398-4884-87E4-B816698CDE65} {455B68D2-C5B6-4BF4-A685-964B07AFAAF8} = {6A35B453-52EC-48AF-89CA-D4A69800F131} + {F3EFFD9F-DD85-48A2-9B11-83A133ECC099} = {DA69F624-5398-4884-87E4-B816698CDE65} EndGlobalSection EndGlobal diff --git a/samples/ClientSample/RawSample.cs b/samples/ClientSample/RawSample.cs index 7ee6bec583..fc402c63c3 100644 --- a/samples/ClientSample/RawSample.cs +++ b/samples/ClientSample/RawSample.cs @@ -7,9 +7,9 @@ using System.Net.Http; using System.Text; using System.Threading; using System.Threading.Tasks; -using Microsoft.AspNetCore.Sockets; using Microsoft.AspNetCore.Sockets.Client; using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Sockets; namespace ClientSample { diff --git a/src/Microsoft.AspNetCore.Sockets.Client/Connection.cs b/src/Microsoft.AspNetCore.Sockets.Client/Connection.cs index 12e2472381..41e5530e83 100644 --- a/src/Microsoft.AspNetCore.Sockets.Client/Connection.cs +++ b/src/Microsoft.AspNetCore.Sockets.Client/Connection.cs @@ -5,8 +5,8 @@ using System; using System.Net.Http; using System.Threading.Tasks; using System.Threading.Tasks.Channels; -using Microsoft.AspNetCore.Sockets.Internal; using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Sockets.Internal; namespace Microsoft.AspNetCore.Sockets.Client { diff --git a/src/Microsoft.AspNetCore.Sockets.Client/Message.cs b/src/Microsoft.AspNetCore.Sockets.Client/Message.cs deleted file mode 100644 index 0e45869608..0000000000 --- a/src/Microsoft.AspNetCore.Sockets.Client/Message.cs +++ /dev/null @@ -1,25 +0,0 @@ -// 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; -using System.IO.Pipelines; - -namespace Microsoft.AspNetCore.Sockets.Client -{ - public struct Message : IDisposable - { - public Format MessageFormat { get; } - public PreservedBuffer Payload { get; } - - public Message(PreservedBuffer payload, Format messageFormat) - { - MessageFormat = messageFormat; - Payload = payload; - } - - public void Dispose() - { - Payload.Dispose(); - } - } -} diff --git a/src/Microsoft.AspNetCore.Sockets.Client/project.json b/src/Microsoft.AspNetCore.Sockets.Client/project.json index 28e8122483..9687f7f105 100644 --- a/src/Microsoft.AspNetCore.Sockets.Client/project.json +++ b/src/Microsoft.AspNetCore.Sockets.Client/project.json @@ -18,14 +18,7 @@ "nowarn": [ "CS1591" ], - "xmlDoc": true, - "compile": { - "include": [ - "../Microsoft.AspNetCore.Sockets/IChannelConnection.cs", - "../Microsoft.AspNetCore.Sockets/Internal/ChannelConnection.cs", - "../Microsoft.AspNetCore.Sockets/Format.cs" - ] - } + "xmlDoc": true }, "dependencies": { @@ -33,6 +26,7 @@ "System.IO.Pipelines": "0.1.0-*", "System.Threading.Tasks.Channels": "0.1.0-*", "Microsoft.Extensions.Logging.Abstractions": "1.2.0-*", + "Microsoft.AspNetCore.Sockets.Common": "1.0.0-*", "Microsoft.Extensions.TaskCache.Sources": { "type": "build", "version": "1.2.0-*" diff --git a/src/Microsoft.AspNetCore.Sockets/Format.cs b/src/Microsoft.AspNetCore.Sockets.Common/Format.cs similarity index 79% rename from src/Microsoft.AspNetCore.Sockets/Format.cs rename to src/Microsoft.AspNetCore.Sockets.Common/Format.cs index e2972ffe8e..0e5b0d950a 100644 --- a/src/Microsoft.AspNetCore.Sockets/Format.cs +++ b/src/Microsoft.AspNetCore.Sockets.Common/Format.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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. namespace Microsoft.AspNetCore.Sockets diff --git a/src/Microsoft.AspNetCore.Sockets/IChannelConnection.cs b/src/Microsoft.AspNetCore.Sockets.Common/IChannelConnection.cs similarity index 100% rename from src/Microsoft.AspNetCore.Sockets/IChannelConnection.cs rename to src/Microsoft.AspNetCore.Sockets.Common/IChannelConnection.cs diff --git a/src/Microsoft.AspNetCore.Sockets/Internal/ChannelConnection.cs b/src/Microsoft.AspNetCore.Sockets.Common/Internal/ChannelConnection.cs similarity index 93% rename from src/Microsoft.AspNetCore.Sockets/Internal/ChannelConnection.cs rename to src/Microsoft.AspNetCore.Sockets.Common/Internal/ChannelConnection.cs index 35f9a126d9..892cb7deba 100644 --- a/src/Microsoft.AspNetCore.Sockets/Internal/ChannelConnection.cs +++ b/src/Microsoft.AspNetCore.Sockets.Common/Internal/ChannelConnection.cs @@ -1,7 +1,6 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; using System.Threading.Tasks.Channels; namespace Microsoft.AspNetCore.Sockets.Internal diff --git a/src/Microsoft.AspNetCore.Sockets/Message.cs b/src/Microsoft.AspNetCore.Sockets.Common/Message.cs similarity index 82% rename from src/Microsoft.AspNetCore.Sockets/Message.cs rename to src/Microsoft.AspNetCore.Sockets.Common/Message.cs index 86c6e95f52..44d86ba2c1 100644 --- a/src/Microsoft.AspNetCore.Sockets/Message.cs +++ b/src/Microsoft.AspNetCore.Sockets.Common/Message.cs @@ -12,6 +12,12 @@ namespace Microsoft.AspNetCore.Sockets public Format MessageFormat { get; } public PreservedBuffer Payload { get; } + public Message(PreservedBuffer payload, Format messageFormat) + : this(payload, messageFormat, endOfMessage: true) + { + + } + public Message(PreservedBuffer payload, Format messageFormat, bool endOfMessage) { MessageFormat = messageFormat; diff --git a/src/Microsoft.AspNetCore.Sockets.Common/Microsoft.AspNetCore.Sockets.Common.xproj b/src/Microsoft.AspNetCore.Sockets.Common/Microsoft.AspNetCore.Sockets.Common.xproj new file mode 100644 index 0000000000..b20699bcd5 --- /dev/null +++ b/src/Microsoft.AspNetCore.Sockets.Common/Microsoft.AspNetCore.Sockets.Common.xproj @@ -0,0 +1,21 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + f3effd9f-dd85-48a2-9b11-83a133ecc099 + Microsoft.AspNetCore.Sockets.Common + .\obj + .\bin\ + v4.5.2 + + + + 2.0 + + + diff --git a/src/Microsoft.AspNetCore.Sockets.Common/Properties/AssemblyInfo.cs b/src/Microsoft.AspNetCore.Sockets.Common/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..d1c7de1bdc --- /dev/null +++ b/src/Microsoft.AspNetCore.Sockets.Common/Properties/AssemblyInfo.cs @@ -0,0 +1,12 @@ +// 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.Reflection; +using System.Resources; + +[assembly: AssemblyMetadata("Serviceable", "True")] +[assembly: NeutralResourcesLanguage("en-us")] +[assembly: AssemblyCompany("Microsoft Corporation.")] +[assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")] +[assembly: AssemblyProduct("Microsoft ASP.NET Core")] + diff --git a/src/Microsoft.AspNetCore.Sockets.Common/project.json b/src/Microsoft.AspNetCore.Sockets.Common/project.json new file mode 100644 index 0000000000..517e843261 --- /dev/null +++ b/src/Microsoft.AspNetCore.Sockets.Common/project.json @@ -0,0 +1,31 @@ +{ + "version": "1.0.0-*", + "description": "Common primitives for Sockets clients and servers", + "packOptions": { + "repository": { + "type": "git", + "url": "git://github.com/aspnet/signalr" + }, + "tags": [ + "aspnetcore", + "signalr" + ] + }, + "buildOptions": { + "warningsAsErrors": true, + "keyFile": "../../tools/Key.snk", + "nowarn": [ + "CS1591" + ], + "xmlDoc": true + }, + "dependencies": { + "System.IO.Pipelines": "0.1.0-*", + "NETStandard.Library": "1.6.2-*", + "System.Threading.Tasks.Channels": "0.1.0-*" + }, + + "frameworks": { + "netstandard1.3": {} + } + } diff --git a/src/Microsoft.AspNetCore.Sockets/project.json b/src/Microsoft.AspNetCore.Sockets/project.json index e975f6f78e..62ef4825bc 100644 --- a/src/Microsoft.AspNetCore.Sockets/project.json +++ b/src/Microsoft.AspNetCore.Sockets/project.json @@ -20,19 +20,18 @@ "xmlDoc": true }, "dependencies": { - "System.Threading.Tasks.Channels": "0.1.0-*", - - "System.Security.Claims": "4.4.0-*", - "System.Reflection.TypeExtensions": "4.4.0-*", - "Microsoft.AspNetCore.Hosting.Abstractions": "1.2.0-*", "Microsoft.AspNetCore.Routing": "1.2.0-*", + "Microsoft.AspNetCore.Sockets.Common": "1.0.0-*", "Microsoft.AspNetCore.WebSockets.Internal": "0.1.0-*", "Microsoft.Extensions.TaskCache.Sources": { "version": "1.2.0-*", "type": "build" }, - "NETStandard.Library": "1.6.2-*" + "NETStandard.Library": "1.6.2-*", + "System.Reflection.TypeExtensions": "4.4.0-*", + "System.Security.Claims": "4.4.0-*", + "System.Threading.Tasks.Channels": "0.1.0-*" }, "frameworks": { "netstandard1.3": {} diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs index 0010e15390..73c36285e1 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs @@ -2,13 +2,20 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.IO.Pipelines; +using System.Net.Http; using System.Net.WebSockets; using System.Text; using System.Threading; using System.Threading.Tasks; +using Microsoft.AspNetCore.Sockets; +using Microsoft.AspNetCore.Sockets.Client; using Microsoft.AspNetCore.Testing.xunit; +using Microsoft.Extensions.Logging; using Xunit; +using ClientConnection = Microsoft.AspNetCore.Sockets.Client.Connection; + namespace Microsoft.AspNetCore.SignalR.Tests { [CollectionDefinition(Name)] @@ -45,5 +52,44 @@ namespace Microsoft.AspNetCore.SignalR.Tests await ws.CloseAsync(WebSocketCloseStatus.Empty, "", CancellationToken.None); } } + + [Fact] + public async Task ConnectionCanSendAndReceiveMessages() + { + const string message = "Major Key"; + var baseUrl = _serverFixture.BaseUrl; + var loggerFactory = new LoggerFactory(); + + using (var httpClient = new HttpClient()) + { + var transport = new LongPollingTransport(httpClient, loggerFactory); + using (var connection = await ClientConnection.ConnectAsync(new Uri(baseUrl + "/echo"), transport, httpClient, loggerFactory)) + { + await connection.Output.WriteAsync(new Message( + ReadableBuffer.Create(Encoding.UTF8.GetBytes(message)).Preserve(), + Format.Text)); + + var received = await ReceiveMessage(connection).OrTimeout(); + Assert.Equal(message, received); + } + } + } + + private static async Task ReceiveMessage(ClientConnection connection) + { + Message message; + while (await connection.Input.WaitToReadAsync()) + { + if (connection.Input.TryRead(out message)) + { + using (message) + { + return Encoding.UTF8.GetString(message.Payload.Buffer.ToArray()); + } + } + } + + return null; + } } } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/project.json b/test/Microsoft.AspNetCore.SignalR.Tests/project.json index 03f7faa33d..8a8c533b03 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/project.json +++ b/test/Microsoft.AspNetCore.SignalR.Tests/project.json @@ -8,6 +8,7 @@ "Microsoft.AspNetCore.Hosting": "1.2.0-*", "Microsoft.AspNetCore.Sockets": "0.1.0-*", "Microsoft.AspNetCore.SignalR": "1.0.0-*", + "Microsoft.AspNetCore.SignalR.Client": "1.0.0-*", "Microsoft.Extensions.DependencyInjection": "1.2.0-*", "Microsoft.Extensions.Logging": "1.2.0-*", "Microsoft.Extensions.TaskCache.Sources": { diff --git a/test/Microsoft.AspNetCore.Sockets.Client.Tests/LongPollingTransportTests.cs b/test/Microsoft.AspNetCore.Sockets.Client.Tests/LongPollingTransportTests.cs index 8b067637b1..86bc2f7a8d 100644 --- a/test/Microsoft.AspNetCore.Sockets.Client.Tests/LongPollingTransportTests.cs +++ b/test/Microsoft.AspNetCore.Sockets.Client.Tests/LongPollingTransportTests.cs @@ -7,11 +7,11 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; using System.Threading.Tasks.Channels; -using Microsoft.AspNetCore.Sockets.Internal; using Microsoft.Extensions.Logging; using Moq; using Moq.Protected; using Xunit; +using Microsoft.AspNetCore.Sockets.Internal; namespace Microsoft.AspNetCore.Sockets.Client.Tests { diff --git a/test/Microsoft.AspNetCore.Sockets.Client.Tests/project.json b/test/Microsoft.AspNetCore.Sockets.Client.Tests/project.json index 277a80267f..d88f0cced2 100644 --- a/test/Microsoft.AspNetCore.Sockets.Client.Tests/project.json +++ b/test/Microsoft.AspNetCore.Sockets.Client.Tests/project.json @@ -5,6 +5,7 @@ "dependencies": { "Microsoft.AspNetCore.Sockets.Client": "1.0.0-*", + "Microsoft.AspNetCore.Sockets.Common": "1.0.0-*", "Microsoft.Extensions.Logging": "1.2.0-*", "dotnet-test-xunit": "2.2.0-*", "Moq": "4.6.36-*",