Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-garcia committed Sep 29, 2021
1 parent cec1cd1 commit 388d2f3
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion test/Sentry.Tests/GlobalSessionManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private class Fixture : IDisposable

public ISystemClock Clock { get; }

public Func<string, PersistedSessionUpdate> PersistedSessionProvider { get; }
public Func<string, PersistedSessionUpdate> PersistedSessionProvider { get; set; }

public Fixture(Action<SentryOptions> configureOptions = null)
{
Expand Down Expand Up @@ -355,6 +355,71 @@ public void TryGetPersistentInstallationId_SessionStarted_CrashDelegateReturnsFa
persistedSessionUpdate!.EndStatus.Should().Be(SessionEndStatus.Abnormal);
}

[Fact]
public void TryGetPersistentInstallationId_CrashDelegateReturnsTrueWithPauseTimestamp_EndsAsCrashed()
{
// Arrange
_fixture.Options.CrashedLastRun = () => true;
// Session was paused before persisted:
var pausedTimestamp = DateTimeOffset.Now;
_fixture.PersistedSessionProvider = _ => new PersistedSessionUpdate(
AnySessionUpdate(),
pausedTimestamp);

var sut = _fixture.GetSut();

// Act
var persistedSessionUpdate = sut.TryRecoverPersistedSession();

// Assert
persistedSessionUpdate.Should().NotBeNull();
persistedSessionUpdate!.EndStatus.Should().Be(SessionEndStatus.Crashed);
}

[Fact]
public void TryGetPersistentInstallationId_CrashDelegateIsNullWithPauseTimestamp_EndsAsExited()
{
// Arrange
_fixture.Options.CrashedLastRun = null;
// Session was paused before persisted:
var pausedTimestamp = DateTimeOffset.Now;
_fixture.PersistedSessionProvider = _ => new PersistedSessionUpdate(
AnySessionUpdate(),
pausedTimestamp);

var sut = _fixture.GetSut();

// Act
var persistedSessionUpdate = sut.TryRecoverPersistedSession();

// Assert
persistedSessionUpdate.Should().NotBeNull();
persistedSessionUpdate!.EndStatus.Should().Be(SessionEndStatus.Exited);
}

[Fact]
public void TryGetPersistentInstallationId_CrashDelegateIsNullWithoutPauseTimestamp_EndsAsAbnormal()
{
// Arrange
_fixture.Options.CrashedLastRun = null;
var pausedTimestamp = DateTimeOffset.Now;
_fixture.PersistedSessionProvider = _ => new PersistedSessionUpdate(
AnySessionUpdate(),
// No pause timestamp:
null);

var sut = _fixture.GetSut();

sut.StartSession();

// Act
var persistedSessionUpdate = sut.TryRecoverPersistedSession();

// Assert
persistedSessionUpdate.Should().NotBeNull();
persistedSessionUpdate!.EndStatus.Should().Be(SessionEndStatus.Abnormal);
}

[Fact]
public void TryGetPersistentInstallationId_SessionStarted_CrashDelegateReturnsTrue_EndsAsCrashed()
{
Expand Down Expand Up @@ -407,5 +472,21 @@ public void TryGetPersistentInstallationId_SessionEnded_ReturnsNull()
// Assert
persistedSessionUpdate.Should().BeNull();
}

// A session update (of which the state doesn't matter for the test):
private static SessionUpdate AnySessionUpdate()
=> new(
SentryId.Create(),
"did",
DateTimeOffset.Now,
"release",
"env",
"ip",
"ua",
0,
true,
DateTimeOffset.Now,
1,
null);
}
}

0 comments on commit 388d2f3

Please sign in to comment.