-
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
1 parent
f83d0ba
commit 207ae55
Showing
18 changed files
with
260 additions
and
11 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
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,12 @@ | ||
using System; | ||
|
||
namespace EventStore.Core.Tests; | ||
|
||
public static class DisposableExtensions { | ||
public static T DisposeWith<T>(this T disposable, Disposables disposables) | ||
where T : IDisposable { | ||
|
||
disposables.Add(disposable); | ||
return disposable; | ||
} | ||
} |
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,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace EventStore.Core.Tests; | ||
|
||
public sealed class Disposables : IDisposable { | ||
private readonly List<IDisposable> _disposables = new(); | ||
|
||
public void Dispose() { | ||
for (int i = _disposables.Count - 1; i >= 0; i--) { | ||
_disposables[i]?.Dispose(); | ||
} | ||
} | ||
|
||
public void Add(IDisposable disposable) => | ||
_disposables.Add(disposable); | ||
} |
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
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
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
88 changes: 88 additions & 0 deletions
88
src/EventStore.Core.XUnit.Tests/Telemetry/CacheHitsMiseesTrackerTests.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,88 @@ | ||
using System; | ||
using System.Diagnostics.Metrics; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using EventStore.Core.Telemetry; | ||
using EventStore.Core.Tests; | ||
using Xunit; | ||
|
||
using static EventStore.Common.Configuration.TelemetryConfiguration; | ||
|
||
namespace EventStore.Core.XUnit.Tests.Telemetry; | ||
|
||
public sealed class CacheHitsMissesTrackerTests : IDisposable { | ||
private readonly Disposables _disposables = new(); | ||
|
||
public void Dispose() { | ||
_disposables?.Dispose(); | ||
} | ||
|
||
private (CacheHitsMissesTracker, TestMeterListener<long>) GenSut( | ||
Cache[] enabledCaches, | ||
[CallerMemberName] string callerName = "") { | ||
|
||
var meter = new Meter($"{typeof(CacheHitsMissesTrackerTests)}-{callerName}").DisposeWith(_disposables); | ||
var listener = new TestMeterListener<long>(meter).DisposeWith(_disposables); | ||
var metric = new CacheHitsMissesMetric(meter, "the-metric", enabledCaches); | ||
var sut = new CacheHitsMissesTracker(metric); | ||
return (sut, listener); | ||
} | ||
|
||
[Fact] | ||
public void observes_all_caches() { | ||
var (sut, listener) = GenSut(new[] { Cache.Chunk, Cache.StreamInfo }); | ||
sut.Register(Cache.Chunk, () => 1, () => 2); | ||
sut.Register(Cache.StreamInfo, () => 3, () => 4); | ||
AssertMeasurements(listener, | ||
AssertMeasurement("chunk", "hits", 1), | ||
AssertMeasurement("chunk", "misses", 2), | ||
AssertMeasurement("stream-info", "hits", 3), | ||
AssertMeasurement("stream-info", "misses", 4)); | ||
} | ||
|
||
[Fact] | ||
public void ignores_disabled_cache() { | ||
var (sut, listener) = GenSut(new[] { Cache.StreamInfo }); | ||
sut.Register(Cache.Chunk, () => 1, () => 2); | ||
sut.Register(Cache.StreamInfo, () => 3, () => 4); | ||
AssertMeasurements(listener, | ||
AssertMeasurement("stream-info", "hits", 3), | ||
AssertMeasurement("stream-info", "misses", 4)); | ||
} | ||
|
||
[Fact] | ||
public void ignores_unregistered_cache() { | ||
var (sut, listener) = GenSut(new[] { Cache.Chunk, Cache.StreamInfo }); | ||
sut.Register(Cache.Chunk, () => 1, () => 2); | ||
AssertMeasurements(listener, | ||
AssertMeasurement("chunk", "hits", 1), | ||
AssertMeasurement("chunk", "misses", 2)); | ||
} | ||
|
||
static Action<TestMeterListener<long>.TestMeasurement> AssertMeasurement( | ||
string cacheName, | ||
string kind, | ||
long expectedValue) => | ||
|
||
actualMeasurement => { | ||
Assert.Equal(expectedValue, actualMeasurement.Value); | ||
Assert.Collection( | ||
actualMeasurement.Tags.ToArray(), | ||
tag => { | ||
Assert.Equal("cache", tag.Key); | ||
Assert.Equal(cacheName, tag.Value); | ||
}, | ||
tag => { | ||
Assert.Equal("kind", tag.Key); | ||
Assert.Equal(kind, tag.Value); | ||
}); | ||
}; | ||
|
||
static void AssertMeasurements( | ||
TestMeterListener<long> listener, | ||
params Action<TestMeterListener<long>.TestMeasurement>[] actions) { | ||
|
||
listener.Observe(); | ||
Assert.Collection(listener.RetrieveMeasurements("the-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
Oops, something went wrong.