|
| 1 | +using Akka.Actor; |
| 2 | +using Akka.Hosting; |
| 3 | +using Akka.Hosting.TestKit; |
| 4 | +using Akka.Persistence.Hosting; |
| 5 | +using Akka.TestKit; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Microsoft.Extensions.Hosting; |
| 8 | +using SqlSharding.Host.Actors; |
| 9 | +using SqlSharding.Shared; |
| 10 | +using SqlSharding.Shared.Commands; |
| 11 | +using SqlSharding.Shared.Queries; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace SqlSharding.Tests; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// Exercises <see cref="ProductTotalsActor"/>, the persistent (ReceivePersistentActor) |
| 18 | +/// shard entity, against the in-memory journal/snapshot store. These tests confirm |
| 19 | +/// command handling, the CommandResponse contract, and that state is recovered by |
| 20 | +/// replaying persisted events after an actor restart — WITHOUT requiring SQL Server. |
| 21 | +/// </summary> |
| 22 | +public class ProductTotalsActorTests : TestKit |
| 23 | +{ |
| 24 | + // Entity id used as the actor's persistenceId argument. ProductTotalsActor |
| 25 | + // prepends the "totals-" entity-name constant to form the real PersistenceId. |
| 26 | + private const string EntityId = "product-foo"; |
| 27 | + |
| 28 | + // Created inside ConfigureAkka's WithActors callback (NOT the constructor). |
| 29 | + // The TestKit base initializes Sys during InitializeAsync, which runs after |
| 30 | + // the constructor, so CreateTestProbe() in the constructor throws NRE. |
| 31 | + private TestProbe? _sender; |
| 32 | + private IActorRef? _totalsActor; |
| 33 | + |
| 34 | + protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider) |
| 35 | + { |
| 36 | + builder |
| 37 | + .WithInMemoryJournal() |
| 38 | + .WithInMemorySnapshotStore() |
| 39 | + .WithActors((system, registry, resolver) => |
| 40 | + { |
| 41 | + _sender = CreateTestProbe(); |
| 42 | + _totalsActor = system.ActorOf(resolver.Props<ProductTotalsActor>(EntityId), "totals-actor"); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public async Task CreateProduct_Persists_And_Replies_With_CommandResponse() |
| 48 | + { |
| 49 | + var actor = _totalsActor!; |
| 50 | + actor.Tell(new CreateProduct(EntityId, "Foo", 10.99m, 5, new[] { "tag-a" }), _sender!.Ref); |
| 51 | + |
| 52 | + var response = _sender.ExpectMsg<ProductCommandResponse>(TimeSpan.FromSeconds(5)); |
| 53 | + Assert.True(response.Success); |
| 54 | + Assert.Equal(EntityId, response.ProductId); |
| 55 | + // CreateProduct yields ProductCreated + ProductInventoryChanged. |
| 56 | + Assert.Equal(2, response.ResponseEvents.Count); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public async Task SupplyProduct_Updates_Inventory_Totals() |
| 61 | + { |
| 62 | + var actor = _totalsActor!; |
| 63 | + actor.Tell(new CreateProduct(EntityId, "Foo", 10.99m, 5, new[] { "tag-a" }), _sender!.Ref); |
| 64 | + _sender.ExpectMsg<ProductCommandResponse>(TimeSpan.FromSeconds(5)); |
| 65 | + |
| 66 | + actor.Tell(new SupplyProduct(EntityId, 10), _sender.Ref); |
| 67 | + var supply = _sender.ExpectMsg<ProductCommandResponse>(TimeSpan.FromSeconds(5)); |
| 68 | + Assert.True(supply.Success); |
| 69 | + |
| 70 | + // Verify the projected totals via the query. |
| 71 | + actor.Tell(new FetchProduct(EntityId), _sender.Ref); |
| 72 | + var result = _sender.ExpectMsg<FetchResult>(TimeSpan.FromSeconds(5)); |
| 73 | + Assert.Equal(15, result.State.Totals.RemainingInventory); |
| 74 | + Assert.Equal("Foo", result.State.Data.ProductName); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public async Task Restart_Replays_Events_And_Recovers_State() |
| 79 | + { |
| 80 | + var actor = _totalsActor!; |
| 81 | + // Build up some persisted state. |
| 82 | + actor.Tell(new CreateProduct(EntityId, "Foo", 10.99m, 5, new[] { "tag-a" }), _sender!.Ref); |
| 83 | + _sender.ExpectMsg<ProductCommandResponse>(TimeSpan.FromSeconds(5)); |
| 84 | + |
| 85 | + actor.Tell(new SupplyProduct(EntityId, 10), _sender.Ref); |
| 86 | + var supply = _sender.ExpectMsg<ProductCommandResponse>(TimeSpan.FromSeconds(5)); |
| 87 | + Assert.True(supply.Success); |
| 88 | + |
| 89 | + // Stop the actor and wait for termination before recreating it with the |
| 90 | + // same persistenceId so recovery must come from the journal replay. |
| 91 | + Watch(actor); |
| 92 | + actor.Tell(PoisonPill.Instance); |
| 93 | + ExpectTerminated(actor, TimeSpan.FromSeconds(5)); |
| 94 | + |
| 95 | + var recovered = Sys.ActorOf(Props.Create(() => new ProductTotalsActor(EntityId)), "totals-actor-recovered"); |
| 96 | + |
| 97 | + // After restart, querying must return the replayed state (remaining = 15). |
| 98 | + recovered.Tell(new FetchProduct(EntityId), _sender.Ref); |
| 99 | + var result = _sender.ExpectMsg<FetchResult>(TimeSpan.FromSeconds(5)); |
| 100 | + Assert.Equal(15, result.State.Totals.RemainingInventory); |
| 101 | + Assert.Equal("Foo", result.State.Data.ProductName); |
| 102 | + } |
| 103 | +} |
0 commit comments