Client side Callbacks Testing

Project Hosting
NuGet Package NServiceBus.Callbacks.Testing (3-pre)
Target NServiceBus Version: 7.x
This page targets a pre-release version and is subject to change prior to the final release.

Shows the usage of the NServiceBus.Callback.Testing.

Prerequisites for callback testing functionality

The approach shown here works with the NServiceBus.Callbacks NuGet package version 1.1 or greater. Install the NServiceBus.Callbacks.Testing NuGet package.

Int

The integer response scenario allows any integer value to be returned in a strongly typed manner.

Testing

The response type returned by the When definition needs to be of type int.

var request = new Message();
var simulatedResponse = 42;

var session = new TestableCallbackAwareSession();
session.When(
    matcher: (Message message) =>
    {
        return message == request;
    },
    response: simulatedResponse);

var result = await session.Request<int>(request)
    .ConfigureAwait(false);

Assert.AreEqual(simulatedResponse, result);

Enum

The enum response scenario allows any enum value to be returned in a strongly typed manner.

Testing

The response type returned by the When definition needs to be of the enum type expected.

var request = new Message();
var simulatedResponse = Status.OK;

var session = new TestableCallbackAwareSession();
session.When(
    matcher: (Message message) =>
    {
        return message == request;
    },
    response: simulatedResponse);

var result = await session.Request<Status>(request)
    .ConfigureAwait(false);

Assert.AreEqual(simulatedResponse, result);

Object

The Object response scenario allows an object instance to be returned.

Testing

The response type returned by the When definition needs to be of the object response type expected.

var request = new Message();
var simulatedResponse = new ResponseMessage();

var session = new TestableCallbackAwareSession();
session.When(
    matcher: (Message message) =>
    {
        return message == request;
    },
    response: simulatedResponse);

var result = await session.Request<ResponseMessage>(request)
    .ConfigureAwait(false);

Assert.AreEqual(simulatedResponse, result);

Testing with SendOptions

The When definition provides a matcher overload which allows matching against the response and the send options passed into the callback function.

var request = new Message();
var simulatedResponse = new ResponseMessage();

var session = new TestableCallbackAwareSession();
session.When(
    matcher: (Message message, SendOptions options) =>
    {
        return message == request && options.GetHeaders()
                   .ContainsKey("Simulated.Header");
    },
    response: simulatedResponse);

var sendOptions = new SendOptions();
sendOptions.SetHeader("Simulated.Header", "value");
var result = await session.Request<ResponseMessage>(request, sendOptions)
    .ConfigureAwait(false);

Assert.AreEqual(simulatedResponse, result);

Cancellation

The asynchronous callback can be canceled by registering a CancellationToken provided by a CancellationTokenSource.

Testing

var tokenSource = new CancellationTokenSource();

var request = new Message();

var session = new TestableCallbackAwareSession();
session.When(
    matcher: (Message message) =>
    {
        // When request matches, cancel the token source
        if (message == request)
        {
            tokenSource.Cancel();
        }
        return false;
    },
    response: 42);

Assert.ThrowsAsync<OperationCanceledException>(
    @delegate: async () =>
    {
        await session.Request<int>(request, tokenSource.Token)
            .ConfigureAwait(false);
    });

Samples

Related Articles

  • Client side Callbacks
    The client (or sending process) has its own queue. When messages arrive in the queue, they are handled by a message handler.

Last modified