Skip to content

Commit ca3744b

Browse files
authored
fix: profiler flaky tests (#3118)
1 parent 5fab09e commit ca3744b

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/Sentry.Profiling/SampleProfilerSession.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,29 @@ private SampleProfilerSession(SentryStopwatch stopwatch, EventPipeSession sessio
5151

5252
public TraceLog TraceLog => _eventSource.TraceLog;
5353

54-
internal static bool ThrowOnNextStartupForTests = false;
54+
// default is false, set 1 for true.
55+
private static int _throwOnNextStartupForTests = 0;
56+
57+
internal static bool ThrowOnNextStartupForTests
58+
{
59+
get { return Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 1, 1) == 1; }
60+
set
61+
{
62+
if (value)
63+
Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 1, 0);
64+
else
65+
Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 0, 1);
66+
}
67+
}
5568

5669
public static SampleProfilerSession StartNew(IDiagnosticLogger? logger = null)
5770
{
5871
try
5972
{
6073
var client = new DiagnosticsClient(Environment.ProcessId);
6174

62-
if (ThrowOnNextStartupForTests)
75+
if (Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 0, 1) == 1)
6376
{
64-
ThrowOnNextStartupForTests = false;
6577
throw new Exception("Test exception");
6678
}
6779

test/Sentry.Profiling.Tests/SamplingTransactionProfilerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private void RunForMs(int milliseconds)
8888
while (clock.ElapsedMilliseconds < milliseconds)
8989
{
9090
_testOutputLogger.LogDebug("Sleeping... time remaining: {0} ms", milliseconds - clock.ElapsedMilliseconds);
91-
Thread.Sleep((int)Math.Min(milliseconds / 5, milliseconds - clock.ElapsedMilliseconds));
91+
Thread.Sleep((int)Math.Max(0, Math.Min(milliseconds / 5, milliseconds - clock.ElapsedMilliseconds)));
9292
}
9393
}
9494

test/Sentry.Tests/ProfilerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private void RunForMs(int milliseconds)
8585
while (clock.ElapsedMilliseconds < milliseconds)
8686
{
8787
_testOutputLogger.LogDebug("Sleeping... time remaining: {0} ms", milliseconds - clock.ElapsedMilliseconds);
88-
Thread.Sleep((int)Math.Min(milliseconds / 5, milliseconds - clock.ElapsedMilliseconds));
88+
Thread.Sleep((int)Math.Max(0, Math.Min(milliseconds / 5, milliseconds - clock.ElapsedMilliseconds)));
8989
}
9090
}
9191
}

0 commit comments

Comments
 (0)