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: Refactor Caller.Dapr #365

Merged
merged 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ public static DefaultDaprClientBuilder UseDapr(this CallerOptions callerOptions,
builder.Configure?.Invoke(daprClientBuilder);
});

callerOptions.Services.TryAddSingleton<ICallerProvider, DefaultCallerProvider>();
callerOptions.Services.AddOptions();
AddCallerExtensions.AddCaller(callerOptions, name,
serviceProvider =>
{
var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>().CurrentValue;
string appId = builder.AppId;
if (daprOptions.AppPort > 0 && daprOptions.IsIncompleteAppId())
{
appId = $"{appId}{daprOptions.AppIdDelimiter}{daprOptions.AppIdSuffix ?? NetworkUtils.GetPhysicalAddress()}";
}
var daprCaller = new DaprCaller(serviceProvider, name, appId);
var daprCaller = new DaprCaller(serviceProvider,
name,
serviceProvider.GetRequiredService<ICallerProvider>().CompletionAppId(appId));
return daprCaller;
});
return new DefaultDaprClientBuilder(callerOptions.Services, name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.DaprClient;

public class DefaultCallerProvider : ICallerProvider
{
private readonly IOptionsMonitor<DaprOptions> _daprOptions;
private readonly IConfiguration? _configuration;

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

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()) return appId;

return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.DaprClient;

public interface ICallerProvider
{
/// <summary>
/// According to the dapr appid obtained by the service id
/// when the dapr appid of the specified service does not exist in the configuration, return the service id as the dapr appid
/// </summary>
/// <param name="appId">service appid</param>
/// <returns></returns>
string CompletionAppId(string appId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\BuildingBlocks\Configuration\Masa.BuildingBlocks.Configuration\Masa.BuildingBlocks.Configuration.csproj" />
<ProjectReference Include="..\..\..\..\BuildingBlocks\Development\Masa.BuildingBlocks.Development.DaprStarter\Masa.BuildingBlocks.Development.DaprStarter.csproj" />
<ProjectReference Include="..\..\..\..\Utils\Extensions\Masa.Utils.Extensions.DotNet\Masa.Utils.Extensions.DotNet.csproj" />
<ProjectReference Include="..\Masa.Contrib.Service.Caller\Masa.Contrib.Service.Caller.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

global using Dapr.Client;
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.Options;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.DependencyInjection.Extensions;
global using Microsoft.Extensions.Logging;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// 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]
public class DefaultCallerProviderTest
{
[DataTestMethod]
[DataRow(5000, "test", "appid", "appid-test")]
public void TestCompletionAppId(int appPort, string appIdSuffix, string appId, string expectedAppId)
{
var services = new ServiceCollection();
services.Configure<DaprOptions>(options =>
{
options.AppPort = (ushort)appPort;
options.AppIdSuffix = appIdSuffix;
});
var serviceProvider = services.BuildServiceProvider();
var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();

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

[TestMethod]
public void TestCompletionAppId2()
{
string appId = "appid";
string expectedAppId = $"{appId}-{NetworkUtils.GetPhysicalAddress()}";
var services = new ServiceCollection();
services.Configure<DaprOptions>(options =>
{
options.AppPort = 5000;
});
var serviceProvider = services.BuildServiceProvider();
var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>();

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

[TestMethod]
public void TestCompletionAppId3()
{
string appId = "appid";
string expectedAppId = $"{appId}-{Guid.NewGuid()}";
var services = new ServiceCollection();
services.Configure<DaprOptions>(options =>
{
options.AppPort = 5000;
});
Mock<IMasaConfiguration> masaConfiguration = new();
masaConfiguration.Setup(configuration => configuration.Local.GetSection($"{appId}-{NetworkUtils.GetPhysicalAddress()}").Value)
.Returns(() => expectedAppId);
Mock<IConfiguration> configuration = new();

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

var callerProvider = new DefaultCallerProvider(daprOptions, configuration.Object, masaConfiguration.Object);
string actualAppId = callerProvider.CompletionAppId(appId);
Assert.AreEqual(expectedAppId, actualAppId);
}

[TestMethod]
public void TestCompletionAppId4()
{
string appId = "appid";
var services = new ServiceCollection();
services.Configure<DaprOptions>(options =>
{
options.AppPort = 5000;
options.AppIdSuffix = "suffix";
});
string expectedAppId = $"{appId}-{Guid.NewGuid()}";

Mock<IMasaConfiguration> masaConfiguration = new();
masaConfiguration.Setup(configuration => configuration.Local.GetSection($"{appId}-suffix").Value)
.Returns(() => expectedAppId);
Mock<IConfiguration> configuration = new();

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

var callerProvider = new DefaultCallerProvider(daprOptions, configuration.Object, masaConfiguration.Object);
string actualAppId = callerProvider.CompletionAppId(appId);
Assert.AreEqual(expectedAppId, actualAppId);
}

[TestMethod]
public void TestCompletionAppId5()
{
string appId = "appid";
var services = new ServiceCollection();
services.Configure<DaprOptions>(options =>
{
options.AppPort = 5000;
options.AppIdSuffix = "suffix";
});
string expectedAppId = $"{appId}-{Guid.NewGuid()}";

Mock<IConfiguration> configuration = new();
configuration.Setup(c => c.GetSection($"{appId}-suffix").Value).Returns(() => expectedAppId);

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

var callerProvider = new DefaultCallerProvider(daprOptions, configuration.Object);
string actualAppId = callerProvider.CompletionAppId(appId);
Assert.AreEqual(expectedAppId, actualAppId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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;
Expand All @@ -17,6 +19,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public void TestDaprCallerReturnCallerProviderIsNotNull()
var serviceProvider = _builder.Services.BuildServiceProvider();
var caller = serviceProvider.GetRequiredService<DaprCaller>();
Assert.IsTrue(caller.CallerProviderIsNotNull());

var callerClient = (Masa.Contrib.Service.Caller.DaprClient.DaprCaller)caller.GetCaller();
var field = callerClient.GetType().GetField("AppId", BindingFlags.Instance | BindingFlags.NonPublic);
Assert.IsNotNull(field);
Assert.AreEqual("DaprCaller", field.GetValue(callerClient));
}

[TestMethod]
Expand All @@ -44,4 +49,13 @@ public void TestCustomDaprBaseReturnAppIdIsEqualUserService()
var userCaller = serviceProvider.GetRequiredService<UserCaller>();
Assert.IsTrue(roleCaller.GetAppId() == "User-Service" && userCaller.GetAppId() == "User-Service");
}

[TestMethod]
public void TestCallerProvider()
{
_builder.Services.AddCaller();
var serviceProvider = _builder.Services.BuildServiceProvider();
var callerProvider = serviceProvider.GetService<ICallerProvider>();
Assert.IsNotNull(callerProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public DaprCaller(IServiceProvider serviceProvider) : base(serviceProvider)
protected override string AppId { get; set; }

public bool CallerProviderIsNotNull() => Caller != null;

public ICaller GetCaller() => Caller;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace System;

public static class StringExtensions
{
public static bool IsNullOrWhiteSpace(this string? value)
public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? value)
=> string.IsNullOrWhiteSpace(value);

public static bool IsNullOrEmpty(this string? value)
public static bool IsNullOrEmpty([NotNullWhen(false)] this string? value)
=> string.IsNullOrEmpty(value);

public static void CheckIsNullOrWhiteSpace(this string? value, [CallerArgumentExpression("value")] string? paramName = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
global using System.Collections;
global using System.ComponentModel;
global using System.Diagnostics;
global using System.Diagnostics.CodeAnalysis;
global using System.Dynamic;
global using System.Globalization;
global using System.Reflection;
Expand Down