Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[main] Forward port metrics tests #91026

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}