gpt4 book ai didi

c# - 为什么 NetMQ 不能在 NUnit 环境中工作

转载 作者:太空狗 更新时间:2023-10-30 01:28:48 26 4
gpt4 key购买 nike

我已经用我的 NetMQ 实现创建了一个要点,因为我觉得粘贴在这里有点多:https://gist.github.com/gthvidsten/e626d7e6c51012b1ba152d22e034d93d

如果我在 .Net Core 控制台应用程序中执行以下操作,一切正常并且我收到 MessageReceived 事件:

static void Main(string[] args)
{
_transportWithHost = new NetMqTransport(
"tcp://localhost:9990",
"tcp://localhost:9991",
true);
_transportWithHost.Start();

Console.WriteLine("Press [Enter] to publish");
Console.ReadLine();

_transportWithHost.MessageReceived += (sender, e) =>
{
; // Breakpoints here are hit
};
_transportWithHost.Publish(new byte[] { 1, 2, 3, 4 });

Console.WriteLine("Press [Enter] to exit");
Console.ReadLine();
}

但是,如果我尝试在 NUnit 测试环境中执行相同的操作,MessageReceived 事件永远不会触发!

class NetMqTransportTests
{
private NetMqTransport _transportWithHost;

[OneTimeSetUp]
public void Setup()
{
_transportWithHost = new NetMqTransport(
"tcp://localhost:9990",
"tcp://localhost:9991",
true);
_transportWithHost.Start();
}

[Test]
public void PublishTest()
{
ManualResetEvent mre = new ManualResetEvent(false);

_transportWithHost.MessageReceived += (sender, e) =>
{
mre.Set();
// Breakpoints here are never hit as MessageReceived is never called
};

_transportWithHost.Publish(new byte[] { 1, 2, 3, 4 });

bool eventFired = mre.WaitOne(new TimeSpan(0, 0, 5));
Assert.True(eventFired);
}
}

为什么几乎相同的代码在控制台应用程序中有效,但在 NUnit 环境中却无效?

最佳答案

我能够重现它并找到这个线程 https://github.com/zeromq/netmq/issues/482这表明启动发布者和订阅者接收消息的时间之间存在时间问题。

using NetMQ;
using NetMQ.Sockets;
using NUnit.Framework;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Tests
{
class NetMqTransportTests
{
[Test]
public void TestMulticastNetMQ()
{
bool wasCalled = false;

var coreEventbus = "tcp://localhost:12345";

Task.Run(() =>
{
using (var subSocket = new SubscriberSocket())
{
subSocket.Connect(coreEventbus);
subSocket.Subscribe("account");

while (true)
{
string messageTopicReceived = subSocket.ReceiveFrameString();
string messageReceived = subSocket.ReceiveFrameString();

Assert.IsTrue(messageReceived == "testing");
wasCalled = true;
break;
}
}

});

Thread.Sleep(TimeSpan.FromSeconds(1));

using (var pubSocket = new PublisherSocket())
{
pubSocket.Bind(coreEventbus);

Thread.Sleep(500);
pubSocket.SendMoreFrame("account").SendFrame("testing");
}

Thread.Sleep(TimeSpan.FromSeconds(5));

Assert.IsTrue(wasCalled);
}
}
}

更新:

以下是 NetMQ 库附带的单元测试:https://github.com/zeromq/netmq/blob/master/src/NetMQ.Tests/XPubSubTests.cs

了解他们如何将 NetMqTransport 的实例化分解为同时使用 XPublisherSocket 和 XPublisherSocket...

另请注意,根据问题 482,他们会延迟 500 毫秒让订阅者在收到消息之前连接,就像他们在问题中谈论的那样:

[Fact]
public void TopicPubSub()
{
using (var pub = new XPublisherSocket())
using (var sub = new XPublisherSocket())
{
var port = pub.BindRandomPort("tcp://127.0.0.1");
sub.Connect("tcp://127.0.0.1:" + port);
sub.SendFrame(new byte[] { 1, (byte)'A' });

// let the subscriber connect to the publisher before sending a message
Thread.Sleep(500);

var msg = pub.ReceiveFrameBytes();

关于c# - 为什么 NetMQ 不能在 NUnit 环境中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56975021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com