Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
getsentry-bot committed Oct 15, 2024
1 parent 4c0ccc8 commit ffad135
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 161 deletions.
29 changes: 15 additions & 14 deletions samples/Sentry.Samples.Console.Basic/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,21 @@
});

#if NET6_0_OR_GREATER
await Task.Delay(1000);
Console.WriteLine();
Console.WriteLine("Choose a demo:");
Console.WriteLine("1. Tracing");
Console.WriteLine("2. Heap Dump");
Console.WriteLine("... or press any other key to quit.");
switch (Console.ReadKey().KeyChar) {
case '1':
await TracingDemo();
break;
case '2':
await HeapDumpDemo(cts.Token);
break;
}
await Task.Delay(1000);
Console.WriteLine();
Console.WriteLine("Choose a demo:");
Console.WriteLine("1. Tracing");
Console.WriteLine("2. Heap Dump");
Console.WriteLine("... or press any other key to quit.");
switch (Console.ReadKey().KeyChar)
{
case '1':
await TracingDemo();
break;
case '2':
await HeapDumpDemo(cts.Token);
break;
}
#else
await TracingDemo();
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/Sentry/Debouncer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private Debouncer(DebouncerInterval intervalType, int eventMaximum = 1, TimeSpan
/// <param name="cooldown">An optional obligatory cooldown since the last event before any other events will be processed</param>
/// <returns></returns>
public static Debouncer PerMinute(int eventMaximum = 1, TimeSpan? cooldown = null)
=> new (DebouncerInterval.Minute, eventMaximum, cooldown);
=> new(DebouncerInterval.Minute, eventMaximum, cooldown);

/// <summary>
/// Creates a debouncer that limits the number of events per hour
Expand All @@ -39,7 +39,7 @@ public static Debouncer PerMinute(int eventMaximum = 1, TimeSpan? cooldown = nul
/// <param name="cooldown">An optional obligatory cooldown since the last event before any other events will be processed</param>
/// <returns></returns>
public static Debouncer PerHour(int eventMaximum = 1, TimeSpan? cooldown = null)
=> new (DebouncerInterval.Hour, eventMaximum, cooldown);
=> new(DebouncerInterval.Hour, eventMaximum, cooldown);

/// <summary>
/// Creates a debouncer that limits the number of events per day
Expand All @@ -48,7 +48,7 @@ public static Debouncer PerHour(int eventMaximum = 1, TimeSpan? cooldown = null)
/// <param name="cooldown">An optional obligatory cooldown since the last event before any other events will be processed</param>
/// <returns></returns>
public static Debouncer PerDay(int eventMaximum = 1, TimeSpan? cooldown = null)
=> new (DebouncerInterval.Day, eventMaximum, cooldown);
=> new(DebouncerInterval.Day, eventMaximum, cooldown);

/// <summary>
/// Creates a debouncer that limits the number of events that will be processed for the lifetime of the application
Expand All @@ -57,7 +57,7 @@ public static Debouncer PerDay(int eventMaximum = 1, TimeSpan? cooldown = null)
/// <param name="cooldown">An optional obligatory cooldown since the last event before any other events will be processed</param>
/// <returns></returns>
public static Debouncer PerApplicationLifetime(int eventMaximum = 1, TimeSpan? cooldown = null)
=> new (DebouncerInterval.ApplicationLifetime, eventMaximum, cooldown);
=> new(DebouncerInterval.ApplicationLifetime, eventMaximum, cooldown);

private TimeSpan IntervalTimeSpan()
{
Expand Down
5 changes: 3 additions & 2 deletions src/Sentry/HeapDumpTriggers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Sentry;

internal static class HeapDumpTriggers
{
internal static HeapDumpTrigger Never { get; } = (_, _) => false;
internal static HeapDumpTrigger Never { get; } = (_, _) => false;
internal static HeapDumpTrigger MemoryPercentageThreshold(int memoryPercentageThreshold)
{
if (memoryPercentageThreshold is < 0 or > 100)
Expand All @@ -24,4 +24,5 @@ internal static HeapDumpTrigger MemoryPercentageThreshold(int memoryPercentageTh
var thresholdBytes = (long)Math.Ceiling(portion * totalMemory);
return usedMemory > thresholdBytes;
};
}}
}
}
280 changes: 140 additions & 140 deletions test/Sentry.Tests/DebouncerTests.cs
Original file line number Diff line number Diff line change
@@ -1,144 +1,144 @@
namespace Sentry.Tests;

public class DebouncerTests
public class DebouncerTests
{
[Fact]
public void PerMinute_InitialisedCorrectly()
{
[Fact]
public void PerMinute_InitialisedCorrectly()
{
// Act
var debouncer = Debouncer.PerMinute();

// Assert
debouncer.Should().NotBeNull();
debouncer._intervalType.Should().Be(Debouncer.DebouncerInterval.Minute);
debouncer._eventMaximum.Should().Be(1);
}

[Fact]
public void PerHour_InitialisedCorrectly()
{
// Act
var debouncer = Debouncer.PerHour();

// Assert
debouncer.Should().NotBeNull();
debouncer._intervalType.Should().Be(Debouncer.DebouncerInterval.Hour);
debouncer._eventMaximum.Should().Be(1);
}

[Fact]
public void PerDay_InitialisedCorrectly()
{
// Act
var debouncer = Debouncer.PerDay();

// Assert
debouncer.Should().NotBeNull();
debouncer._intervalType.Should().Be(Debouncer.DebouncerInterval.Day);
debouncer._eventMaximum.Should().Be(1);
}

[Fact]
public void PerApplicationLifetime_InitialisedCorrectly()
{
// Act
var debouncer = Debouncer.PerApplicationLifetime();

// Assert
debouncer.Should().NotBeNull();
debouncer._intervalType.Should().Be(Debouncer.DebouncerInterval.ApplicationLifetime);
debouncer._eventMaximum.Should().Be(1);
}

[Fact]
public void CanProcess_WithCooldown_RespectsCooldown()
{
// Arrange
var debouncer = Debouncer.PerMinute(10, TimeSpan.FromMinutes(1));
var initialTime = DateTimeOffset.UtcNow;
debouncer.RecordOccurence(initialTime);

// Act & Assert
debouncer.CanProcess(initialTime.AddSeconds(59)).Should().BeFalse();

// Act & Assert
debouncer.CanProcess(initialTime.AddSeconds(60)).Should().BeTrue();

// Act & Assert
debouncer.CanProcess(initialTime.AddSeconds(61)).Should().BeTrue();
}

[Fact]
public void CanProcess_NoCooldown_IgnoresCooldown()
{
// Arrange
var debouncer = Debouncer.PerMinute(10);
var initialTime = DateTimeOffset.UtcNow;
debouncer.RecordOccurence(initialTime);

// Act & Assert
debouncer.CanProcess(initialTime.AddSeconds(59)).Should().BeTrue();

// Act & Assert
debouncer.CanProcess(initialTime.AddSeconds(60)).Should().BeTrue();

// Act & Assert
debouncer.CanProcess(initialTime.AddSeconds(61)).Should().BeTrue();
}

[Fact]
public void CanProcess_RespectsMaximum()
{
// Arrange
var debouncer = Debouncer.PerApplicationLifetime(1);

// Act
var canProcess = debouncer.CanProcess();

// Assert
canProcess.Should().BeTrue();

// Act
debouncer.RecordOccurence();
canProcess = debouncer.CanProcess();

// Assert
canProcess.Should().BeFalse();
}

[Fact]
public void RecordOccurence_WithinInterval_IncrementsOccurrences()
{
// Arrange
var debouncer = Debouncer.PerMinute(10);
var initialTime = DateTimeOffset.UtcNow;
var secondEventTime = initialTime.AddSeconds(10);

// Act
debouncer.RecordOccurence(initialTime);
debouncer.RecordOccurence(secondEventTime);

// Assert
debouncer._occurrences.Should().Be(2);
debouncer._intervalStart.Should().Be(initialTime);
debouncer._lastEvent.Should().Be(secondEventTime);
}

[Fact]
public void RecordOccurence_AfterInterval_ResetsInterval()
{
// Arrange
var debouncer = Debouncer.PerMinute(10);
var initialTime = DateTimeOffset.UtcNow;
var secondEventTime = initialTime.AddSeconds(70);

// Act
debouncer.RecordOccurence(initialTime);
debouncer.RecordOccurence(secondEventTime);

// Assert
debouncer._occurrences.Should().Be(1);
debouncer._intervalStart.Should().Be(secondEventTime);
debouncer._lastEvent.Should().Be(secondEventTime);
}
// Act
var debouncer = Debouncer.PerMinute();

// Assert
debouncer.Should().NotBeNull();
debouncer._intervalType.Should().Be(Debouncer.DebouncerInterval.Minute);
debouncer._eventMaximum.Should().Be(1);
}

[Fact]
public void PerHour_InitialisedCorrectly()
{
// Act
var debouncer = Debouncer.PerHour();

// Assert
debouncer.Should().NotBeNull();
debouncer._intervalType.Should().Be(Debouncer.DebouncerInterval.Hour);
debouncer._eventMaximum.Should().Be(1);
}

[Fact]
public void PerDay_InitialisedCorrectly()
{
// Act
var debouncer = Debouncer.PerDay();

// Assert
debouncer.Should().NotBeNull();
debouncer._intervalType.Should().Be(Debouncer.DebouncerInterval.Day);
debouncer._eventMaximum.Should().Be(1);
}

[Fact]
public void PerApplicationLifetime_InitialisedCorrectly()
{
// Act
var debouncer = Debouncer.PerApplicationLifetime();

// Assert
debouncer.Should().NotBeNull();
debouncer._intervalType.Should().Be(Debouncer.DebouncerInterval.ApplicationLifetime);
debouncer._eventMaximum.Should().Be(1);
}

[Fact]
public void CanProcess_WithCooldown_RespectsCooldown()
{
// Arrange
var debouncer = Debouncer.PerMinute(10, TimeSpan.FromMinutes(1));
var initialTime = DateTimeOffset.UtcNow;
debouncer.RecordOccurence(initialTime);

// Act & Assert
debouncer.CanProcess(initialTime.AddSeconds(59)).Should().BeFalse();

// Act & Assert
debouncer.CanProcess(initialTime.AddSeconds(60)).Should().BeTrue();

// Act & Assert
debouncer.CanProcess(initialTime.AddSeconds(61)).Should().BeTrue();
}

[Fact]
public void CanProcess_NoCooldown_IgnoresCooldown()
{
// Arrange
var debouncer = Debouncer.PerMinute(10);
var initialTime = DateTimeOffset.UtcNow;
debouncer.RecordOccurence(initialTime);

// Act & Assert
debouncer.CanProcess(initialTime.AddSeconds(59)).Should().BeTrue();

// Act & Assert
debouncer.CanProcess(initialTime.AddSeconds(60)).Should().BeTrue();

// Act & Assert
debouncer.CanProcess(initialTime.AddSeconds(61)).Should().BeTrue();
}

[Fact]
public void CanProcess_RespectsMaximum()
{
// Arrange
var debouncer = Debouncer.PerApplicationLifetime(1);

// Act
var canProcess = debouncer.CanProcess();

// Assert
canProcess.Should().BeTrue();

// Act
debouncer.RecordOccurence();
canProcess = debouncer.CanProcess();

// Assert
canProcess.Should().BeFalse();
}

[Fact]
public void RecordOccurence_WithinInterval_IncrementsOccurrences()
{
// Arrange
var debouncer = Debouncer.PerMinute(10);
var initialTime = DateTimeOffset.UtcNow;
var secondEventTime = initialTime.AddSeconds(10);

// Act
debouncer.RecordOccurence(initialTime);
debouncer.RecordOccurence(secondEventTime);

// Assert
debouncer._occurrences.Should().Be(2);
debouncer._intervalStart.Should().Be(initialTime);
debouncer._lastEvent.Should().Be(secondEventTime);
}

[Fact]
public void RecordOccurence_AfterInterval_ResetsInterval()
{
// Arrange
var debouncer = Debouncer.PerMinute(10);
var initialTime = DateTimeOffset.UtcNow;
var secondEventTime = initialTime.AddSeconds(70);

// Act
debouncer.RecordOccurence(initialTime);
debouncer.RecordOccurence(secondEventTime);

// Assert
debouncer._occurrences.Should().Be(1);
debouncer._intervalStart.Should().Be(secondEventTime);
debouncer._lastEvent.Should().Be(secondEventTime);
}
}
2 changes: 1 addition & 1 deletion test/Sentry.Tests/Internals/MemoryMonitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private class Fixture
DiagnosticLogger = Substitute.For<IDiagnosticLogger>()
};

public Action<string> OnDumpCollected { get; set; } = _ => { };
public Action<string> OnDumpCollected { get; set; } = _ => { };

private const short ThresholdPercentage = 5;

Expand Down

0 comments on commit ffad135

Please sign in to comment.