Skip to content

Commit 8fe1659

Browse files
authored
refactor(Caller): The default data source comes from environment variables, and then query the configuration (#368)
* refactor: The default data source comes from environment variables, and then query the configuration * chore: reset environment variables
1 parent c55cf04 commit 8fe1659

File tree

8 files changed

+223
-3
lines changed

8 files changed

+223
-3
lines changed

Masa.Framework.sln

+11
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Models ", "Models ", "{6FB6
637637
EndProject
638638
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}"
639639
EndProject
640+
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}"
641+
EndProject
640642
Global
641643
GlobalSection(SolutionConfigurationPlatforms) = preSolution
642644
Debug|Any CPU = Debug|Any CPU
@@ -2261,6 +2263,14 @@ Global
22612263
{29125077-70C0-456A-BACD-E614FD71B16E}.Release|Any CPU.Build.0 = Release|Any CPU
22622264
{29125077-70C0-456A-BACD-E614FD71B16E}.Release|x64.ActiveCfg = Release|Any CPU
22632265
{29125077-70C0-456A-BACD-E614FD71B16E}.Release|x64.Build.0 = Release|Any CPU
2266+
{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2267+
{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Debug|Any CPU.Build.0 = Debug|Any CPU
2268+
{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Debug|x64.ActiveCfg = Debug|Any CPU
2269+
{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Debug|x64.Build.0 = Debug|Any CPU
2270+
{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|Any CPU.ActiveCfg = Release|Any CPU
2271+
{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|Any CPU.Build.0 = Release|Any CPU
2272+
{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|x64.ActiveCfg = Release|Any CPU
2273+
{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|x64.Build.0 = Release|Any CPU
22642274
EndGlobalSection
22652275
GlobalSection(SolutionProperties) = preSolution
22662276
HideSolutionNode = FALSE
@@ -2575,6 +2585,7 @@ Global
25752585
{AE12559A-5C8F-4590-9B18-B935155B15C5} = {F83E3E53-2DFE-4B1F-B988-204CA4A42572}
25762586
{6FB69510-B616-4F6D-82CC-36AD52CEF4D3} = {5944A182-13B8-4DA6-AEE2-0A01E64A9648}
25772587
{29125077-70C0-456A-BACD-E614FD71B16E} = {6FB69510-B616-4F6D-82CC-36AD52CEF4D3}
2588+
{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1} = {E8681596-D4BF-484A-A428-06D749FD4C5D}
25782589
EndGlobalSection
25792590
GlobalSection(ExtensibilityGlobals) = postSolution
25802591
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}

src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.DaprClient/DefaultCallerProvider.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,27 @@ public class DefaultCallerProvider : ICallerProvider
77
{
88
private readonly IOptionsMonitor<DaprOptions> _daprOptions;
99
private readonly IConfiguration? _configuration;
10+
private readonly IMasaConfiguration? _masaConfiguration;
1011

1112
public DefaultCallerProvider(IOptionsMonitor<DaprOptions> daprOptions,
1213
IConfiguration? configuration = null,
1314
IMasaConfiguration? masaConfiguration = null)
1415
{
1516
_daprOptions = daprOptions;
16-
_configuration = masaConfiguration?.Local ?? configuration;
17+
_configuration = configuration;
18+
_masaConfiguration = masaConfiguration;
1719
}
1820

1921
public string CompletionAppId(string appId)
2022
{
2123
var daprOptions = _daprOptions.CurrentValue;
2224
if (daprOptions.AppPort > 0 && daprOptions.IsIncompleteAppId())
2325
appId = $"{appId}{daprOptions.AppIdDelimiter}{daprOptions.AppIdSuffix ?? NetworkUtils.GetPhysicalAddress()}";
26+
2427
var value = _configuration?.GetSection(appId).Value;
28+
if (value.IsNullOrWhiteSpace())
29+
value = _masaConfiguration?.Local.GetSection(appId).Value;
30+
2531
if (value.IsNullOrWhiteSpace()) return appId;
2632

2733
return value;

src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/DefaultCallerProviderTest.cs

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4-
using Microsoft.Extensions.Configuration;
5-
64
namespace Masa.Contrib.Service.Caller.Tests;
75

86
[TestClass]
@@ -58,6 +56,8 @@ public void TestCompletionAppId3()
5856
masaConfiguration.Setup(configuration => configuration.Local.GetSection($"{appId}-{NetworkUtils.GetPhysicalAddress()}").Value)
5957
.Returns(() => expectedAppId);
6058
Mock<IConfiguration> configuration = new();
59+
configuration.Setup(c => c.GetSection($"{appId}-{NetworkUtils.GetPhysicalAddress()}").Value)
60+
.Returns(() => expectedAppId);
6161

6262
var serviceProvider = services.BuildServiceProvider();
6363
var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();
@@ -83,6 +83,8 @@ public void TestCompletionAppId4()
8383
masaConfiguration.Setup(configuration => configuration.Local.GetSection($"{appId}-suffix").Value)
8484
.Returns(() => expectedAppId);
8585
Mock<IConfiguration> configuration = new();
86+
configuration.Setup(c => c.GetSection($"{appId}-suffix").Value)
87+
.Returns(() => expectedAppId);
8688

8789
var serviceProvider = services.BuildServiceProvider();
8890
var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();
@@ -114,4 +116,26 @@ public void TestCompletionAppId5()
114116
string actualAppId = callerProvider.CompletionAppId(appId);
115117
Assert.AreEqual(expectedAppId, actualAppId);
116118
}
119+
120+
[TestMethod]
121+
public void TestCompletionAppId6()
122+
{
123+
string appId = "appid";
124+
string expectedAppId = $"{appId}-{Guid.NewGuid()}";
125+
Environment.SetEnvironmentVariable($"{appId}-suffix", expectedAppId);
126+
var builder = WebApplication.CreateBuilder();
127+
builder.Services.Configure<DaprOptions>(options =>
128+
{
129+
options.AppPort = 5000;
130+
options.AppIdSuffix = "suffix";
131+
});
132+
133+
var serviceProvider = builder.Services.BuildServiceProvider();
134+
135+
var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();
136+
137+
var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration);
138+
string actualAppId = callerProvider.CompletionAppId(appId);
139+
Assert.AreEqual(expectedAppId, actualAppId);
140+
}
117141
}

src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/_Imports.cs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
global using Masa.Contrib.Service.Caller.Tests.Requesties;
1212
global using Masa.Contrib.Service.Caller.Tests.Response;
1313
global using Microsoft.AspNetCore.Builder;
14+
global using Microsoft.Extensions.Configuration;
1415
global using Microsoft.Extensions.DependencyInjection;
1516
global using Microsoft.Extensions.Logging;
1617
global using Microsoft.Extensions.Options;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Service.Caller.Configuration.Tests;
5+
6+
[TestClass]
7+
public class DefaultCallerProviderTest
8+
{
9+
private readonly string _appid = "appid";
10+
11+
[TestInitialize]
12+
public void InitializeData()
13+
{
14+
Environment.SetEnvironmentVariable($"{_appid}-suffix", "");
15+
}
16+
17+
[TestMethod]
18+
public void TestCompletionAppId()
19+
{
20+
string expectedAppId = "expected-appid";
21+
var builder = WebApplication.CreateBuilder();
22+
builder.Configuration.AddJsonFile("customAppsettings.json");
23+
builder.Services.Configure<DaprOptions>(options =>
24+
{
25+
options.AppPort = 5000;
26+
options.AppIdSuffix = "suffix";
27+
});
28+
29+
var serviceProvider = builder.Services.BuildServiceProvider();
30+
31+
var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();
32+
33+
var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration);
34+
string actualAppId = callerProvider.CompletionAppId(_appid);
35+
Assert.AreEqual(expectedAppId, actualAppId);
36+
}
37+
38+
[TestMethod]
39+
public void TestCompletionAppId2()
40+
{
41+
string expectedAppId = $"{_appid}-{Guid.NewGuid()}";
42+
43+
Environment.SetEnvironmentVariable($"{_appid}-suffix", expectedAppId);
44+
var builder = WebApplication.CreateBuilder();
45+
46+
//If the newly added data source exists, the environment variable data source will no longer be queried
47+
builder.Configuration.AddJsonFile("customAppsettings.json");
48+
builder.Services.Configure<DaprOptions>(options =>
49+
{
50+
options.AppPort = 5000;
51+
options.AppIdSuffix = "suffix";
52+
});
53+
54+
var serviceProvider = builder.Services.BuildServiceProvider();
55+
56+
var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();
57+
58+
var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration);
59+
string actualAppId = callerProvider.CompletionAppId(_appid);
60+
Assert.AreEqual("expected-appid", actualAppId);
61+
}
62+
63+
[TestMethod]
64+
public void TestCompletionAppId3()
65+
{
66+
string expectedAppId = "expected-appid";
67+
var builder = WebApplication.CreateBuilder();
68+
builder.Services.AddMasaConfiguration(options => options.AddJsonFile("customAppsettings.json"));
69+
builder.Services.Configure<DaprOptions>(options =>
70+
{
71+
options.AppPort = 5000;
72+
options.AppIdSuffix = "suffix";
73+
});
74+
75+
var serviceProvider = builder.Services.BuildServiceProvider();
76+
77+
var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();
78+
79+
var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration, builder.Services.GetMasaConfiguration());
80+
string actualAppId = callerProvider.CompletionAppId(_appid);
81+
Assert.AreEqual(expectedAppId, actualAppId);
82+
}
83+
84+
[TestMethod]
85+
public void TestCompletionAppId4()
86+
{
87+
string expectedAppId = $"{_appid}-{Guid.NewGuid()}";
88+
89+
Environment.SetEnvironmentVariable($"{_appid}-suffix", expectedAppId);
90+
var builder = WebApplication.CreateBuilder();
91+
builder.Services.AddMasaConfiguration(options => options.AddJsonFile("customAppsettings.json"));
92+
builder.Services.Configure<DaprOptions>(options =>
93+
{
94+
options.AppPort = 5000;
95+
options.AppIdSuffix = "suffix";
96+
});
97+
98+
var serviceProvider = builder.Services.BuildServiceProvider();
99+
100+
var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();
101+
102+
var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration, builder.Services.GetMasaConfiguration());
103+
string actualAppId = callerProvider.CompletionAppId(_appid);
104+
Assert.AreEqual(expectedAppId, actualAppId);
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftTeskSdkPackageVersion)" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="$(MSTestPackageVersion)" />
13+
<PackageReference Include="MSTest.TestFramework" Version="$(MSTestPackageVersion)" />
14+
<PackageReference Include="coverlet.collector" Version="$(CoverletPackageVersion)">
15+
<PrivateAssets>all</PrivateAssets>
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
</PackageReference>
18+
<PackageReference Include="coverlet.msbuild" Version="$(CoverletPackageVersion)">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
<PackageReference Include="Moq" Version="$(MoqPackageVersion)" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\..\..\..\..\Configuration\Masa.Contrib.Configuration\Masa.Contrib.Configuration.csproj" />
27+
<ProjectReference Include="..\..\..\Masa.Contrib.Service.Caller.DaprClient\Masa.Contrib.Service.Caller.DaprClient.csproj" />
28+
</ItemGroup>
29+
30+
<ItemGroup>
31+
<None Remove="customAppsettings.json" />
32+
<Content Include="customAppsettings.json">
33+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
34+
</Content>
35+
</ItemGroup>
36+
37+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
global using Masa.BuildingBlocks.Configuration;
5+
global using Masa.BuildingBlocks.Development.DaprStarter;
6+
global using Masa.BuildingBlocks.Service.Caller;
7+
global using Masa.BuildingBlocks.Service.Caller.Options;
8+
global using Masa.Contrib.Service.Caller.DaprClient;
9+
global using Microsoft.AspNetCore.Builder;
10+
global using Microsoft.Extensions.Configuration;
11+
global using Microsoft.Extensions.DependencyInjection;
12+
global using Microsoft.Extensions.Logging;
13+
global using Microsoft.Extensions.Options;
14+
global using Microsoft.VisualStudio.TestTools.UnitTesting;
15+
global using Moq;
16+
global using Moq.Protected;
17+
global using System.Net;
18+
global using System.Net.Http.Json;
19+
global using System.Net.NetworkInformation;
20+
global using System.Reflection;
21+
global using System.Runtime.ExceptionServices;
22+
global using System.Text;
23+
global using System.Text.Json;
24+
global using System.Text.Json.Serialization;
25+
global using System.Xml.Serialization;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Microsoft": "Information"
7+
}
8+
},
9+
"appid-suffix": "expected-appid"
10+
}

0 commit comments

Comments
 (0)