-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShopModuleTests.cs
More file actions
149 lines (124 loc) · 5.61 KB
/
Copy pathShopModuleTests.cs
File metadata and controls
149 lines (124 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using Codefinity.EventEmitter.Sample.Inventory;
using Codefinity.EventEmitter.Sample.Orders;
using Codefinity.EventEmitter.Sample.Shipping;
using Codefinity.EventEmitter.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Codefinity.EventEmitter.Sample.Tests;
/// <summary>
/// Integration tests for the Orders, Inventory and Shipping modules, using EventEmitter.Testing.
/// Each test gets its own host, so state doesn't leak between tests.
/// </summary>
public sealed class ShopModuleTests : IAsyncLifetime
{
private IHost _host = null!;
private Scenario Scenario => _host.Services.GetRequiredService<Scenario>();
private PublishedEvents Published => _host.Services.GetRequiredService<PublishedEvents>();
private CarrierGateway Carrier => _host.Services.GetRequiredService<CarrierGateway>();
public async Task InitializeAsync()
{
var builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings { DisableDefaults = true });
builder.Services
.AddOrdersModule()
.AddInventoryModule()
.AddShippingModule()
.AddEventEmitterTesting();
_host = builder.Build();
await _host.StartAsync();
}
public async Task DisposeAsync()
{
await _host.StopAsync();
_host.Dispose();
}
[Fact]
public async Task Completing_an_order_reserves_stock()
{
// Wait for an event published by another assembly's background listener.
var reserved = await Scenario
.Stimulate(sp => sp.GetRequiredService<OrderService>().CompleteAsync("order-1", "alice"))
.AndWaitForEventOfType<StockReserved>()
.Matching(e => e.OrderId == "order-1")
.ToArriveAsync();
Assert.Equal(3, reserved.Items);
}
[Fact]
public async Task Completing_an_order_books_a_shipment()
{
// Wait for state changed two modules away.
var trackingNumber = await Scenario
.Stimulate(sp => sp.GetRequiredService<OrderService>().CompleteAsync("order-1", "alice"))
.AndWaitForStateChange(sp => sp.GetRequiredService<CarrierGateway>().Shipments.GetValueOrDefault("order-1"))
.ToHappenAsync();
Assert.Equal("TRK-order-1", trackingNumber);
}
[Fact]
public async Task Inventory_reacts_to_an_OrderCompleted_published_directly()
{
// Publish the event itself as the stimulus, bypassing OrderService.
await Scenario
.Publish(new OrderCompleted("order-5", "dave"))
.AndWaitForEventOfType<StockReserved>()
.Matching(e => e.OrderId == "order-5")
.ToArriveAsync(TimeSpan.FromSeconds(2));
}
[Fact]
public async Task Waits_for_a_custom_condition()
{
var shipmentCount = await Scenario
.Publish(new OrderCompleted("order-6", "erin"))
.AndWaitForStateChange(
sp => sp.GetRequiredService<CarrierGateway>().Shipments.Count,
acceptWhen: count => count >= 1)
.ToHappenAsync();
Assert.Equal(1, shipmentCount);
}
[Fact]
public async Task A_rolled_back_order_reaches_no_other_module()
{
await using (var scope = _host.Services.CreateAsyncScope())
{
await Assert.ThrowsAsync<InvalidOperationException>(() => scope.ServiceProvider
.GetRequiredService<OrderService>()
.CompleteAsync("order-2", "bob", failBeforeCommit: true));
}
// Waiting for StockReserved must time out: Inventory never saw the event.
await Assert.ThrowsAsync<TimeoutException>(() => Scenario
.Stimulate(_ => Task.CompletedTask)
.AndWaitForEventOfType<StockReserved>()
.ToArriveAsync(TimeSpan.FromMilliseconds(300)));
// OrderCompleted was published (and recorded) before the rollback.
Assert.Single(Published.OfType<OrderCompleted>().Matching(e => e.OrderId == "order-2"));
Assert.Empty(Published.OfType<StockReserved>());
}
[Fact]
public async Task Cancelling_an_order_publishes_an_order_event()
{
await Scenario
.Stimulate(sp => sp.GetRequiredService<OrderService>().CancelAsync("order-3", "changed mind"))
.AndWaitForEventOfType<IOrderEvent>()
.Matching(e => e is OrderCancelled { Reason: "changed mind" })
.ToArriveAsync();
}
[Fact]
public async Task Failed_shipments_can_be_resubmitted()
{
var incomplete = _host.Services.GetRequiredService<IIncompleteEventPublications>();
Carrier.IsAvailable = false;
await Scenario
.Stimulate(sp => sp.GetRequiredService<OrderService>().CompleteAsync("order-4", "carol"))
.AndWaitForStateChange(_ => HasFailedShipment(incomplete))
.ToHappenAsync();
Carrier.IsAvailable = true;
// The stimulus can be any async call, here the resubmit itself.
var trackingNumber = await Scenario
.Stimulate(sp => sp.GetRequiredService<IIncompleteEventPublications>()
.ResubmitAsync(p => p.ListenerId == "shipping.book-shipment"))
.AndWaitForStateChange(sp => sp.GetRequiredService<CarrierGateway>().Shipments.GetValueOrDefault("order-4"))
.ToHappenAsync();
Assert.Equal("TRK-order-4", trackingNumber);
}
// The in-memory repository completes synchronously, so this doesn't block.
private static bool HasFailedShipment(IIncompleteEventPublications incomplete) =>
incomplete.FindAllAsync().Result.Any(p => p.ListenerId == "shipping.book-shipment" && p.Attempts == 1);
}