Skip to content

Fix behaviour with multiple subscribers #5031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -180,25 +180,26 @@ public Void call() throws Exception {
});

// TODO(mrober): Replace with a real session implementation.
firebaseSessions.register(new SessionSubscriber() {
@Override
public void onSessionChanged(@NonNull SessionDetails sessionDetails) {
Logger.getLogger().d("onSessionChanged: " + sessionDetails);
// TODO(mrober): Set new field in report and remove this.
core.setInternalKey("sessionId", sessionDetails.getSessionId());
}

@Override
public boolean isDataCollectionEnabled() {
return arbiter.isAutomaticDataCollectionEnabled();
}

@NonNull
@Override
public SessionSubscriber.Name getSessionSubscriberName() {
return SessionSubscriber.Name.CRASHLYTICS;
}
});
firebaseSessions.register(
new SessionSubscriber() {
@Override
public void onSessionChanged(@NonNull SessionDetails sessionDetails) {
Logger.getLogger().d("onSessionChanged: " + sessionDetails);
// TODO(mrober): Set new field in report and remove this.
core.setInternalKey("sessionId", sessionDetails.getSessionId());
}

@Override
public boolean isDataCollectionEnabled() {
return arbiter.isAutomaticDataCollectionEnabled();
}

@NonNull
@Override
public SessionSubscriber.Name getSessionSubscriberName() {
return SessionSubscriber.Name.CRASHLYTICS;
}
});

return new FirebaseCrashlytics(core);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ internal constructor(
"Registering Sessions SDK subscriber with name: ${subscriber.sessionSubscriberName}, " +
"data collection enabled: ${subscriber.isDataCollectionEnabled}"
)

// Immediately call the callback if Sessions generated a session before the
// subscriber subscribed, otherwise subscribers might miss the first session.
if (sessionGenerator.hasGenerateSession) {
subscriber.onSessionChanged(
SessionSubscriber.SessionDetails(sessionGenerator.currentSession.sessionId)
)
}
}

private fun initiateSessionStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ internal class SessionGenerator(
lateinit var currentSession: SessionDetails
private set

/** Returns if a session has been generated. */
val hasGenerateSession: Boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we use this value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used as the condition to send the current session when a subscriber subscribes in FirebaseSessions.

get() = ::currentSession.isInitialized

/** Generates a new session. The first session's sessionId will match firstSessionId. */
fun generateNewSession(): SessionDetails {
sessionIndex++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ class SessionGeneratorTest {
sessionGenerator.currentSession
}

@Test
fun hasGenerateSession_beforeGenerate_returnsFalse() {
val sessionGenerator = SessionGenerator(collectEvents = false)

assertThat(sessionGenerator.hasGenerateSession).isFalse()
}

@Test
fun hasGenerateSession_afterGenerate_returnsTrue() {
val sessionGenerator = SessionGenerator(collectEvents = false)

sessionGenerator.generateNewSession()

assertThat(sessionGenerator.hasGenerateSession).isTrue()
}

@Test
fun generateNewSession_generatesValidSessionIds() {
val sessionGenerator = SessionGenerator(collectEvents = true) // defaults to UUID::randomUUID
Expand Down