Skip to content

Commit

Permalink
[Core] Incorporate CancellationToken and/or IConsumerContext to the C…
Browse files Browse the repository at this point in the history
…onsumer interfaces

Signed-off-by: Tomasz Maruszak <maruszaktomasz@gmail.com>
  • Loading branch information
zarusz committed Jun 1, 2024
1 parent 606d219 commit 90f4277
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,71 +39,95 @@ public void When_PathSet_Given_Path_Then_Path_ProperlySet()
subject.ConsumerSettings.PathKind.Should().Be(PathKind.Topic);
}

[Fact]
public void When_Configured_Given_RequestResponse_Then_ProperSettings()
[Theory]
[InlineData(false)]
[InlineData(true)]
public void When_Configured_Given_RequestResponse_Then_ProperSettings(bool ofContext)
{
// arrange
var path = "topic";

var consumerContextMock = new Mock<IConsumerContext>();
consumerContextMock.SetupGet(x => x.CancellationToken).Returns(new CancellationToken());

var consumerType = ofContext ? typeof(SomeRequestMessageHandlerOfContext) : typeof(SomeRequestMessageHandler);

// act
var subject = new HandlerBuilder<SomeRequest, SomeResponse>(messageBusSettings)
.Topic(path)
.Instances(3)
.WithHandler<SomeRequestMessageHandler>();
.Instances(3);

if (ofContext)
{
subject.WithHandlerOfContext<SomeRequestMessageHandlerOfContext>();
}
else
{
subject.WithHandler<SomeRequestMessageHandler>();
}

// assert
subject.ConsumerSettings.MessageType.Should().Be(typeof(SomeRequest));
subject.ConsumerSettings.Path.Should().Be(path);
subject.ConsumerSettings.Instances.Should().Be(3);

subject.ConsumerSettings.ConsumerType.Should().Be(typeof(SomeRequestMessageHandler));
subject.ConsumerSettings.ConsumerType.Should().Be(consumerType);
subject.ConsumerSettings.ConsumerMode.Should().Be(ConsumerMode.RequestResponse);

subject.ConsumerSettings.ResponseType.Should().Be(typeof(SomeResponse));

subject.ConsumerSettings.Invokers.Count.Should().Be(1);

var consumerInvokerSettings = subject.ConsumerSettings.Invokers.Single(x => x.MessageType == typeof(SomeRequest));
var consumerInvokerSettings = subject.ConsumerSettings.Invokers.Single();
consumerInvokerSettings.MessageType.Should().Be(typeof(SomeRequest));
consumerInvokerSettings.ConsumerType.Should().Be(typeof(SomeRequestMessageHandler));
Func<Task> call = () => consumerInvokerSettings.ConsumerMethod(new SomeRequestMessageHandler(), new SomeRequest(), consumerContextMock.Object, consumerContextMock.Object.CancellationToken);
consumerInvokerSettings.ConsumerType.Should().Be(consumerType);
Func<Task> call = () => consumerInvokerSettings.ConsumerMethod(ofContext ? new SomeRequestMessageHandlerOfContext() : new SomeRequestMessageHandler(), new SomeRequest(), consumerContextMock.Object, consumerContextMock.Object.CancellationToken);
call.Should().ThrowAsync<NotImplementedException>().WithMessage(nameof(SomeRequest));
}

[Fact]
public void When_Configured_Given_RequestWithoutResponse_Then_ProperSettings()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void When_Configured_Given_RequestWithoutResponse_Then_ProperSettings(bool ofContext)
{
// arrange
var path = "topic";

var consumerContextMock = new Mock<IConsumerContext>();
consumerContextMock.SetupGet(x => x.CancellationToken).Returns(new CancellationToken());

var consumerType = ofContext ? typeof(SomeRequestWithoutResponseHandlerOfContext) : typeof(SomeRequestWithoutResponseHandler);

// act
var subject = new HandlerBuilder<SomeRequestWithoutResponse>(messageBusSettings)
.Topic(path)
.Instances(3)
.WithHandler<SomeRequestWithoutResponseHandler>();
.Instances(3);

if (ofContext)
{
subject.WithHandlerOfContext<SomeRequestWithoutResponseHandlerOfContext>();
}
else
{
subject.WithHandler<SomeRequestWithoutResponseHandler>();
}

// assert
subject.ConsumerSettings.MessageType.Should().Be(typeof(SomeRequestWithoutResponse));
subject.ConsumerSettings.Path.Should().Be(path);
subject.ConsumerSettings.Instances.Should().Be(3);

subject.ConsumerSettings.ConsumerType.Should().Be(typeof(SomeRequestWithoutResponseHandler));
subject.ConsumerSettings.ConsumerType.Should().Be(consumerType);
subject.ConsumerSettings.ConsumerMode.Should().Be(ConsumerMode.RequestResponse);

subject.ConsumerSettings.ResponseType.Should().BeNull();

subject.ConsumerSettings.Invokers.Count.Should().Be(1);

var consumerInvokerSettings = subject.ConsumerSettings.Invokers.Single(x => x.MessageType == typeof(SomeRequestWithoutResponse));
var consumerInvokerSettings = subject.ConsumerSettings.Invokers.Single();
consumerInvokerSettings.MessageType.Should().Be(typeof(SomeRequestWithoutResponse));
consumerInvokerSettings.ConsumerType.Should().Be(typeof(SomeRequestWithoutResponseHandler));
Func<Task> call = () => consumerInvokerSettings.ConsumerMethod(new SomeRequestWithoutResponseHandler(), new SomeRequestWithoutResponse(), consumerContextMock.Object, consumerContextMock.Object.CancellationToken);
consumerInvokerSettings.ConsumerType.Should().Be(consumerType);
Func<Task> call = () => consumerInvokerSettings.ConsumerMethod(ofContext ? new SomeRequestWithoutResponseHandlerOfContext() : new SomeRequestWithoutResponseHandler(), new SomeRequestWithoutResponse(), consumerContextMock.Object, consumerContextMock.Object.CancellationToken);
call.Should().ThrowAsync<NotImplementedException>().WithMessage(nameof(SomeRequestWithoutResponse));
}
}
13 changes: 13 additions & 0 deletions src/Tests/SlimMessageBus.Host.Configuration.Test/SampleMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,21 @@ public Task<SomeResponse> OnHandle(SomeRequest request, CancellationToken cancel
=> throw new NotImplementedException(nameof(SomeRequest));
}

public class SomeRequestMessageHandlerOfContext : IRequestHandler<IConsumerContext<SomeRequest>, SomeResponse>
{
public Task<SomeResponse> OnHandle(IConsumerContext<SomeRequest> request, CancellationToken cancellationToken)
=> throw new NotImplementedException(nameof(SomeRequest));
}


public class SomeRequestWithoutResponseHandler : IRequestHandler<SomeRequestWithoutResponse>
{
public Task OnHandle(SomeRequestWithoutResponse request, CancellationToken cancellationToken)
=> throw new NotImplementedException(nameof(SomeRequestWithoutResponse));
}

public class SomeRequestWithoutResponseHandlerOfContext : IRequestHandler<IConsumerContext<SomeRequestWithoutResponse>>
{
public Task OnHandle(IConsumerContext<SomeRequestWithoutResponse> request, CancellationToken cancellationToken)
=> throw new NotImplementedException(nameof(SomeRequestWithoutResponse));
}

0 comments on commit 90f4277

Please sign in to comment.