Skip to content

Commit 2ad2469

Browse files
Added ClearStaleLocations tests
1 parent fa491ee commit 2ad2469

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

src/Sentry/MetricAggregator.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal class MetricAggregator : IMetricAggregator
3131
private readonly Lazy<Dictionary<long, ConcurrentDictionary<string, Metric>>> _buckets
3232
= new(() => new Dictionary<long, ConcurrentDictionary<string, Metric>>());
3333

34-
private long _lastClearedStaleLocations = DateTimeOffset.UtcNow.GetDayBucketKey();
34+
internal long _lastClearedStaleLocations = DateTimeOffset.UtcNow.GetDayBucketKey();
3535
internal readonly ConcurrentDictionary<long, HashSet<MetricResourceIdentifier>> _seenLocations = new();
3636
internal Dictionary<long, Dictionary<MetricResourceIdentifier, SentryStackFrame>> _pendingLocations = new();
3737

@@ -474,9 +474,9 @@ private Dictionary<long, Dictionary<MetricResourceIdentifier, SentryStackFrame>>
474474
/// <summary>
475475
/// Clear out stale seen locations once a day
476476
/// </summary>
477-
private void ClearStaleLocations()
477+
internal void ClearStaleLocations(DateTimeOffset? testNow = null)
478478
{
479-
var now = DateTimeOffset.UtcNow;
479+
var now = testNow ?? DateTimeOffset.UtcNow;
480480
var today = now.GetDayBucketKey();
481481
if (_lastClearedStaleLocations == today)
482482
{

test/Sentry.Tests/MetricAggregatorTests.cs

+75-3
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,9 @@ public async Task Cancel_NonZeroTimeout_SchedulesShutdown()
417417

418418
// Act
419419
await _fixture.CancellationTokenSource.CancelAsync();
420-
await Task.Delay(1000);
420+
#pragma warning disable xUnit1031
421+
sut._loopTask.Wait(10000);
422+
#pragma warning restore xUnit1031
421423

422424
// Assert
423425
_fixture.Logger.Received(1).Log(SentryLevel.Debug, MetricAggregator.ShutdownScheduledMessage, null, Arg.Any<TimeSpan>());
@@ -434,7 +436,9 @@ public async Task Cancel_ZeroTimeout_ShutdownImmediately()
434436

435437
// Act
436438
await _fixture.CancellationTokenSource.CancelAsync();
437-
await Task.Delay(1000);
439+
#pragma warning disable xUnit1031
440+
sut._loopTask.Wait(10000);
441+
#pragma warning restore xUnit1031
438442

439443
// Assert
440444
_fixture.Logger.Received(1).Log(SentryLevel.Debug, MetricAggregator.ShutdownImmediatelyMessage, null);
@@ -471,9 +475,77 @@ public async Task FlushAsync_Cancel_Exists()
471475

472476
// Act
473477
await sut.FlushAsync(true, cancellationTokenSource.Token);
474-
// await Task.Delay(1000);
475478

476479
// Assert
477480
_fixture.Logger.Received(1).Log(SentryLevel.Info, MetricAggregator.FlushShutdownMessage, null);
478481
}
482+
483+
[Fact]
484+
public void ClearStaleLocations_SameDay_NoClear()
485+
{
486+
// Arrange
487+
var time = new DateTimeOffset(2000, 1, 1, 12, 0, 0, TimeSpan.Zero);
488+
489+
var sut = _fixture.GetSut();
490+
sut._lastClearedStaleLocations = time.GetDayBucketKey();
491+
492+
var type = MetricType.Counter;
493+
var key = "counter_key";
494+
var unit = MeasurementUnit.None;
495+
var stackLevel = 1;
496+
sut.RecordCodeLocation(type, key, unit, stackLevel, time.Subtract(TimeSpan.FromDays(1)));
497+
498+
// Act
499+
sut.ClearStaleLocations(time);
500+
501+
// Assert
502+
// (You need some way to check that "_seenLocations" are not modified. This is stubbed in as "SeenLocations")
503+
sut._seenLocations.Should().NotBeEmpty();
504+
}
505+
506+
[Fact]
507+
public void ClearStaleLocations_GraceTime_NoClear()
508+
{
509+
// Arrange
510+
var time = new DateTimeOffset(2000, 1, 1, 0, 0, 30, TimeSpan.Zero);
511+
512+
var sut = _fixture.GetSut();
513+
sut._lastClearedStaleLocations = time.GetDayBucketKey() - 1;
514+
515+
var type = MetricType.Counter;
516+
var key = "counter_key";
517+
var unit = MeasurementUnit.None;
518+
var stackLevel = 1;
519+
sut.RecordCodeLocation(type, key, unit, stackLevel, time.Subtract(TimeSpan.FromDays(1)));
520+
521+
// Act
522+
sut.ClearStaleLocations(time);
523+
524+
// Assert
525+
// (You need some way to check that "_seenLocations" are not modified. This is stubbed in as "SeenLocations")
526+
sut._seenLocations.Should().NotBeEmpty();
527+
}
528+
529+
[Fact]
530+
public void ClearStaleLocations_AfterGraceTime_Clear()
531+
{
532+
// Arrange
533+
var time = new DateTimeOffset(2000, 1, 1, 0, 1, 30, TimeSpan.Zero);
534+
535+
var sut = _fixture.GetSut();
536+
sut._lastClearedStaleLocations = time.GetDayBucketKey() - 1;
537+
538+
var type = MetricType.Counter;
539+
var key = "counter_key";
540+
var unit = MeasurementUnit.None;
541+
var stackLevel = 1;
542+
sut.RecordCodeLocation(type, key, unit, stackLevel, time.Subtract(TimeSpan.FromDays(1)));
543+
544+
// Act
545+
sut.ClearStaleLocations(time);
546+
547+
// Assert
548+
// (You need some way to check that "_seenLocations" are not modified. This is stubbed in as "SeenLocations")
549+
sut._seenLocations.Should().BeEmpty();
550+
}
479551
}

0 commit comments

Comments
 (0)