Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions sentry-core/src/main/java/io/sentry/core/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ SessionPair startSession() {
new Session(
options.getDistinctId(), user, options.getEnvironment(), options.getRelease());

pair = new SessionPair(session, previousSession);
final Session previousClone = previousSession != null ? previousSession.clone() : null;
pair = new SessionPair(session.clone(), previousClone);
}
return pair;
}
Expand Down Expand Up @@ -461,7 +462,7 @@ Session endSession() {
synchronized (sessionLock) {
if (session != null) {
session.end();
previousSession = session;
previousSession = session.clone();
session = null;
}
}
Expand Down
27 changes: 25 additions & 2 deletions sentry-core/src/main/java/io/sentry/core/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public Session(
this(
State.Ok,
DateUtils.getCurrentDateTime(),
null,
DateUtils.getCurrentDateTime(),
Comment thread
marandaneto marked this conversation as resolved.
0,
distinctId,
UUID.randomUUID(),
true,
0L,
null,
Comment thread
marandaneto marked this conversation as resolved.
null,
(user != null ? user.getIpAddress() : null),
null,
Expand Down Expand Up @@ -257,4 +257,27 @@ private long getSequenceTimestamp(final @NotNull Date timestamp) {
}
return sequence;
}

/**
* Ctor copy of the Session
*
* @return a copy of the Session
*/
@SuppressWarnings("MissingOverride")
public @NotNull Session clone() {
return new Session(
status,
started,
timestamp,
errorCount.get(),
distinctId,
sessionId,
init,
sequence,
duration,
ipAddress,
userAgent,
environment,
release);
}
}
129 changes: 129 additions & 0 deletions sentry-core/src/test/java/io/sentry/core/ScopeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNotSame
import kotlin.test.assertNull
import kotlin.test.assertTrue

class ScopeTest {
@Test
Expand Down Expand Up @@ -268,4 +269,132 @@ class ScopeTest {
assertNull(it)
}
}

@Test
fun `Scope clones the start and end session objects`() {
val options = SentryOptions()
val scope = Scope(options)

val sessionPair = scope.startSession()
val endSession = scope.endSession()!!

assertNotSame(sessionPair.current, endSession)
}

@Test
fun `Scope sets init to null when mutating a session`() {
val options = SentryOptions()
val scope = Scope(options)

val start = scope.startSession().current

scope.withSession {
it!!.update(null, null, false)
}

val end = scope.endSession()!!

assertTrue(start.init!!)
assertNull(end.init)
}

@Test
fun `Scope increases session error count when capturing an error`() {
val options = SentryOptions()
val scope = Scope(options)

val start = scope.startSession().current

scope.withSession {
it!!.update(null, null, true)
}

val end = scope.endSession()!!

assertEquals(0, start.errorCount())
assertEquals(1, end.errorCount())
}

@Test
fun `Scope sets status when capturing a fatal error`() {
val options = SentryOptions()
val scope = Scope(options)

val start = scope.startSession().current

scope.withSession {
it!!.update(Session.State.Crashed, null, true)
}

val end = scope.endSession()!!

assertEquals(Session.State.Ok, start.status)
assertEquals(Session.State.Crashed, end.status)
}

@Test
fun `Scope sets user agent when capturing an error`() {
val options = SentryOptions()
val scope = Scope(options)

val start = scope.startSession().current

scope.withSession {
it!!.update(null, "jamesBond", true)
}

val end = scope.endSession()!!

assertNull(start.userAgent)
assertEquals("jamesBond", end.userAgent)
}

@Test
fun `Scope sets timestamp when capturing an error`() {
val options = SentryOptions()
val scope = Scope(options)

val start = scope.startSession().current

scope.withSession {
it!!.update(null, null, true)
}
val end = scope.endSession()!!

assertNotSame(end.timestamp!!, start.timestamp)
}

@Test
fun `Scope increases sequence when capturing an error`() {
val options = SentryOptions()
val scope = Scope(options)

val start = scope.startSession().current

scope.withSession {
it!!.update(null, null, true)
}

val end = scope.endSession()!!

assertNull(start.sequence)
assertTrue(end.sequence!! > 0)
}

@Test
fun `Scope sets duration when ending a session`() {
val options = SentryOptions()
val scope = Scope(options)

val start = scope.startSession().current

scope.withSession {
it!!.update(null, null, true)
}

val end = scope.endSession()!!

assertNull(start.duration)
assertNotNull(end.duration)
}
}
32 changes: 21 additions & 11 deletions sentry-core/src/test/java/io/sentry/core/SentryClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,14 @@ class SentryClientTest {
@Test
fun `When event is Fatal or not handled, mark session as Crashed`() {
val scope = Scope(fixture.sentryOptions)
val session = scope.startSession().current
scope.startSession().current
val event = SentryEvent().apply {
level = SentryLevel.FATAL
}
fixture.getSut().updateSessionData(event, null, scope)
assertEquals(Session.State.Crashed, session.status)
scope.withSession {
assertEquals(Session.State.Crashed, it!!.status)
}
}

@Test
Expand All @@ -480,25 +482,29 @@ class SentryClientTest {
@Test
fun `When event is Fatal, increase errorCount`() {
val scope = Scope(fixture.sentryOptions)
val session = scope.startSession().current
scope.startSession().current
val event = SentryEvent().apply {
level = SentryLevel.FATAL
}
fixture.getSut().updateSessionData(event, null, scope)
assertEquals(1, session.errorCount())
scope.withSession {
assertEquals(1, it!!.errorCount())
}
}

@Test
fun `When event is Errored, increase errorCount`() {
val scope = Scope(fixture.sentryOptions)
val session = scope.startSession().current
scope.startSession().current
val exceptions = mutableListOf<SentryException>()
exceptions.add(SentryException())
val event = SentryEvent().apply {
setExceptions(exceptions)
}
fixture.getSut().updateSessionData(event, null, scope)
assertEquals(1, session.errorCount())
scope.withSession {
assertEquals(1, it!!.errorCount())
}
}

@Test
Expand All @@ -514,14 +520,16 @@ class SentryClientTest {
@Test
fun `When event has userAgent, set it into session`() {
val scope = Scope(fixture.sentryOptions)
val session = scope.startSession().current
scope.startSession().current
val event = SentryEvent().apply {
request = Request()
request.headers = mutableMapOf()
request.headers["user-agent"] = "jamesBond"
}
fixture.getSut().updateSessionData(event, null, scope)
assertEquals("jamesBond", session.userAgent)
scope.withSession {
assertEquals("jamesBond", it!!.userAgent)
}
}

@Test
Expand Down Expand Up @@ -589,10 +597,12 @@ class SentryClientTest {
level = SentryLevel.FATAL
}
val scope = Scope(fixture.sentryOptions)
val session = scope.startSession().current
scope.startSession().current
sut.captureEvent(event, scope, null)
assertEquals(Session.State.Crashed, session.status)
assertEquals(1, session.errorCount())
scope.withSession {
assertEquals(Session.State.Crashed, it!!.status)
assertEquals(1, it.errorCount())
}
}

private fun createScope(): Scope {
Expand Down
17 changes: 14 additions & 3 deletions sentry-core/src/test/java/io/sentry/core/SessionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.util.Date
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNotSame
import kotlin.test.assertNull
import kotlin.test.assertTrue

Expand All @@ -24,7 +25,7 @@ class SessionTest {
assertEquals("distinctId", session.distinctId)
assertEquals("127.0.0.1", session.ipAddress)
assertTrue(session.init!!)
assertEquals(0L, session.sequence)
assertNull(session.sequence)
assertNotNull(session.sessionId)
assertNotNull(session.started)
assertEquals(Session.State.Ok, session.status)
Expand Down Expand Up @@ -96,12 +97,11 @@ class SessionTest {
}
val session = createSession(user)
val timestamp = session.started
val sequecence = session.sequence
session.update(null, null, true)

assertNull(session.init)
assertTrue(session.timestamp!! >= timestamp)
assertTrue(session.sequence!! > sequecence!!)
assertNotNull(session.sequence)
}

@Test
Expand All @@ -116,6 +116,17 @@ class SessionTest {
assertEquals(1489552, session.sequence)
}

@Test
fun `Clone session returns a copy of the session`() {
val user = User().apply {
ipAddress = "127.0.0.1"
}
val session = createSession(user)
val clone = session.clone()

assertNotSame(clone, session)
}

private fun createSession(user: User = User()): Session {
return Session("distinctId", user, "env", "rel")
}
Expand Down