You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
|
|
|
|
|
using FluentAssertions;
|
|
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.Primitives;
|
|
|
|
|
|
|
|
|
|
namespace OptionStudy.UnitApp
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 5.1.7 配置内容同步
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class SyncConfigTest : IDisposable
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private readonly ITestOutputHelper? testOutput;
|
|
|
|
|
public SyncConfigTest(ITestOutputHelperAccessor helperAccessor)
|
|
|
|
|
{
|
|
|
|
|
testOutput = helperAccessor.Output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Sync_Test()
|
|
|
|
|
{
|
|
|
|
|
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Configs/appsettings.json");
|
|
|
|
|
|
|
|
|
|
var config = new ConfigurationBuilder()
|
|
|
|
|
.AddJsonFile(filePath, false, true)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
Action run = () => // 执行事件
|
|
|
|
|
{
|
|
|
|
|
var configJsonText = System.Text.Json.JsonSerializer.Serialize(config.Get<AppOption>());
|
|
|
|
|
testOutput?.WriteLine(configJsonText);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ChangeToken.OnChange
|
|
|
|
|
(
|
|
|
|
|
() => config.GetReloadToken(), // IChangeToken
|
|
|
|
|
run
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
File.AppendAllText(filePath," ");
|
|
|
|
|
|
|
|
|
|
//等待一会,监控到文件变化后会执行回调方法
|
|
|
|
|
Thread.Sleep(2*1000);
|
|
|
|
|
|
|
|
|
|
//todo:断言方法至少执行一次
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|