我正在努力按照他们的教程进行测试:链接到奥尔良测试文档
但显示的配置非常简单:
var builder = new TestClusterBuilder();
var cluster = builder.Build();
cluster.Deploy();
并且不显示如何添加SMSProvider。
假设我有以下测试:
[Fact]
public async Task SaysHelloCorrectly()
{
var hello = _cluster.GrainFactory.GetGrain<ISomeGrain>(Guid.NewGuid(), "<Some stream ID>", "Some.Class.Path");
var guid = Guid.NewGuid();
var streamProvider = _cluster.Client.GetStreamProvider("SMSProvider");
var photoStream = streamProvider.GetStream<string>(guid, "<Some stream ID>");
await photoStream.OnNextAsync("Hi");
Assert.Equal("Hello, World", "Hello, World");
}
然后我在行中得到一个错误:
var streamProvider = _cluster.Client.GetStreamProvider("SMSProvider");
例如:
[xUnit.net 00:00:02.02] Tests.SaysHelloCorrectly [FAIL]
Failed Tests.FilterTests.SaysHelloCorrectly [8 ms]
Error Message:
System.Collections.Generic.KeyNotFoundException : SMSProvider
我认为正确配置ClusterFixture将是解决方案的一部分,或者我在单元测试中获得StreamProvider的方式可能不正确。
所有代码如下所示:
ClusterCollection:
using System;
using Orleans.TestingHost;
using Xunit;
namespace Tests {
[CollectionDefinition(ClusterCollection.Name)]
public class ClusterCollection : ICollectionFixture<ClusterFixture>
{
public const string Name = "ClusterCollection";
}
}
集群固定装置:
public class ClusterFixture : IDisposable
{
private sealed class Configurator : ISiloBuilderConfigurator
{
public void Configure(ISiloHostBuilder siloBuilder)
{
siloBuilder.AddSimpleMessageStreamProvider("SMSProvider");
siloBuilder.AddMemoryGrainStorage("PubSubStore");
}
}
public ClusterFixture()
{
var builder = new TestClusterBuilder();
builder.AddSiloBuilderConfigurator<Configurator>();
this.Cluster = builder.Build();
this.Cluster.Deploy();
}
public void Dispose()
{
this.Cluster.StopAllSilos();
}
public TestCluster Cluster { get; private set; }
}
某些测试:
[Collection(ClusterCollection.Name)]
public class SomeTests
{
private readonly TestCluster _cluster;
public SomeTests(ClusterFixture fixture)
{
_cluster = fixture.Cluster;
}
[Fact]
public async Task SaysHelloCorrectly()
{
var hello = _cluster.GrainFactory.GetGrain<ISomeGrain>(Guid.NewGuid(), "<Some stream ID>", "Some.Class.Path");
var guid = Guid.NewGuid();
var streamProvider = _cluster.Client.GetStreamProvider("SMSProvider");
var photoStream = streamProvider.GetStream<string>(guid, "<Some stream ID>");
await photoStream.OnNextAsync("Hi");
Assert.Equal("Hello, World", "Hello, World");
}
}
设置需要改进。您可以从Orleans GitHub官方页面查看此示例:https://github.com/dotnet/orleans/blob/main/test/TesterInternal/StreamingTests/SMSStreamingTests.cs
因此,在ClusterFixture
类中,您应该添加SiloConfigurator
:
private class SiloConfigurator : ISiloConfigurator
{
public void Configure(ISiloBuilder hostBuilder) =>
hostBuilder.AddSimpleMessageStreamProvider("SMSProvider")
.AddMemoryGrainStorage("PubSubStore");
}
和ClientConfiguretor
:
private class ClientConfiguretor : IClientBuilderConfigurator
{
public void Configure(IConfiguration configuration, IClientBuilder clientBuilder) =>
clientBuilder.AddSimpleMessageStreamProvider("SMSProvider");
}
然后在您的ClusterFixture
构造函数中:
public ClusterFixture()
{
var builder = new TestClusterBuilder();
builder.AddSiloBuilderConfigurator<SiloConfigurator>();
builder.AddClientBuilderConfigurator<ClientConfiguretor>();
this.Cluster = builder.Build();
this.Cluster.Deploy();
}