Skip to content

Commit

Permalink
fix: profiler flaky tests (#3118)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind authored Feb 5, 2024
1 parent 5fab09e commit ca3744b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
18 changes: 15 additions & 3 deletions src/Sentry.Profiling/SampleProfilerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,29 @@ private SampleProfilerSession(SentryStopwatch stopwatch, EventPipeSession sessio

public TraceLog TraceLog => _eventSource.TraceLog;

internal static bool ThrowOnNextStartupForTests = false;
// default is false, set 1 for true.
private static int _throwOnNextStartupForTests = 0;

internal static bool ThrowOnNextStartupForTests
{
get { return Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 1, 1) == 1; }
set
{
if (value)
Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 1, 0);
else
Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 0, 1);
}
}

public static SampleProfilerSession StartNew(IDiagnosticLogger? logger = null)
{
try
{
var client = new DiagnosticsClient(Environment.ProcessId);

if (ThrowOnNextStartupForTests)
if (Interlocked.CompareExchange(ref _throwOnNextStartupForTests, 0, 1) == 1)
{
ThrowOnNextStartupForTests = false;
throw new Exception("Test exception");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private void RunForMs(int milliseconds)
while (clock.ElapsedMilliseconds < milliseconds)
{
_testOutputLogger.LogDebug("Sleeping... time remaining: {0} ms", milliseconds - clock.ElapsedMilliseconds);
Thread.Sleep((int)Math.Min(milliseconds / 5, milliseconds - clock.ElapsedMilliseconds));
Thread.Sleep((int)Math.Max(0, Math.Min(milliseconds / 5, milliseconds - clock.ElapsedMilliseconds)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/Sentry.Tests/ProfilerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private void RunForMs(int milliseconds)
while (clock.ElapsedMilliseconds < milliseconds)
{
_testOutputLogger.LogDebug("Sleeping... time remaining: {0} ms", milliseconds - clock.ElapsedMilliseconds);
Thread.Sleep((int)Math.Min(milliseconds / 5, milliseconds - clock.ElapsedMilliseconds));
Thread.Sleep((int)Math.Max(0, Math.Min(milliseconds / 5, milliseconds - clock.ElapsedMilliseconds)));
}
}
}

0 comments on commit ca3744b

Please sign in to comment.