On 5/18/2016 @bording wrote:
Since both the requesting and replying endpoints need the package installed ...
Endpoint requires instance discriminator even when only replying to a callback
I've read through the documentation (listed on Add usage examples / documentation to Readme.MD) and I haven't seen this requirement listed anywhere.
Is this statement accurate? I was hoping to use this library to upgrade some integration testing code I've written to NSB 6. On NSB 5 I wrote a simple library that I could use from an integration test I could use to Send
a message to a handler (running on my actual host) and wait for a Reply
:
public class NServiceBusNUnitTestHarnessHost{
public IBus Bus {get;} // initialized in constructor (code removed for brevity)
public void SendMessageAndWaitForResult<TRequest, TResponse>(
string queue, TRequest message, Action<TResponse> continuation, int timeoutInMilliseconds = 60 * 1000)
where TRequest : IMessage
where TResponse : class, IMessage
{
var task =
Task.Run(
async () =>
{
var cb = Bus.Send(queue, message);
var replyMessage =
await cb.Register(cr =>
{
if (null == cr.Messages)
return null;
return
(cr.Messages ?? new object[0])
.OfType<TResponse>()
.FirstOrDefault();
});
if (null != replyMessage)
continuation(replyMessage);
});
if (!Task.WaitAll(new[] { task }, timeoutInMilliseconds))
throw new Exception(
$"Did not receive expected response [{typeof(TResponse).Name}] in the given timeout [{timeoutInMilliseconds / 1000} seconds]");
}
}
then from my test, I could write something like
[Test}]
public void ExampleTest()
{
Result expectedResult;
new NServiceBusNUnitTestHarnessHost().SendMessageAndWaitForResult<Request, Result>(
"example-queue",
new Request(),
r => expectedResult = r);
// Asserts on expectedResult
}
This solution did not require any modification to the handler listening on "example-queue"
.
Question: Is this library not meant for this use case? Or will I now need to modify the project containing my handlers to support integration testing?
I had hoped to re-write my Test as:
[Test}]
public async Task ExampleTest()
{
IEndpointInstance endpoint; // initialization code removed for brevity
var expectedResult =
await endpoint.Request<Result>(
new Request(),
"example-queue");
// Asserts on expectedResult
}