From 4112f77c6657e3cf060c707e44c6f4bf9271eecc Mon Sep 17 00:00:00 2001 From: zhenlei520 Date: Thu, 8 Dec 2022 15:18:47 +0800 Subject: [PATCH 1/2] refactor: The default data source comes from environment variables, and then query the configuration --- Masa.Framework.sln | 11 ++ .../DefaultCallerProvider.cs | 8 +- .../DefaultCallerProviderTest.cs | 24 ++++- .../_Imports.cs | 1 + .../DefaultCallerProviderTest.cs | 102 ++++++++++++++++++ ....Service.Caller.Configuration.Tests.csproj | 37 +++++++ .../_Imports.cs | 25 +++++ .../customAppsettings.json | 10 ++ 8 files changed, 215 insertions(+), 3 deletions(-) create mode 100644 src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/DefaultCallerProviderTest.cs create mode 100644 src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/Masa.Contrib.Service.Caller.Configuration.Tests.csproj create mode 100644 src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/_Imports.cs create mode 100644 src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/customAppsettings.json diff --git a/Masa.Framework.sln b/Masa.Framework.sln index 49a91b091..b9c36795c 100644 --- a/Masa.Framework.sln +++ b/Masa.Framework.sln @@ -637,6 +637,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Models ", "Models ", "{6FB6 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Utils.Models.Config", "src\Utils\Models\Masa.Utils.Models.Config\Masa.Utils.Models.Config.csproj", "{29125077-70C0-456A-BACD-E614FD71B16E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Service.Caller.Configuration.Tests", "src\Contrib\Service\Caller\Tests\Scenes\Masa.Contrib.Service.Caller.Configuration.Tests\Masa.Contrib.Service.Caller.Configuration.Tests.csproj", "{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -2261,6 +2263,14 @@ Global {29125077-70C0-456A-BACD-E614FD71B16E}.Release|Any CPU.Build.0 = Release|Any CPU {29125077-70C0-456A-BACD-E614FD71B16E}.Release|x64.ActiveCfg = Release|Any CPU {29125077-70C0-456A-BACD-E614FD71B16E}.Release|x64.Build.0 = Release|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Debug|x64.ActiveCfg = Debug|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Debug|x64.Build.0 = Debug|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|Any CPU.Build.0 = Release|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|x64.ActiveCfg = Release|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -2575,6 +2585,7 @@ Global {AE12559A-5C8F-4590-9B18-B935155B15C5} = {F83E3E53-2DFE-4B1F-B988-204CA4A42572} {6FB69510-B616-4F6D-82CC-36AD52CEF4D3} = {5944A182-13B8-4DA6-AEE2-0A01E64A9648} {29125077-70C0-456A-BACD-E614FD71B16E} = {6FB69510-B616-4F6D-82CC-36AD52CEF4D3} + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1} = {E8681596-D4BF-484A-A428-06D749FD4C5D} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03} diff --git a/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.DaprClient/DefaultCallerProvider.cs b/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.DaprClient/DefaultCallerProvider.cs index 479a075bc..fbe3e71de 100644 --- a/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.DaprClient/DefaultCallerProvider.cs +++ b/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.DaprClient/DefaultCallerProvider.cs @@ -7,13 +7,15 @@ public class DefaultCallerProvider : ICallerProvider { private readonly IOptionsMonitor _daprOptions; private readonly IConfiguration? _configuration; + private readonly IMasaConfiguration? _masaConfiguration; public DefaultCallerProvider(IOptionsMonitor daprOptions, IConfiguration? configuration = null, IMasaConfiguration? masaConfiguration = null) { _daprOptions = daprOptions; - _configuration = masaConfiguration?.Local ?? configuration; + _configuration = configuration; + _masaConfiguration = masaConfiguration; } public string CompletionAppId(string appId) @@ -21,7 +23,11 @@ public string CompletionAppId(string appId) var daprOptions = _daprOptions.CurrentValue; if (daprOptions.AppPort > 0 && daprOptions.IsIncompleteAppId()) appId = $"{appId}{daprOptions.AppIdDelimiter}{daprOptions.AppIdSuffix ?? NetworkUtils.GetPhysicalAddress()}"; + var value = _configuration?.GetSection(appId).Value; + if (value.IsNullOrWhiteSpace()) + value = _masaConfiguration?.Local.GetSection(appId).Value; + if (value.IsNullOrWhiteSpace()) return appId; return value; diff --git a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/DefaultCallerProviderTest.cs b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/DefaultCallerProviderTest.cs index 8955518d5..a34e8260d 100644 --- a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/DefaultCallerProviderTest.cs +++ b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/DefaultCallerProviderTest.cs @@ -1,8 +1,6 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. -using Microsoft.Extensions.Configuration; - namespace Masa.Contrib.Service.Caller.Tests; [TestClass] @@ -114,4 +112,26 @@ public void TestCompletionAppId5() string actualAppId = callerProvider.CompletionAppId(appId); Assert.AreEqual(expectedAppId, actualAppId); } + + [TestMethod] + public void TestCompletionAppId6() + { + string appId = "appid"; + string expectedAppId = $"{appId}-{Guid.NewGuid()}"; + Environment.SetEnvironmentVariable($"{appId}-suffix", expectedAppId); + var builder = WebApplication.CreateBuilder(); + builder.Services.Configure(options => + { + options.AppPort = 5000; + options.AppIdSuffix = "suffix"; + }); + + var serviceProvider = builder.Services.BuildServiceProvider(); + + var daprOptions = serviceProvider.GetRequiredService>(); + + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration); + string actualAppId = callerProvider.CompletionAppId(appId); + Assert.AreEqual(expectedAppId, actualAppId); + } } diff --git a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/_Imports.cs b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/_Imports.cs index 0709e12b5..4a690947b 100644 --- a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/_Imports.cs +++ b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/_Imports.cs @@ -11,6 +11,7 @@ global using Masa.Contrib.Service.Caller.Tests.Requesties; global using Masa.Contrib.Service.Caller.Tests.Response; global using Microsoft.AspNetCore.Builder; +global using Microsoft.Extensions.Configuration; global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.Logging; global using Microsoft.Extensions.Options; diff --git a/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/DefaultCallerProviderTest.cs b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/DefaultCallerProviderTest.cs new file mode 100644 index 000000000..dc217bcec --- /dev/null +++ b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/DefaultCallerProviderTest.cs @@ -0,0 +1,102 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.Contrib.Service.Caller.Configuration.Tests; + +[TestClass] +public class DefaultCallerProviderTest +{ + [TestMethod] + public void TestCompletionAppId() + { + string appId = "appid"; + string expectedAppId = "expected-appid"; + var builder = WebApplication.CreateBuilder(); + builder.Configuration.AddJsonFile("customAppsettings.json"); + builder.Services.Configure(options => + { + options.AppPort = 5000; + options.AppIdSuffix = "suffix"; + }); + + var serviceProvider = builder.Services.BuildServiceProvider(); + + var daprOptions = serviceProvider.GetRequiredService>(); + + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration); + string actualAppId = callerProvider.CompletionAppId(appId); + Assert.AreEqual(expectedAppId, actualAppId); + } + + [TestMethod] + public void TestCompletionAppId2() + { + string appId = "appid"; + string expectedAppId = $"{appId}-{Guid.NewGuid()}"; + + Environment.SetEnvironmentVariable($"{appId}-suffix", expectedAppId); + var builder = WebApplication.CreateBuilder(); + + //If the newly added data source exists, the environment variable data source will no longer be queried + builder.Configuration.AddJsonFile("customAppsettings.json"); + builder.Services.Configure(options => + { + options.AppPort = 5000; + options.AppIdSuffix = "suffix"; + }); + + var serviceProvider = builder.Services.BuildServiceProvider(); + + var daprOptions = serviceProvider.GetRequiredService>(); + + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration); + string actualAppId = callerProvider.CompletionAppId(appId); + Assert.AreEqual("expected-appid", actualAppId); + } + + [TestMethod] + public void TestCompletionAppId3() + { + string appId = "appid"; + string expectedAppId = "expected-appid"; + var builder = WebApplication.CreateBuilder(); + builder.Services.AddMasaConfiguration(options => options.AddJsonFile("customAppsettings.json")); + builder.Services.Configure(options => + { + options.AppPort = 5000; + options.AppIdSuffix = "suffix"; + }); + + var serviceProvider = builder.Services.BuildServiceProvider(); + + var daprOptions = serviceProvider.GetRequiredService>(); + + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration, builder.Services.GetMasaConfiguration()); + string actualAppId = callerProvider.CompletionAppId(appId); + Assert.AreEqual(expectedAppId, actualAppId); + } + + [TestMethod] + public void TestCompletionAppId4() + { + string appId = "appid"; + string expectedAppId = $"{appId}-{Guid.NewGuid()}"; + + Environment.SetEnvironmentVariable($"{appId}-suffix", expectedAppId); + var builder = WebApplication.CreateBuilder(); + builder.Services.AddMasaConfiguration(options => options.AddJsonFile("customAppsettings.json")); + builder.Services.Configure(options => + { + options.AppPort = 5000; + options.AppIdSuffix = "suffix"; + }); + + var serviceProvider = builder.Services.BuildServiceProvider(); + + var daprOptions = serviceProvider.GetRequiredService>(); + + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration, builder.Services.GetMasaConfiguration()); + string actualAppId = callerProvider.CompletionAppId(appId); + Assert.AreEqual(expectedAppId, actualAppId); + } +} diff --git a/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/Masa.Contrib.Service.Caller.Configuration.Tests.csproj b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/Masa.Contrib.Service.Caller.Configuration.Tests.csproj new file mode 100644 index 000000000..8d0d7f40f --- /dev/null +++ b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/Masa.Contrib.Service.Caller.Configuration.Tests.csproj @@ -0,0 +1,37 @@ + + + + net6.0 + enable + enable + false + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + Always + + + + diff --git a/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/_Imports.cs b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/_Imports.cs new file mode 100644 index 000000000..6e6088bba --- /dev/null +++ b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/_Imports.cs @@ -0,0 +1,25 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +global using Masa.BuildingBlocks.Configuration; +global using Masa.BuildingBlocks.Development.DaprStarter; +global using Masa.BuildingBlocks.Service.Caller; +global using Masa.BuildingBlocks.Service.Caller.Options; +global using Masa.Contrib.Service.Caller.DaprClient; +global using Microsoft.AspNetCore.Builder; +global using Microsoft.Extensions.Configuration; +global using Microsoft.Extensions.DependencyInjection; +global using Microsoft.Extensions.Logging; +global using Microsoft.Extensions.Options; +global using Microsoft.VisualStudio.TestTools.UnitTesting; +global using Moq; +global using Moq.Protected; +global using System.Net; +global using System.Net.Http.Json; +global using System.Net.NetworkInformation; +global using System.Reflection; +global using System.Runtime.ExceptionServices; +global using System.Text; +global using System.Text.Json; +global using System.Text.Json.Serialization; +global using System.Xml.Serialization; diff --git a/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/customAppsettings.json b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/customAppsettings.json new file mode 100644 index 000000000..b5eb3a6b7 --- /dev/null +++ b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/customAppsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + }, + "appid-suffix": "expected-appid" +} \ No newline at end of file From df8fb61ae4bc35c8c8530392d9bf99c89360cf3e Mon Sep 17 00:00:00 2001 From: zhenlei520 Date: Thu, 8 Dec 2022 15:39:15 +0800 Subject: [PATCH 2/2] chore: reset environment variables --- .../DefaultCallerProviderTest.cs | 4 +++ .../DefaultCallerProviderTest.cs | 28 +++++++++++-------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/DefaultCallerProviderTest.cs b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/DefaultCallerProviderTest.cs index a34e8260d..a639bbda9 100644 --- a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/DefaultCallerProviderTest.cs +++ b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/DefaultCallerProviderTest.cs @@ -56,6 +56,8 @@ public void TestCompletionAppId3() masaConfiguration.Setup(configuration => configuration.Local.GetSection($"{appId}-{NetworkUtils.GetPhysicalAddress()}").Value) .Returns(() => expectedAppId); Mock configuration = new(); + configuration.Setup(c => c.GetSection($"{appId}-{NetworkUtils.GetPhysicalAddress()}").Value) + .Returns(() => expectedAppId); var serviceProvider = services.BuildServiceProvider(); var daprOptions = serviceProvider.GetRequiredService>(); @@ -81,6 +83,8 @@ public void TestCompletionAppId4() masaConfiguration.Setup(configuration => configuration.Local.GetSection($"{appId}-suffix").Value) .Returns(() => expectedAppId); Mock configuration = new(); + configuration.Setup(c => c.GetSection($"{appId}-suffix").Value) + .Returns(() => expectedAppId); var serviceProvider = services.BuildServiceProvider(); var daprOptions = serviceProvider.GetRequiredService>(); diff --git a/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/DefaultCallerProviderTest.cs b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/DefaultCallerProviderTest.cs index dc217bcec..8bda88da2 100644 --- a/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/DefaultCallerProviderTest.cs +++ b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/DefaultCallerProviderTest.cs @@ -6,10 +6,17 @@ namespace Masa.Contrib.Service.Caller.Configuration.Tests; [TestClass] public class DefaultCallerProviderTest { + private readonly string _appid = "appid"; + + [TestInitialize] + public void InitializeData() + { + Environment.SetEnvironmentVariable($"{_appid}-suffix", ""); + } + [TestMethod] public void TestCompletionAppId() { - string appId = "appid"; string expectedAppId = "expected-appid"; var builder = WebApplication.CreateBuilder(); builder.Configuration.AddJsonFile("customAppsettings.json"); @@ -24,17 +31,16 @@ public void TestCompletionAppId() var daprOptions = serviceProvider.GetRequiredService>(); var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration); - string actualAppId = callerProvider.CompletionAppId(appId); + string actualAppId = callerProvider.CompletionAppId(_appid); Assert.AreEqual(expectedAppId, actualAppId); } [TestMethod] public void TestCompletionAppId2() { - string appId = "appid"; - string expectedAppId = $"{appId}-{Guid.NewGuid()}"; + string expectedAppId = $"{_appid}-{Guid.NewGuid()}"; - Environment.SetEnvironmentVariable($"{appId}-suffix", expectedAppId); + Environment.SetEnvironmentVariable($"{_appid}-suffix", expectedAppId); var builder = WebApplication.CreateBuilder(); //If the newly added data source exists, the environment variable data source will no longer be queried @@ -50,14 +56,13 @@ public void TestCompletionAppId2() var daprOptions = serviceProvider.GetRequiredService>(); var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration); - string actualAppId = callerProvider.CompletionAppId(appId); + string actualAppId = callerProvider.CompletionAppId(_appid); Assert.AreEqual("expected-appid", actualAppId); } [TestMethod] public void TestCompletionAppId3() { - string appId = "appid"; string expectedAppId = "expected-appid"; var builder = WebApplication.CreateBuilder(); builder.Services.AddMasaConfiguration(options => options.AddJsonFile("customAppsettings.json")); @@ -72,17 +77,16 @@ public void TestCompletionAppId3() var daprOptions = serviceProvider.GetRequiredService>(); var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration, builder.Services.GetMasaConfiguration()); - string actualAppId = callerProvider.CompletionAppId(appId); + string actualAppId = callerProvider.CompletionAppId(_appid); Assert.AreEqual(expectedAppId, actualAppId); } [TestMethod] public void TestCompletionAppId4() { - string appId = "appid"; - string expectedAppId = $"{appId}-{Guid.NewGuid()}"; + string expectedAppId = $"{_appid}-{Guid.NewGuid()}"; - Environment.SetEnvironmentVariable($"{appId}-suffix", expectedAppId); + Environment.SetEnvironmentVariable($"{_appid}-suffix", expectedAppId); var builder = WebApplication.CreateBuilder(); builder.Services.AddMasaConfiguration(options => options.AddJsonFile("customAppsettings.json")); builder.Services.Configure(options => @@ -96,7 +100,7 @@ public void TestCompletionAppId4() var daprOptions = serviceProvider.GetRequiredService>(); var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration, builder.Services.GetMasaConfiguration()); - string actualAppId = callerProvider.CompletionAppId(appId); + string actualAppId = callerProvider.CompletionAppId(_appid); Assert.AreEqual(expectedAppId, actualAppId); } }