Skip to content

Commit 669e571

Browse files
authored
[tests] fix flaky ExportThreadSamplesInMixedMode test (#4857)
1 parent 8edcccc commit 669e571

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

test/IntegrationTests/SelectiveSamplerTests.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ public void ExportThreadSamplesInMixedMode()
7878
samples.Count(sample => sample.SelectedForFrequentSampling) == 1)
7979
> 1);
8080

81-
var counter = 0;
82-
8381
// Sampling starts early, at the start of instrumentation init.
8482
var groupingStartingWithAllThreadSamples = groupedByTimestampAscending
8583
.SkipWhile(
@@ -90,31 +88,59 @@ public void ExportThreadSamplesInMixedMode()
9088
// Omit last group from verification, as it may be collected after activity stopped.
9189
.SkipLast(1);
9290

91+
var selectiveSinceLastContinuous = 3;
92+
var missedSelectiveBetweenContinuous = false;
93+
var missedSelectiveInContinuous = false;
94+
9395
foreach (var group in groupingStartingWithAllThreadSamples)
9496
{
9597
// Based on plugin configuration, the expectation is for every 4th
9698
// batch to contain multiple samples as a result of continuous profiling.
97-
if (counter % 4 == 0)
99+
if (IsContinuousProfilingSamplesBatch(group))
98100
{
101+
if (selectiveSinceLastContinuous == 2)
102+
{
103+
Assert.False(missedSelectiveBetweenContinuous, "Missing selective sample between continuous batches allowed only once per test run.");
104+
missedSelectiveBetweenContinuous = true;
105+
}
106+
else
107+
{
108+
Assert.Equal(3, selectiveSinceLastContinuous);
109+
}
110+
111+
selectiveSinceLastContinuous = 0;
99112
#if NET
100113
// on .NET Framework there is no guarantee that samples collected from all threads
101114
Assert.NotEqual(1, group.Count());
102115
#endif
103116
// Sample for thread selected for frequent sampling when collecting samples of all threads
104117
// should be marked with SelectedForFrequentSampling flag.
105-
Assert.Single(group, sample => sample.SelectedForFrequentSampling);
118+
var selectedForFrequentSampling = group.SingleOrDefault(sample => sample.SelectedForFrequentSampling);
119+
if (selectedForFrequentSampling != null)
120+
{
121+
Assert.True(HasSpanContextAssociated(selectedForFrequentSampling));
122+
}
123+
else
124+
{
125+
Assert.False(missedSelectiveInContinuous, "Missing selective sample in continuous batches allowed only once per test run.");
126+
missedSelectiveInContinuous = true;
127+
}
106128
}
107129
else
108130
{
109-
Assert.Single(group);
131+
Assert.True(IndicatesSelectiveSampling(group));
132+
var sample = Assert.Single(group);
133+
Assert.True(HasSpanContextAssociated(sample));
134+
selectiveSinceLastContinuous++;
110135
}
111-
112-
Assert.Single(group, HasSpanContextAssociated);
113-
114-
counter++;
115136
}
116137
}
117138

139+
private static bool IsContinuousProfilingSamplesBatch(IGrouping<long, ConsoleThreadSample> group)
140+
{
141+
return group.All(sample => sample.Source == "continuous-profiler");
142+
}
143+
118144
private static bool IndicatesSelectiveSampling(IGrouping<long, ConsoleThreadSample> samples)
119145
{
120146
return samples.Count() == 1 && samples.Single().Source == "selective-sampler";

0 commit comments

Comments
 (0)