Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MessagePumpAsync to catch error and create MessageMapp… #3011

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/Paramore.Brighter.ServiceActivator/MessagePumpAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,21 @@ private async void SendAsync(TRequest request)
{
await CommandProcessorProvider.Get().SendAsync(request, continueOnCapturedContext: true);
}

private async Task<TRequest> TranslateAsync(Message message)
{
var request = await _unwrapPipeline.UnwrapAsync(message);
return request;
try
{
return await _unwrapPipeline.UnwrapAsync(message);
}
catch (ConfigurationException)
{
throw;
}
catch (Exception exception)
{
throw new MessageMappingException($"Failed to map message {message.Id} using pipeline for type {typeof(TRequest).FullName} ", exception);
}
}

}
}
20 changes: 15 additions & 5 deletions tests/Paramore.Brighter.Extensions.Tests/TestTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Paramore.Brighter.Extensions.Tests;

public class TestTransform : IAmAMessageTransformAsync
public class TestTransform : IAmAMessageTransformAsync, IAmAMessageTransform
{
public List<object> WrapInitializerList { get; set; } = new List<object>();
public List<object> UnwrapInitializerList { get; set; } = new List<object>();
Expand All @@ -24,17 +24,27 @@ public void InitializeUnwrapFromAttributeParams(params object[] initializerList)
UnwrapInitializerList.AddRange(initializerList);
}

public async Task<Message> WrapAsync(Message message, CancellationToken cancellationToken)
public Message Wrap(Message message)
{
return message;
}

public Message Unwrap(Message message)
{
return message;
}

public Task<Message> WrapAsync(Message message, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<Message>(TaskCreationOptions.RunContinuationsAsynchronously);
tcs.SetResult(message);
return tcs.Task.Result;
return Task.FromResult(tcs.Task.Result);
}

public async Task<Message> UnwrapAsync(Message message, CancellationToken cancellationToken)
public Task<Message> UnwrapAsync(Message message, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<Message>(TaskCreationOptions.RunContinuationsAsynchronously);
tcs.SetResult(message);
return tcs.Task.Result;
return Task.FromResult(tcs.Task.Result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Paramore.Brighter.Extensions.Tests;
public class TransformerFactoryTests
{
private ServiceProviderTransformerFactory _transformFactory;
private ServiceProviderTransformerFactoryAsync _transformFactoryAsync;

[Fact]
public void When_resolving_a_transformer_from_the_factory()
Expand All @@ -27,6 +28,24 @@ public void When_resolving_a_transformer_from_the_factory()
testTransform.Should().NotBeNull();
}

[Fact]
public void When_resolving_a_transformer_from_the_factory_async()
{
//arrange
var collection = new ServiceCollection();
collection.AddSingleton(typeof(TestTransform),new TestTransform());
collection.AddSingleton<IBrighterOptions>(new BrighterOptions { TransformerLifetime = ServiceLifetime.Singleton });
var provider = collection.BuildServiceProvider(new ServiceProviderOptions{ValidateOnBuild = true});

_transformFactoryAsync = new ServiceProviderTransformerFactoryAsync(provider);

//act
var testTransform = _transformFactoryAsync.Create(typeof(TestTransform));

//assert
testTransform.Should().NotBeNull();
}

[Fact]
public void When_resolving_a_missing_transformer_from_the_factory()
{
Expand All @@ -43,4 +62,21 @@ public void When_resolving_a_missing_transformer_from_the_factory()
//assert
testTransform.Should().BeNull();
}

[Fact]
public void When_resolving_a_missing_transformer_from_the_factory_async()
{
//arrange
var collection = new ServiceCollection();
collection.AddSingleton<IBrighterOptions>(new BrighterOptions { TransformerLifetime = ServiceLifetime.Singleton });
var provider = collection.BuildServiceProvider();

_transformFactoryAsync = new ServiceProviderTransformerFactoryAsync(provider);

//act
var testTransform = _transformFactoryAsync.Create(typeof(TestTransform));

//assert
testTransform.Should().BeNull();
}
}
Loading