-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ClusterMetricsExtensionSpec.cs
166 lines (143 loc) · 6.06 KB
/
ClusterMetricsExtensionSpec.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
//-----------------------------------------------------------------------
// <copyright file="ClusterMetricsExtensionSpec.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 System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Akka.Cluster.Metrics.Tests.Helpers;
using Akka.Cluster.TestKit;
using Akka.Configuration;
using Akka.Remote.TestKit;
using FluentAssertions;
using Akka.Configuration;
using Akka.MultiNode.TestAdapter;
using FluentAssertions.Extensions;
using ConfigurationFactory = Akka.Configuration.ConfigurationFactory;
namespace Akka.Cluster.Metrics.Tests.MultiNode
{
public class ClusterMetricsCommonConfig : MultiNodeConfig
{
public readonly RoleName Node1;
public readonly RoleName Node2;
public readonly RoleName Node3;
public readonly RoleName Node4;
public readonly RoleName Node5;
public Config EnableMetricsExtension => ConfigurationFactory.ParseString(@"
akka.extensions=[""Akka.Cluster.Metrics.ClusterMetricsExtensionProvider, Akka.Cluster.Metrics""]
akka.cluster.metrics.collector.enabled = on
");
public Config DisableMetricsExtension => ConfigurationFactory.ParseString(@"
akka.extensions=[""Akka.Cluster.Metrics.ClusterMetricsExtensionProvider, Akka.Cluster.Metrics""]
akka.cluster.metrics.collector.enabled = off
");
public ClusterMetricsCommonConfig()
{
Node1 = Role("node-1");
Node2 = Role("node-2");
Node3 = Role("node-3");
Node4 = Role("node-4");
Node5 = Role("node-5");
}
}
public class ClusterMetricsDisabledConfig : ClusterMetricsCommonConfig
{
public ClusterMetricsDisabledConfig()
{
CommonConfig = DisableMetricsExtension
.WithFallback(DebugConfig(on: false))
.WithFallback(MultiNodeClusterSpec.ClusterConfigWithFailureDetectorPuppet());
}
}
public class ClusterMetricsEnabledConfig : ClusterMetricsCommonConfig
{
public ClusterMetricsEnabledConfig()
{
CommonConfig = EnableMetricsExtension
.WithFallback(DebugConfig(on: false))
.WithFallback(MultiNodeClusterSpec.ClusterConfigWithFailureDetectorPuppet());
}
}
public class ClusterMetricsEnabledSpec : MultiNodeClusterSpec
{
private readonly ClusterMetricsCommonConfig _config;
public ClusterMetricsView MetricsView { get; }
private ClusterMetricsEnabledSpec(ClusterMetricsCommonConfig config) : base(config, typeof(ClusterMetricsEnabledSpec))
{
_config = config;
MetricsView = new ClusterMetricsView(Cluster.System);
}
public ClusterMetricsEnabledSpec() : this(new ClusterMetricsEnabledConfig())
{
}
[MultiNodeFact]
public async Task ClusterMetrics_Should_work_when_enabled()
{
await Should_collect_and_publish_metrics_and_gossip_them_around_the_node_ring();
await Should_reflect_the_correct_number_or_node_metrics_in_cluster_view();
}
private async Task Should_collect_and_publish_metrics_and_gossip_them_around_the_node_ring()
{
await AwaitAssertAsync(() =>
{
AwaitClusterUp(Roles.ToArray());
}, TimeSpan.FromSeconds(30));
EnterBarrier("cluster_started");
await AwaitAssertAsync(() =>
{
Cluster.State.Members.Count(m => m.Status == MemberStatus.Up).Should().Be(Roles.Count);
}, TimeSpan.FromSeconds(30));
await AwaitAssertAsync(() =>
{
MetricsView.ClusterMetrics.Count.Should().Be(Roles.Count);
}, TimeSpan.FromSeconds(30));
await WithinAsync(10.Seconds(), async () =>
{
var collector = new MetricsCollectorBuilder().Build(Cluster.System);
collector.Sample().Metrics.Count.Should().BeGreaterThan(3);
EnterBarrier("after");
});
}
private async Task Should_reflect_the_correct_number_or_node_metrics_in_cluster_view()
{
await WithinAsync(30.Seconds(), async () =>
{
RunOn(() =>
{
Cluster.Leave(Node(_config.Node1).Address);
}, _config.Node2);
EnterBarrier("first-left");
await RunOnAsync(async () =>
{
MarkNodeAsUnavailable(Node(_config.Node1).Address);
await AwaitAssertAsync(() => MetricsView.ClusterMetrics.Count.Should().Be(Roles.Count - 1));
}, _config.Node2, _config.Node3, _config.Node4, _config.Node5);
EnterBarrier("finished");
});
}
}
public class ClusterMetricsDisabledSpec : MultiNodeClusterSpec
{
public ClusterMetricsView MetricsView { get; }
private ClusterMetricsDisabledSpec(ClusterMetricsDisabledConfig config) : base(config, typeof(ClusterMetricsDisabledSpec))
{
MetricsView = new ClusterMetricsView(Cluster.System);
}
public ClusterMetricsDisabledSpec() : this(new ClusterMetricsDisabledConfig())
{
}
[MultiNodeFact]
public void ClusterMetrics_Should_not_collect_publish_and_gossip_metrics_when_disabled()
{
AwaitClusterUp(Roles.ToArray());
MetricsView.ClusterMetrics.Count.Should().Be(0);
ClusterMetrics.Get(Sys).Subscribe(TestActor);
ExpectNoMsg();
MetricsView.ClusterMetrics.Count.Should().Be(0);
EnterBarrier("after");
}
}
}