Skip to content

Commit 4becbdd

Browse files
cleaned up RememberEntitiesFailureSpecs (#7400)
- converted to `async` APIs - fixed `Cl;usterSingletonManager.DefaultConfig` build warning - style cleanup
1 parent 0968e9d commit 4becbdd

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

src/contrib/cluster/Akka.Cluster.Sharding.Tests/RememberEntitiesFailureSpec.cs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Collections.Immutable;
1010
using System.Linq;
1111
using System.Threading;
12+
using System.Threading.Tasks;
1213
using Akka.Actor;
1314
using Akka.Cluster.Sharding.Internal;
1415
using Akka.Cluster.Tools.Singleton;
@@ -26,19 +27,19 @@ public class RememberEntitiesFailureSpec : AkkaSpec
2627
{
2728
internal class EntityActor : ActorBase
2829
{
29-
private readonly ILoggingAdapter log = Context.GetLogger();
30+
private readonly ILoggingAdapter _log = Context.GetLogger();
3031

3132
public EntityActor()
3233
{
33-
log.Info("Entity actor [{0}] starting up", Context.Self.Path.Name);
34+
_log.Info("Entity actor [{0}] starting up", Context.Self.Path.Name);
3435
}
3536

3637
protected override bool Receive(object message)
3738
{
3839
switch (message)
3940
{
4041
case "stop":
41-
log.Info("Stopping myself!");
42+
_log.Info("Stopping myself!");
4243
Context.Stop(Self);
4344
return true;
4445
case "graceful-stop":
@@ -122,8 +123,8 @@ public Delay(TimeSpan howLong)
122123
}
123124

124125
// outside store since we need to be able to set them before sharding initializes
125-
private static ImmutableDictionary<string, IFail> failShardGetEntities = ImmutableDictionary<string, IFail>.Empty;
126-
private static readonly IFail failCoordinatorGetShards = null;
126+
private static ImmutableDictionary<string, IFail> _failShardGetEntities = ImmutableDictionary<string, IFail>.Empty;
127+
private static readonly IFail FailCoordinatorGetShards = null;
127128

128129
private class ShardStoreCreated
129130
{
@@ -221,7 +222,7 @@ protected override bool Receive(object message)
221222
switch (message)
222223
{
223224
case RememberEntitiesShardStore.GetEntities _:
224-
switch (failShardGetEntities.GetValueOrDefault(shardId))
225+
switch (_failShardGetEntities.GetValueOrDefault(shardId))
225226
{
226227
case null:
227228
Sender.Tell(new RememberEntitiesShardStore.RememberedEntities(ImmutableHashSet<string>.Empty));
@@ -317,7 +318,7 @@ protected override bool Receive(object message)
317318
switch (message)
318319
{
319320
case RememberEntitiesCoordinatorStore.GetShards _:
320-
switch (failCoordinatorGetShards)
321+
switch (FailCoordinatorGetShards)
321322
{
322323
case null:
323324
Sender.Tell(new RememberEntitiesCoordinatorStore.RememberedShards(ImmutableHashSet<string>.Empty));
@@ -390,7 +391,7 @@ protected override bool Receive(object message)
390391
akka.cluster.sharding.updating-state-timeout = 1s
391392
akka.cluster.sharding.verbose-debug-logging = on
392393
akka.cluster.sharding.fail-on-invalid-entity-state-transition = on")
393-
.WithFallback(ClusterSingletonManager.DefaultConfig()
394+
.WithFallback(ClusterSingleton.DefaultConfig()
394395
.WithFallback(ClusterSharding.DefaultConfig()));
395396

396397
public RememberEntitiesFailureSpec(ITestOutputHelper helper) : base(SpecConfig, helper)
@@ -441,7 +442,7 @@ public void Remember_entities_handling_in_sharding_must_recover_when_initial_rem
441442
private void Remember_entities_handling_in_sharding_must_recover_when_initial_remember_entities_load_fails(IFail wayToFail)
442443
{
443444
Log.Debug("Getting entities for shard 1 will fail");
444-
failShardGetEntities = ImmutableDictionary<string, IFail>.Empty.Add("1", wayToFail);
445+
_failShardGetEntities = ImmutableDictionary<string, IFail>.Empty.Add("1", wayToFail);
445446

446447
try
447448
{
@@ -456,7 +457,7 @@ private void Remember_entities_handling_in_sharding_must_recover_when_initial_re
456457
probe.ExpectNoMsg(); // message is lost because shard crashes
457458

458459
Log.Debug("Resetting initial fail");
459-
failShardGetEntities = ImmutableDictionary<string, IFail>.Empty;
460+
_failShardGetEntities = ImmutableDictionary<string, IFail>.Empty;
460461

461462
// shard should be restarted and eventually succeed
462463
AwaitAssert(() =>
@@ -469,7 +470,7 @@ private void Remember_entities_handling_in_sharding_must_recover_when_initial_re
469470
}
470471
finally
471472
{
472-
failShardGetEntities = ImmutableDictionary<string, IFail>.Empty;
473+
_failShardGetEntities = ImmutableDictionary<string, IFail>.Empty;
473474
}
474475
}
475476

@@ -694,41 +695,41 @@ private void Remember_entities_handling_in_sharding_must_recover_on_graceful_ent
694695
}
695696

696697
[Fact]
697-
public void Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails_NoResponse()
698+
public Task Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails_NoResponse()
698699
{
699-
Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails(new NoResponse());
700+
return Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails(new NoResponse());
700701
}
701702

702703
[Fact]
703-
public void Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails_CrashStore()
704+
public Task Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails_CrashStore()
704705
{
705-
Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails(new CrashStore());
706+
return Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails(new CrashStore());
706707
}
707708

708709
[Fact]
709-
public void Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails_StopStore()
710+
public Task Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails_StopStore()
710711
{
711-
Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails(new StopStore());
712+
return Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails(new StopStore());
712713
}
713714

714715
[Fact]
715-
public void Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails_Delay_500()
716+
public Task Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails_Delay_500()
716717
{
717-
Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails(new Delay(TimeSpan.FromMilliseconds(500)));
718+
return Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails(new Delay(TimeSpan.FromMilliseconds(500)));
718719
}
719720

720721
[Fact]
721-
public void Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails_Delay_1000()
722+
public Task Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails_Delay_1000()
722723
{
723-
Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails(new Delay(TimeSpan.FromSeconds(1)));
724+
return Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails(new Delay(TimeSpan.FromSeconds(1)));
724725
}
725726

726-
private void Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails(IFail wayToFail)
727+
private async Task Remember_entities_handling_in_sharding_must_recover_when_coordinator_storing_shard_start_fails(IFail wayToFail)
727728
{
728729
var storeProbe = CreateTestProbe();
729730
Sys.EventStream.Subscribe(storeProbe.Ref, typeof(CoordinatorStoreCreated));
730731

731-
var sharding = ClusterSharding.Get(Sys).Start(
732+
var sharding = await ClusterSharding.Get(Sys).StartAsync(
732733
$"coordinatorStoreStopGraceful-{wayToFail}",
733734
Props.Create(() => new EntityActor()),
734735
ClusterShardingSettings.Create(Sys).WithRememberEntities(true),
@@ -739,12 +740,12 @@ private void Remember_entities_handling_in_sharding_must_recover_when_coordinato
739740
var probe = CreateTestProbe();
740741

741742
// coordinator store is triggered by coordinator starting up
742-
var coordinatorStore = storeProbe.ExpectMsg<CoordinatorStoreCreated>().Store;
743+
var coordinatorStore = (await storeProbe.ExpectMsgAsync<CoordinatorStoreCreated>()).Store;
743744
coordinatorStore.Tell(new FakeCoordinatorStoreActor.FailAddShard("1", wayToFail), probe.Ref);
744-
probe.ExpectMsg<Done>();
745+
await probe.ExpectMsgAsync<Done>();
745746

746747
sharding.Tell(new EntityEnvelope(1, "hello-1"), probe.Ref);
747-
probe.ExpectNoMsg(TimeSpan.FromSeconds(1)); // because shard cannot start while store failing
748+
await probe.ExpectNoMsgAsync(TimeSpan.FromSeconds(1)); // because shard cannot start while store failing
748749

749750
if (wayToFail is StopStore or CrashStore)
750751
{
@@ -754,12 +755,12 @@ private void Remember_entities_handling_in_sharding_must_recover_when_coordinato
754755

755756
// fail it when stopping
756757
coordinatorStore.Tell(new FakeCoordinatorStoreActor.ClearFailShard("1"), storeProbe.Ref);
757-
storeProbe.ExpectMsg<Done>();
758+
await storeProbe.ExpectMsgAsync<Done>();
758759

759-
probe.AwaitAssert(() =>
760+
await probe.AwaitAssertAsync(async () =>
760761
{
761762
sharding.Tell(new EntityEnvelope(1, "hello-2"), probe.Ref);
762-
probe.ExpectMsg("hello-2"); // should now work again
763+
await probe.ExpectMsgAsync("hello-2"); // should now work again
763764
}, TimeSpan.FromSeconds(5));
764765

765766
Sys.Stop(sharding);

0 commit comments

Comments
 (0)