-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ClusterLogSpec.cs
151 lines (137 loc) · 5.55 KB
/
ClusterLogSpec.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
//-----------------------------------------------------------------------
// <copyright file="ClusterLogSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Linq;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Configuration;
using Akka.TestKit;
using Akka.TestKit.Extensions;
using Akka.Util.Internal;
using FluentAssertions.Extensions;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
namespace Akka.Cluster.Tests
{
public abstract class ClusterLogSpec : AkkaSpec
{
public const string Config = @"
akka.cluster {
auto-down-unreachable-after = 0s
publish-stats-interval = 0s # always, when it happens
run-coordinated-shutdown-when-down = off
}
akka.actor.provider = ""cluster""
akka.remote.log-remote-lifecycle-events = off
akka.remote.dot-netty.tcp.port = 0
akka.loglevel = ""INFO""
akka.loggers = [""Akka.TestKit.TestEventListener, Akka.TestKit""]";
protected const string upLogMessage = " - event MemberUp";
protected const string downLogMessage = " - event MemberDowned";
protected readonly Address _selfAddress;
protected readonly Cluster _cluster;
internal ClusterReadView ClusterView { get { return _cluster.ReadView; } }
protected ClusterLogSpec(ITestOutputHelper output, Config config = null)
: base(config ?? Config, output)
{
_selfAddress = Sys.AsInstanceOf<ExtendedActorSystem>().Provider.DefaultAddress;
_cluster = Cluster.Get(Sys);
}
protected async Task AwaitUpAsync()
{
await WithinAsync(TimeSpan.FromSeconds(10), async() =>
{
await AwaitConditionAsync(async () => ClusterView.IsSingletonCluster);
ClusterView.Self.Address.ShouldBe(_selfAddress);
ClusterView.Members.Select(m => m.Address).ShouldBe(new Address[] { _selfAddress });
await AwaitAssertAsync(() => ClusterView.Status.ShouldBe(MemberStatus.Up));
});
}
/// <summary>
/// The expected log info pattern to intercept after a <see cref="Cluster.Join(Address)"/>.
/// </summary>
protected async Task JoinAsync(string expected)
{
await EventFilter
.Info(contains: expected)
.ExpectOneAsync(10.Seconds(), async () => {
var tcs = new TaskCompletionSource<bool>();
_cluster.RegisterOnMemberUp(() =>
{
tcs.TrySetResult(true);
});
_cluster.Join(_selfAddress);
await tcs.Task.ShouldCompleteWithin(10.Seconds());
});
}
/// <summary>
/// The expected log info pattern to intercept after a <see cref="Cluster.Down(Address)"/>.
/// </summary>
/// <param name="expected"></param>
protected async Task DownAsync(string expected)
{
await EventFilter
.Info(contains: expected)
.ExpectOneAsync(10.Seconds(), async () =>
{
var tcs = new TaskCompletionSource<bool>();
_cluster.RegisterOnMemberRemoved(() =>
{
tcs.TrySetResult(true);
});
_cluster.Down(_selfAddress);
await tcs.Task.ShouldCompleteWithin(10.Seconds());
});
}
}
public class ClusterLogDefaultSpec : ClusterLogSpec
{
public ClusterLogDefaultSpec(ITestOutputHelper output)
: base(output)
{ }
[Fact]
public async Task A_cluster_must_log_a_message_when_becoming_and_stopping_being_a_leader()
{
_cluster.Settings.LogInfo.ShouldBeTrue();
_cluster.Settings.LogInfoVerbose.ShouldBeFalse();
await JoinAsync("is the new leader");
await AwaitUpAsync();
await DownAsync("is no longer leader");
}
}
public class ClusterLogVerboseDefaultSpec : ClusterLogSpec
{
public ClusterLogVerboseDefaultSpec(ITestOutputHelper output)
: base(output)
{ }
[Fact]
public async Task A_cluster_must_not_log_verbose_cluster_events_by_default()
{
_cluster.Settings.LogInfoVerbose.ShouldBeFalse();
await JoinAsync(upLogMessage).ShouldThrowWithin<XunitException>(11.Seconds());
await AwaitUpAsync();
await DownAsync(downLogMessage).ShouldThrowWithin<XunitException>(11.Seconds());
}
}
public class ClusterLogVerboseEnabledSpec : ClusterLogSpec
{
public ClusterLogVerboseEnabledSpec(ITestOutputHelper output)
: base(output, ConfigurationFactory
.ParseString("akka.cluster.log-info-verbose = on")
.WithFallback(ConfigurationFactory.ParseString(Config)))
{ }
[Fact]
public async Task A_cluster_must_log_verbose_cluster_events_when_log_info_verbose_is_on()
{
_cluster.Settings.LogInfoVerbose.ShouldBeTrue();
await JoinAsync(upLogMessage);
await AwaitUpAsync();
await DownAsync(downLogMessage);
}
}
}