Skip to content

Commit

Permalink
Merge d031a9c into 061cf2f
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Feb 13, 2019
2 parents 061cf2f + d031a9c commit ada8d12
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.bugsnag.android

import com.bugsnag.android.BugsnagTestUtils.generateClient
import com.bugsnag.android.BugsnagTestUtils.generateConfiguration
import com.bugsnag.android.BugsnagTestUtils.generateSessionStore
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test

class SessionTrackerStopResumeTest {

private val configuration = generateConfiguration().also {
it.autoCaptureSessions = false
}
private val sessionStore = generateSessionStore()
private lateinit var tracker: SessionTracker

@Before
fun setUp() {
tracker = SessionTracker(configuration, generateClient(), sessionStore)
}

/**
* Verifies that a session can be resumed after it is stopped
*/
@Test
fun resumeFromStoppedSession() {
tracker.startSession(false)
val originalSession = tracker.currentSession
assertNotNull(originalSession)

tracker.stopSession()
assertNull(tracker.currentSession)

assertTrue(tracker.resumeSession())
assertEquals(originalSession, tracker.currentSession)
}

/**
* Verifies that a new session is started when calling [SessionTracker.resumeSession],
* if there is no stopped session
*/
@Test
fun resumeWithNoStoppedSession() {
assertNull(tracker.currentSession)
assertFalse(tracker.resumeSession())
assertNotNull(tracker.currentSession)
}

/**
* Verifies that a new session can be created after the previous one is stopped
*/
@Test
fun startNewAfterStoppedSession() {
tracker.startSession(false)
val originalSession = tracker.currentSession

tracker.stopSession()
tracker.startSession(false)
assertNotEquals(originalSession, tracker.currentSession)
}

/**
* Verifies that calling [SessionTracker.resumeSession] multiple times only starts one session
*/
@Test
fun multipleResumesHaveNoEffect() {
tracker.startSession(false)
val original = tracker.currentSession
tracker.stopSession()

assertTrue(tracker.resumeSession())
assertEquals(original, tracker.currentSession)

assertFalse(tracker.resumeSession())
assertEquals(original, tracker.currentSession)
}

/**
* Verifies that calling [SessionTracker.stopSession] multiple times only stops one session
*/
@Test
fun multipleStopsHaveNoEffect() {
tracker.startSession(false)
assertNotNull(tracker.currentSession)

tracker.stopSession()
assertNull(tracker.currentSession)

tracker.stopSession()
assertNull(tracker.currentSession)
}

/**
* Verifies that if a handled or unhandled error occurs when a session is stopped, the
* error count is not updated
*/
@Test
fun stoppedSessionDoesNotIncrement() {
tracker.startSession(false)
tracker.incrementHandledError()
tracker.incrementUnhandledError()
assertEquals(1, tracker.currentSession?.handledCount)
assertEquals(1, tracker.currentSession?.unhandledCount)

tracker.stopSession()
tracker.incrementHandledError()
tracker.incrementUnhandledError()
tracker.resumeSession()
assertEquals(1, tracker.currentSession?.handledCount)
assertEquals(1, tracker.currentSession?.unhandledCount)

tracker.incrementHandledError()
tracker.incrementUnhandledError()
assertEquals(2, tracker.currentSession?.handledCount)
assertEquals(2, tracker.currentSession?.unhandledCount)
}
}
10 changes: 9 additions & 1 deletion sdk/src/main/java/com/bugsnag/android/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,15 @@ public void update(@NonNull Observable observable, @NonNull Object arg) {
* session everytime the app enters the foreground.
*/
public void startSession() {
sessionTracker.startNewSession(new Date(), user, false);
sessionTracker.startSession(false);
}

public final void stopSession() {
sessionTracker.stopSession();
}

public final boolean resumeSession() {
return sessionTracker.resumeSession();
}

/**
Expand Down
1 change: 1 addition & 0 deletions sdk/src/main/java/com/bugsnag/android/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public Session(String id, Date startedAt, User user, boolean autoCaptured) {
private AtomicInteger unhandledCount = new AtomicInteger();
private AtomicInteger handledCount = new AtomicInteger();
private AtomicBoolean tracked = new AtomicBoolean(false);
final AtomicBoolean isStopped = new AtomicBoolean(false);

String getId() {
return id;
Expand Down
39 changes: 35 additions & 4 deletions sdk/src/main/java/com/bugsnag/android/SessionTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;

import java.io.File;
import java.util.Arrays;
Expand Down Expand Up @@ -65,7 +66,9 @@ class SessionTracker extends Observable implements Application.ActivityLifecycle
* @param date the session start date
* @param user the session user (if any)
*/
@Nullable Session startNewSession(@NonNull Date date, @Nullable User user,
@Nullable
@VisibleForTesting
Session startNewSession(@NonNull Date date, @Nullable User user,
boolean autoCaptured) {
if (configuration.getSessionEndpoint() == null) {
Logger.warn("The session tracking endpoint has not been set. "
Expand All @@ -78,6 +81,29 @@ class SessionTracker extends Observable implements Application.ActivityLifecycle
return session;
}

void startSession(boolean autoCaptured) {
startNewSession(new Date(), client.getUser(), autoCaptured);
}

void stopSession() {
Session session = currentSession.get();

if (session != null) {
session.isStopped.set(true);
}
}

boolean resumeSession() {
Session session = currentSession.get();

if (session == null) {
startSession(false);
return false;
} else {
return session.isStopped.compareAndSet(true, false);
}
}

/**
* Determines whether or not a session should be tracked. If this is true, the session will be
* stored and sent to the Bugsnag API, otherwise no action will occur in this method.
Expand Down Expand Up @@ -141,18 +167,23 @@ private String getReleaseStage() {

@Nullable
Session getCurrentSession() {
return currentSession.get();
Session session = currentSession.get();

if (session != null && !session.isStopped.get()) {
return session;
}
return null;
}

void incrementUnhandledError() {
Session session = currentSession.get();
Session session = getCurrentSession();
if (session != null) {
session.incrementUnhandledErrCount();
}
}

void incrementHandledError() {
Session session = currentSession.get();
Session session = getCurrentSession();
if (session != null) {
session.incrementHandledErrCount();
}
Expand Down

0 comments on commit ada8d12

Please sign in to comment.