Skip to content

Fix settings not propogate to plugins when offline #269

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 3 commits into from
Jun 24, 2025
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 @@ -3,12 +3,18 @@ package com.segment.analytics.kotlin.android
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import com.segment.analytics.kotlin.android.plugins.AndroidContextPlugin
import com.segment.analytics.kotlin.android.plugins.AndroidLifecyclePlugin
import com.segment.analytics.kotlin.android.utilities.DeepLinkUtils
import com.segment.analytics.kotlin.core.Analytics
import com.segment.analytics.kotlin.core.Configuration
import com.segment.analytics.kotlin.core.checkSettings
import com.segment.analytics.kotlin.core.platform.plugins.logger.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

// A set of functions tailored to the Android implementation of analytics

Expand Down Expand Up @@ -67,6 +73,25 @@ public fun Analytics(
private fun Analytics.startup() {
add(AndroidContextPlugin())
add(AndroidLifecyclePlugin())
registerLifecycle()
}

private fun Analytics.registerLifecycle() {
analyticsScope.launch(Dispatchers.Main) {
ProcessLifecycleOwner.get().lifecycle.addObserver(object : DefaultLifecycleObserver {
var lastCheckSettings = java.lang.System.currentTimeMillis()
val CHECK_SETTINGS_INTERVAL = 10 * 1000L

override fun onStart(owner: LifecycleOwner) {
analyticsScope.launch(analyticsDispatcher) {
if (java.lang.System.currentTimeMillis() - lastCheckSettings > CHECK_SETTINGS_INTERVAL) {
checkSettings()
lastCheckSettings = java.lang.System.currentTimeMillis()
}
}
}
})
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ suspend fun Analytics.checkSettings() {
settingsObj?.let {
log("Dispatching update settings on ${Thread.currentThread().name}")
store.dispatch(System.UpdateSettingsAction(settingsObj), System::class)
update(settingsObj)
}

store.currentState(System::class)?.let { system ->
system.settings?.let { settings ->
log("Propagating settings on ${Thread.currentThread().name}")
update(settings)
}
}

// we're good to go back to a running state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.segment.analytics.kotlin.core.platform.Plugin
import com.segment.analytics.kotlin.core.utils.StubPlugin
import com.segment.analytics.kotlin.core.utils.mockHTTPClient
import com.segment.analytics.kotlin.core.utils.testAnalytics
import io.mockk.every
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.test.TestScope
Expand Down Expand Up @@ -75,6 +76,34 @@ class SettingsTests {
}

@Test
fun `cached settings propagates to plugins when network error`() = runTest {
every { anyConstructed<HTTPClient>().settings("cdn-settings.segment.com/v1") } throws Exception()
val mockPlugin = spyk(StubPlugin())

val settings = Settings(
integrations = buildJsonObject {
put(
"cachedSettings",
buildJsonObject {
put(
"apiKey",
"1vNgUqwJeCHmqgI9S1sOm9UHCyfYqbaQ"
)
})
},
plan = emptyJsonObject,
edgeFunction = emptyJsonObject,
middlewareSettings = emptyJsonObject
)
analytics.store.dispatch(System.UpdateSettingsAction(settings), System::class)
analytics.add(mockPlugin)
verify {
mockPlugin.update(settings, Plugin.UpdateType.Initial)
}
}

// Disabled because now we always propagate settings regardless network status
@Test @Disabled
fun `plugin added before settings is available updates plugin correctly`() = runTest {
// forces settings to fail
mockHTTPClient("")
Expand Down
8 changes: 4 additions & 4 deletions gradle/codecov.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ task codeCoverageReport(type: JacocoReport) {
executionData.setFrom(execData)

reports {
xml.enabled true
xml.destination file("${buildDir}/reports/jacoco/report.xml")
html.enabled true
csv.enabled false
xml.required = true
xml.outputLocation = file("${buildDir}/reports/jacoco/report.xml")
html.required = true
csv.required = false
}
}
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Jan 22 15:05:55 PST 2021
#Tue Jun 24 14:05:08 CDT 2025
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip