Skip to content

Commit

Permalink
Support nullability in SetMeasurementEventCallback (fixes #68025) (#6…
Browse files Browse the repository at this point in the history
  • Loading branch information
soroshsabz authored Apr 15, 2022
1 parent a5fad2e commit 7368836
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
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 */};
}
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

0 comments on commit 7368836

Please sign in to comment.