Skip to content

Commit 1687ae3

Browse files
tarekghCopilot
andauthored
Fix metric tests timeout issue (#117630)
* Fix metric tests timeout issue * Update src/libraries/System.Diagnostics.DiagnosticSource/tests/MetricsTests.cs Co-authored-by: Copilot <[email protected]> * Fix the RemoteInvokeOptions initialization * mark the propert as readonly * Ensure RemoteExecutor.IsSupported before creating RemoteInvokeOptions --------- Co-authored-by: Copilot <[email protected]>
1 parent 885879e commit 1687ae3

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/libraries/System.Diagnostics.DiagnosticSource/tests/MetricsTests.cs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ namespace System.Diagnostics.Metrics.Tests
1313
{
1414
public class MetricsTests
1515
{
16+
// We increase the timeout for remote execution to allow for longer-running tests.
17+
// Ensure RemoteExecutor.IsSupported, otherwise the execution can throw PlatformNotSupportedException.
18+
private static readonly RemoteInvokeOptions? s_remoteExecutionOptions = RemoteExecutor.IsSupported ? new RemoteInvokeOptions { TimeOut = 600_000 } : null;
19+
1620
[Fact]
1721
public void MeasurementConstructionTest()
1822
{
@@ -53,7 +57,7 @@ public void MeterConstructionTest()
5357
Assert.Equal("v1.0", meter.Version);
5458

5559
Assert.Throws<ArgumentNullException>(() => new Meter(name: null));
56-
}).Dispose();
60+
}, s_remoteExecutionOptions).Dispose();
5761
}
5862

5963
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -82,7 +86,7 @@ public void InstrumentCreationTest()
8286

8387
ObservableGauge<double> observableGauge = meter.CreateObservableGauge<double>("ObservableGauge", () => 10, "Fahrenheit", "Fahrenheit ObservableGauge");
8488
ValidateInstrumentInfo(observableGauge, "ObservableGauge", "Fahrenheit", "Fahrenheit ObservableGauge", false, true);
85-
}).Dispose();
89+
}, s_remoteExecutionOptions).Dispose();
8690
}
8791

8892
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -99,7 +103,7 @@ public void CreateInstrumentParametersTest()
99103
Assert.Throws<ArgumentNullException>(() => meter.CreateObservableUpDownCounter<Decimal>(null, () => 0, "items", "Items ObservableUpDownCounter"));
100104
Assert.Throws<ArgumentNullException>(() => meter.CreateObservableGauge<double>(null, () => 0, "seconds", "Seconds ObservableGauge"));
101105

102-
}).Dispose();
106+
}, s_remoteExecutionOptions).Dispose();
103107
}
104108

105109
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -169,7 +173,7 @@ public void SupportedGenericParameterTypesTest()
169173
Assert.Throws<InvalidOperationException>(() => meter.CreateHistogram<ulong>("histogram1", "seconds", "Seconds histogram"));
170174
Assert.Throws<InvalidOperationException>(() => meter.CreateObservableCounter<sbyte>("observableCounter3", () => 0, "seconds", "Seconds ObservableCounter"));
171175
Assert.Throws<InvalidOperationException>(() => meter.CreateObservableGauge<ushort>("observableGauge7", () => 0, "seconds", "Seconds ObservableGauge"));
172-
}).Dispose();
176+
}, s_remoteExecutionOptions).Dispose();
173177
}
174178

175179
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -228,7 +232,7 @@ public void ListeningToInstrumentsPublishingTest()
228232
// MeasurementsCompleted should be called 4 times for every instrument.
229233
Assert.Equal(0, instrumentsEncountered);
230234
}
231-
}).Dispose();
235+
}, s_remoteExecutionOptions).Dispose();
232236
}
233237

234238
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -280,7 +284,7 @@ public void ThrowingExceptionsFromObservableInstrumentCallbacks()
280284
Assert.Equal(11, accumulated);
281285
}
282286

283-
}).Dispose();
287+
}, s_remoteExecutionOptions).Dispose();
284288
}
285289

286290
[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -376,7 +380,7 @@ public void InstrumentMeasurementTest(bool useSpan)
376380
Histogram<decimal> histogram6 = meter.CreateHistogram<decimal>("decimalHistogram");
377381
InstrumentMeasurementAggregationValidation(histogram6, (value, tags) => { Record(histogram6, value, tags, useSpan); } );
378382

379-
}, useSpan.ToString()).Dispose();
383+
}, useSpan.ToString(), s_remoteExecutionOptions).Dispose();
380384

381385
void AddToCounter<T>(Counter<T> counter, T delta, KeyValuePair<string, object?>[] tags, bool useSpan) where T : struct
382386
{
@@ -723,7 +727,7 @@ public void ObservableInstrumentMeasurementTest()
723727
ObservableGauge<decimal> observableGauge20 = meter.CreateObservableGauge<decimal>("decimalObservableGauge", () => decimalGaugeMeasurementList);
724728
ObservableInstrumentMeasurementAggregationValidation(observableGauge20, decimalGaugeMeasurementList);
725729

726-
}).Dispose();
730+
}, s_remoteExecutionOptions).Dispose();
727731
}
728732

729733
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -875,7 +879,7 @@ public void PassingVariableTagsParametersTest()
875879
PublishHistogramMeasurement(instrument as Histogram<decimal>, value, tags);
876880
return (decimal)(value * 2);
877881
});
878-
}).Dispose();
882+
}, s_remoteExecutionOptions).Dispose();
879883
}
880884

881885
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -963,7 +967,7 @@ public void MeterDisposalsTest()
963967
listener.RecordObservableInstruments();
964968
Assert.Equal(13, count);
965969

966-
}).Dispose();
970+
}, s_remoteExecutionOptions).Dispose();
967971
}
968972

969973
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1034,7 +1038,7 @@ public void ListenerDisposalsTest()
10341038

10351039
listener.RecordObservableInstruments();
10361040
Assert.Equal(7, count);
1037-
}).Dispose();
1041+
}, s_remoteExecutionOptions).Dispose();
10381042
}
10391043

10401044
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1102,7 +1106,7 @@ public void ListenerWithoutMeasurementsCompletedDisposalsTest()
11021106

11031107
listener.RecordObservableInstruments();
11041108
Assert.Equal(7, count);
1105-
}).Dispose();
1109+
}, s_remoteExecutionOptions).Dispose();
11061110
}
11071111

11081112
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1147,7 +1151,7 @@ public void MultipleListenersTest()
11471151

11481152
gauge.Record(1);
11491153
Assert.Equal(15, count);
1150-
}).Dispose();
1154+
}, s_remoteExecutionOptions).Dispose();
11511155
}
11521156

11531157
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1184,7 +1188,7 @@ public void NullMeasurementEventCallbackTest()
11841188
Assert.Equal(2, count);
11851189

11861190
Assert.Throws<InvalidOperationException>(() => listener.SetMeasurementEventCallback<ulong>(null));
1187-
}).Dispose();
1191+
}, s_remoteExecutionOptions).Dispose();
11881192
}
11891193

11901194
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1225,7 +1229,7 @@ public void EnableListeningMultipleTimesWithDifferentState()
12251229

12261230
listener.Dispose();
12271231
Assert.Equal(4, completedCount);
1228-
}).Dispose();
1232+
}, s_remoteExecutionOptions).Dispose();
12291233
}
12301234

12311235
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1268,7 +1272,7 @@ public void ParallelRunningTest()
12681272
Task.WaitAll(taskList);
12691273

12701274
Assert.Equal(loopLength * 17, totalCount);
1271-
}).Dispose();
1275+
}, s_remoteExecutionOptions).Dispose();
12721276
}
12731277

12741278
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1337,7 +1341,7 @@ public void SerializedEventsTest()
13371341
Task.WaitAll(jobs);
13381342
listener.Dispose();
13391343
Assert.Equal(0, instruments.Count);
1340-
}).Dispose();
1344+
}, s_remoteExecutionOptions).Dispose();
13411345
}
13421346

13431347
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1441,7 +1445,7 @@ public void TestRecordingMeasurementsWithTagList()
14411445
expectedTags[8], expectedTags[9], expectedTags[10], expectedTags[11], expectedTags[12] });
14421446
}
14431447

1444-
}).Dispose();
1448+
}, s_remoteExecutionOptions).Dispose();
14451449
}
14461450

14471451
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1529,7 +1533,7 @@ public void TestMeterCreationWithOptions()
15291533
Assert.Equal("10.0", meter10.Version);
15301534
Assert.Equal("Scope10", meter10.Scope);
15311535
Assert.Equal("TestMeterCreationWithOptions10", meter10.Name);
1532-
}).Dispose();
1536+
}, s_remoteExecutionOptions).Dispose();
15331537
}
15341538

15351539
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1662,7 +1666,7 @@ public void TestCachedInstruments()
16621666

16631667
Gauge<int> gauge12 = meter.CreateGauge<int>("name9", null, null, t1);
16641668
Assert.NotSame(gauge9, gauge12);
1665-
}).Dispose();
1669+
}, s_remoteExecutionOptions).Dispose();
16661670
}
16671671

16681672
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1728,7 +1732,7 @@ public void TestInstrumentCreationWithTags()
17281732
{
17291733
Assert.True(string.Compare(insArray[i].Key, insArray[i + 1].Key, StringComparison.Ordinal) <= 0);
17301734
}
1731-
}).Dispose();
1735+
}, s_remoteExecutionOptions).Dispose();
17321736
}
17331737

17341738
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1748,7 +1752,7 @@ public void TestHistogramCreationWithAdvice()
17481752

17491753
Assert.NotNull(histogramWithAdvice.Advice?.HistogramBucketBoundaries);
17501754
Assert.Equal(explicitBucketBoundaries, histogramWithAdvice.Advice.HistogramBucketBoundaries);
1751-
}).Dispose();
1755+
}, s_remoteExecutionOptions).Dispose();
17521756
}
17531757

17541758
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1771,7 +1775,7 @@ public void TestRecordingWithEmptyTagList()
17711775
counter.Add(1, new TagList(Array.Empty<KeyValuePair<string, object>>()));
17721776

17731777
Assert.Equal(4, count);
1774-
}).Dispose();
1778+
}, s_remoteExecutionOptions).Dispose();
17751779
}
17761780

17771781
private void PublishCounterMeasurement<T>(Counter<T> counter, T value, KeyValuePair<string, object?>[] tags) where T : struct

0 commit comments

Comments
 (0)