-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ClusterShardingSingleShardPerEntitySpec.cs
138 lines (115 loc) · 5.26 KB
/
ClusterShardingSingleShardPerEntitySpec.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
//-----------------------------------------------------------------------
// <copyright file="ClusterShardingSingleShardPerEntitySpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using Akka.Actor;
using Akka.Configuration;
using Akka.MultiNode.TestAdapter;
using Akka.Remote.TestKit;
using Akka.Remote.Transport;
using Akka.TestKit;
using FluentAssertions;
namespace Akka.Cluster.Sharding.Tests
{
public class ClusterShardingSingleShardPerEntitySpecConfig : MultiNodeClusterShardingConfig
{
public RoleName First { get; }
public RoleName Second { get; }
public RoleName Third { get; }
public RoleName Fourth { get; }
public RoleName Fifth { get; }
public Config R1Config { get; }
public Config R2Config { get; }
public ClusterShardingSingleShardPerEntitySpecConfig()
: base(loglevel: "DEBUG", additionalConfig: @"
akka.cluster.sharding.updating-state-timeout = 1s
")
{
First = Role("first");
Second = Role("second");
Third = Role("third");
Fourth = Role("fourth");
Fifth = Role("fifth");
TestTransport = true;
}
}
public class ClusterShardingSingleShardPerEntitySpec : MultiNodeClusterShardingSpec<ClusterShardingSingleShardPerEntitySpecConfig>
{
#region setup
private readonly Lazy<IActorRef> _region;
public ClusterShardingSingleShardPerEntitySpec()
: this(new ClusterShardingSingleShardPerEntitySpecConfig(), typeof(ClusterShardingSingleShardPerEntitySpec))
{
}
protected ClusterShardingSingleShardPerEntitySpec(ClusterShardingSingleShardPerEntitySpecConfig config, Type type)
: base(config, type)
{
_region = new Lazy<IActorRef>(() => ClusterSharding.Get(Sys).ShardRegion("Entity"));
}
private void Join(RoleName from, RoleName to)
{
Join(
from,
to,
() => StartSharding(
Sys,
typeName: "Entity",
entityProps: Props.Create(() => new ShardedEntity()),
extractEntityId: IntExtractEntityId,
extractShardId: IntExtractShardId));
}
private void JoinAndAllocate(RoleName node, int entityId)
{
Within(TimeSpan.FromSeconds(10), () =>
{
Join(node, config.First);
RunOn(() =>
{
_region.Value.Tell(entityId);
ExpectMsg(entityId);
LastSender.Path.Should().Be(_region.Value.Path / $"{entityId}" / $"{entityId}");
}, node);
});
EnterBarrier($"started-{entityId}");
}
#endregion
[MultiNodeFact]
public void Cluster_sharding_with_single_shard_per_entity_specs()
{
Cluster_sharding_with_single_shard_per_entity_must_use_specified_region();
}
private void Cluster_sharding_with_single_shard_per_entity_must_use_specified_region()
{
JoinAndAllocate(config.First, 1);
JoinAndAllocate(config.Second, 2);
JoinAndAllocate(config.Third, 3);
JoinAndAllocate(config.Fourth, 4);
JoinAndAllocate(config.Fifth, 5);
RunOn(() =>
{
// coordinator is on 'first', blackhole 3 other means that it can't update with WriteMajority
TestConductor.Blackhole(config.First, config.Third, ThrottleTransportAdapter.Direction.Both).Wait();
TestConductor.Blackhole(config.First, config.Fourth, ThrottleTransportAdapter.Direction.Both).Wait();
TestConductor.Blackhole(config.First, config.Fifth, ThrottleTransportAdapter.Direction.Both).Wait();
// shard 6 not allocated yet and due to the blackhole it will not be completed
_region.Value.Tell(6);
// shard 1 location is know by 'first' region, not involving coordinator
_region.Value.Tell(1);
ExpectMsg(1);
// shard 2 location not known at 'first' region yet, but coordinator is on 'first' and should
// be able to answer GetShardHome even though previous request for shard 4 has not completed yet
_region.Value.Tell(2);
ExpectMsg(2);
LastSender.Path.Should().Be(Node(config.Second) / "system" / "sharding" / "Entity" / "2" / "2");
TestConductor.PassThrough(config.First, config.Third, ThrottleTransportAdapter.Direction.Both).Wait();
TestConductor.PassThrough(config.First, config.Fourth, ThrottleTransportAdapter.Direction.Both).Wait();
TestConductor.PassThrough(config.First, config.Fifth, ThrottleTransportAdapter.Direction.Both).Wait();
ExpectMsg(6, TimeSpan.FromSeconds(10));
}, config.First);
EnterBarrier("after-1");
}
}
}