masstransit中的InMemoryTransportCache发生了什么,现在是否有任何选项可以测试内存中两个busControl之间的连接?
答案 0 :(得分:0)
您要测试内存中有2个不同的主机通过总线进行通信,对吗?
您可以让服务共享相同的IBusControl实例,并使用“ ConnectRecieveEndpoint”方法注册每个使用者。
一个简短的示例来说明“ ConnectReceiveEndpoint”的用法:
public class TestMessage { public string Text { get; set; } }
[Test]
public async Task Can_publish_and_receive_message()
{
var consumer1Semaphore = new SemaphoreSlim(0);
var consumer2Semaphore = new SemaphoreSlim(0);
IInMemoryHost host = null;
var busControl = MassTransit.Bus.Factory.CreateUsingInMemory(inMemoryBusFactoryConfigurator =>
{
host = inMemoryBusFactoryConfigurator.Host;
inMemoryBusFactoryConfigurator.ReceiveEndpoint("consumer1", ep =>
{
ep.Handler<TestMessage>(context =>
{
context.Message.Text.Should().Be("Hi");
consumer1Semaphore.Release();
return Task.CompletedTask;
});
});
});
host.ConnectReceiveEndpoint("consumer2", inMemoryReceiveEndpointConfigurator =>
{
inMemoryReceiveEndpointConfigurator.Handler<TestMessage>(context =>
{
context.Message.Text.Should().Be("Hi");
consumer2Semaphore.Release();
return Task.CompletedTask;
});
});
await busControl.StartAsync();
await busControl.Publish(new TestMessage{Text = "Hi"});
(await consumer1Semaphore.WaitAsync(2.Seconds())).Should().BeTrue();
(await consumer2Semaphore.WaitAsync(2.Seconds())).Should().BeTrue();
await busControl.StopAsync();
}
测试2种虚构服务的简单方法; ServiceA和ServiceB(不在实际环境中共同托管)。下面的IBusControlFactory用于交换掉Azure Service Bus传输,以支持测试中的“内存中”传输。
public interface IBusControlFactory
{
IBusControl Create(string queueName = null, Action<IReceiveEndpointConfigurator> configurator = null);
}
// Test replacement for a factory that normally creates a Azure Service Bus bus instance
public class Harness : IBusControlFactory, IDisposable
{
public readonly IBusControl BusControl;
private IInMemoryHost _host;
public Harness() => BusControl = MassTransit.Bus.Factory.CreateUsingInMemory(configure: cfg => _host = cfg.Host);
public static Harness Start() => new Harness();
public IBusControl Create(string queueName = null, Action<IReceiveEndpointConfigurator> configurator = null)
{
if(queueName != null && configurator != null)
_host.ConnectReceiveEndpoint(queueName, configurator);
return BusControl;
}
public void Dispose() => BusControl.Stop();
}
public class ServiceA
{
private readonly IBus _bus;
public ServiceA(IBusControlFactory factory) => _bus = factory.Create();
public Task SayHi() => _bus.Publish(new TestMessage { Text = "Hi" });
}
public class ServiceB
{
public ServiceB(IBusControlFactory factory)
{
factory.Create("consumer2", ep =>
{
ep.Handler<TestMessage>(context =>
{
context.Message.Text.Should().Be("Hi");
return Task.CompletedTask;
});
});
}
}
[Test]
public async Task Can_publish_and_receive_message_take_2()
{
using (var harness = Harness.Start())
{
var serviceA = new ServiceA(harness);
var serviceB = new ServiceB(harness);
var spy = new Spy();
harness.BusControl.ConnectConsumeMessageObserver(spy);
await serviceA.SayHi();
await spy.WaitOne(2.Seconds());
}
}
public class Spy : IConsumeMessageObserver<TestMessage>
{
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);
public Task PreConsume(ConsumeContext<TestMessage> context) => Task.CompletedTask;
public Task PostConsume(ConsumeContext<TestMessage> context)
{
_semaphore.Release();
return Task.CompletedTask;
}
public Task ConsumeFault(ConsumeContext<TestMessage> context, Exception exception) => Task.CompletedTask;
public async Task WaitOne(TimeSpan timeout) { (await _semaphore.WaitAsync(timeout)).Should().BeTrue(); }
}
答案 1 :(得分:0)
似乎已被重构。
class AModelAdmin(admin.ModelAdmin):
list_display = ['eth_address']
def get_fields(self, request, obj=None):
fields = super().get_fields(request, obj)
if obj:
fields.remove('private_key')
return fields
admin.site.register(A, AAdmin)
将永远不会解析另一个总线GetSendEndpoint
实例,因为:
ISendEndpoint
忽略Uri(var queueName = address.AbsolutePath.Split('/').Last();
)中的“虚拟主机”
有一个测试有两个总线实例loopback://localhost/VirtualHost/QueueName
,它只是通过总线实例进行通信。
我们的选项是在测试时查找总线实例/发送端点或具有唯一队列的单个共享总线。
编辑:
处理程序内部第一个解决方案InMemoryDuo_Specs.cs
。ConsumeContext
的另一个问题也不起作用,因此您必须以一种肮脏的方式来获取正确的实例。