forked from MassTransit/MassTransit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added WhenAllCompletedOrFaulted to Durable Future
- Loading branch information
Showing
11 changed files
with
263 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace MassTransit.TestFramework.Futures; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
|
||
public interface BatchCompleted | ||
{ | ||
public Guid CorrelationId { get; } | ||
public IReadOnlyList<string> ProcessedJobsNumbers { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace MassTransit.TestFramework.Futures; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
|
||
public interface BatchFaulted | ||
{ | ||
public Guid CorrelationId { get; } | ||
public IReadOnlyList<string> ProcessedJobsNumbers { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace MassTransit.TestFramework.Futures; | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
|
||
public class BatchFuture : | ||
Future<BatchRequest, BatchCompleted, BatchFaulted> | ||
{ | ||
public BatchFuture() | ||
{ | ||
ConfigureCommand(x => x.CorrelateById(context => context.Message.CorrelationId)); | ||
|
||
SendRequests<string, ProcessBatchItem>(x => x.JobNumbers, | ||
x => | ||
{ | ||
x.UsingRequestInitializer(context => new | ||
{ | ||
CorrelationId = InVar.Id, | ||
JobNumber = context.Message | ||
}); | ||
x.TrackPendingRequest(message => message.CorrelationId); | ||
}) | ||
.OnResponseReceived<ProcessBatchItemCompleted>(x => | ||
{ | ||
x.CompletePendingRequest(y => y.CorrelationId); | ||
}); | ||
|
||
WhenAllCompleted(r => r.SetCompletedUsingInitializer(MapResponse)); | ||
WhenAllCompletedOrFaulted(r => r.SetFaultedUsingInitializer(MapResponse)); | ||
} | ||
|
||
object MapResponse(BehaviorContext<FutureState> context) | ||
{ | ||
var command = context.GetCommand<BatchRequest>(); | ||
List<string> processedJobNumbers = context | ||
.SelectResults<ProcessBatchItemCompleted>() | ||
.Select(r => r.JobNumber).ToList(); | ||
|
||
return new | ||
{ | ||
command.CorrelationId, | ||
ProcessedJobsNumbers = processedJobNumbers | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace MassTransit.TestFramework.Futures; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
|
||
public interface BatchRequest | ||
{ | ||
public DateTime? BatchExpiry { get; } | ||
public Guid CorrelationId { get; } | ||
public IReadOnlyList<string> JobNumbers { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace MassTransit.TestFramework.Futures; | ||
|
||
using System; | ||
|
||
|
||
public interface ProcessBatchItem | ||
{ | ||
public Guid CorrelationId { get; } | ||
public string JobNumber { get; } | ||
} |
10 changes: 10 additions & 0 deletions
10
src/MassTransit.TestFramework/Futures/ProcessBatchItemCompleted.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace MassTransit.TestFramework.Futures; | ||
|
||
using System; | ||
|
||
|
||
public interface ProcessBatchItemCompleted | ||
{ | ||
public Guid CorrelationId { get; } | ||
public string JobNumber { get; } | ||
} |
29 changes: 29 additions & 0 deletions
29
src/MassTransit.TestFramework/Futures/ProcessBatchItemConsumer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace MassTransit.TestFramework.Futures; | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
|
||
|
||
public class ProcessBatchItemConsumer : | ||
IConsumer<ProcessBatchItem> | ||
{ | ||
public Task Consume(ConsumeContext<ProcessBatchItem> context) | ||
{ | ||
async Task WaitAndRespond(int milliSecond) | ||
{ | ||
await Task.Delay(milliSecond); | ||
await context.RespondAsync<ProcessBatchItemCompleted>(new | ||
{ | ||
context.Message.CorrelationId, | ||
context.Message.JobNumber | ||
}); | ||
} | ||
|
||
return context.Message.JobNumber switch | ||
{ | ||
"Delay" => WaitAndRespond(2000), | ||
"Error" => throw new InvalidOperationException(), | ||
_ => WaitAndRespond(0) | ||
}; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/MassTransit.TestFramework/Futures/Tests/BatchFuture_Specs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace MassTransit.TestFramework.Futures.Tests; | ||
|
||
using NUnit.Framework; | ||
|
||
|
||
[TestFixture] | ||
public class BatchFuture_Specs : | ||
FutureTestFixture | ||
{ | ||
public BatchFuture_Specs(IFutureTestFixtureConfigurator testFixtureConfigurator) | ||
: base(testFixtureConfigurator) | ||
{ | ||
} | ||
|
||
protected override void ConfigureMassTransit(IBusRegistrationConfigurator configurator) | ||
{ | ||
configurator.AddConsumer<ProcessBatchItemConsumer>(); | ||
configurator.AddFuture<BatchFuture>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
tests/MassTransit.Tests/ContainerTests/Scenarios/WhenAllCompletedOrFaulted.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
namespace MassTransit.Tests.ContainerTests.Scenarios; | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NUnit.Framework; | ||
using TestFramework.Futures; | ||
using TestFramework.Futures.Tests; | ||
|
||
|
||
[TestFixture] | ||
public class WhenAllCompletedOrFaulted : | ||
BatchFuture_Specs | ||
{ | ||
[Test] | ||
public async Task Delayed_success() | ||
{ | ||
var batchId = NewId.NextGuid(); | ||
var jobNumbers = new[] { "C12345", "Delay" }; | ||
|
||
var scope = Provider.CreateScope(); | ||
|
||
var client = scope.ServiceProvider.GetRequiredService<IRequestClient<BatchRequest>>(); | ||
|
||
Response<BatchCompleted> response = await client.GetResponse<BatchCompleted>(new | ||
{ | ||
CorrelationId = batchId, | ||
JobNumbers = jobNumbers | ||
}, timeout: RequestTimeout.After(s: 5)); | ||
|
||
Assert.That(response.Message.ProcessedJobsNumbers, Is.EqualTo(jobNumbers)); | ||
} | ||
|
||
[Test] | ||
public async Task Error_partially_uploaded() | ||
{ | ||
var batchId = NewId.NextGuid(); | ||
var jobNumbers = new[] { "C12345", "Error", "C54321", "Error", "C33454" }; | ||
|
||
var scope = Provider.CreateScope(); | ||
|
||
var client = scope.ServiceProvider.GetRequiredService<IRequestClient<BatchRequest>>(); | ||
|
||
Response response = await client.GetResponse<BatchCompleted, BatchFaulted>(new | ||
{ | ||
CorrelationId = batchId, | ||
JobNumbers = jobNumbers | ||
}); | ||
|
||
switch (response) | ||
{ | ||
case (_, BatchFaulted faulted): | ||
//Batch is partially successful, downstream consumers are notified of succeeded uploads | ||
Assert.That(faulted.ProcessedJobsNumbers, Is.EquivalentTo(new[] { "C12345", "C54321", "C33454" })); | ||
break; | ||
default: | ||
Assert.Fail("Unexpected response"); | ||
break; | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task Should_succeed() | ||
{ | ||
var batchId = NewId.NextGuid(); | ||
var jobNumbers = new[] { "C12345", "C54321" }; | ||
|
||
var scope = Provider.CreateScope(); | ||
|
||
var client = scope.ServiceProvider.GetRequiredService<IRequestClient<BatchRequest>>(); | ||
|
||
Response<BatchCompleted> response = await client.GetResponse<BatchCompleted>(new | ||
{ | ||
CorrelationId = batchId, | ||
JobNumbers = jobNumbers | ||
}, timeout: RequestTimeout.After(s: 5)); | ||
|
||
Assert.That(response.Message.ProcessedJobsNumbers, Is.EquivalentTo(jobNumbers)); | ||
} | ||
|
||
public WhenAllCompletedOrFaulted() | ||
: base(new InMemoryFutureTestFixtureConfigurator()) | ||
{ | ||
} | ||
} |