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

Support nullability in SetMeasurementEventCallback (fixes #68025) #68071

Merged
merged 6 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
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
Expand Up @@ -117,33 +117,37 @@ public void EnableMeasurementEvents(Instrument instrument, object? state = null)
/// <param name="measurementCallback">The callback which can be used to get measurement recording of numeric type T.</param>
public void SetMeasurementEventCallback<T>(MeasurementCallback<T>? measurementCallback) where T : struct
{
if (measurementCallback is MeasurementCallback<byte> byteCallback)
if (measurementCallback is null)
{
_byteMeasurementCallback = (measurementCallback is null) ? ((instrument, measurement, tags, state) => { /* no-op */}) : byteCallback;
measurementCallback = (instrument, measurement, tags, state) => { /* no-op */};
soroshsabz marked this conversation as resolved.
Show resolved Hide resolved
}
else if (measurementCallback is MeasurementCallback<int> intCallback)
if (typeof(T) == typeof(byte))
{
_byteMeasurementCallback = (MeasurementCallback<byte>)(object)measurementCallback;
}
else if (typeof(T) == typeof(int))
{
_intMeasurementCallback = (measurementCallback is null) ? ((instrument, measurement, tags, state) => { /* no-op */}) : intCallback;
_intMeasurementCallback = (MeasurementCallback<int>)(object)measurementCallback;
}
else if (measurementCallback is MeasurementCallback<float> floatCallback)
else if (typeof(T) == typeof(float))
{
_floatMeasurementCallback = (measurementCallback is null) ? ((instrument, measurement, tags, state) => { /* no-op */}) : floatCallback;
_floatMeasurementCallback = (MeasurementCallback<float>)(object)measurementCallback;
}
else if (measurementCallback is MeasurementCallback<double> doubleCallback)
else if (typeof(T) == typeof(double))
{
_doubleMeasurementCallback = (measurementCallback is null) ? ((instrument, measurement, tags, state) => { /* no-op */}) : doubleCallback;
_doubleMeasurementCallback = (MeasurementCallback<double>)(object)measurementCallback;
}
else if (measurementCallback is MeasurementCallback<decimal> decimalCallback)
else if (typeof(T) == typeof(decimal))
{
_decimalMeasurementCallback = (measurementCallback is null) ? ((instrument, measurement, tags, state) => { /* no-op */}) : decimalCallback;
_decimalMeasurementCallback = (MeasurementCallback<decimal>)(object)measurementCallback;
}
else if (measurementCallback is MeasurementCallback<short> shortCallback)
else if (typeof(T) == typeof(short))
{
_shortMeasurementCallback = (measurementCallback is null) ? ((instrument, measurement, tags, state) => { /* no-op */}) : shortCallback;
_shortMeasurementCallback = (MeasurementCallback<short>)(object)measurementCallback;
}
else if (measurementCallback is MeasurementCallback<long> longCallback)
else if (typeof(T) == typeof(long))
{
_longMeasurementCallback = (measurementCallback is null) ? ((instrument, measurement, tags, state) => { /* no-op */}) : longCallback;
_longMeasurementCallback = (MeasurementCallback<long>)(object)measurementCallback;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,36 @@ public void MultipleListenersTest()
}).Dispose();
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void NullMeasurementEventCallbackTest()
{
RemoteExecutor.Invoke(() => {
Meter meter = new Meter("NullMeasurementEventCallbackTest");

Counter<int> counter = meter.CreateCounter<int>("Counter");

MeterListener listener = new MeterListener();

listener.InstrumentPublished = (theInstrument, theListener) => theListener.EnableMeasurementEvents(theInstrument, theInstrument);

int count = 0;
listener.SetMeasurementEventCallback<int>((inst, measurement, tags, state) => count++);

listener.Start();

Assert.Equal(0, count);

counter.Add(1);
Assert.Equal(1, count);

listener.SetMeasurementEventCallback<int>(null);
counter.Add(1);
Assert.Equal(1, count);

Assert.Throws<InvalidOperationException>(() => listener.SetMeasurementEventCallback<ulong>(null));
}).Dispose();
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void EnableListeneingMultipleTimesWithDifferentState()
{
Expand Down