forked from akkadotnet/akka.net
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWrappedShardBufferedMessageSpec.cs
250 lines (204 loc) · 7.96 KB
/
WrappedShardBufferedMessageSpec.cs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// -----------------------------------------------------------------------
// <copyright file="WrappedShardBufferedMessageSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2025 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2025 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------
using System.Collections.Immutable;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Cluster.Sharding.Internal;
using Akka.Cluster.Tools.Singleton;
using Akka.Configuration;
using Akka.Event;
using Akka.TestKit;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace Akka.Cluster.Sharding.Tests;
public class WrappedShardBufferedMessageSpec: AkkaSpec
{
#region Custom Classes
private sealed class Message
{
public static readonly Message Instance = new();
public string Payload { get; } = Msg;
private Message()
{ }
public override string ToString()
=> $"[Message: {Payload}]";
}
private sealed class MyEnvelope : IWrappedMessage
{
public MyEnvelope(object message)
{
Message = message;
}
public object Message { get; }
}
private sealed class BufferMessageAdapter: IShardingBufferMessageAdapter
{
public object Apply(object message, IActorContext context)
=> new MyEnvelope(message);
public object UnApply(object message, IActorContext context)
{
return message is MyEnvelope envelope ? envelope.Message : message;
}
}
private class EchoActor: UntypedActor
{
private readonly ILoggingAdapter _log = Context.GetLogger();
protected override void OnReceive(object message)
{
_log.Info($">>>> OnReceive {message}");
if(message is string)
Sender.Tell(message);
else
Unhandled(message);
}
}
private sealed class FakeRememberEntitiesProvider: IRememberEntitiesProvider
{
private readonly IActorRef _probe;
public FakeRememberEntitiesProvider(IActorRef probe)
{
_probe = probe;
}
public Props CoordinatorStoreProps() => FakeCoordinatorStoreActor.Props();
public Props ShardStoreProps(string shardId) => FakeShardStoreActor.Props(shardId, _probe);
}
private class ShardStoreCreated
{
public ShardStoreCreated(IActorRef store, string shardId)
{
Store = store;
ShardId = shardId;
}
public IActorRef Store { get; }
public string ShardId { get; }
}
private class CoordinatorStoreCreated
{
public CoordinatorStoreCreated(IActorRef store)
{
Store = store;
}
public IActorRef Store { get; }
}
private class FakeShardStoreActor : ActorBase
{
public static Props Props(string shardId, IActorRef probe) => Actor.Props.Create(() => new FakeShardStoreActor(shardId, probe));
private readonly string _shardId;
private readonly IActorRef _probe;
public FakeShardStoreActor(string shardId, IActorRef probe)
{
_shardId = shardId;
_probe = probe;
Context.System.EventStream.Publish(new ShardStoreCreated(Self, shardId));
}
protected override bool Receive(object message)
{
switch (message)
{
case RememberEntitiesShardStore.GetEntities:
Sender.Tell(new RememberEntitiesShardStore.RememberedEntities(ImmutableHashSet<string>.Empty));
return true;
case RememberEntitiesShardStore.Update m:
_probe.Tell(new RememberEntitiesShardStore.UpdateDone(m.Started, m.Stopped));
return true;
}
return false;
}
}
private class FakeCoordinatorStoreActor : ActorBase
{
public static Props Props() => Actor.Props.Create(() => new FakeCoordinatorStoreActor());
public FakeCoordinatorStoreActor()
{
Context.System.EventStream.Publish(new CoordinatorStoreCreated(Context.Self));
}
protected override bool Receive(object message)
{
switch (message)
{
case RememberEntitiesCoordinatorStore.GetShards _:
Sender.Tell(new RememberEntitiesCoordinatorStore.RememberedShards(ImmutableHashSet<string>.Empty));
return true;
case RememberEntitiesCoordinatorStore.AddShard m:
Sender.Tell(new RememberEntitiesCoordinatorStore.UpdateDone(m.ShardId));
return true;
}
return false;
}
}
private static Config GetConfig()
{
return ConfigurationFactory.ParseString(@"
akka.loglevel=DEBUG
akka.actor.provider = cluster
akka.remote.dot-netty.tcp.port = 0
akka.cluster.sharding.state-store-mode = ddata
akka.cluster.sharding.remember-entities = on
# no leaks between test runs thank you
akka.cluster.sharding.distributed-data.durable.keys = []
akka.cluster.sharding.verbose-debug-logging = on
akka.cluster.sharding.fail-on-invalid-entity-state-transition = on")
.WithFallback(Sharding.ClusterSharding.DefaultConfig())
.WithFallback(DistributedData.DistributedData.DefaultConfig())
.WithFallback(ClusterSingleton.DefaultConfig());
}
private class MessageExtractor: HashCodeMessageExtractor
{
public MessageExtractor() : base(10)
{
}
public override string EntityId(object message)
{
return message switch
{
Message m => m.Payload,
Passivate => Msg,
_ => null
};
}
}
#endregion
private const string Msg = "hit";
private readonly IActorRef _shard;
private IActorRef _store;
public WrappedShardBufferedMessageSpec(ITestOutputHelper output) : base(GetConfig(), output)
{
Sys.EventStream.Subscribe(TestActor, typeof(ShardStoreCreated));
Sys.EventStream.Subscribe(TestActor, typeof(CoordinatorStoreCreated));
_shard = ChildActorOf(Shard.Props(
typeName: "test",
shardId: "test",
entityProps: _ => Props.Create(() => new EchoActor()),
settings: ClusterShardingSettings.Create(Sys),
extractor: new ExtractorAdapter(new MessageExtractor()),
handOffStopMessage: PoisonPill.Instance,
rememberEntitiesProvider: new FakeRememberEntitiesProvider(TestActor),
bufferMessageAdapter: new BufferMessageAdapter()));
}
private async Task<RememberEntitiesShardStore.UpdateDone> ExpectShardStartup()
{
var createdEvent = await ExpectMsgAsync<ShardStoreCreated>();
createdEvent.ShardId.Should().Be("test");
_store = createdEvent.Store;
await ExpectMsgAsync<ShardInitialized>();
_shard.Tell(new ShardRegion.StartEntity(Msg));
return await ExpectMsgAsync<RememberEntitiesShardStore.UpdateDone>();
}
[Fact(DisplayName = "Message wrapped in ShardingEnvelope, buffered by Shard, transformed by BufferMessageAdapter, must arrive in entity actor")]
public async Task WrappedMessageDelivery()
{
IgnoreMessages<ShardRegion.StartEntityAck>();
var continueMessage = await ExpectShardStartup();
// this message should be buffered
_shard.Tell(new ShardingEnvelope(Msg, Msg));
await Task.Yield();
// Tell shard to continue processing
_shard.Tell(continueMessage);
await ExpectMsgAsync(Msg);
}
}