Skip to content

Commit

Permalink
Fix crash with null pointer when accessing session (close #581)
Browse files Browse the repository at this point in the history
PR #582
  • Loading branch information
matus-tomlein committed Feb 15, 2023
1 parent bbc9919 commit 3585118
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
Expand Up @@ -112,6 +112,15 @@ public void sessionInitialization() {
assertEquals(expectedBackground, background);
}

@Test
public void sessionIdEmptyBeforeSessionInitialized() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
NetworkConfiguration networkConfig = new NetworkConfiguration("fake-url", HttpMethod.POST);
TrackerController tracker = Snowplow.createTracker(context, "emptySessionIdTest", networkConfig);
String sessionId = tracker.getSession().getSessionId();
assertEquals("", sessionId);
}

@Test
public void sessionControllerUnavailableWhenContextTurnedOff() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
Expand Down
Expand Up @@ -26,6 +26,7 @@
import com.snowplowanalytics.snowplow.emitter.EventStore;
import com.snowplowanalytics.snowplow.event.SelfDescribing;
import com.snowplowanalytics.snowplow.event.Structured;
import com.snowplowanalytics.snowplow.network.HttpMethod;
import com.snowplowanalytics.snowplow.payload.SelfDescribingJson;
import com.snowplowanalytics.snowplow.tracker.DevicePlatform;
import com.snowplowanalytics.snowplow.internal.emitter.Emitter;
Expand All @@ -35,6 +36,7 @@
import com.snowplowanalytics.snowplow.event.ScreenView;
import com.snowplowanalytics.snowplow.event.Timing;
import com.snowplowanalytics.snowplow.tracker.LogLevel;
import com.snowplowanalytics.snowplow.tracker.MockNetworkConnection;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -399,6 +401,26 @@ public void testTrackWithSession() throws Exception {
mockWebServer.shutdown();
}

// This test was used to replicate issue #581 and fix it
@Test
public void testTrackWithPausedSessionDoesntThrowException() {
Emitter emitter = new Emitter(
getContext(),
"",
new Emitter.EmitterBuilder()
.networkConnection(new MockNetworkConnection(HttpMethod.POST, 200)));

tracker = new Tracker(new Tracker.TrackerBuilder(emitter, "pausedSession", "myAppId", getContext())
.sessionContext(true)
);

// Pause session tracking
tracker.pauseSessionChecking();

// Track an event to make sure it does not throw an exception
tracker.track(new ScreenView("screen1"));
}

@Test
public void testTrackScreenView() {
String namespace = "myNamespace";
Expand Down
Expand Up @@ -15,6 +15,7 @@ public interface SessionController extends SessionConfigurationInterface {
/**
* The session identifier.
* A unique identifier which is used to identify the session.
* Returns an empty string if accessed before the session is initialized.
*/
@NonNull
String getSessionId();
Expand Down
Expand Up @@ -181,7 +181,7 @@ private synchronized static String retrieveUserId(Context context, SessionState
*
* @return a SelfDescribingJson containing the session context
*/
@NonNull
@Nullable
public synchronized SelfDescribingJson getSessionContext(@NonNull String eventId, long eventTimestamp, boolean userAnonymisation) {
Logger.v(TAG, "Getting session context...");
if (isSessionCheckerEnabled) {
Expand All @@ -197,6 +197,10 @@ public synchronized SelfDescribingJson getSessionContext(@NonNull String eventId
}
lastSessionCheck = System.currentTimeMillis();
}

SessionState state = getState();
if (state == null) { return null; }

eventIndex += 1;

Map<String, Object> sessionValues = state.getSessionValues();
Expand Down
Expand Up @@ -69,7 +69,12 @@ public String getSessionId() {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return "";
}
return session.getState().getSessionId();
SessionState state = session.getState();
if (state == null) {
Logger.track(TAG, "Attempt to access session information before session was initialized, use the onSessionUpdate callback");
return "";
}
return state.getSessionId();
}

@NonNull
Expand Down
Expand Up @@ -755,7 +755,9 @@ private void workaroundForIncoherentSessionContext(@NonNull TrackerEvent event)
return;
}
SelfDescribingJson sessionContextJson = sessionManager.getSessionContext(eventId, eventTimestamp, userAnonymisation);
event.contexts.add(sessionContextJson);
if (sessionContextJson != null) {
event.contexts.add(sessionContextJson);
}
}
}

Expand Down

0 comments on commit 3585118

Please sign in to comment.