-
Notifications
You must be signed in to change notification settings - Fork 637
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
122 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
"Scavenge": true | ||
}, | ||
|
||
"ElectionsCount": true, | ||
|
||
"Checkpoints": { | ||
"Writer": true, | ||
"Index": true, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/EventStore.Core.XUnit.Tests/Metrics/ElectionsCounterTrackerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Metrics; | ||
using System.Net; | ||
using System.Runtime.CompilerServices; | ||
using EventStore.Core.Data; | ||
using EventStore.Core.Messages; | ||
using EventStore.Core.Metrics; | ||
using EventStore.Core.Tests; | ||
using Xunit; | ||
|
||
namespace EventStore.Core.XUnit.Tests.Metrics; | ||
|
||
public class ElectionsCounterTrackerTests : IDisposable { | ||
private readonly Disposables _disposables = new(); | ||
private readonly ElectionMessage.ElectionsDone _electionsDoneMessage; | ||
|
||
public ElectionsCounterTrackerTests() { | ||
var endPoint = new DnsEndPoint("127.0.0.1", 1113); | ||
var memberInfo = Cluster.MemberInfo.Initial(Guid.Empty, DateTime.UtcNow, | ||
VNodeState.Unknown, true, | ||
endPoint, endPoint, endPoint, endPoint, endPoint, | ||
null, 0, 0, 0, false); | ||
_electionsDoneMessage = new ElectionMessage.ElectionsDone(1, 1, memberInfo); | ||
} | ||
|
||
public void Dispose() { | ||
_disposables?.Dispose(); | ||
} | ||
|
||
private (ElectionsCounterTracker, TestMeterListener<long>) GenSut( | ||
[CallerMemberName]string callerName = "") { | ||
|
||
var meter = new Meter($"{typeof(ElectionsCounterTrackerTests)}--{callerName}").DisposeWith(_disposables); | ||
var listener = new TestMeterListener<long>(meter).DisposeWith(_disposables); | ||
var metric = new CounterMetric(meter, "test-metric", unit:""); | ||
var sut = new ElectionsCounterTracker(new CounterSubMetric(metric, [])); | ||
|
||
return (sut, listener); | ||
} | ||
|
||
[Fact] | ||
public void test_election_count_for_one_election() { | ||
var (sut, listener) = GenSut(); | ||
sut.Handle(_electionsDoneMessage); | ||
|
||
AssertMeasurements(listener, AssertMeasurement(1)); | ||
} | ||
|
||
[Fact] | ||
public void test_election_count_for_five_elections() { | ||
var (sut, listener) = GenSut(); | ||
sut.Handle(_electionsDoneMessage); | ||
sut.Handle(_electionsDoneMessage); | ||
sut.Handle(_electionsDoneMessage); | ||
sut.Handle(_electionsDoneMessage); | ||
sut.Handle(_electionsDoneMessage); | ||
|
||
AssertMeasurements(listener, AssertMeasurement(5)); | ||
} | ||
|
||
static Action<TestMeterListener<long>.TestMeasurement> AssertMeasurement( | ||
int expectedValue) => | ||
actualMeasurement => { | ||
Assert.Equal(expectedValue, actualMeasurement.Value); | ||
}; | ||
|
||
static void AssertMeasurements( | ||
TestMeterListener<long> listener, | ||
params Action<TestMeterListener<long>.TestMeasurement>[] actions) { | ||
|
||
listener.Observe(); | ||
Assert.Collection(listener.RetrieveMeasurements("test-metric"), actions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using EventStore.Core.Bus; | ||
using EventStore.Core.Messages; | ||
|
||
|
||
namespace EventStore.Core.Metrics; | ||
|
||
public interface IElectionCounterTracker : IHandle<ElectionMessage.ElectionsDone> { | ||
} | ||
|
||
public class ElectionsCounterTracker : IElectionCounterTracker { | ||
private readonly CounterSubMetric _electionsCounter; | ||
|
||
public ElectionsCounterTracker(CounterSubMetric electionsCounter) { | ||
_electionsCounter = electionsCounter; | ||
} | ||
|
||
public void Handle(ElectionMessage.ElectionsDone message) { | ||
_electionsCounter.Add(1); | ||
} | ||
|
||
public class NoOp : IElectionCounterTracker { | ||
public void Handle(ElectionMessage.ElectionsDone message) { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters