-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into graceful-shutdown
- Loading branch information
Showing
6 changed files
with
138 additions
and
96 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/Microsoft.Azure.SignalR.Common/Endpoints/IConfigurationExtension.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,36 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.Azure.SignalR | ||
{ | ||
internal static class IConfigurationExtension | ||
{ | ||
/// <summary> | ||
/// Gets SignalR service endpoints configured in a section. | ||
/// </summary> | ||
/// <remarks> | ||
/// The SignalR service endpoint whose key is exactly the section name is not extracted. Only children of the section are extracted. | ||
/// </remarks> | ||
public static ServiceEndpoint[] GetSignalRServiceEndpoints(this IConfiguration configuration, string sectionName) | ||
{ | ||
var section = configuration.GetSection(sectionName); | ||
return GetEndpoints(section).ToArray(); | ||
} | ||
|
||
private static IEnumerable<ServiceEndpoint> GetEndpoints(IConfiguration section) | ||
{ | ||
foreach (var entry in section.AsEnumerable(true)) | ||
{ | ||
if (!string.IsNullOrEmpty(entry.Value)) | ||
{ | ||
yield return new ServiceEndpoint(entry.Key, entry.Value); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
63 changes: 63 additions & 0 deletions
63
test/Microsoft.Azure.SignalR.Tests/ServiceOptionsSetupFacts.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,63 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.Configuration; | ||
using Xunit; | ||
|
||
namespace Microsoft.Azure.SignalR.Tests | ||
{ | ||
public class ServiceOptionsSetupFacts | ||
{ | ||
public const string FakeConnectionString = "Endpoint=http://fake;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;Port=8080;Version=1.0"; | ||
|
||
private static readonly string[] ConnectionStringKeyPrefixs = new string[] { Constants.Keys.ConnectionStringKeyPrefix, Constants.Keys.ConnectionStringSecondaryKeyPrefix }; | ||
|
||
private static readonly Dictionary<string, (string, EndpointType)> EndpointDict = new Dictionary<string, (string, EndpointType)> | ||
{ | ||
{"a",("a",EndpointType.Primary) }, | ||
{"secondary",("secondary",EndpointType.Primary) }, | ||
{"a:secondary",("a",EndpointType.Secondary) }, | ||
{":secondary",(string.Empty,EndpointType.Secondary) } | ||
}; | ||
|
||
public static IEnumerable<object[]> ParseServiceEndpointData = from section in ConnectionStringKeyPrefixs | ||
from tuple in EndpointDict | ||
select new object[] { section + tuple.Key, tuple.Value.Item1, tuple.Value.Item2 }; | ||
|
||
[Theory] | ||
[MemberData(nameof(ParseServiceEndpointData))] | ||
public void ParseServiceEndpointTest(string key, string endpointName, EndpointType type) | ||
{ | ||
IConfiguration configuration = new ConfigurationBuilder().AddInMemoryCollection().Build(); | ||
configuration[key] = FakeConnectionString; | ||
var setup = new ServiceOptionsSetup(configuration); | ||
var options = new ServiceOptions(); | ||
setup.Configure(options); | ||
|
||
var resultEndpoint = options.Endpoints.Single(); | ||
Assert.Equal(endpointName, resultEndpoint.Name); | ||
Assert.Equal(type, resultEndpoint.EndpointType); | ||
} | ||
|
||
[Fact] | ||
public void ParseMultipleEndpointsTest() | ||
{ | ||
IConfiguration configuration = new ConfigurationBuilder().AddInMemoryCollection().Build(); | ||
var defaultConnectionString = "Endpoint=http://default;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;Port=8080;Version=1.0"; | ||
configuration[$"{Constants.Keys.ConnectionStringDefaultKey}"] = defaultConnectionString; | ||
foreach (var key in EndpointDict.Keys) | ||
{ | ||
configuration[ConfigurationPath.Combine(Constants.Keys.ConnectionStringDefaultKey, key)] = FakeConnectionString; | ||
} | ||
|
||
var setup = new ServiceOptionsSetup(configuration); | ||
var options = new ServiceOptions(); | ||
setup.Configure(options); | ||
|
||
Assert.Equal(defaultConnectionString, options.ConnectionString); | ||
Assert.Equal(EndpointDict.Count, options.Endpoints.Length); | ||
} | ||
} | ||
} |