Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(Caller): The default data source comes from environment variables, and then query the configuration #368

Merged
merged 2 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Masa.Framework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@ public class DefaultCallerProvider : ICallerProvider
{
private readonly IOptionsMonitor<DaprOptions> _daprOptions;
private readonly IConfiguration? _configuration;
private readonly IMasaConfiguration? _masaConfiguration;

public DefaultCallerProvider(IOptionsMonitor<DaprOptions> daprOptions,
IConfiguration? configuration = null,
IMasaConfiguration? masaConfiguration = null)
{
_daprOptions = daprOptions;
_configuration = masaConfiguration?.Local ?? configuration;
_configuration = configuration;
_masaConfiguration = masaConfiguration;
}

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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -58,6 +56,8 @@ public void TestCompletionAppId3()
masaConfiguration.Setup(configuration => configuration.Local.GetSection($"{appId}-{NetworkUtils.GetPhysicalAddress()}").Value)
.Returns(() => expectedAppId);
Mock<IConfiguration> configuration = new();
configuration.Setup(c => c.GetSection($"{appId}-{NetworkUtils.GetPhysicalAddress()}").Value)
.Returns(() => expectedAppId);

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

var serviceProvider = services.BuildServiceProvider();
var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();
Expand Down Expand Up @@ -114,4 +116,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<DaprOptions>(options =>
{
options.AppPort = 5000;
options.AppIdSuffix = "suffix";
});

var serviceProvider = builder.Services.BuildServiceProvider();

var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();

var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration);
string actualAppId = callerProvider.CompletionAppId(appId);
Assert.AreEqual(expectedAppId, actualAppId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// 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
{
private readonly string _appid = "appid";

[TestInitialize]
public void InitializeData()
{
Environment.SetEnvironmentVariable($"{_appid}-suffix", "");
}

[TestMethod]
public void TestCompletionAppId()
{
string expectedAppId = "expected-appid";
var builder = WebApplication.CreateBuilder();
builder.Configuration.AddJsonFile("customAppsettings.json");
builder.Services.Configure<DaprOptions>(options =>
{
options.AppPort = 5000;
options.AppIdSuffix = "suffix";
});

var serviceProvider = builder.Services.BuildServiceProvider();

var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();

var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration);
string actualAppId = callerProvider.CompletionAppId(_appid);
Assert.AreEqual(expectedAppId, actualAppId);
}

[TestMethod]
public void TestCompletionAppId2()
{
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<DaprOptions>(options =>
{
options.AppPort = 5000;
options.AppIdSuffix = "suffix";
});

var serviceProvider = builder.Services.BuildServiceProvider();

var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();

var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration);
string actualAppId = callerProvider.CompletionAppId(_appid);
Assert.AreEqual("expected-appid", actualAppId);
}

[TestMethod]
public void TestCompletionAppId3()
{
string expectedAppId = "expected-appid";
var builder = WebApplication.CreateBuilder();
builder.Services.AddMasaConfiguration(options => options.AddJsonFile("customAppsettings.json"));
builder.Services.Configure<DaprOptions>(options =>
{
options.AppPort = 5000;
options.AppIdSuffix = "suffix";
});

var serviceProvider = builder.Services.BuildServiceProvider();

var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();

var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration, builder.Services.GetMasaConfiguration());
string actualAppId = callerProvider.CompletionAppId(_appid);
Assert.AreEqual(expectedAppId, actualAppId);
}

[TestMethod]
public void TestCompletionAppId4()
{
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<DaprOptions>(options =>
{
options.AppPort = 5000;
options.AppIdSuffix = "suffix";
});

var serviceProvider = builder.Services.BuildServiceProvider();

var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();

var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration, builder.Services.GetMasaConfiguration());
string actualAppId = callerProvider.CompletionAppId(_appid);
Assert.AreEqual(expectedAppId, actualAppId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftTeskSdkPackageVersion)" />
<PackageReference Include="MSTest.TestAdapter" Version="$(MSTestPackageVersion)" />
<PackageReference Include="MSTest.TestFramework" Version="$(MSTestPackageVersion)" />
<PackageReference Include="coverlet.collector" Version="$(CoverletPackageVersion)">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="$(CoverletPackageVersion)">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Moq" Version="$(MoqPackageVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\..\Configuration\Masa.Contrib.Configuration\Masa.Contrib.Configuration.csproj" />
<ProjectReference Include="..\..\..\Masa.Contrib.Service.Caller.DaprClient\Masa.Contrib.Service.Caller.DaprClient.csproj" />
</ItemGroup>

<ItemGroup>
<None Remove="customAppsettings.json" />
<Content Include="customAppsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"appid-suffix": "expected-appid"
}