Skip to content

Commit

Permalink
fix: session id not set before plugin setup (#189)
Browse files Browse the repository at this point in the history
* fix: ensure sessionId is set before Plugin.setup is called
  • Loading branch information
justin-fiedler authored Mar 27, 2024
1 parent 1428953 commit 42e4fef
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
15 changes: 12 additions & 3 deletions android/src/main/java/com/amplitude/android/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.amplitude.core.platform.plugins.AmplitudeDestination
import com.amplitude.core.platform.plugins.GetAmpliExtrasPlugin
import com.amplitude.core.utilities.FileStorage
import com.amplitude.id.IdentityConfiguration
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.launch

open class Amplitude(
Expand Down Expand Up @@ -51,10 +52,19 @@ open class Amplitude(
)
}

override fun build(): Deferred<Boolean> {
val deferred = super.build()

// Start session before adding plugins
(timeline as Timeline).initSession()

return deferred
}

override suspend fun buildInternal(identityConfiguration: IdentityConfiguration) {
// Migrations
ApiKeyStorageMigration(this).execute()
if ((this.configuration as Configuration).migrateLegacyData) {
if ((configuration as Configuration).migrateLegacyData) {
RemnantDataMigration(this).execute()
}

Expand Down Expand Up @@ -84,8 +94,7 @@ open class Amplitude(
}
}

val androidTimeline = timeline as Timeline
androidTimeline.start()
(timeline as Timeline).start()
}

/**
Expand Down
12 changes: 8 additions & 4 deletions android/src/main/java/com/amplitude/android/Timeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ class Timeline : Timeline() {
internal var sessionId: Long = Session.EMPTY_SESSION_ID
get() = if (session == null) Session.EMPTY_SESSION_ID else session.sessionId

internal suspend fun start() {
internal fun initSession() {
this.session = Session(
amplitude.configuration as Configuration,
amplitude.storage,
amplitude.store
)
}

internal suspend fun start() {
val sessionEvents = session.startNewSessionIfNeeded(
SystemTime.getCurrentTimeMillis(),
amplitude.configuration.sessionId
Expand All @@ -49,9 +51,11 @@ class Timeline : Timeline() {
}
}

runBlocking {
sessionEvents?.forEach {
processImmediately(it)
if (!amplitude.configuration.optOut) {
runBlocking {
sessionEvents?.forEach {
processImmediately(it)
}
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions android/src/test/java/com/amplitude/android/AmplitudeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.amplitude.android.utils.mockSystemTime
import com.amplitude.common.android.AndroidContextProvider
import com.amplitude.core.StorageProvider
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.platform.DestinationPlugin
import com.amplitude.core.platform.EventPlugin
import com.amplitude.core.platform.Plugin
import com.amplitude.core.utilities.ConsoleLoggerProvider
Expand Down Expand Up @@ -231,6 +232,38 @@ class AmplitudeTest {
}
}

@Test
fun amplitude_should_set_sessionId_before_plugin_setup() = runTest {
class SessionIdPlugin() : DestinationPlugin() {
override val type: Plugin.Type = Plugin.Type.Destination

override lateinit var amplitude: com.amplitude.core.Amplitude

override fun setup(amplitude: com.amplitude.core.Amplitude) {
super.setup(amplitude)

this.amplitude = amplitude

val sessionId = (amplitude as Amplitude).sessionId

Assertions.assertNotNull(sessionId)
}
}

// set session Id in the config
val config = createConfiguration()
// isolate storage from other tests
config.instanceName = "session-id-for-plugin-setup"
val amp = Amplitude(config)
amp.add(SessionIdPlugin())

setDispatcher(testScheduler)

if (amp?.isBuilt!!.await()) {
Assertions.assertNotNull(amp?.sessionId)
}
}

@Test
fun amplitude_should_set_deviceId_from_configuration() = runTest {
val testDeviceId = "test device id"
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/com/amplitude/core/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ open class Amplitude internal constructor(
protected open fun build(): Deferred<Boolean> {
val amplitude = this

storage = configuration.storageProvider.getStorage(amplitude)

val built =
amplitudeScope.async(amplitudeDispatcher, CoroutineStart.LAZY) {
storage = configuration.storageProvider.getStorage(amplitude)
identifyInterceptStorage =
configuration.identifyInterceptStorageProvider.getStorage(
amplitude,
Expand Down

0 comments on commit 42e4fef

Please sign in to comment.