Skip to content

Commit 38b75df

Browse files
committed
fix Ok Session termination test
1 parent 3dd986c commit 38b75df

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public void run() {
107107
@Nullable Date timestamp = getTimestampFromCrashMarkerFile(crashMarkerFile);
108108
session.update(Session.State.Crashed, null, true);
109109
session.end(timestamp);
110+
} else if (session.getAbnormalMechanism() == null) {
111+
// this means the session hasn't crashed or abnormally exited and there was no native crash marker, we only end it _now_.
112+
session.end();
110113
}
111114

112115
// Independent of whether the TombstoneIntegration is active or the Native SDK operates

sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,45 @@ class PreviousSessionFinalizerTest {
113113

114114
@Test
115115
fun `if previous session exists, sends session update with current end time`() {
116+
// we start the session 20 seconds before now, this should create enough distance to the current
117+
// DateTime to detect an update
118+
val sessionCreationCaptureInterval = 20000
119+
val sessionCreationTimestamp =
120+
DateUtils.getDateTime(DateUtils.getCurrentDateTime().time - sessionCreationCaptureInterval)
121+
116122
val finalizer =
117-
fixture.getSut(tmpDir, session = Session(null, null, null, "io.sentry.sample@1.0"))
123+
fixture.getSut(
124+
tmpDir,
125+
session =
126+
Session(
127+
Session.State.Ok,
128+
sessionCreationTimestamp,
129+
sessionCreationTimestamp,
130+
0,
131+
null,
132+
SentryUUID.generateSentryId(),
133+
true,
134+
null,
135+
null,
136+
null,
137+
null,
138+
null,
139+
"io.sentry.sample@1.0",
140+
null,
141+
),
142+
)
118143
finalizer.run()
119144

120145
verify(fixture.scopes)
121146
.captureEnvelope(
122147
argThat {
148+
val captureEnvelopeTimestamp = DateUtils.getCurrentDateTime().time
123149
val session = fixture.sessionFromEnvelope(this)
124150
session.release == "io.sentry.sample@1.0" &&
125-
session.timestamp!!.time - DateUtils.getCurrentDateTime().time < 1000
151+
session.started!!.time == sessionCreationTimestamp.time &&
152+
captureEnvelopeTimestamp - session.started!!.time > sessionCreationCaptureInterval &&
153+
session.timestamp!!.time != sessionCreationTimestamp.time &&
154+
captureEnvelopeTimestamp - session.timestamp!!.time < 1000
126155
}
127156
)
128157
}

0 commit comments

Comments
 (0)