-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release/8.0] Only initialize listeners once (#90932)
* Only initialize listeners once. * Add test * Second call --------- Co-authored-by: Chris R <Tratcher@outlook.com>
- Loading branch information
1 parent
e2c8d69
commit 482bfb9
Showing
3 changed files
with
102 additions
and
1 deletion.
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
44 changes: 44 additions & 0 deletions
44
src/libraries/Microsoft.Extensions.Diagnostics/tests/MetricsSubscriptionManagerTests.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,44 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Metrics; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.Metrics; | ||
using Microsoft.Extensions.Options; | ||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.Diagnostics.Tests | ||
{ | ||
public class MetricsSubscriptionManagerTests | ||
{ | ||
[Fact] | ||
public void AddMetrics_InitializesListeners() | ||
{ | ||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddMetrics(); // Duplicate call, should not add things twice. | ||
serviceCollection.AddMetrics(l => l.AddListener<FakeListener>()); | ||
var serviceProvider = serviceCollection.BuildServiceProvider(); | ||
|
||
// Make sure the subscription manager is started. | ||
serviceProvider.GetRequiredService<IStartupValidator>().Validate(); | ||
|
||
var listeners = serviceProvider.GetRequiredService<IEnumerable<IMetricsListener>>(); | ||
|
||
var listener = Assert.Single(listeners); | ||
var fakeListener = Assert.IsType<FakeListener>(listener); | ||
Assert.Equal(1, fakeListener.InitializeCount); | ||
} | ||
|
||
private class FakeListener : IMetricsListener | ||
{ | ||
public string Name => "Fake"; | ||
public int InitializeCount { get; private set; } | ||
public MeasurementHandlers GetMeasurementHandlers() => new MeasurementHandlers(); | ||
public void Initialize(IObservableInstrumentsSource source) => InitializeCount++; | ||
public bool InstrumentPublished(Instrument instrument, out object? userState) => throw new NotImplementedException(); | ||
public void MeasurementsCompleted(Instrument instrument, object? userState) => throw new NotImplementedException(); | ||
} | ||
} | ||
} |