From c5b7d776908d327ccaddc2a5df900115d9997f03 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 21:16:41 +0400 Subject: [PATCH 001/109] fix(sdk): requests made before the device has a did are neither lost nor stuck when /init releases them: the did check, the queueing and the release share one lock, so the release no longer races new requests into a ConcurrentModificationException --- .../network/NetworkRepositoryImpl.kt | 31 +++-- .../network/NetworkRepositoryQueueTest.kt | 113 ++++++++++++++++++ 2 files changed, 132 insertions(+), 12 deletions(-) create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryQueueTest.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt index 747d07f5..d466d665 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt @@ -22,7 +22,6 @@ import java.net.ConnectException import java.net.HttpURLConnection import java.net.URL import java.nio.charset.StandardCharsets -import java.util.Collections import javax.inject.Inject class NetworkRepositoryImpl @Inject constructor( @@ -34,7 +33,13 @@ class NetworkRepositoryImpl @Inject constructor( /** How long a request may wait for the next bytes of an answer; tests shorten it. */ internal var readTimeoutMs: Int = READ_TIMEOUT_MS - private val queue: MutableList = Collections.synchronizedList(ArrayList()) + /** + * Requests made before the device has a `did`. Guarded by [queueLock] together with the `did` + * check in [sendAsync]: a request either sees the `did` or lands here before [executeQueueTasks] + * drains, so none is left behind and none starts twice. + */ + private val queueLock = Any() + private val queue = ArrayList() override fun initialize( baseUrl: String @@ -124,24 +129,26 @@ class NetworkRepositoryImpl @Inject constructor( private fun sendAsync(sendFunction: () -> Unit) { val thread = Thread(sendFunction) - if (userSettingsRepository.getDid() - .isNotEmpty() - ) { - thread.start() - } else { - addTaskToQueue(thread) + val startNow = synchronized(queueLock) { + if (userSettingsRepository.getDid().isNotEmpty()) { + true + } else { + queue.add(thread) + false + } } + if (startNow) thread.start() } override fun executeQueueTasks() { - for (thread in queue) { - thread.start() + val tasks = synchronized(queueLock) { + ArrayList(queue).also { queue.clear() } } - queue.clear() + tasks.forEach { it.start() } } override fun addTaskToQueue(thread: Thread) { - queue.add(thread) + synchronized(queueLock) { queue.add(thread) } } private fun sendSecretMethod( diff --git a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryQueueTest.kt new file mode 100644 index 00000000..97e5f5a2 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryQueueTest.kt @@ -0,0 +1,113 @@ +package com.personalization.sdk.data.repositories.network + +import com.personalization.api.OnApiCallbackListener +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import io.mockk.every +import io.mockk.mockk +import org.json.JSONObject +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.net.ServerSocket +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.atomic.AtomicReference + +/** + * Requests made before the device has a `did` wait in a queue that `/init` drains. Adding and + * draining race on different threads: no request may be lost, stuck or started twice. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class NetworkRepositoryQueueTest { + + private val did = AtomicReference("") + private val settings = mockk(relaxed = true) { + every { getDid() } answers { did.get() } + every { getShopId() } returns "shop" + every { getSid() } returns "sid" + every { getSegmentForABTesting() } returns "A" + every { getStream() } returns "android" + } + private val repository = NetworkRepositoryImpl(settings).apply { + // A port nobody listens on: every request ends at once in onError, which is all we count. + val port = ServerSocket(0).use { it.localPort } + initialize("http://127.0.0.1:$port/") + } + + @Test + fun `tasks added while the queue drains all run once`() { + val producers = 8 + val perProducer = 200 + val ran = AtomicInteger() + val failures = CopyOnWriteArrayList() + val producing = CountDownLatch(producers) + + repeat(producers) { + Thread { + repeat(perProducer) { repository.addTaskToQueue(Thread { ran.incrementAndGet() }) } + producing.countDown() + }.start() + } + val drainer = Thread { + try { + while (producing.count > 0) repository.executeQueueTasks() + } catch (throwable: Throwable) { + failures += throwable + } + } + drainer.start() + assertTrue(producing.await(10, TimeUnit.SECONDS)) + drainer.join(10_000) + repository.executeQueueTasks() + + waitFor { ran.get() == producers * perProducer } + assertEquals(emptyList(), failures) + assertEquals(producers * perProducer, ran.get()) + } + + @Test + fun `requests made around the arrival of the did are all sent`() { + val requests = 200 + val ended = AtomicInteger() + val listener = object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) { + ended.incrementAndGet() + } + + override fun onError(code: Int, msg: String?) { + ended.incrementAndGet() + } + } + val start = CountDownLatch(1) + val senders = (1..4).map { + Thread { + start.await() + repeat(requests / 4) { repository.getAsync("products", JSONObject(), listener) } + }.apply { start() } + } + val init = Thread { + start.await() + Thread.sleep(2) + did.set("did") + repository.executeQueueTasks() + }.apply { start() } + + start.countDown() + senders.forEach { it.join(10_000) } + init.join(10_000) + + waitFor { ended.get() == requests } + assertEquals(requests, ended.get()) + } + + private fun waitFor(timeoutMs: Long = 15_000, condition: () -> Boolean) { + val deadline = System.currentTimeMillis() + timeoutMs + while (!condition() && System.currentTimeMillis() < deadline) Thread.sleep(10) + } +} From b4b0a50156731990ecfa05ae9475bfca34968bcc Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 21:24:58 +0400 Subject: [PATCH 002/109] feat(sdk): the SDK's log has levels and a handler for the app: Rees46Config.logLevel (warning by default) and Rees46.setLogHandler with SdkLogMessage (level, stable code, message, shop, context); requests are logged by method and path, and answer bodies, push payloads and the stories answer stay out of the log on every level --- .../com/personalization/AbstractParams.kt | 11 +- .../personalization/HmsMessagingService.kt | 6 +- .../kotlin/com/personalization/LogLevel.kt | 20 +++ .../com/personalization/MessagingService.kt | 8 +- .../main/kotlin/com/personalization/Params.kt | 9 +- .../main/kotlin/com/personalization/Rees46.kt | 37 +++-- .../com/personalization/Rees46Config.kt | 5 +- .../com/personalization/RegisterManager.kt | 41 +++-- .../main/kotlin/com/personalization/SDK.kt | 53 +++++-- .../com/personalization/SdkLogMessage.kt | 27 ++++ .../personalization/errors/actions.error.kt | 9 +- .../com/personalization/errors/base.error.kt | 9 +- .../errors/emptyField.error.kt | 9 +- .../personalization/errors/firebase.error.kt | 10 +- .../errors/invalidDomain.error.kt | 9 +- .../personalization/errors/network.error.kt | 9 +- .../personalization/errors/parsing.error.kt | 9 +- .../errors/resourceLoading.error.kt | 9 +- .../errors/response.error.kt.kt | 6 +- .../impl/InAppNotificationManagerImpl.kt | 15 +- .../helpers/NotificationClickProcessor.kt | 3 +- .../trackEvent/impl/TrackEventManagerImpl.kt | 6 +- .../com/personalization/logs/SdkLogs.kt | 130 ++++++++++++++++ .../com/personalization/popups/PopupsV3Api.kt | 8 +- .../personalization/push/PushTokenManager.kt | 18 ++- .../network/NetworkRepositoryImpl.kt | 36 ++++- .../personalization/stories/StoriesManager.kt | 15 +- .../stories/viewAdapters/ProductsAdapter.kt | 8 +- .../stories/views/PausableProgressBar.kt | 2 - .../stories/views/StoryView.kt | 8 +- .../stories/views/storyItem/StoryItemView.kt | 8 +- .../PersonalizationRecommenderBlock.kt | 3 +- .../ui/widgets/PersonalizationStories.kt | 3 +- .../ui/widgets/StoriesPollsApi.kt | 7 +- .../com/personalization/logs/SdkLogsTest.kt | 142 ++++++++++++++++++ .../network/NetworkRepositoryImplTest.kt | 4 +- .../ui/widgets/StoriesPollsApiTest.kt | 12 +- .../ui/widgets/StoryPollTest.kt | 11 ++ 38 files changed, 594 insertions(+), 141 deletions(-) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/LogLevel.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/SdkLogMessage.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/logs/SdkLogsTest.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/AbstractParams.kt b/personalization-sdk/src/main/kotlin/com/personalization/AbstractParams.kt index 9f066931..028086d0 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/AbstractParams.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/AbstractParams.kt @@ -1,7 +1,8 @@ package com.personalization +import com.personalization.logs.SdkLogs +import com.personalization.logs.LogCodes import android.text.TextUtils -import android.util.Log import org.json.JSONException import org.json.JSONObject @@ -19,7 +20,7 @@ abstract class AbstractParams

> { try { params.put(param.value, value) } catch (e: JSONException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } return this as P } @@ -32,7 +33,7 @@ abstract class AbstractParams

> { try { params.put(param.value, value) } catch (e: JSONException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } return this as P } @@ -41,7 +42,7 @@ abstract class AbstractParams

> { try { params.put(param.value, value) } catch (e: JSONException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } return this as P } @@ -54,7 +55,7 @@ abstract class AbstractParams

> { try { params.put(param.value, TextUtils.join(",", value)) } catch (e: JSONException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } return this as P } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/HmsMessagingService.kt b/personalization-sdk/src/main/kotlin/com/personalization/HmsMessagingService.kt index 561fd526..8245aff6 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/HmsMessagingService.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/HmsMessagingService.kt @@ -1,5 +1,7 @@ package com.personalization +import com.personalization.logs.SdkLogs +import com.personalization.logs.LogCodes import com.huawei.hms.push.HmsMessageService import com.huawei.hms.push.RemoteMessage @@ -22,11 +24,11 @@ class HmsMessagingService : HmsMessageService() { // lazily-registered shop the push targets, so a cold-process push (app not running) reaches // the second shop instead of being dropped for not being live yet. if (data.isNotEmpty()) { - SDK.debug("HMS message data keys: ${data.keys}") + SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_RECEIVED, "HMS data message", data["shop_id"], mapOf("keys" to data.keys.sorted())) Rees46.handlePush(data, PushEventType.RECEIVED) } } catch (throwable: Throwable) { - SDK.error("Failed to handle HMS push message", throwable) + SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "An HMS message could not be handled", throwable = throwable) } } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/LogLevel.kt b/personalization-sdk/src/main/kotlin/com/personalization/LogLevel.kt new file mode 100644 index 00000000..d2914fcb --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/LogLevel.kt @@ -0,0 +1,20 @@ +package com.personalization + +/** + * How much the SDK writes to its log, from the most detailed to nothing. + * + * A message is written when its level is at least the configured one: the default [WARNING] writes + * warnings and errors; [DEBUG] adds requests (method and path), state changes and decisions; [NONE] + * turns the SDK's log off. Whatever the level, the log never carries request data, response bodies, + * personal data, push payloads or the `/init` answer. + */ +enum class LogLevel { + DEBUG, + INFO, + WARNING, + ERROR, + NONE; + + internal fun allows(message: LogLevel): Boolean = + this != NONE && message != NONE && message.ordinal >= ordinal +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/MessagingService.kt b/personalization-sdk/src/main/kotlin/com/personalization/MessagingService.kt index 05bbda9d..d2b1fee9 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/MessagingService.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/MessagingService.kt @@ -1,5 +1,7 @@ package com.personalization +import com.personalization.logs.SdkLogs +import com.personalization.logs.LogCodes import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage @@ -17,7 +19,7 @@ class MessagingService : FirebaseMessagingService() { try { // Check if message contains a data payload. if (remoteMessage.data.isNotEmpty()) { - SDK.debug("Message data payload keys: ${remoteMessage.data.keys}") + SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_RECEIVED, "FCM data message", remoteMessage.data["shop_id"], mapOf("keys" to remoteMessage.data.keys.sorted())) // Route through the facade so a lazily-registered shop the push targets is materialized // on demand — the cold process FCM starts to deliver a data-only message only has the // eagerly-initialized shops live, so a lazy shop would otherwise be dropped at routing. @@ -26,10 +28,10 @@ class MessagingService : FirebaseMessagingService() { // Check if message contains a notification payload. if (remoteMessage.notification != null) { - SDK.debug("Message has a notification payload") + SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_RECEIVED, "FCM message with a notification payload") } } catch (throwable: Throwable) { - SDK.error("Failed to handle push message", throwable) + SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "An FCM message could not be handled", throwable = throwable) } } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Params.kt b/personalization-sdk/src/main/kotlin/com/personalization/Params.kt index 20522857..5edc642a 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Params.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Params.kt @@ -1,6 +1,7 @@ package com.personalization -import android.util.Log +import com.personalization.logs.SdkLogs +import com.personalization.logs.LogCodes import com.personalization.api.params.ProductItemParams import org.json.JSONArray import org.json.JSONException @@ -189,7 +190,7 @@ class Params : AbstractParams() { params.put(field.value, code) } } catch (e: JSONException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } return this } @@ -230,7 +231,7 @@ class Params : AbstractParams() { } array.put(jsonObject) } catch (e: JSONException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } return this @@ -248,7 +249,7 @@ class Params : AbstractParams() { params.put("custom", jsonObject) } catch (e: JSONException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } return this diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt index 4b2388ed..331950d0 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt @@ -1,5 +1,7 @@ package com.personalization +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import android.content.Context import android.os.Bundle import com.personalization.features.inAppNotification.impl.ForegroundActivity @@ -36,6 +38,7 @@ object Rees46 { fun initialize(context: Context, config: Rees46Config): SDK { val sdk = SDK() sdk.enableAutoPopupPresentation = config.enableAutoPopupPresentation + sdk.logLevel = config.logLevel sdk.initialize( context = context, shopId = config.shopId, @@ -158,10 +161,13 @@ object Rees46 { // not warned about. InstanceResolver.Resolution.NotRegistered -> { if (shopId != null) { - SDK.warn( - "Rees46.awaitInstance(\"$shopId\"): no shop with this id is initialized or " + - "registered. Waiting for it to register — if you never register it (typo, or " + - "a missing Rees46.initialize/registerShops), onReady will never fire." + SdkLogs.log( + LogLevel.WARNING, + LogCodes.INVALID_ARGUMENT, + "Rees46.awaitInstance: no shop with this id is initialized or registered. " + + "Waiting for it to register — if it never is (typo, or a missing " + + "Rees46.initialize/registerShops), onReady will never fire.", + shopId ) } SdkRegistry.onNextRegister(shopId, onReady) @@ -199,13 +205,13 @@ object Rees46 { // a tracking/init failure must never undo the shown notification. SdkRegistry.dispatchMessage(shopId = shopId, data = payload.toNotificationData()) runCatching { targetInstance(resolution, shopId)?.onPushReceived(payload) } - .onFailure { SDK.error("handlePush: failed to track received (shop=$shopId)", it) } + .onFailure { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "handlePush: the delivery was not tracked", shopId, throwable = it) } } private fun clickPush(payload: Map) { val (resolution, shopId) = resolvePushTarget(payload[PreferencesPartition.SHOP_ID_FIELD]) ?: return runCatching { targetInstance(resolution, shopId)?.notificationClicked(payload.toBundle()) } - .onFailure { SDK.error("handlePush: failed to track click (shop=$shopId)", it) } + .onFailure { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "handlePush: the click was not tracked", shopId, throwable = it) } } /** @@ -224,9 +230,12 @@ object Rees46 { is InstanceResolver.Resolution.Pending -> resolution to resolution.shopId InstanceResolver.Resolution.Ambiguous, InstanceResolver.Resolution.NotRegistered -> { - SDK.warn( - "handlePush: push dropped — no shop resolves it (shop_id=$payloadShopId, " + - "live=${SdkRegistry.shopIds()}, pending=${pending.keys})." + SdkLogs.log( + LogLevel.WARNING, + LogCodes.PUSH_DROPPED, + "handlePush: push dropped — no shop resolves it", + payloadShopId, + mapOf("live" to SdkRegistry.shopIds().size, "pending" to pending.size) ) null } @@ -265,6 +274,16 @@ object Rees46 { SdkRegistry.setMessageListener(listener) } + /** + * Sets the process-global receiver of the SDK's log messages: each message of every shop that + * passes that shop's [Rees46Config.logLevel], for example to forward it to the app's crash + * reporter. The handler runs on a background thread; an exception it throws is caught. Messages + * carry no personal data. Replaces any previously set handler; pass null to clear. + */ + fun setLogHandler(handler: SdkLogHandler?) { + SdkLogs.handler = handler + } + private fun Map.toBundle(): Bundle = Bundle().also { bundle -> forEach { (key, value) -> bundle.putString(key, value) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt index 42502afc..d5bf3b54 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt @@ -16,6 +16,8 @@ package com.personalization * @property tag log tag for this instance. * @property enableAutoPopupPresentation whether server popups are shown in the activity on screen * while no [SDK.popupPresentationListener] is set. + * @property logLevel how much this shop's instance writes to the log; [LogLevel.WARNING] by default. + * Messages that pass it also reach [Rees46.setLogHandler]. * * Constructors are overloaded for Java: every trailing parameter with a default may be left out, so * Java code written against the seven-argument constructor keeps compiling as parameters are added. @@ -28,5 +30,6 @@ data class Rees46Config @JvmOverloads constructor( val needReInitialization: Boolean = false, val addTrailingSlash: Boolean = true, val tag: String = "SDK", - val enableAutoPopupPresentation: Boolean = true + val enableAutoPopupPresentation: Boolean = true, + val logLevel: LogLevel = LogLevel.WARNING, ) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt index ca41f7fb..a3bbdd76 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt @@ -1,5 +1,7 @@ package com.personalization +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import android.content.ContentResolver import android.content.Context import android.provider.Settings @@ -114,7 +116,7 @@ class RegisterManager @Inject constructor( } ) } catch (e: Exception) { - SDK.error(e.message, e) + log(LogLevel.ERROR, LogCodes.INIT_FAILED, "/init could not be sent", throwable = e) } } @@ -160,29 +162,50 @@ class RegisterManager @Inject constructor( try { onInitFinished?.invoke(popupsV3) } catch (e: Exception) { - SDK.error("Popups v3 failed to start: ${e.message}", e) + log(LogLevel.ERROR, LogCodes.POPUPS_V3, "Popups v3 failed to start", throwable = e) } } - private fun handleInitError(code: Int, msg: String?, attempt: Int, knownDevice: Boolean) { + // The answer's body stays out of the log: a server often repeats the request's data in it. + private fun handleInitError(code: Int, @Suppress("UNUSED_PARAMETER") msg: String?, attempt: Int, knownDevice: Boolean) { if (code in 500..Int.MAX_VALUE || code <= 0) { if (attempt < MAX_INIT_RETRIES) { + log( + LogLevel.WARNING, + LogCodes.INIT_RETRY, + "/init failed, retrying", + mapOf("status" to code, "attempt" to attempt + 1) + ) CoroutineScope(Dispatchers.Main).launch { delay(RETRY_DELAY_MILLISECONDS * (attempt + 1)) sendInitRequestWithRetry(attempt + 1, knownDevice) } - } else if (knownDevice) { - notifyInitFinished(popupsV3 = false) + } else { + log( + LogLevel.ERROR, + LogCodes.INIT_FAILED, + "/init failed after all retries", + mapOf("status" to code, "attempts" to attempt + 1) + ) + if (knownDevice) notifyInitFinished(popupsV3 = false) } } else { if (knownDevice) notifyInitFinished(popupsV3 = false) - BaseInfoError( - TAG, - message = "Init error: code: $code, message: $msg" - ).logError() + log(LogLevel.ERROR, LogCodes.INIT_REFUSED, "The shop refused /init", mapOf("status" to code)) } } + private fun log( + level: LogLevel, + code: String, + message: String, + context: Map = emptyMap(), + throwable: Throwable? = null, + ) { + val shopId = runCatching { getUserSettingsValueUseCase.getShopId() }.getOrNull()?.takeIf { it.isNotEmpty() } + SdkLogs.log(level, code, message, shopId, context, throwable) + } + private fun initializeSdk(seance: String?) { val finalSeance = seance ?: generateOrRetrieveSeance() updateUserSettingsValueUseCase.updateSid(value = finalSeance) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 8effe64d..09b4a706 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -2,10 +2,11 @@ package com.personalization import android.content.Context import android.os.Bundle -import android.util.Log import androidx.core.util.Consumer import androidx.fragment.app.FragmentManager import com.google.firebase.messaging.RemoteMessage +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import com.personalization.Params.InternalParameter import com.personalization.Params.TrackEvent import com.personalization.api.OnApiCallbackListener @@ -163,6 +164,12 @@ open class SDK { applyPopupPresentation() } + /** The level of this instance's log; [Rees46Config.logLevel] sets it for instances made through [Rees46]. */ + internal var logLevel: LogLevel = LogLevel.WARNING + + /** The shop this instance logs for, once it is initialized. */ + private var logShopId: String? = null + /** `popups_v3` of the last `/init` answer; null while `/init` has not answered. */ @Volatile internal var popupsV3InitFlag: Boolean? = null @@ -260,6 +267,7 @@ open class SDK { */ internal fun initializeForPush(context: Context, config: Rees46Config) { enableAutoPopupPresentation = config.enableAutoPopupPresentation + logLevel = config.logLevel initializeInternal( context = context, shopId = config.shopId, @@ -302,6 +310,8 @@ open class SDK { sdkComponent.inject(sdk = this) this.context = context TAG = tag + SdkLogs.register(shopId = shopId, level = logLevel, tag = tag) + logShopId = shopId // Before the init request goes out: its response may already carry a popup. ForegroundActivity.install(context) @@ -371,11 +381,18 @@ open class SDK { .toJson(), listener = object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { - debug("Profile GAID sent successfully") + SdkLogs.log(LogLevel.DEBUG, LogCodes.REQUEST, "Advertising id sent", shopId) } override fun onError(code: Int, msg: String?) { - warn("Profile GAID send failed: $code | $msg") + // Without the answer's body: an error answer often repeats the request's data. + SdkLogs.log( + LogLevel.WARNING, + LogCodes.REQUEST_FAILED, + "Advertising id was not sent", + shopId, + mapOf("status" to code) + ) } } ) @@ -451,7 +468,7 @@ open class SDK { event = event, code = code, storyId = storyId, slideId = slideId ) } else { - Log.i(TAG, "storiesManager is not initialized") + SdkLogs.log(LogLevel.WARNING, LogCodes.STORIES, "storiesManager is not initialized") } } @@ -626,6 +643,8 @@ open class SDK { */ fun release() { SdkRegistry.unregister(this) + // Another instance of the same shop (a repeated initialize) keeps the shop's log level. + logShopId?.takeIf { SdkRegistry.byShopId(it) == null }?.let { SdkLogs.unregister(it) } } /** @@ -944,7 +963,7 @@ open class SDK { } sendAsync(SUBSCRIPTION_UNSUBSCRIBE_PRICE, params, listener) } catch (e: JSONException) { - Log.e(TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } } @@ -1001,7 +1020,7 @@ open class SDK { } sendAsync(SUBSCRIPTION_UNSUBSCRIBE, params, listener) } catch (e: JSONException) { - Log.e(TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } } @@ -1078,7 +1097,7 @@ open class SDK { sendAsync(SUBSCRIPTION_MANAGE, params, listener) popupsV3?.onContextChange(SUBSCRIPTION_MANAGE, params.toString()) } catch (e: JSONException) { - Log.e(TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } } @@ -1143,7 +1162,7 @@ open class SDK { sendAsync("$SEGMENTS_FIELD/$method", params, listener) popupsV3?.onContextChange(SEGMENTS_FIELD, "$method $params") } catch (e: JSONException) { - Log.e(TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } } @@ -1172,7 +1191,7 @@ open class SDK { sendNetworkMethodUseCase.postAsync(TRACK_RECEIVED, params) } } catch (e: JSONException) { - Log.e(TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } } @@ -1243,31 +1262,39 @@ open class SDK { } /** + * Writes [message] to the SDK's log at [LogLevel.DEBUG] (see [Rees46Config.logLevel]). + * * @param message Message */ fun debug(message: String) { - Log.d(TAG, message) + SdkLogs.log(LogLevel.DEBUG, LogCodes.SDK_LOG, message) } /** + * Writes [message] to the SDK's log at [LogLevel.WARNING]. + * * @param message Message */ fun warn(message: String?) { - Log.w(TAG, message.toString()) + SdkLogs.log(LogLevel.WARNING, LogCodes.SDK_LOG, message.toString()) } /** + * Writes [message] to the SDK's log at [LogLevel.ERROR]. + * * @param message Error message */ fun error(message: String?) { - Log.e(TAG, message.toString()) + SdkLogs.log(LogLevel.ERROR, LogCodes.SDK_LOG, message.toString()) } /** + * Writes [message] and [e] to the SDK's log at [LogLevel.ERROR]. + * * @param message Error message */ fun error(message: String?, e: Throwable?) { - Log.e(TAG, message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.SDK_LOG, message.toString(), throwable = e) } /** diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SdkLogMessage.kt b/personalization-sdk/src/main/kotlin/com/personalization/SdkLogMessage.kt new file mode 100644 index 00000000..8900ce36 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/SdkLogMessage.kt @@ -0,0 +1,27 @@ +package com.personalization + +/** + * One message of the SDK's log, as [Rees46.setLogHandler] receives it. + * + * @property level how serious the message is. + * @property code a stable code of the event, such as `init_retry` or `decoding_failed`: match on it, + * not on [message]. + * @property message an English text without personal data. + * @property shopId the shop the message is about, or `null` when it is not about one shop. + * @property context details without personal data: the request path, the HTTP code, the attempt. + */ +data class SdkLogMessage( + val level: LogLevel, + val code: String, + val message: String, + val shopId: String?, + val context: Map = emptyMap(), +) + +/** + * Receives the SDK's log messages that pass the shop's [Rees46Config.logLevel], for example to forward + * them to the app's crash reporter. Called on a background thread; an exception it throws is caught. + */ +fun interface SdkLogHandler { + fun onLog(message: SdkLogMessage) +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/actions.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/actions.error.kt index e93a4c10..b267c667 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/actions.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/actions.error.kt @@ -1,6 +1,8 @@ package com.personalization.errors -import android.util.Log +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs class ActionsError( private val tag: String, @@ -10,9 +12,6 @@ class ActionsError( ) { fun logError() { - Log.e( - /* tag = */ tag, - /* msg = */ "Error caught in $functionName : $message $actionName" - ) + SdkLogs.log(LogLevel.ERROR, LogCodes.UI, "Error caught in $functionName : $message $actionName") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/base.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/base.error.kt index 8f10dc51..5354bdd7 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/base.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/base.error.kt @@ -1,6 +1,8 @@ package com.personalization.errors -import android.util.Log +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs class BaseInfoError( private val tag: String, @@ -9,9 +11,6 @@ class BaseInfoError( ) { fun logError() { - Log.e( - /* tag = */ tag, - /* msg = */"$message: ${exception?.message ?: "Unknown error"}" - ) + SdkLogs.log(LogLevel.ERROR, LogCodes.SDK_LOG, "$message: ${exception?.message ?: "Unknown error"}") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/emptyField.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/emptyField.error.kt index 92a29651..7444a6fa 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/emptyField.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/emptyField.error.kt @@ -1,6 +1,8 @@ package com.personalization.errors -import android.util.Log +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs class EmptyFieldError( private val tag: String, @@ -9,9 +11,6 @@ class EmptyFieldError( ) { fun logError() { - Log.e( - /* tag = */ tag, - /* msg = */ "Field is empty or null in $functionName : $message" - ) + SdkLogs.log(LogLevel.ERROR, LogCodes.INVALID_ARGUMENT, "Field is empty or null in $functionName : $message") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/firebase.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/firebase.error.kt index 9d5d3bdb..57841b9c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/firebase.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/firebase.error.kt @@ -1,6 +1,8 @@ package com.personalization.errors -import android.util.Log +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs class FirebaseError( private val tag: String, @@ -8,10 +10,6 @@ class FirebaseError( ) { fun logError() { - Log.e( - /* tag = */ tag, - /* msg = */ - "Failed to retrieve Firebase token: ${exception?.message ?: "Unknown error"}" - ) + SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_TOKEN_FAILED, "Failed to retrieve Firebase token: ${exception?.message ?: "Unknown error"}") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/invalidDomain.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/invalidDomain.error.kt index 1049f426..905e7ab2 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/invalidDomain.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/invalidDomain.error.kt @@ -1,6 +1,8 @@ package com.personalization.errors -import android.util.Log +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs class InvalidDomainError( private val tag: String, @@ -11,10 +13,7 @@ class InvalidDomainError( * Logs the error with a specific message */ fun logError(message: String) { - Log.e( - /* tag = */ tag, - /* msg = */ "Error caught in $functionName: $message" - ) + SdkLogs.log(LogLevel.ERROR, LogCodes.INVALID_CONFIG, "Error caught in $functionName: $message") } /** diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/network.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/network.error.kt index dd214068..207c98e7 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/network.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/network.error.kt @@ -1,6 +1,8 @@ package com.personalization.errors -import android.util.Log +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs class NetworkError( private val tag: String, @@ -8,9 +10,6 @@ class NetworkError( ) { fun logError() { - Log.e( - /* tag = */ tag, - /* msg = */ "Error caught in $functionName : Network connection issue " - ) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_FAILED, "Error caught in $functionName : Network connection issue ") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/parsing.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/parsing.error.kt index 19f961a7..51dc3c88 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/parsing.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/parsing.error.kt @@ -1,6 +1,8 @@ package com.personalization.errors -import android.util.Log +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs class ParsingJsonError( private val tag: String, @@ -9,9 +11,6 @@ class ParsingJsonError( ) { fun logError() { - Log.e( - /* tag = */ tag, - /* msg = */ "Error caught in $functionName: $message" - ) + SdkLogs.log(LogLevel.ERROR, LogCodes.DECODING_FAILED, "Error caught in $functionName: $message") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/resourceLoading.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/resourceLoading.error.kt index 874041b4..aece26a3 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/resourceLoading.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/resourceLoading.error.kt @@ -1,6 +1,8 @@ package com.personalization.errors -import android.util.Log +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs class ResourceLoadError( private val tag: String, @@ -9,9 +11,6 @@ class ResourceLoadError( ) { fun logError() { - Log.e( - /* tag = */ tag, - /* msg = */ "Error caught in $functionName: $message" - ) + SdkLogs.log(LogLevel.ERROR, LogCodes.UI, "Error caught in $functionName: $message") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/response.error.kt.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/response.error.kt.kt index 53f1b94d..1f1a8242 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/response.error.kt.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/response.error.kt.kt @@ -1,6 +1,8 @@ package com.personalization.errors -import android.util.Log +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import org.json.JSONObject /** @@ -48,6 +50,6 @@ class JsonResponseErrorHandler( message: String? = this.message, exception: Exception? = null ) { - Log.e(tag, message, exception) + SdkLogs.log(LogLevel.ERROR, LogCodes.DECODING_FAILED, message ?: "Response is null or incorrect", throwable = exception) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt index e6521bf3..4d02b191 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt @@ -1,5 +1,8 @@ package com.personalization.features.inAppNotification.impl +import com.personalization.logs.SdkLogs +import com.personalization.logs.LogCodes +import com.personalization.LogLevel import android.app.Activity import android.content.Context import android.content.Intent @@ -93,11 +96,11 @@ class InAppNotificationManagerImpl @Inject constructor( val activity = try { listener(popupDto) } catch (exception: Exception) { - SDK.error("Popup ${popupDto.id} was not shown: the presentation listener failed", exception) + SdkLogs.log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "Popup was not shown: the presentation listener threw", context = mapOf("popup" to popupDto.id), throwable = exception) return } if (activity == null) { - SDK.debug("Popup ${popupDto.id} was kept back by the presentation listener") + SdkLogs.log(LogLevel.DEBUG, LogCodes.POPUP_NOT_SHOWN, "Popup kept back by the presentation listener", context = mapOf("popup" to popupDto.id)) return } activity.supportFragmentManager.takeIf { it.canShowDialog() } @@ -106,18 +109,18 @@ class InAppNotificationManagerImpl @Inject constructor( presentation.autoPresentation -> defaultTarget() else -> { - SDK.debug("Popup ${popupDto.id} was not shown: automatic presentation is off") + SdkLogs.log(LogLevel.DEBUG, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown: automatic presentation is off", context = mapOf("popup" to popupDto.id)) return } } if (target == null) { - SDK.warn("Popup ${popupDto.id} was not shown: no activity that can take a dialog") + SdkLogs.log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown: no activity that can take a dialog", context = mapOf("popup" to popupDto.id)) return } try { showDialog(target, extractDialogData(popupDto)) } catch (exception: Exception) { - SDK.error("Popup ${popupDto.id} was not shown: ${exception.message}", exception) + SdkLogs.log(LogLevel.ERROR, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown", context = mapOf("popup" to popupDto.id), throwable = exception) return } @@ -259,7 +262,7 @@ class InAppNotificationManagerImpl @Inject constructor( target: FragmentManager? ) { if (target == null) { - SDK.warn("Dialog was not shown: no activity on screen to show it in") + SdkLogs.log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Dialog was not shown: no activity on screen to show it in") return } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt index 47bee2a6..6ea82360 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt @@ -46,7 +46,8 @@ class NotificationClickProcessor @Inject constructor( EmptyFieldError( tag = TAG, functionName = FUNCTION_NAME, - message = errorMessage + extras + // Not the extras themselves: they carry the push's content. + message = "$errorMessage; extras keys: ${extras?.keySet()?.sorted()}" ).logError() } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt index ab2443d5..3df63db8 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt @@ -1,6 +1,8 @@ package com.personalization.features.trackEvent.impl -import android.util.Log +import com.personalization.logs.SdkLogs +import com.personalization.logs.LogCodes +import com.personalization.LogLevel import com.personalization.SDK import com.personalization.SdkRegistry import com.personalization.Params @@ -235,7 +237,7 @@ internal class TrackEventManagerImpl @Inject constructor( .put(SOURCE_CODE_PARAMETER, source.code) ) } catch (e: JSONException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt new file mode 100644 index 00000000..9547af26 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt @@ -0,0 +1,130 @@ +package com.personalization.logs + +import android.util.Log +import com.personalization.LogLevel +import com.personalization.SDK +import com.personalization.SdkLogHandler +import com.personalization.SdkLogMessage +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.Executor +import java.util.concurrent.Executors + +/** + * The SDK's log: writes to logcat and hands each message to the app's [SdkLogHandler]. + * + * Each shop has its own level ([register]); a message about a shop passes that level. A message that + * is not about one shop passes the most detailed level among the shops, or [LogLevel.WARNING] while + * there is none. Callers pass only data without personal data: method and path, codes, counts. + */ +internal object SdkLogs { + + private data class Shop(val level: LogLevel, val tag: String) + + private val shops = ConcurrentHashMap() + + @Volatile + var handler: SdkLogHandler? = null + + /** Where the handler runs: one background thread, so messages keep their order. */ + @Volatile + internal var handlerExecutor: Executor = Executors.newSingleThreadExecutor { runnable -> + Thread(runnable, "rees46-log").apply { isDaemon = true } + } + + fun register(shopId: String, level: LogLevel, tag: String) { + shops[shopId] = Shop(level, tag) + } + + fun unregister(shopId: String) { + shops.remove(shopId) + } + + internal fun reset() { + shops.clear() + handler = null + } + + /** The level of [shopId], or for a message without a shop the most detailed level of any shop. */ + fun levelFor(shopId: String?): LogLevel { + shopId?.let { shops[it] }?.let { return it.level } + return shops.values.minByOrNull { it.level.ordinal }?.level ?: LogLevel.WARNING + } + + fun log( + level: LogLevel, + code: String, + message: String, + shopId: String? = null, + context: Map = emptyMap(), + throwable: Throwable? = null, + ) { + if (!levelFor(shopId).allows(level)) return + val tag = shopId?.let { shops[it]?.tag } ?: SDK.TAG + val fullContext = if (throwable == null) context else context + ("exception" to throwable.javaClass.simpleName) + writeToLogcat(level, tag, code, message, fullContext, throwable) + val handler = handler ?: return + val entry = SdkLogMessage(level, code, message, shopId, fullContext) + try { + handlerExecutor.execute { + try { + handler.onLog(entry) + } catch (failure: Throwable) { + Log.w(tag, "[log_handler_failed] the app's log handler threw", failure) + } + } + } catch (rejected: Throwable) { + Log.w(tag, "[log_handler_failed] the log handler could not be scheduled", rejected) + } + } + + private fun writeToLogcat( + level: LogLevel, + tag: String, + code: String, + message: String, + context: Map, + throwable: Throwable?, + ) { + val line = buildString { + append('[').append(code).append("] ").append(message) + if (context.isNotEmpty()) append(' ').append(context) + } + when (level) { + LogLevel.DEBUG -> Log.d(tag, line, throwable) + LogLevel.INFO -> Log.i(tag, line, throwable) + LogLevel.WARNING -> Log.w(tag, line, throwable) + LogLevel.ERROR -> Log.e(tag, line, throwable) + LogLevel.NONE -> Unit + } + } +} + +/** Stable codes of the SDK's log events. */ +internal object LogCodes { + /** A free-text message written through [SDK.debug], [SDK.warn] or [SDK.error]. */ + const val SDK_LOG = "sdk_log" + + const val REQUEST = "request" + const val REQUEST_FAILED = "request_failed" + const val REQUEST_BUILD_FAILED = "request_build_failed" + const val DECODING_FAILED = "decoding_failed" + const val CALLBACK_FAILED = "callback_failed" + const val INVALID_ARGUMENT = "invalid_argument" + const val INVALID_CONFIG = "invalid_config" + + const val INIT_RETRY = "init_retry" + const val INIT_REFUSED = "init_refused" + const val INIT_FAILED = "init_failed" + + const val PUSH_RECEIVED = "push_received" + const val PUSH_FAILED = "push_failed" + const val PUSH_DROPPED = "push_dropped" + const val PUSH_TOKEN_SENT = "push_token_sent" + const val PUSH_TOKEN_FAILED = "push_token_failed" + + const val POPUP_NOT_SHOWN = "popup_not_shown" + const val POPUPS_V3 = "popups_v3" + const val STORIES = "stories" + const val STORIES_V3 = "stories_v3" + const val UI = "ui" +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Api.kt b/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Api.kt index 4b471c2d..91f47b18 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Api.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Api.kt @@ -1,5 +1,8 @@ package com.personalization.popups +import com.personalization.logs.SdkLogs +import com.personalization.logs.LogCodes +import com.personalization.LogLevel import android.content.Context import android.content.SharedPreferences import com.personalization.PreferencesPartition @@ -60,7 +63,8 @@ internal class PopupsV3Api( null -> null } } else { - SDK.debug("popups v3: popup $id request failed: ${response.code} ${message(response.body)}") + // Without the answer's body: an error answer often repeats the request's data. + SdkLogs.log(LogLevel.DEBUG, LogCodes.POPUPS_V3, "Popup request failed", context = mapOf("popup" to id, "status" to response.code)) null } } catch (throwable: Throwable) { @@ -113,7 +117,7 @@ internal class PopupsV3Api( } override fun onError(code: Int, msg: String?) { - SDK.debug("popups v3: promo code of list $listId failed: $code $msg") + SdkLogs.log(LogLevel.DEBUG, LogCodes.POPUPS_V3, "Promo code request failed", context = mapOf("list" to listId, "status" to code)) MainThread.run { callback(null) } } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt index afe81a23..44b4387e 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt @@ -1,7 +1,9 @@ package com.personalization.push +import com.personalization.logs.SdkLogs +import com.personalization.logs.LogCodes +import com.personalization.LogLevel import android.content.Context -import android.util.Log import com.personalization.OnPushTokenListener import com.personalization.PushProvider import com.personalization.api.OnApiCallbackListener @@ -161,17 +163,21 @@ class PushTokenManager @Inject constructor( // Released only after the token is persisted, so a delivery arriving in // between is still deduplicated — by the cache rather than by the claim. tokensInFlight.remove(inFlightKey(provider, token)) - Log.d(TAG, "${provider.id} push token successfully sent and saved") + SdkLogs.log(LogLevel.INFO, LogCodes.PUSH_TOKEN_SENT, "Push token sent", null, mapOf("provider" to provider.id)) external?.onSuccess(response) } override fun onError(code: Int, msg: String?) { // Nothing was persisted, so free the claim and let a later delivery retry. tokensInFlight.remove(inFlightKey(provider, token)) - BaseInfoError( - tag = TAG, - message = "${provider.id} push token failed. Code: $code, Message: $msg" - ).logError() + // Without the answer's body: an error answer often repeats the request's data. + SdkLogs.log( + LogLevel.ERROR, + LogCodes.PUSH_TOKEN_FAILED, + "Push token was not sent", + null, + mapOf("provider" to provider.id, "status" to code) + ) external?.onError(code, msg) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt index d466d665..1170f88a 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt @@ -1,5 +1,8 @@ package com.personalization.sdk.data.repositories.network +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import android.net.Uri import com.personalization.SDK import com.personalization.api.OnApiCallbackListener @@ -175,7 +178,7 @@ class NetworkRepositoryImpl @Inject constructor( executeMethod(networkMethod, newParams, listener) } catch (e: JSONException) { - SDK.error(e.message, e) + log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "A request could not be built", networkMethod, throwable = e) } } @@ -252,7 +255,7 @@ class NetworkRepositoryImpl @Inject constructor( responseCode = connection.responseCode // Method and path only: the query string and the body carry the user's data. - SDK.debug("$responseCode: ${networkMethod.type} ${networkMethod.method}") + log(LogLevel.DEBUG, LogCodes.REQUEST, "Request answered", networkMethod, responseCode) if (responseCode in HTTP_SUCCESS) { // A body the read timeout cuts short throws here and ends in onError. @@ -269,16 +272,16 @@ class NetworkRepositoryImpl @Inject constructor( } responseBodyForLog = error // Without the body: an error answer often repeats the request's data. - SDK.error("$responseCode: ${networkMethod.type} ${networkMethod.method}") + log(LogLevel.ERROR, LogCodes.REQUEST_FAILED, "Request failed", networkMethod, responseCode) Outcome.Failure(responseCode, error) } } catch (e: ConnectException) { - SDK.error("${networkMethod.type} ${networkMethod.method}: ${e.message}") + log(LogLevel.ERROR, LogCodes.REQUEST_FAILED, "No connection to the server", networkMethod, throwable = e) errorForLog = e.message responseCode = 504 Outcome.Failure(504, e.message) } catch (e: Exception) { - SDK.error("${networkMethod.type} ${networkMethod.method}: ${e.message}", e) + log(LogLevel.ERROR, LogCodes.REQUEST_FAILED, "Request ended without an answer", networkMethod, throwable = e) errorForLog = e.message Outcome.Failure(-1, e.message) } finally { @@ -294,7 +297,7 @@ class NetworkRepositoryImpl @Inject constructor( error = errorForLog ) } catch (e: Exception) { - SDK.error("NetworkLogger failed: ${e.message}", e) + log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "The app's NetworkLogger threw", networkMethod, throwable = e) } } @@ -333,7 +336,7 @@ class NetworkRepositoryImpl @Inject constructor( else -> listener.onSuccess(body as JSONObject) } } catch (e: Exception) { - SDK.error("onSuccess of a request failed: ${e.message}", e) + log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "onSuccess of a request threw", throwable = e) fail(listener, -1, e.message) } is Outcome.Failure -> fail(listener, outcome.code, outcome.message) @@ -344,7 +347,7 @@ class NetworkRepositoryImpl @Inject constructor( try { listener.onError(code, message) } catch (e: Exception) { - SDK.error("onError of a request failed: ${e.message}", e) + log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "onError of a request threw", throwable = e) } } @@ -407,6 +410,23 @@ class NetworkRepositoryImpl @Inject constructor( buildString { reader.forEachLine { append(it) } } } + /** Writes to the log by method and path only: the query and the body carry the user's data. */ + private fun log( + level: LogLevel, + code: String, + message: String, + method: NetworkMethod? = null, + status: Int? = null, + throwable: Throwable? = null, + ) { + val context = buildMap { + method?.let { put("method", it.type); put("path", it.method) } + status?.let { put("status", it) } + } + val shopId = runCatching { userSettingsRepository.getShopId() }.getOrNull()?.takeIf { it.isNotEmpty() } + SdkLogs.log(level, code, message, shopId, context, throwable) + } + private companion object { const val CONNECT_TIMEOUT_MS = 5_000 const val READ_TIMEOUT_MS = 10_000 diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/StoriesManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/StoriesManager.kt index 8b0f6b6b..93577022 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/StoriesManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/StoriesManager.kt @@ -1,8 +1,10 @@ package com.personalization.stories +import com.personalization.logs.SdkLogs +import com.personalization.logs.LogCodes +import com.personalization.LogLevel import android.os.Handler import android.os.Looper -import android.util.Log import com.personalization.SDK import com.personalization.api.OnApiCallbackListener import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase @@ -48,7 +50,7 @@ class StoriesManager @Inject constructor( try { onLoaded(getStories(response)) } catch (e: JSONException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) } } } @@ -58,7 +60,7 @@ class StoriesManager @Inject constructor( internal fun showStories(looper: Looper, code: String) { val view = lastAttachedView?.get() if (view == null) { - Log.w(SDK.TAG, "showStories($code): no StoriesView is attached to present from") + SdkLogs.log(LogLevel.WARNING, LogCodes.STORIES, "showStories: no StoriesView is attached to present from", context = mapOf("code" to code)) return } requestStories( @@ -66,7 +68,6 @@ class StoriesManager @Inject constructor( listener = object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { response?.let { - Log.d("stories", response.toString()) try { val stories = getStories(response) @@ -76,7 +77,7 @@ class StoriesManager @Inject constructor( showStories(looper, view, stories) } catch (e: JSONException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) } } } @@ -131,7 +132,7 @@ class StoriesManager @Inject constructor( val effectiveCode = code ?: lastRequestedCode if (effectiveCode == null) { val message = "trackStory($event): no stories code given and no block loaded yet" - Log.w(SDK.TAG, message) + SdkLogs.log(LogLevel.WARNING, LogCodes.STORIES, message) listener?.onError(CLIENT_VALIDATION_ERROR_CODE, message) return } @@ -146,7 +147,7 @@ class StoriesManager @Inject constructor( sendNetworkMethodUseCase.postAsync(TRACK_STORIES_METHOD, params, listener) } catch (e: JSONException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) listener?.onError(CLIENT_VALIDATION_ERROR_CODE, e.message) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/viewAdapters/ProductsAdapter.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/viewAdapters/ProductsAdapter.kt index 87d946a0..bee642ac 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/viewAdapters/ProductsAdapter.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/viewAdapters/ProductsAdapter.kt @@ -2,12 +2,14 @@ package com.personalization.stories.viewAdapters +import com.personalization.logs.SdkLogs +import com.personalization.logs.LogCodes +import com.personalization.LogLevel import android.annotation.SuppressLint import android.content.ActivityNotFoundException import android.content.Intent import android.graphics.Paint import android.net.Uri -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -104,7 +106,7 @@ internal class ProductsAdapter( private fun setupClickListener(product: Product) { itemView.setOnClickListener { - Log.d(SDK.TAG, "click: ${product.name}, ${product.url}") + SdkLogs.log(LogLevel.DEBUG, LogCodes.STORIES, "Story product clicked") handleProductClick(product) } } @@ -126,7 +128,7 @@ internal class ProductsAdapter( onCloseStories.invoke() } } catch (e: ActivityNotFoundException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) } } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/PausableProgressBar.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/PausableProgressBar.kt index 421b300b..8efcb8fd 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/PausableProgressBar.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/PausableProgressBar.kt @@ -2,7 +2,6 @@ package com.personalization.stories.views import android.content.Context import android.util.AttributeSet -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.animation.Animation @@ -116,7 +115,6 @@ internal class PausableProgressBar @JvmOverloads constructor( override fun onAnimationEnd(animation: Animation) { if (!paused) { - Log.d(SDK.TAG, "onAnimationEnd") callback?.onFinishProgress() } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoryView.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoryView.kt index 5a0cbdde..ca1b85da 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoryView.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoryView.kt @@ -2,9 +2,11 @@ package com.personalization.stories.views +import com.personalization.logs.SdkLogs +import com.personalization.logs.LogCodes +import com.personalization.LogLevel import android.annotation.SuppressLint import android.graphics.Color -import android.util.Log import android.util.Pair import android.view.MotionEvent import android.view.View @@ -223,7 +225,7 @@ internal class StoryView( } override fun onPlayerError(error: PlaybackException) { - Log.e(TAG, "player error: " + error.message + ", story: " + story?.id) + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Story video failed", context = mapOf("story" to story?.id), throwable = error) val holder = currentHolder holder?.storyItem?.let { storyItem -> storyItem.reloadLayout.visibility = VISIBLE @@ -275,7 +277,7 @@ internal class StoryView( try { storiesProgressView.resume() } catch (e: IndexOutOfBoundsException) { - Log.e(TAG, "Video onPrepared $e") + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Story video could not start", throwable = e) } } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/storyItem/StoryItemView.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/storyItem/StoryItemView.kt index 529efee1..4b942008 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/storyItem/StoryItemView.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/storyItem/StoryItemView.kt @@ -2,6 +2,9 @@ package com.personalization.stories.views.storyItem +import com.personalization.logs.SdkLogs +import com.personalization.logs.LogCodes +import com.personalization.LogLevel import android.annotation.SuppressLint import android.content.ActivityNotFoundException import android.content.ClipData @@ -13,7 +16,6 @@ import android.graphics.Paint import android.graphics.Typeface import android.graphics.drawable.Drawable import android.net.Uri -import android.util.Log import android.view.Gravity import android.view.MotionEvent import android.view.View @@ -340,10 +342,10 @@ class StoryItemView( } storyStateListener.onStoryStateChanged(StoryState.CLOSE) } catch (e: ActivityNotFoundException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) Toast.makeText(context, "Unknown error", Toast.LENGTH_SHORT).show() } catch (e: NullPointerException) { - Log.e(SDK.TAG, e.message, e) + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) Toast.makeText(context, "Unknown error", Toast.LENGTH_SHORT).show() } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationRecommenderBlock.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationRecommenderBlock.kt index 87cb80b3..c1cdb5e0 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationRecommenderBlock.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationRecommenderBlock.kt @@ -413,7 +413,8 @@ class PersonalizationRecommenderBlock @JvmOverloads constructor( } private fun report(code: Int, message: String?) { - SDK.error("PersonalizationRecommenderBlock: $code $message") + // The code only: the message of a failed request is the answer's body. + SDK.error("PersonalizationRecommenderBlock: request failed ($code)") onError?.invoke(code, message) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStories.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStories.kt index ce3f9393..a3a924d7 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStories.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStories.kt @@ -283,7 +283,8 @@ class PersonalizationStories @JvmOverloads constructor( instanceHandle?.cancel() instanceHandle = null pendingOpen = null - SDK.error("PersonalizationStories: $code $message") + // The code only: the message of a failed request is the answer's body. + SDK.error("PersonalizationStories: request failed ($code)") if (stories.isEmpty()) visibility = View.GONE onError?.invoke(code, message) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesPollsApi.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesPollsApi.kt index 19bc0396..29af6db6 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesPollsApi.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesPollsApi.kt @@ -28,7 +28,7 @@ internal class StoriesPollsApi(private val sdk: SDK, private val domain: String) val claimed = AtomicBoolean(false) val timeout = Runnable { if (!claimed.compareAndSet(false, true)) return@Runnable - SDK.debug("stories poll: $url failed: no did in ${DID_TIMEOUT_MS / 1000} s") + SDK.debug("stories poll: ${pathOf(url)} failed: no did in ${DID_TIMEOUT_MS / 1000} s") callback(StoriesPollsResult.Failure(StoriesLoader.ERROR_CODE, "no did")) } handler.postDelayed(timeout, DID_TIMEOUT_MS) @@ -70,11 +70,14 @@ internal class StoriesPollsApi(private val sdk: SDK, private val domain: String) .put("did", sdk.getDid().orEmpty()) .put("sid", runCatching { sdk.getSid() }.getOrNull().orEmpty()) + // Path only, without the answer: the query carries did and sid, an error answer often repeats them. private fun failure(url: String, code: Int, message: String?): StoriesPollsResult { - SDK.debug("stories poll: $url failed: $code $message") + SDK.debug("stories poll: ${pathOf(url)} failed: $code") return StoriesPollsResult.Failure(code, message) } + private fun pathOf(url: String): String = runCatching { java.net.URL(url).path }.getOrDefault("") + /** `message` ответа с ошибкой, иначе начало тела. */ private fun message(body: String): String? = runCatching { JSONObject(body).optString("message") }.getOrNull()?.takeIf { it.isNotEmpty() } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/logs/SdkLogsTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/logs/SdkLogsTest.kt new file mode 100644 index 00000000..e9b772aa --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/logs/SdkLogsTest.kt @@ -0,0 +1,142 @@ +package com.personalization.logs + +import com.personalization.LogLevel +import com.personalization.Rees46 +import com.personalization.SDK +import com.personalization.SdkLogMessage +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import org.robolectric.shadows.ShadowLog +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.Executor + +/** The SDK's log: levels per shop, the app's handler, and what reaches logcat. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class SdkLogsTest { + + private val received = CopyOnWriteArrayList() + private lateinit var previousExecutor: Executor + + @Before + fun setUp() { + SdkLogs.reset() + previousExecutor = SdkLogs.handlerExecutor + SdkLogs.handlerExecutor = Executor { it.run() } + Rees46.setLogHandler { received += it } + ShadowLog.clear() + } + + @After + fun tearDown() { + SdkLogs.reset() + SdkLogs.handlerExecutor = previousExecutor + } + + @Test + fun `warning is the default level`() { + SdkLogs.log(LogLevel.DEBUG, "c_debug", "debug") + SdkLogs.log(LogLevel.INFO, "c_info", "info") + SdkLogs.log(LogLevel.WARNING, "c_warning", "warning") + SdkLogs.log(LogLevel.ERROR, "c_error", "error") + + assertEquals(listOf("c_warning", "c_error"), received.map { it.code }) + } + + @Test + fun `a shop's message passes that shop's level`() { + SdkLogs.register("debug-shop", LogLevel.DEBUG, "SDK") + SdkLogs.register("quiet-shop", LogLevel.ERROR, "SDK") + + SdkLogs.log(LogLevel.DEBUG, "request", "to debug-shop", shopId = "debug-shop") + SdkLogs.log(LogLevel.WARNING, "init_retry", "to quiet-shop", shopId = "quiet-shop") + SdkLogs.log(LogLevel.ERROR, "init_refused", "to quiet-shop", shopId = "quiet-shop") + + assertEquals(listOf("request" to "debug-shop", "init_refused" to "quiet-shop"), received.map { it.code to it.shopId }) + } + + @Test + fun `a message without a shop passes the most detailed level of any shop`() { + SdkLogs.register("a", LogLevel.ERROR, "SDK") + SdkLogs.register("b", LogLevel.INFO, "SDK") + + SdkLogs.log(LogLevel.DEBUG, "c_debug", "debug") + SdkLogs.log(LogLevel.INFO, "c_info", "info") + + assertEquals(listOf("c_info"), received.map { it.code }) + } + + @Test + fun `none turns the shop's log off, logcat included`() { + SdkLogs.register("silent", LogLevel.NONE, "SilentTag") + + SdkLogs.log(LogLevel.ERROR, "init_refused", "refused", shopId = "silent") + + assertTrue(received.isEmpty()) + assertTrue(ShadowLog.getLogsForTag("SilentTag").isEmpty()) + } + + @Test + fun `the handler gets the code, the shop and the context`() { + SdkLogs.register("shop", LogLevel.WARNING, "SDK") + + SdkLogs.log( + LogLevel.ERROR, + "request_failed", + "Request failed", + shopId = "shop", + context = mapOf("path" to "push", "status" to 500), + throwable = IllegalStateException("boom"), + ) + + val message = received.single() + assertEquals(LogLevel.ERROR, message.level) + assertEquals("Request failed", message.message) + assertEquals("shop", message.shopId) + assertEquals(mapOf("path" to "push", "status" to 500, "exception" to "IllegalStateException"), message.context) + } + + @Test + fun `a handler that throws does not reach the caller`() { + Rees46.setLogHandler { throw IllegalStateException("the app's handler failed") } + + SdkLogs.log(LogLevel.ERROR, "init_refused", "refused") + + assertTrue(ShadowLog.getLogs().any { it.msg.contains("log_handler_failed") }) + } + + @Test + fun `logcat gets the code and the shop's tag`() { + SdkLogs.register("shop", LogLevel.DEBUG, "ShopTag") + + SdkLogs.log(LogLevel.INFO, "push_token_sent", "Push token sent", shopId = "shop", context = mapOf("provider" to "fcm")) + + val line = ShadowLog.getLogsForTag("ShopTag").single().msg + assertEquals("[push_token_sent] Push token sent {provider=fcm}", line) + } + + @Test + fun `the static SDK functions write through the log at their level`() { + SDK.debug("hidden at the default level") + SDK.warn("shown") + + assertEquals(listOf("sdk_log" to "shown"), received.map { it.code to it.message }) + assertFalse(received.any { it.message.contains("hidden") }) + } + + @Test + fun `log levels allow from their own level up`() { + assertTrue(LogLevel.WARNING.allows(LogLevel.ERROR)) + assertTrue(LogLevel.WARNING.allows(LogLevel.WARNING)) + assertFalse(LogLevel.WARNING.allows(LogLevel.INFO)) + assertFalse(LogLevel.NONE.allows(LogLevel.ERROR)) + assertFalse(LogLevel.DEBUG.allows(LogLevel.NONE)) + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImplTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImplTest.kt index 68422723..b8727277 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImplTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImplTest.kt @@ -168,7 +168,7 @@ class NetworkRepositoryImplTest { assertEquals(listOf("error:-1"), listener.awaitCalls(timeoutMs = 5_000)) val log = ShadowLog.getLogs().joinToString("\n") { it.msg.orEmpty() } - assertTrue("the failed request is named: $log", log.contains("GET products/get:")) + assertTrue("the failed request is named: $log", log.contains("method=GET, path=products/get")) } @Test @@ -225,7 +225,7 @@ class NetworkRepositoryImplTest { assertFalse(log, log.contains("79990001122")) assertFalse(log, log.contains("answer@example.com")) assertFalse(log, log.contains("79990004455")) - assertTrue(log, log.contains("422: POST profile/set")) + assertTrue(log, log.contains("method=POST, path=profile/set, status=422")) } /** Reads one HTTP/1.1 request from [socket], records it and writes the [handler]'s reply. */ diff --git a/personalization-sdk/src/test/kotlin/com/personalization/ui/widgets/StoriesPollsApiTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/ui/widgets/StoriesPollsApiTest.kt index e9198005..4a58d39b 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/ui/widgets/StoriesPollsApiTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/ui/widgets/StoriesPollsApiTest.kt @@ -1,5 +1,7 @@ package com.personalization.ui.widgets +import com.personalization.logs.SdkLogs +import com.personalization.LogLevel import android.os.Looper import android.util.Log import com.personalization.SDK @@ -40,6 +42,8 @@ class StoriesPollsApiTest { every { sdk.getSid() } returns "sid1" every { sdk.getUserSettingsValueUseCase.getShopId() } returns "shop1" StoriesApiV3.runner = { it.run() } + // The debug log is what these tests read; the default level hides it. + SdkLogs.register("log-test", LogLevel.DEBUG, "SDK") StoriesApiV3.http = StoriesApiV3.Http { method, url, body -> requests += Triple(method, url, body) networkError?.let { throw it } @@ -51,6 +55,7 @@ class StoriesPollsApiTest { fun tearDown() { StoriesApiV3.http = originalHttp StoriesApiV3.runner = { Thread(it).start() } + SdkLogs.reset() } @Test @@ -91,18 +96,19 @@ class StoriesPollsApiTest { } @Test - fun `a failure goes to the debug log with its code and message`() { + fun `a failure goes to the debug log with its code, without the answer`() { reply = StoriesApiV3.Response(429, """{"status":"fail","message":"Too many requests"}""") answer() val logs = ShadowLog.getLogs().filter { it.type == Log.DEBUG }.map { it.msg } - assertTrue(logs.toString(), logs.any { it.contains("/polls/mini_polls/gift_recipient/answers failed: 429 Too many requests") }) + assertTrue(logs.toString(), logs.any { it.contains("/polls/mini_polls/gift_recipient/answers failed: 429") }) + assertTrue(logs.toString(), logs.none { it.contains("Too many requests") }) } @Test fun `a request is logged by method and path, without the identity and the answer`() { answer(body = mapOf("text" to "my own answer")) val logs = ShadowLog.getLogs().map { it.msg.orEmpty() } - assertTrue(logs.toString(), logs.any { it == "200: POST /api/v3/polls/mini_polls/gift_recipient/answers" }) + assertTrue(logs.toString(), logs.any { it.endsWith("200: POST /api/v3/polls/mini_polls/gift_recipient/answers") }) assertTrue(logs.toString(), logs.none { "did1" in it || "sid1" in it || "my own answer" in it }) } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/ui/widgets/StoryPollTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/ui/widgets/StoryPollTest.kt index 34982d68..0431b977 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/ui/widgets/StoryPollTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/ui/widgets/StoryPollTest.kt @@ -1,5 +1,7 @@ package com.personalization.ui.widgets +import com.personalization.logs.SdkLogs +import com.personalization.LogLevel import android.app.Activity import android.graphics.Color import android.util.Log @@ -254,6 +256,15 @@ class StoryPollTest { @Test fun `unknown kinds go to the debug log`() { + SdkLogs.register("log-test", LogLevel.DEBUG, "SDK") + try { + assertUnknownKindsAreLogged() + } finally { + SdkLogs.reset() + } + } + + private fun assertUnknownKindsAreLogged() { assertNull(StoryPoll.parse(JSONObject("""{"type":"poll","poll_type":"emoji","poll":{"code":"x"}}"""))) assertNull(StoryPoll.parse(JSONObject("""{"type":"poll","poll_type":"mini_poll","poll":{"code":"x","kind":"quiz"}}"""))) val logs = ShadowLog.getLogs().filter { it.type == Log.DEBUG }.map { it.msg } From d43293905a886f758b7ad0d3e5f4589ec98b4b16 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 21:26:57 +0400 Subject: [PATCH 003/109] test(sdk): a harness for the core: an injectable clock and dispatchers, fakes for connectivity and the app's lifecycle, and the shared contract fixtures (contract 0: init, tracking) loaded and checked for shape --- personalization-sdk/build.gradle | 1 + .../com/personalization/core/AppLifecycle.kt | 14 +++ .../kotlin/com/personalization/core/Clock.kt | 13 +++ .../core/ConnectivityMonitor.kt | 11 +++ .../personalization/core/CoreDispatchers.kt | 18 ++++ .../personalization/core/ContractFixtures.kt | 29 ++++++ .../com/personalization/core/CoreFakes.kt | 61 +++++++++++++ .../personalization/core/CoreHarnessTest.kt | 91 +++++++++++++++++++ .../src/test/resources/contract/0/README.md | 45 +++++++++ .../src/test/resources/contract/0/init.json | 52 +++++++++++ .../test/resources/contract/0/tracking.json | 85 +++++++++++++++++ 11 files changed, 420 insertions(+) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/AppLifecycle.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/Clock.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/ConnectivityMonitor.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/CoreDispatchers.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/ContractFixtures.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/CoreFakes.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/CoreHarnessTest.kt create mode 100644 personalization-sdk/src/test/resources/contract/0/README.md create mode 100644 personalization-sdk/src/test/resources/contract/0/init.json create mode 100644 personalization-sdk/src/test/resources/contract/0/tracking.json diff --git a/personalization-sdk/build.gradle b/personalization-sdk/build.gradle index 1f0f174f..3358f7d8 100644 --- a/personalization-sdk/build.gradle +++ b/personalization-sdk/build.gradle @@ -150,6 +150,7 @@ dependencies { testImplementation 'junit:junit:4.13.2' testImplementation 'io.mockk:mockk:1.13.11' + testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4' testImplementation 'androidx.test:core:1.6.1' testImplementation 'org.robolectric:robolectric:4.12.1' // compileOnly dependencies are absent from the unit test classpath, and the Compose compiler diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/AppLifecycle.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/AppLifecycle.kt new file mode 100644 index 00000000..75f10855 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/AppLifecycle.kt @@ -0,0 +1,14 @@ +package com.personalization.core + +import com.personalization.Cancellable + +/** + * Whether the app is in the foreground, and when that changes: going to the background flushes the + * queue, coming back after two hours starts a new session. + */ +internal interface AppLifecycle { + val isForeground: Boolean + + /** Calls [listener] with the new state on every change, until the returned handle is cancelled. */ + fun addListener(listener: (foreground: Boolean) -> Unit): Cancellable +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/Clock.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/Clock.kt new file mode 100644 index 00000000..a0edfa75 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/Clock.kt @@ -0,0 +1,13 @@ +package com.personalization.core + +/** + * The time the core reads: the session window, retry pauses, the queue's limits, the source's + * 48 hours. Tests pass their own to move time without waiting. + */ +internal fun interface Clock { + fun nowMillis(): Long + + companion object { + val SYSTEM: Clock = Clock { System.currentTimeMillis() } + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/ConnectivityMonitor.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/ConnectivityMonitor.kt new file mode 100644 index 00000000..56d73ce1 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/ConnectivityMonitor.kt @@ -0,0 +1,11 @@ +package com.personalization.core + +import com.personalization.Cancellable + +/** Whether the device has a network, and when that changes: the queue sends when it comes back. */ +internal interface ConnectivityMonitor { + val isConnected: Boolean + + /** Calls [listener] with the new state on every change, until the returned handle is cancelled. */ + fun addListener(listener: (connected: Boolean) -> Unit): Cancellable +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/CoreDispatchers.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/CoreDispatchers.kt new file mode 100644 index 00000000..f6f5ad69 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/CoreDispatchers.kt @@ -0,0 +1,18 @@ +package com.personalization.core + +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers + +/** + * Where the core runs its work: network and disk on [io], app callbacks on [main]. Tests pass test + * dispatchers to run the core step by step. + */ +internal class CoreDispatchers( + val io: CoroutineDispatcher = Dispatchers.IO, + val main: CoroutineDispatcher = Dispatchers.Main, + val default: CoroutineDispatcher = Dispatchers.Default, +) { + companion object { + val DEFAULT = CoreDispatchers() + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/ContractFixtures.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/ContractFixtures.kt new file mode 100644 index 00000000..ec6c8cdf --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/ContractFixtures.kt @@ -0,0 +1,29 @@ +package com.personalization.core + +import org.json.JSONArray +import org.json.JSONObject + +/** + * The shared fixtures of the SDK contract, copied from `documentation-unified/sdk/fixtures` into + * `src/test/resources/contract//`. Every SDK checks its implementation against the same + * cases; update them by copying the new files into a folder of the new contract version. + */ +internal object ContractFixtures { + const val VERSION = "0" + + /** The `cases` of `.json`; each file declares the contract version it was written for. */ + fun cases(name: String): List { + val file = load(name) + check(file.getString("contract") == VERSION) { + "$name.json is for contract ${file.getString("contract")}, the tests expect $VERSION" + } + val cases: JSONArray = file.getJSONArray("cases") + return List(cases.length()) { cases.getJSONObject(it) } + } + + private fun load(name: String): JSONObject { + val path = "contract/$VERSION/$name.json" + val stream = requireNotNull(javaClass.classLoader?.getResourceAsStream(path)) { "no fixture $path" } + return JSONObject(stream.bufferedReader(Charsets.UTF_8).use { it.readText() }) + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/CoreFakes.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/CoreFakes.kt new file mode 100644 index 00000000..86d30312 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/CoreFakes.kt @@ -0,0 +1,61 @@ +package com.personalization.core + +import com.personalization.Cancellable +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.TestCoroutineScheduler +import java.util.concurrent.CopyOnWriteArrayList + +/** A clock that moves only when the test says so. */ +internal class FakeClock(var now: Long = 1_700_000_000_000L) : Clock { + override fun nowMillis(): Long = now + + fun advance(millis: Long) { + now += millis + } +} + +/** Network on or off, switched by the test; listeners hear each switch. */ +internal class FakeConnectivity(connected: Boolean = true) : ConnectivityMonitor { + private val listeners = CopyOnWriteArrayList<(Boolean) -> Unit>() + + override var isConnected: Boolean = connected + private set + + override fun addListener(listener: (connected: Boolean) -> Unit): Cancellable { + listeners += listener + return Cancellable { listeners -= listener } + } + + fun set(connected: Boolean) { + if (connected == isConnected) return + isConnected = connected + listeners.forEach { it(connected) } + } +} + +/** Foreground or background, switched by the test; listeners hear each switch. */ +internal class FakeAppLifecycle(foreground: Boolean = true) : AppLifecycle { + private val listeners = CopyOnWriteArrayList<(Boolean) -> Unit>() + + override var isForeground: Boolean = foreground + private set + + override fun addListener(listener: (foreground: Boolean) -> Unit): Cancellable { + listeners += listener + return Cancellable { listeners -= listener } + } + + fun set(foreground: Boolean) { + if (foreground == isForeground) return + isForeground = foreground + listeners.forEach { it(foreground) } + } +} + +/** Every dispatcher of the core on one test scheduler: the test decides when work runs. */ +@OptIn(ExperimentalCoroutinesApi::class) +internal fun testCoreDispatchers(scheduler: TestCoroutineScheduler = TestCoroutineScheduler()): CoreDispatchers { + val dispatcher = StandardTestDispatcher(scheduler) + return CoreDispatchers(io = dispatcher, main = dispatcher, default = dispatcher) +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/CoreHarnessTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/CoreHarnessTest.kt new file mode 100644 index 00000000..27e73239 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/CoreHarnessTest.kt @@ -0,0 +1,91 @@ +package com.personalization.core + +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.launch +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.withContext +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** + * The test harness of the core: the contract fixtures load and are well formed, and the fakes behave. + * No behaviour of the SDK is checked here yet. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class CoreHarnessTest { + + @Test + fun `init fixtures pair a server answer with an expected state`() { + val cases = ContractFixtures.cases("init") + assertTrue(cases.isNotEmpty()) + cases.forEach { case -> + val name = case.getString("name") + assertTrue(name, case.has("response")) + assertTrue(name, case.getJSONObject("expect").has("state")) + val status = case.getJSONObject("response").opt("status") + assertTrue(name, status == null || status == org.json.JSONObject.NULL || status is Int) + } + } + + @Test + fun `tracking fixtures pair a call with a request or an error`() { + val cases = ContractFixtures.cases("tracking") + assertTrue(cases.isNotEmpty()) + cases.forEach { case -> + val name = case.getString("name") + val call = case.getJSONObject("call") + assertEquals(name, "tracking", call.getString("namespace")) + assertTrue(name, call.getString("method").isNotEmpty()) + assertTrue(name, case.has("request") xor case.has("error")) + if (case.has("request")) { + val request = case.getJSONObject("request") + assertTrue(name, request.getString("method") in setOf("GET", "POST")) + assertFalse(name, request.getString("path").startsWith("/")) + } + } + } + + @Test + fun `the fake clock moves only when told`() { + val clock = FakeClock(now = 1_000) + clock.advance(2 * 60 * 60 * 1000L) + assertEquals(1_000 + 2 * 60 * 60 * 1000L, clock.nowMillis()) + } + + @Test + fun `fake connectivity and lifecycle tell listeners about changes until cancelled`() { + val network = FakeConnectivity(connected = false) + val heard = mutableListOf() + val handle = network.addListener { heard += it } + network.set(true) + network.set(true) + handle.cancel() + network.set(false) + assertEquals(listOf(true), heard) + + val lifecycle = FakeAppLifecycle(foreground = true) + val states = mutableListOf() + lifecycle.addListener { states += it } + lifecycle.set(false) + assertEquals(listOf(false), states) + assertFalse(lifecycle.isForeground) + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun `test dispatchers run the core's work when the test lets them`() = runTest { + val dispatchers = testCoreDispatchers(testScheduler) + var ran = false + launch { withContext(dispatchers.io) { ran = true } } + assertFalse(ran) + advanceUntilIdle() + assertTrue(ran) + } +} diff --git a/personalization-sdk/src/test/resources/contract/0/README.md b/personalization-sdk/src/test/resources/contract/0/README.md new file mode 100644 index 00000000..d36898fc --- /dev/null +++ b/personalization-sdk/src/test/resources/contract/0/README.md @@ -0,0 +1,45 @@ +# Фикстуры + +Общие тестовые данные для юнит-тестов всех SDK. Один файл — один неймспейс или механика ядра. SDK копируют файлы в свои тесты целиком и проверяют по ним свои реализации; при обновлении копируется новая версия файла. + +## Формат + +```json +{ + "contract": "0", + "cases": [ … ] +} +``` + +`contract` — версия контракта, по которой написан файл. + +### Вызов → запрос + +```json +{ + "name": "productView с источником", + "call": { "namespace": "tracking", "method": "productView", + "args": { "itemId": "37", "source": { "type": "dynamic", "code": "block-1" } } }, + "request": { "method": "POST", "path": "push", + "body": { "event": "view", "items": [ { "id": "37" } ], + "recommended_by": "dynamic", "recommended_code": "block-1" } } +} +``` + +- `args` — аргументы метода по именам контракта. +- `request.path` — путь без хоста и без ведущей косой черты. +- `request.query` для `GET`, `request.body` для `POST`. Сравниваются только перечисленные поля; служебные `shop_id`, `did`, `sid`, `stream`, `segment`, `time` добавляет ядро, и они проверяются отдельными тестами ядра. +- `error` вместо `request` — метод не должен отправлять запрос и должен вернуть эту ошибку: `{ "type": "invalidArgument" }`. + +### Ответ → результат + +```json +{ + "name": "402 на /init — отказ магазина", + "response": { "status": 402, "body": { "status": "error", "message": "Restricted Mode" } }, + "expect": { "state": "refused" } +} +``` + +- `response.status: null` — ответа нет (нет сети или таймаут). +- `expect` — результат метода, `error` (`SdkError`) или состояние ядра. diff --git a/personalization-sdk/src/test/resources/contract/0/init.json b/personalization-sdk/src/test/resources/contract/0/init.json new file mode 100644 index 00000000..4d34dec3 --- /dev/null +++ b/personalization-sdk/src/test/resources/contract/0/init.json @@ -0,0 +1,52 @@ +{ + "contract": "0", + "cases": [ + { + "name": "новое устройство получает did и sid", + "response": { "status": 200, "body": { "did": "AbCdEf1234", "seance": "XyZ0987654" } }, + "expect": { "state": "ready", "deviceId": "AbCdEf1234", "sessionId": "XyZ0987654" } + }, + { + "name": "2xx без did", + "response": { "status": 200, "body": {} }, + "expect": { "state": "retrying" } + }, + { + "name": "402 — отказ магазина", + "response": { "status": 402, "body": { "status": "error", "message": "Your store is in Restricted Mode." } }, + "expect": { "state": "refused" } + }, + { + "name": "403 — отказ магазина", + "response": { "status": 403, "body": null }, + "expect": { "state": "refused" } + }, + { + "name": "404 — нет магазина", + "response": { "status": 404, "body": { "status": "error", "message": "Shop not found" } }, + "expect": { "state": "refused" } + }, + { + "name": "503 — повтор", + "response": { "status": 503, "body": null }, + "expect": { "state": "retrying" } + }, + { + "name": "нет ответа и нет сохранённого ответа", + "response": { "status": null }, + "expect": { "state": "retrying" } + }, + { + "name": "нет ответа, есть сохранённый ответ", + "saved": { "did": "Saved00001", "seance": "OldSeance1" }, + "response": { "status": null }, + "expect": { "state": "retrying", "deviceId": "Saved00001", "methodsWorkAsReady": true } + }, + { + "name": "402 при сохранённом ответе — сохранённый не используется", + "saved": { "did": "Saved00001", "seance": "OldSeance1" }, + "response": { "status": 402, "body": null }, + "expect": { "state": "refused" } + } + ] +} diff --git a/personalization-sdk/src/test/resources/contract/0/tracking.json b/personalization-sdk/src/test/resources/contract/0/tracking.json new file mode 100644 index 00000000..c81c3f2f --- /dev/null +++ b/personalization-sdk/src/test/resources/contract/0/tracking.json @@ -0,0 +1,85 @@ +{ + "contract": "0", + "cases": [ + { + "name": "productView", + "call": { "namespace": "tracking", "method": "productView", "args": { "itemId": "37" } }, + "request": { "method": "POST", "path": "push", "body": { "event": "view", "items": [ { "id": "37" } ] } } + }, + { + "name": "productView с источником", + "call": { "namespace": "tracking", "method": "productView", + "args": { "itemId": "37", "source": { "type": "instantSearch", "code": "q-1" } } }, + "request": { "method": "POST", "path": "push", + "body": { "event": "view", "items": [ { "id": "37" } ], + "recommended_by": "instant_search", "recommended_code": "q-1" } } + }, + { + "name": "productView с пустым itemId", + "call": { "namespace": "tracking", "method": "productView", "args": { "itemId": "" } }, + "error": { "type": "invalidArgument" } + }, + { + "name": "categoryView", + "call": { "namespace": "tracking", "method": "categoryView", "args": { "categoryId": "146" } }, + "request": { "method": "POST", "path": "push", "body": { "event": "category", "category_id": "146" } } + }, + { + "name": "addToCart с ценой и размером", + "call": { "namespace": "tracking", "method": "addToCart", + "args": { "item": { "id": "37", "quantity": 2, "price": 999.5, "fashionSize": "M" } } }, + "request": { "method": "POST", "path": "push", + "body": { "event": "cart", + "items": [ { "id": "37", "amount": 2, "price": 999.5, "fashion_size": "M" } ] } } + }, + { + "name": "syncCart с пустой корзиной", + "call": { "namespace": "tracking", "method": "syncCart", "args": { "items": [] } }, + "request": { "method": "POST", "path": "push", "body": { "event": "cart", "full_cart": true, "items": [] } } + }, + { + "name": "syncFavorites", + "call": { "namespace": "tracking", "method": "syncFavorites", "args": { "itemIds": [ "37", "42" ] } }, + "request": { "method": "POST", "path": "push", + "body": { "event": "wish", "full_wish": true, "items": [ { "id": "37" }, { "id": "42" } ] } } + }, + { + "name": "purchase", + "call": { "namespace": "tracking", "method": "purchase", + "args": { "request": { "orderId": "A-1001", "orderPrice": 2599.0, "isTaxFree": true, + "items": [ { "id": "37", "amount": 2, "price": 999.0 }, + { "id": "42", "amount": 1, "price": 601.0 } ] } } }, + "request": { "method": "POST", "path": "push", + "body": { "event": "purchase", "order_id": "A-1001", "order_price": 2599.0, "tax_free": true, + "items": [ { "id": "37", "amount": 2, "price": 999.0 }, + { "id": "42", "amount": 1, "price": 601.0 } ] } } + }, + { + "name": "purchase без товаров", + "call": { "namespace": "tracking", "method": "purchase", + "args": { "request": { "orderId": "A-1001", "orderPrice": 0, "items": [] } } }, + "error": { "type": "invalidArgument" } + }, + { + "name": "storyView", + "call": { "namespace": "tracking", "method": "storyView", + "args": { "storyId": "12", "slideId": "34", "code": "main_stories" } }, + "request": { "method": "POST", "path": "track/stories", + "body": { "event": "view", "story_id": 12, "slide_id": "34", "code": "main_stories" } } + }, + { + "name": "custom с полями", + "call": { "namespace": "tracking", "method": "custom", + "args": { "event": "added_to_compare", "category": "catalog", "customFields": { "sku": "37" } } }, + "request": { "method": "POST", "path": "push/custom", + "body": { "event": "added_to_compare", "category": "catalog", "sku": "37", + "payload": { "sku": "37" } } } + }, + { + "name": "custom со служебным ключом в customFields", + "call": { "namespace": "tracking", "method": "custom", + "args": { "event": "e", "customFields": { "did": "x" } } }, + "error": { "type": "invalidArgument" } + } + ] +} From 435b3a0bb3578378f8d7538cdca8633998a3ee0d Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 21:28:27 +0400 Subject: [PATCH 004/109] build: check fails on a new `!!` in the SDK's Kotlin sources; the views that still have them are listed in a baseline that may only shrink, and the registry, purchase tracking, search params and popups v3 handle the null instead --- personalization-sdk/build.gradle | 43 +++++++++++++++++++ personalization-sdk/force-unwrap-baseline.txt | 14 ++++++ .../kotlin/com/personalization/SdkRegistry.kt | 3 +- .../com/personalization/SearchParams.kt | 8 +++- .../trackEvent/impl/TrackEventManagerImpl.kt | 9 +--- .../personalization/popups/PopupsV3Engine.kt | 5 ++- 6 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 personalization-sdk/force-unwrap-baseline.txt diff --git a/personalization-sdk/build.gradle b/personalization-sdk/build.gradle index 3358f7d8..12bfdfde 100644 --- a/personalization-sdk/build.gradle +++ b/personalization-sdk/build.gradle @@ -255,3 +255,46 @@ tasks.register('publishToMavenCentral') { println "Publishing to Maven Central only" } } + +// No new `!!` in the SDK: a force unwrap on a path the app reaches crashes the app instead of +// failing one call. force-unwrap-baseline.txt lists the views that still have them; it may only +// shrink. Plain text scan, so it needs no compiler and runs as part of `check` in CI. +tasks.register('checkForceUnwraps') { + group = 'verification' + description = 'Fails when a Kotlin source has more `!!` than force-unwrap-baseline.txt allows.' + def sourceRoot = file('src/main/kotlin') + def baseline = file('force-unwrap-baseline.txt') + def sources = fileTree(sourceRoot) { include '**/*.kt' } + inputs.files(sources) + inputs.file(baseline) + outputs.upToDateWhen { false } + doLast { + def allowed = [:] + baseline.eachLine { line -> + def entry = line.trim() + if (entry && !entry.startsWith('#')) { + def parts = entry.split(/\s+/) + allowed[parts[0]] = parts[1] as int + } + } + def violations = [] + sources.each { File source -> + def path = sourceRoot.toPath().relativize(source.toPath()).toString().replace(File.separator, '/') + int count = 0 + source.eachLine { line -> + def code = line.trim() + if (code.startsWith('//') || code.startsWith('*') || code.startsWith('/*')) return + int comment = code.indexOf('//') + if (comment >= 0) code = code.substring(0, comment) + count += code.findAll(/!!/).size() + } + int limit = allowed.getOrDefault(path, 0) as int + if (count > limit) violations << "${path}: ${count} `!!`, ${limit} allowed" + } + if (!violations.isEmpty()) { + throw new GradleException("Force unwraps (`!!`) in the SDK — handle the null instead:\n" + violations.join('\n')) + } + } +} + +tasks.named('check') { dependsOn 'checkForceUnwraps' } diff --git a/personalization-sdk/force-unwrap-baseline.txt b/personalization-sdk/force-unwrap-baseline.txt new file mode 100644 index 00000000..aa1aab62 --- /dev/null +++ b/personalization-sdk/force-unwrap-baseline.txt @@ -0,0 +1,14 @@ +# Kotlin sources that may still have `!!`, and how many. The list may only shrink: +# a force unwrap on a path the app reaches crashes the app instead of failing one call. +# Checked by `./gradlew checkForceUnwraps`, which `check` runs. +com/personalization/inAppNotification/view/component/dialog/alert/AlertDialog.kt 1 +com/personalization/inAppNotification/view/component/dialog/bottom/BottomDialog.kt 1 +com/personalization/inAppNotification/view/component/dialog/fullScreen/FullScreenDialog.kt 1 +com/personalization/inAppNotification/view/component/dialog/top/TopDialog.kt 1 +com/personalization/stories/Player.kt 1 +com/personalization/stories/viewAdapters/StoriesAdapter.kt 5 +com/personalization/stories/views/StoriesView.kt 5 +com/personalization/stories/views/StoryDialog.kt 1 +com/personalization/stories/views/StoryView.kt 5 +com/personalization/stories/views/storyItem/StoryItemView.kt 3 +com/personalization/ui/components/PersonalizationProductsGrid.kt 1 diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SdkRegistry.kt b/personalization-sdk/src/main/kotlin/com/personalization/SdkRegistry.kt index 795f2182..dfa0c06c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SdkRegistry.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SdkRegistry.kt @@ -101,7 +101,8 @@ internal object SdkRegistry { onReady(alreadyLive) Cancellable.NOOP } else { - val added = awaiter!! + // Set under the lock whenever nothing was live; the fallback is unreachable. + val added = awaiter ?: return Cancellable.NOOP Cancellable { synchronized(lock) { awaiting.remove(added) } } } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SearchParams.kt b/personalization-sdk/src/main/kotlin/com/personalization/SearchParams.kt index 200f8039..70ae7b19 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SearchParams.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SearchParams.kt @@ -1,5 +1,7 @@ package com.personalization +import com.personalization.logs.SdkLogs +import com.personalization.logs.LogCodes import org.json.JSONArray import org.json.JSONException import org.json.JSONObject @@ -68,6 +70,10 @@ class SearchParams : AbstractParams() { } fun put(param: Parameter?, value: SearchFilters): SearchParams { - return put(param!!, value.toString()) + if (param == null) { + SdkLogs.log(LogLevel.WARNING, LogCodes.INVALID_ARGUMENT, "SearchParams.put: no parameter given, the filters are left out") + return this + } + return put(param, value.toString()) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt index 3df63db8..33ea2894 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt @@ -74,15 +74,10 @@ internal class TrackEventManagerImpl @Inject constructor( } override fun trackPurchase(request: PurchaseTrackingRequest, listener: OnApiCallbackListener?) { - val jsonResult = PurchaseTrackingJsonBuilder.buildOrError(request) - if (jsonResult.isFailure) { - listener?.onError( - PurchaseTrackingJsonBuilder.CLIENT_VALIDATION_ERROR_CODE, - jsonResult.exceptionOrNull()?.message, - ) + val body = PurchaseTrackingJsonBuilder.buildOrError(request).getOrElse { error -> + listener?.onError(PurchaseTrackingJsonBuilder.CLIENT_VALIDATION_ERROR_CODE, error.message) return } - val body = jsonResult.getOrNull()!! popups()?.onPurchase() if (request.recommendedBy == null) { val lastRecommendedBy = getRecommendedByUseCase() diff --git a/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Engine.kt b/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Engine.kt index 226ce6d6..cc8ec141 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Engine.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Engine.kt @@ -416,8 +416,9 @@ internal class PopupsV3Engine( return } showing += id - if (popup.needsPromoCode) { - promoCode(popup.promoCodeListId!!) { code -> + val promoCodeListId = popup.promoCodeListId.takeIf { popup.needsPromoCode } + if (promoCodeListId != null) { + promoCode(promoCodeListId) { code -> if (code == null) { showing -= id debugLog("popup $id skipped: no promo code") From 647ae03b0491c3ec475cc5d0c6bcae2f88c10ebd Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 21:52:03 +0400 Subject: [PATCH 005/109] fix(sdk): the log carries no exception text, URLs or server messages: an exception is written to logcat and the log handler as its class name only, popups and stories log no links or image URLs, and a failed popups v3 list is logged by its HTTP code, not the answer's message --- .../com/personalization/SearchParams.kt | 2 +- .../api/params/SearchParams.kt | 2 +- .../com/personalization/errors/base.error.kt | 2 +- .../personalization/errors/firebase.error.kt | 2 +- .../helpers/NotificationClickProcessor.kt | 2 +- .../com/personalization/logs/SdkLogs.kt | 24 +++++++++++-------- .../com/personalization/popups/PopupsV3Api.kt | 16 +++++-------- .../personalization/popups/PopupsV3Engine.kt | 15 +++++++----- .../personalization/push/PushTokenManager.kt | 2 +- .../ui/popups/PopupV3Elements.kt | 2 +- .../ui/popups/PopupV3Presenter.kt | 10 ++++---- .../widgets/PersonalizationStoriesViewer.kt | 6 ++--- .../personalization/ui/widgets/StoryPoll.kt | 2 +- .../personalization/popups/PopupsV3ApiTest.kt | 4 ++-- .../popups/PopupsV3EngineTest.kt | 2 +- 15 files changed, 48 insertions(+), 45 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SearchParams.kt b/personalization-sdk/src/main/kotlin/com/personalization/SearchParams.kt index 70ae7b19..a76a0467 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SearchParams.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SearchParams.kt @@ -62,7 +62,7 @@ class SearchParams : AbstractParams() { try { json.put(key, JSONArray(value)) } catch (e: JSONException) { - SDK.warn(e.message) + SDK.warn("SearchParams: a filter is left out: ${e.javaClass.simpleName}") } } return json.toString() diff --git a/personalization-sdk/src/main/kotlin/com/personalization/api/params/SearchParams.kt b/personalization-sdk/src/main/kotlin/com/personalization/api/params/SearchParams.kt index 44defc50..6e819bbc 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/api/params/SearchParams.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/api/params/SearchParams.kt @@ -43,7 +43,7 @@ class SearchParams : AbstractParams() { try { json.put(key, JSONArray(value)) } catch (e: JSONException) { - SDK.warn(e.message) + SDK.warn("SearchParams: a filter is left out: ${e.javaClass.simpleName}") } } return json.toString() diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/base.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/base.error.kt index 5354bdd7..31b482de 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/base.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/base.error.kt @@ -11,6 +11,6 @@ class BaseInfoError( ) { fun logError() { - SdkLogs.log(LogLevel.ERROR, LogCodes.SDK_LOG, "$message: ${exception?.message ?: "Unknown error"}") + SdkLogs.log(LogLevel.ERROR, LogCodes.SDK_LOG, "$message: ${exception?.javaClass?.simpleName ?: "unknown error"}") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/firebase.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/firebase.error.kt index 57841b9c..d6185916 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/firebase.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/firebase.error.kt @@ -10,6 +10,6 @@ class FirebaseError( ) { fun logError() { - SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_TOKEN_FAILED, "Failed to retrieve Firebase token: ${exception?.message ?: "Unknown error"}") + SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_TOKEN_FAILED, "Failed to retrieve Firebase token: ${exception?.javaClass?.simpleName ?: "unknown error"}") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt index 6ea82360..f22058ca 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt @@ -37,7 +37,7 @@ class NotificationClickProcessor @Inject constructor( ParsingJsonError( tag = this@NotificationClickProcessor.javaClass.name, functionName = "processClick", - message = e.message.orEmpty() + message = e.javaClass.simpleName ).logError() } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt index 9547af26..4bff8ec8 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt @@ -14,7 +14,8 @@ import java.util.concurrent.Executors * * Each shop has its own level ([register]); a message about a shop passes that level. A message that * is not about one shop passes the most detailed level among the shops, or [LogLevel.WARNING] while - * there is none. Callers pass only data without personal data: method and path, codes, counts. + * there is none. Callers pass only data without personal data: method and path, codes, counts. An + * exception is written as its class name only: its message often carries an answer's values or a URL. */ internal object SdkLogs { @@ -60,8 +61,8 @@ internal object SdkLogs { ) { if (!levelFor(shopId).allows(level)) return val tag = shopId?.let { shops[it]?.tag } ?: SDK.TAG - val fullContext = if (throwable == null) context else context + ("exception" to throwable.javaClass.simpleName) - writeToLogcat(level, tag, code, message, fullContext, throwable) + val fullContext = if (throwable == null) context else context + ("exception" to exceptionName(throwable)) + writeToLogcat(level, tag, code, message, fullContext) val handler = handler ?: return val entry = SdkLogMessage(level, code, message, shopId, fullContext) try { @@ -69,31 +70,34 @@ internal object SdkLogs { try { handler.onLog(entry) } catch (failure: Throwable) { - Log.w(tag, "[log_handler_failed] the app's log handler threw", failure) + Log.w(tag, "[log_handler_failed] the app's log handler threw ${exceptionName(failure)}") } } } catch (rejected: Throwable) { - Log.w(tag, "[log_handler_failed] the log handler could not be scheduled", rejected) + Log.w(tag, "[log_handler_failed] the log handler could not be scheduled: ${exceptionName(rejected)}") } } + /** The class of [throwable], never its message or stack trace: they may carry data. */ + fun exceptionName(throwable: Throwable): String = + throwable.javaClass.simpleName.ifEmpty { throwable.javaClass.name } + private fun writeToLogcat( level: LogLevel, tag: String, code: String, message: String, context: Map, - throwable: Throwable?, ) { val line = buildString { append('[').append(code).append("] ").append(message) if (context.isNotEmpty()) append(' ').append(context) } when (level) { - LogLevel.DEBUG -> Log.d(tag, line, throwable) - LogLevel.INFO -> Log.i(tag, line, throwable) - LogLevel.WARNING -> Log.w(tag, line, throwable) - LogLevel.ERROR -> Log.e(tag, line, throwable) + LogLevel.DEBUG -> Log.d(tag, line) + LogLevel.INFO -> Log.i(tag, line) + LogLevel.WARNING -> Log.w(tag, line) + LogLevel.ERROR -> Log.e(tag, line) LogLevel.NONE -> Unit } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Api.kt b/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Api.kt index 91f47b18..6f19fe3e 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Api.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Api.kt @@ -37,13 +37,14 @@ internal class PopupsV3Api( PopupsV3ListResult.Success(PopupsV3List.parse(response.body)) } catch (throwable: Throwable) { // Ответ не по контракту — как у web: пустой список на срок по умолчанию. - PopupsV3ListResult.ClientError(response.code, throwable.message) + PopupsV3ListResult.ClientError(response.code) } - in 400..499 -> PopupsV3ListResult.ClientError(response.code, message(response.body)) - else -> PopupsV3ListResult.Failure("${response.code} ${message(response.body)}") + // Only the code: the answer's message often repeats the request's data. + in 400..499 -> PopupsV3ListResult.ClientError(response.code) + else -> PopupsV3ListResult.Failure("HTTP ${response.code}") } } catch (throwable: Throwable) { - PopupsV3ListResult.Failure(throwable.message) + PopupsV3ListResult.Failure(throwable.javaClass.simpleName) } MainThread.run { callback(result) } } @@ -68,7 +69,7 @@ internal class PopupsV3Api( null } } catch (throwable: Throwable) { - SDK.debug("popups v3: popup $id request failed: ${throwable.message}") + SDK.debug("popups v3: popup $id request failed: ${throwable.javaClass.simpleName}") null } MainThread.run { callback(popup) } @@ -128,15 +129,10 @@ internal class PopupsV3Api( private fun did(): String = sdk.getDid().orEmpty() private fun sid(): String = runCatching { sdk.getSid() }.getOrNull().orEmpty() - private fun message(body: String): String? = - runCatching { JSONObject(body).optString("message") }.getOrNull()?.takeIf { it.isNotEmpty() } - ?: body.take(MAX_MESSAGE).takeIf { it.isNotEmpty() } - companion object { private const val PATH = "/api/v3/storefront/popups" const val PROMO_METHOD = "promo_codes/fetch" const val EVENTS_PAUSE_MS = 60_000L - private const val MAX_MESSAGE = 200 /** Пауза перед повтором события; в тестах — 0. */ @Volatile diff --git a/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Engine.kt b/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Engine.kt index cc8ec141..dfe33261 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Engine.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Engine.kt @@ -4,11 +4,14 @@ package com.personalization.popups internal sealed class PopupsV3ListResult { class Success(val list: PopupsV3List) : PopupsV3ListResult() - /** 4xx: кешируется пустым списком на [PopupsV3List.DEFAULT_TTL]. */ - class ClientError(val code: Int, val message: String?) : PopupsV3ListResult() + /** 4xx или `2xx` не по контракту: кешируется пустым списком на [PopupsV3List.DEFAULT_TTL]. */ + class ClientError(val code: Int) : PopupsV3ListResult() - /** Сеть, 5xx, неразборчивый ответ: не кешируется, повтор при следующем поводе. */ - class Failure(val message: String?) : PopupsV3ListResult() + /** + * Сеть, 5xx: не кешируется, повтор при следующем поводе. [reason] — код или класс исключения, + * без текста ответа. + */ + class Failure(val reason: String) : PopupsV3ListResult() } /** Сеть попапов v3. Ответы — на главном потоке. */ @@ -311,11 +314,11 @@ internal class PopupsV3Engine( setList(list.popups) } is PopupsV3ListResult.ClientError -> { - debugLog("list request failed (${result.code}): ${result.message}") + debugLog("list request failed: ${result.code}") cache = Cache(currentSid, test, clock() + PopupsV3List.DEFAULT_TTL * 1000, emptyList()) setList(emptyList()) } - is PopupsV3ListResult.Failure -> debugLog("list request failed: ${result.message}") + is PopupsV3ListResult.Failure -> debugLog("list request failed: ${result.reason}") } if (reloadAfterLoad) { reloadAfterLoad = false diff --git a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt index 44b4387e..a0adeb23 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt @@ -92,7 +92,7 @@ class PushTokenManager @Inject constructor( result.exceptionOrNull()?.let { error -> BaseInfoError( tag = TAG, - message = "${source.provider.id} token fetch error: ${error.message}" + message = "${source.provider.id} token fetch error: ${error.javaClass.simpleName}" ).logError() } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Elements.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Elements.kt index 8eed6faf..d9aa1e8c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Elements.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Elements.kt @@ -872,7 +872,7 @@ internal class PopupV3ImageView(context: Context, private val spec: PopupV3Specs override fun onLoadCleared(placeholder: Drawable?) = setImageDrawable(placeholder) override fun onLoadFailed(errorDrawable: Drawable?) { - SDK.debug("popups v3: image $url failed to load") + SDK.debug("popups v3: an image failed to load") } }) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Presenter.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Presenter.kt index 56abe6c0..c8eb607f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Presenter.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Presenter.kt @@ -219,7 +219,7 @@ internal class PopupV3Session( val view = try { PopupV3RootView(host, popup, design, variables, state, actions, sdk, blocking, onOutside = { close(sendEvent = true) }) } catch (throwable: Throwable) { - fail("render failed: ${throwable.message}") + fail("render failed: ${throwable.javaClass.simpleName}") return } try { @@ -232,7 +232,7 @@ internal class PopupV3Session( } catch (throwable: Throwable) { dialog = null (view.parent as? ViewGroup)?.removeView(view) - if (!shown) fail("no window: ${throwable.message}") else SDK.debug("popups v3: popup ${popup.id} could not come back: ${throwable.message}") + if (!shown) fail("no window: ${throwable.javaClass.simpleName}") else SDK.debug("popups v3: popup ${popup.id} could not come back: ${throwable.javaClass.simpleName}") return } activity = WeakReference(host) @@ -725,7 +725,7 @@ internal class PopupV3ActionHandler( val handled = try { handler(link) } catch (throwable: Throwable) { - SDK.error("popups v3: link handler failed on $link", throwable) + SDK.error("popups v3: the link handler threw", throwable) false } if (handled) { @@ -739,9 +739,9 @@ internal class PopupV3ActionHandler( try { host.startActivity(intent) } catch (exception: ActivityNotFoundException) { - SDK.error("popups v3: no app can open $link") + SDK.error("popups v3: no app can open the link") } catch (exception: RuntimeException) { - SDK.error("popups v3: cannot open $link", exception) + SDK.error("popups v3: the link could not be opened", exception) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStoriesViewer.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStoriesViewer.kt index 23e44bd4..60748053 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStoriesViewer.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStoriesViewer.kt @@ -628,7 +628,7 @@ class PersonalizationStoriesViewer( try { (activity ?: hostContext).startActivity(intent) } catch (_: android.content.ActivityNotFoundException) { - SDK.error("PersonalizationStoriesViewer: no app can open $url") + SDK.error("PersonalizationStoriesViewer: no app can open the link") } } @@ -643,7 +643,7 @@ class PersonalizationStoriesViewer( track("share") val shared = shareUrlTransform?.let { transform -> runCatching { transform(url) }.getOrElse { - SDK.error("PersonalizationStoriesViewer: shareUrlTransform failed, sharing $url", it) + SDK.error("PersonalizationStoriesViewer: shareUrlTransform threw, the link is shared as it is", it) url } } ?: url @@ -656,7 +656,7 @@ class PersonalizationStoriesViewer( try { (activity ?: hostContext).startActivity(chooser) } catch (throwable: RuntimeException) { - SDK.error("PersonalizationStoriesViewer: cannot share $url", throwable) + SDK.error("PersonalizationStoriesViewer: the link could not be shared", throwable) endShare() return } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoryPoll.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoryPoll.kt index 9ae089e4..a8bf0335 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoryPoll.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoryPoll.kt @@ -87,7 +87,7 @@ internal class StoryPoll( fun parse(json: JSONObject): StoryPoll? = try { parseOrThrow(json) } catch (throwable: Throwable) { - SDK.debug("stories poll: broken element: ${throwable.message}") + SDK.debug("stories poll: broken element: ${throwable.javaClass.simpleName}") null } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/popups/PopupsV3ApiTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/popups/PopupsV3ApiTest.kt index b6e29a58..7d6c9e76 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/popups/PopupsV3ApiTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/popups/PopupsV3ApiTest.kt @@ -93,13 +93,13 @@ class PopupsV3ApiTest { idle() val error = result as PopupsV3ListResult.ClientError assertEquals(code, error.code) - assertEquals(errors.getJSONObject(name).getString("message"), error.message) } replies += StoriesApiV3.Response(503, "down") var result: PopupsV3ListResult? = null api.list(false, false) { result = it } idle() - assertTrue(result is PopupsV3ListResult.Failure) + // The code only: the answer's text stays out of the log. + assertEquals("HTTP 503", (result as PopupsV3ListResult.Failure).reason) } @Test diff --git a/personalization-sdk/src/test/kotlin/com/personalization/popups/PopupsV3EngineTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/popups/PopupsV3EngineTest.kt index 07819631..f36029a1 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/popups/PopupsV3EngineTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/popups/PopupsV3EngineTest.kt @@ -470,7 +470,7 @@ class PopupsV3EngineTest { assertEquals(1, env.transport.listCalls) val failing = Env(emptyList()) - failing.transport.listResult = { PopupsV3ListResult.ClientError(402, "Payment required") } + failing.transport.listResult = { PopupsV3ListResult.ClientError(402) } failing.engine.start() failing.engine.start() assertEquals(1, failing.transport.listCalls) From b1a057c1967666439446dc29ab8f1fc0d0354d88 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 21:56:03 +0400 Subject: [PATCH 006/109] fix(sdk): a shop's log level applies to its instance's lines: instance methods, stories, popups, stories v3 and the push token log under their shop; PopupsV3.debug writes the engine's lines whatever the level; release() keeps the shop's level while another of its instances is live; the network and init helpers check the level before reading the shop --- .../com/personalization/RegisterManager.kt | 2 + .../main/kotlin/com/personalization/SDK.kt | 43 ++++++++---- .../impl/InAppNotificationManagerImpl.kt | 28 ++++++-- .../com/personalization/logs/SdkLogs.kt | 43 +++++++++--- .../com/personalization/popups/PopupsV3.kt | 39 +++++++++-- .../com/personalization/popups/PopupsV3Api.kt | 18 +++-- .../personalization/push/PushTokenManager.kt | 8 ++- .../network/NetworkRepositoryImpl.kt | 6 +- .../personalization/stories/StoriesManager.kt | 14 ++-- .../stories/viewAdapters/ProductsAdapter.kt | 5 +- .../stories/views/StoryView.kt | 5 +- .../stories/views/storyItem/StoryItemView.kt | 5 +- .../ui/popups/PopupV3Presenter.kt | 14 ++-- .../ui/widgets/StoriesApiV3.kt | 26 ++++--- .../ui/widgets/StoriesPollsApi.kt | 9 ++- .../com/personalization/logs/SdkLogsTest.kt | 67 +++++++++++++++++++ 16 files changed, 251 insertions(+), 81 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt index a3bbdd76..c87d4d3d 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt @@ -195,6 +195,7 @@ class RegisterManager @Inject constructor( } } + /** The level is checked first, so a line no shop writes reads no preferences. */ private fun log( level: LogLevel, code: String, @@ -202,6 +203,7 @@ class RegisterManager @Inject constructor( context: Map = emptyMap(), throwable: Throwable? = null, ) { + if (!SdkLogs.isLoggable(level)) return val shopId = runCatching { getUserSettingsValueUseCase.getShopId() }.getOrNull()?.takeIf { it.isNotEmpty() } SdkLogs.log(level, code, message, shopId, context, throwable) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 09b4a706..5b1d35fc 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -167,8 +167,10 @@ open class SDK { /** The level of this instance's log; [Rees46Config.logLevel] sets it for instances made through [Rees46]. */ internal var logLevel: LogLevel = LogLevel.WARNING - /** The shop this instance logs for, once it is initialized. */ - private var logShopId: String? = null + /** The shop this instance logs for, once it is initialized: its lines pass that shop's level. */ + @Volatile + internal var logShopId: String? = null + private set /** `popups_v3` of the last `/init` answer; null while `/init` has not answered. */ @Volatile @@ -310,8 +312,10 @@ open class SDK { sdkComponent.inject(sdk = this) this.context = context TAG = tag - SdkLogs.register(shopId = shopId, level = logLevel, tag = tag) + SdkLogs.register(shopId = shopId, level = logLevel, tag = tag, owner = this) logShopId = shopId + pushTokenManager.logShopId = shopId + storiesManager.logShopId = shopId // Before the init request goes out: its response may already carry a popup. ForegroundActivity.install(context) @@ -468,7 +472,7 @@ open class SDK { event = event, code = code, storyId = storyId, slideId = slideId ) } else { - SdkLogs.log(LogLevel.WARNING, LogCodes.STORIES, "storiesManager is not initialized") + log(LogLevel.WARNING, LogCodes.STORIES, "storiesManager is not initialized") } } @@ -643,10 +647,19 @@ open class SDK { */ fun release() { SdkRegistry.unregister(this) - // Another instance of the same shop (a repeated initialize) keeps the shop's log level. - logShopId?.takeIf { SdkRegistry.byShopId(it) == null }?.let { SdkLogs.unregister(it) } + // Only this instance's level goes: another live instance of the shop keeps logging at its own. + logShopId?.let { SdkLogs.unregister(it, owner = this) } } + /** Writes to the SDK's log under this instance's shop, so the shop's [Rees46Config.logLevel] applies. */ + internal fun log( + level: LogLevel, + code: String, + message: String, + context: Map = emptyMap(), + throwable: Throwable? = null, + ) = SdkLogs.log(level, code, message, logShopId, context, throwable) + /** * Quick search * @@ -691,7 +704,7 @@ open class SDK { .put(InternalParameter.SEARCH_QUERY, query) getAsync(SEARCH_FIELD, params.build(), listener) } else { - warn("Search not initialized") + log(LogLevel.WARNING, LogCodes.SDK_LOG, "Search not initialized") } } @@ -719,7 +732,7 @@ open class SDK { listener.onSuccess(search?.blank) } } else { - warn("Search not initialized") + log(LogLevel.WARNING, LogCodes.SDK_LOG, "Search not initialized") } } @@ -963,7 +976,7 @@ open class SDK { } sendAsync(SUBSCRIPTION_UNSUBSCRIBE_PRICE, params, listener) } catch (e: JSONException) { - SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) + log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } } @@ -1020,7 +1033,7 @@ open class SDK { } sendAsync(SUBSCRIPTION_UNSUBSCRIBE, params, listener) } catch (e: JSONException) { - SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) + log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } } @@ -1097,7 +1110,7 @@ open class SDK { sendAsync(SUBSCRIPTION_MANAGE, params, listener) popupsV3?.onContextChange(SUBSCRIPTION_MANAGE, params.toString()) } catch (e: JSONException) { - SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) + log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } } @@ -1162,7 +1175,7 @@ open class SDK { sendAsync("$SEGMENTS_FIELD/$method", params, listener) popupsV3?.onContextChange(SEGMENTS_FIELD, "$method $params") } catch (e: JSONException) { - SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) + log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } } @@ -1171,7 +1184,9 @@ open class SDK { */ fun notificationReceived(data: Map) { if (!isSdkInitialized) { - warn( + log( + LogLevel.WARNING, + LogCodes.SDK_LOG, "notificationReceived() called before SDK.initialize(); ignoring. " + "Initialize SDK.instance in Application.onCreate so push delivery works in a cold process." ) @@ -1191,7 +1206,7 @@ open class SDK { sendNetworkMethodUseCase.postAsync(TRACK_RECEIVED, params) } } catch (e: JSONException) { - SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) + log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "Request parameters could not be built", throwable = e) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt index 4d02b191..33a848e8 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt @@ -96,11 +96,11 @@ class InAppNotificationManagerImpl @Inject constructor( val activity = try { listener(popupDto) } catch (exception: Exception) { - SdkLogs.log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "Popup was not shown: the presentation listener threw", context = mapOf("popup" to popupDto.id), throwable = exception) + log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "Popup was not shown: the presentation listener threw", context = mapOf("popup" to popupDto.id), throwable = exception) return } if (activity == null) { - SdkLogs.log(LogLevel.DEBUG, LogCodes.POPUP_NOT_SHOWN, "Popup kept back by the presentation listener", context = mapOf("popup" to popupDto.id)) + log(LogLevel.DEBUG, LogCodes.POPUP_NOT_SHOWN, "Popup kept back by the presentation listener", context = mapOf("popup" to popupDto.id)) return } activity.supportFragmentManager.takeIf { it.canShowDialog() } @@ -109,18 +109,18 @@ class InAppNotificationManagerImpl @Inject constructor( presentation.autoPresentation -> defaultTarget() else -> { - SdkLogs.log(LogLevel.DEBUG, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown: automatic presentation is off", context = mapOf("popup" to popupDto.id)) + log(LogLevel.DEBUG, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown: automatic presentation is off", context = mapOf("popup" to popupDto.id)) return } } if (target == null) { - SdkLogs.log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown: no activity that can take a dialog", context = mapOf("popup" to popupDto.id)) + log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown: no activity that can take a dialog", context = mapOf("popup" to popupDto.id)) return } try { showDialog(target, extractDialogData(popupDto)) } catch (exception: Exception) { - SdkLogs.log(LogLevel.ERROR, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown", context = mapOf("popup" to popupDto.id), throwable = exception) + log(LogLevel.ERROR, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown", context = mapOf("popup" to popupDto.id), throwable = exception) return } @@ -262,7 +262,7 @@ class InAppNotificationManagerImpl @Inject constructor( target: FragmentManager? ) { if (target == null) { - SdkLogs.log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Dialog was not shown: no activity on screen to show it in") + log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Dialog was not shown: no activity on screen to show it in") return } @@ -369,6 +369,22 @@ class InAppNotificationManagerImpl @Inject constructor( } } + /** + * Writes to the log under the instance's shop. The level is checked before the shop is read from + * the preferences, so a line below every shop's level costs nothing. + */ + private fun log( + level: LogLevel, + code: String, + message: String, + context: Map = emptyMap(), + throwable: Throwable? = null, + ) { + if (!SdkLogs.isLoggable(level)) return + val shopId = runCatching { getUserSettingsValueUseCase.getShopId() }.getOrNull()?.takeIf { it.isNotEmpty() } + SdkLogs.log(level, code, message, shopId, context, throwable) + } + companion object { private const val TAG = "InAppNotificationManagerImpl" private const val FUNC_OPENING_BROWSER = "openUrlInBrowser" diff --git a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt index 4bff8ec8..8eebf03c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt @@ -19,9 +19,11 @@ import java.util.concurrent.Executors */ internal object SdkLogs { - private data class Shop(val level: LogLevel, val tag: String) + /** One live instance of a shop and the level it asked for. */ + private class Registration(val owner: Any, val level: LogLevel, val tag: String) - private val shops = ConcurrentHashMap() + /** The live instances of each shop, the latest last. */ + private val shops = ConcurrentHashMap>() @Volatile var handler: SdkLogHandler? = null @@ -32,12 +34,21 @@ internal object SdkLogs { Thread(runnable, "rees46-log").apply { isDaemon = true } } - fun register(shopId: String, level: LogLevel, tag: String) { - shops[shopId] = Shop(level, tag) + /** + * Gives [shopId] the level of [owner], an instance of that shop. While several instances of a shop + * are live, the shop logs at the most detailed of their levels, under the tag of the latest one. + */ + fun register(shopId: String, level: LogLevel, tag: String, owner: Any = shopId) { + shops.compute(shopId) { _, current -> + current.orEmpty().filterNot { it.owner == owner } + Registration(owner, level, tag) + } } - fun unregister(shopId: String) { - shops.remove(shopId) + /** [owner] is gone; the shop keeps a level while another of its instances is live. */ + fun unregister(shopId: String, owner: Any = shopId) { + shops.computeIfPresent(shopId) { _, current -> + current.filterNot { it.owner == owner }.takeIf { it.isNotEmpty() } + } } internal fun reset() { @@ -47,10 +58,20 @@ internal object SdkLogs { /** The level of [shopId], or for a message without a shop the most detailed level of any shop. */ fun levelFor(shopId: String?): LogLevel { - shopId?.let { shops[it] }?.let { return it.level } - return shops.values.minByOrNull { it.level.ordinal }?.level ?: LogLevel.WARNING + val registrations = shopId?.let { shops[it] } ?: shops.values.flatten() + return registrations.minByOrNull { it.level.ordinal }?.level ?: LogLevel.WARNING } + /** + * Whether a message at [level] would be written. Without [shopId] it answers for the most detailed + * shop, so a caller can skip building a message before it knows the shop. + */ + fun isLoggable(level: LogLevel, shopId: String? = null): Boolean = levelFor(shopId).allows(level) + + /** + * Writes one message. [force] writes it whatever the level, for logs the app asked for explicitly + * (`PopupsV3.debug`). + */ fun log( level: LogLevel, code: String, @@ -58,9 +79,11 @@ internal object SdkLogs { shopId: String? = null, context: Map = emptyMap(), throwable: Throwable? = null, + force: Boolean = false, ) { - if (!levelFor(shopId).allows(level)) return - val tag = shopId?.let { shops[it]?.tag } ?: SDK.TAG + if (level == LogLevel.NONE) return + if (!force && !isLoggable(level, shopId)) return + val tag = shopId?.let { shops[it]?.lastOrNull()?.tag } ?: SDK.TAG val fullContext = if (throwable == null) context else context + ("exception" to exceptionName(throwable)) writeToLogcat(level, tag, code, message, fullContext) val handler = handler ?: return diff --git a/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3.kt b/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3.kt index 2ba19342..fc581662 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3.kt @@ -4,7 +4,10 @@ import android.app.Activity import android.os.Handler import android.os.Looper import com.personalization.InternalPersonalizationV3Api +import com.personalization.LogLevel import com.personalization.SDK +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import com.personalization.features.inAppNotification.impl.ForegroundActivity import com.personalization.ui.components.PersonalizationProduct import com.personalization.ui.popups.PopupV3Presenter @@ -44,7 +47,10 @@ class PopupsV3 internal constructor(private val sdk: SDK) { */ var testMode: Boolean = false - /** `debug=1` в списке и каждое решение движка в лог SDK. */ + /** + * `debug=1` в списке и каждое решение движка в лог SDK. Это явная просьба приложения: строки + * движка пишутся при любом `Rees46Config.logLevel`. + */ var debug: Boolean = false /** Хост личного кабинета, на котором живёт API попапов. */ @@ -91,7 +97,7 @@ class PopupsV3 internal constructor(private val sdk: SDK) { sid = sid, testMode = { testMode }, debug = { debug }, - log = SDK::debug, + log = { message -> log(LogLevel.DEBUG, message) }, autoPresentation = { sdk.enableAutoPopupPresentation } ).also { ForegroundActivity.addListener(lifecycle) } } @@ -120,7 +126,7 @@ class PopupsV3 internal constructor(private val sdk: SDK) { fun show(id: Int) { MainThread.run { if (!enabled) { - SDK.debug("popups v3: show($id) ignored: popups are not enabled") + log(LogLevel.DEBUG, "popups v3: show($id) ignored: popups are not enabled") return@run } engine.showManual(id) @@ -131,7 +137,7 @@ class PopupsV3 internal constructor(private val sdk: SDK) { internal fun onInit(flag: Boolean) { MainThread.run { initFlag = flag - if (debug) SDK.debug("popups v3: /init popups_v3=$flag") + if (debug) log(LogLevel.DEBUG, "popups v3: /init popups_v3=$flag") maybeStart(refresh = true) } } @@ -150,12 +156,23 @@ class PopupsV3 internal constructor(private val sdk: SDK) { MainThread.run { if (enabled) engine.onContextChange(type, signature) } } + /** + * Строка попапов v3 в лог под магазином экземпляра. С [debug] она пишется при любом уровне лога: + * приложение само попросило подробности. + */ + internal fun log( + level: LogLevel, + message: String, + context: Map = emptyMap(), + throwable: Throwable? = null, + ) = popupsV3Log(sdk, level, message, context, throwable) + /** [refresh] — новый ответ `/init`: уже работающий движок обновляет список. */ private fun maybeStart(refresh: Boolean = false) { if (!enabled) return val flag = initFlag ?: return if (!flag && !ignoreInitFlag) { - if (debug) SDK.debug("popups v3: off — /init did not send popups_v3") + if (debug) log(LogLevel.DEBUG, "popups v3: off — /init did not send popups_v3") return } if (engine.isStarted && !refresh) return @@ -167,3 +184,15 @@ class PopupsV3 internal constructor(private val sdk: SDK) { const val DEFAULT_APP_HOST = "app.rees46.ru" } } + +/** + * A popups v3 line under the shop of [sdk]. With `PopupsV3.debug` on it is written whatever the log + * level: the app asked for the details. + */ +internal fun popupsV3Log( + sdk: SDK, + level: LogLevel, + message: String, + context: Map = emptyMap(), + throwable: Throwable? = null, +) = SdkLogs.log(level, LogCodes.POPUPS_V3, message, sdk.logShopId, context, throwable, force = sdk.popupsV3?.debug == true) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Api.kt b/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Api.kt index 6f19fe3e..694659fe 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Api.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/popups/PopupsV3Api.kt @@ -1,7 +1,5 @@ package com.personalization.popups -import com.personalization.logs.SdkLogs -import com.personalization.logs.LogCodes import com.personalization.LogLevel import android.content.Context import android.content.SharedPreferences @@ -31,7 +29,7 @@ internal class PopupsV3Api( StoriesApiV3.whenDid(sdk) { val url = listUrl(appHost(), shopId(), did(), sid(), testMode, debug) val result = try { - val response = StoriesApiV3.get(url) + val response = StoriesApiV3.get(url, sdk.logShopId) when (response.code) { in 200..299 -> try { PopupsV3ListResult.Success(PopupsV3List.parse(response.body)) @@ -53,23 +51,23 @@ internal class PopupsV3Api( override fun one(id: Int, testMode: Boolean, callback: (PopupV3?) -> Unit) { StoriesApiV3.whenDid(sdk) { val popup = try { - val response = StoriesApiV3.get(oneUrl(appHost(), id, shopId(), did(), sid(), testMode)) + val response = StoriesApiV3.get(oneUrl(appHost(), id, shopId(), did(), sid(), testMode), sdk.logShopId) if (response.code in 200..299) { when (val parsed = PopupsV3List.parseOne(response.body)) { is PopupV3.Parsed.Ok -> parsed.popup is PopupV3.Parsed.Skipped -> { - SDK.debug("popups v3: popup $id skipped: ${parsed.reason}") + popupsV3Log(sdk, LogLevel.DEBUG, "popups v3: popup $id skipped: ${parsed.reason}") null } null -> null } } else { // Without the answer's body: an error answer often repeats the request's data. - SdkLogs.log(LogLevel.DEBUG, LogCodes.POPUPS_V3, "Popup request failed", context = mapOf("popup" to id, "status" to response.code)) + popupsV3Log(sdk, LogLevel.DEBUG, "Popup request failed", mapOf("popup" to id, "status" to response.code)) null } } catch (throwable: Throwable) { - SDK.debug("popups v3: popup $id request failed: ${throwable.javaClass.simpleName}") + popupsV3Log(sdk, LogLevel.DEBUG, "popups v3: popup $id request failed", throwable = throwable) null } MainThread.run { callback(popup) } @@ -82,7 +80,7 @@ internal class PopupsV3Api( */ override fun event(id: Int, event: String, channel: String?) { if (System.currentTimeMillis() < eventsPausedUntil) { - SDK.debug("popups v3: event $event of popup $id dropped: too many requests") + popupsV3Log(sdk, LogLevel.DEBUG, "popups v3: event $event of popup $id dropped: too many requests") return } StoriesApiV3.whenDid(sdk) { @@ -90,7 +88,7 @@ internal class PopupsV3Api( val body = eventBody(shopId(), did(), sid(), event, channel) for (attempt in 0..1) { val code = try { - StoriesApiV3.post(url, body).code + StoriesApiV3.post(url, body, sdk.logShopId).code } catch (throwable: Throwable) { -1 } @@ -118,7 +116,7 @@ internal class PopupsV3Api( } override fun onError(code: Int, msg: String?) { - SdkLogs.log(LogLevel.DEBUG, LogCodes.POPUPS_V3, "Promo code request failed", context = mapOf("list" to listId, "status" to code)) + popupsV3Log(sdk, LogLevel.DEBUG, "Promo code request failed", mapOf("list" to listId, "status" to code)) MainThread.run { callback(null) } } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt index a0adeb23..c5e9fd69 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt @@ -37,6 +37,10 @@ class PushTokenManager @Inject constructor( private var autoSendPushToken: Boolean = false private var listener: OnPushTokenListener? = null + /** The shop of the instance, so the token's log lines pass that shop's level. */ + @Volatile + internal var logShopId: String? = null + /** * Tokens whose request is already on the wire, as `provider:token`. * @@ -163,7 +167,7 @@ class PushTokenManager @Inject constructor( // Released only after the token is persisted, so a delivery arriving in // between is still deduplicated — by the cache rather than by the claim. tokensInFlight.remove(inFlightKey(provider, token)) - SdkLogs.log(LogLevel.INFO, LogCodes.PUSH_TOKEN_SENT, "Push token sent", null, mapOf("provider" to provider.id)) + SdkLogs.log(LogLevel.INFO, LogCodes.PUSH_TOKEN_SENT, "Push token sent", logShopId, mapOf("provider" to provider.id)) external?.onSuccess(response) } @@ -175,7 +179,7 @@ class PushTokenManager @Inject constructor( LogLevel.ERROR, LogCodes.PUSH_TOKEN_FAILED, "Push token was not sent", - null, + logShopId, mapOf("provider" to provider.id, "status" to code) ) external?.onError(code, msg) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt index 1170f88a..a295d692 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt @@ -410,7 +410,10 @@ class NetworkRepositoryImpl @Inject constructor( buildString { reader.forEachLine { append(it) } } } - /** Writes to the log by method and path only: the query and the body carry the user's data. */ + /** + * Writes to the log by method and path only: the query and the body carry the user's data. The + * level is checked first, so a line no shop writes reads no preferences and builds nothing. + */ private fun log( level: LogLevel, code: String, @@ -419,6 +422,7 @@ class NetworkRepositoryImpl @Inject constructor( status: Int? = null, throwable: Throwable? = null, ) { + if (!SdkLogs.isLoggable(level)) return val context = buildMap { method?.let { put("method", it.type); put("path", it.method) } status?.let { put("status", it) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/StoriesManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/StoriesManager.kt index 93577022..8046f49e 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/StoriesManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/StoriesManager.kt @@ -31,6 +31,10 @@ class StoriesManager @Inject constructor( private var lastAttachedView: WeakReference? = null + /** The shop of the instance, so the stories' log lines pass that shop's level. */ + @Volatile + internal var logShopId: String? = null + /** The block the SDK last requested — the fallback for tracking calls that omit the code. */ @Volatile private var lastRequestedCode: String? = null @@ -50,7 +54,7 @@ class StoriesManager @Inject constructor( try { onLoaded(getStories(response)) } catch (e: JSONException) { - SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", logShopId, throwable = e) } } } @@ -60,7 +64,7 @@ class StoriesManager @Inject constructor( internal fun showStories(looper: Looper, code: String) { val view = lastAttachedView?.get() if (view == null) { - SdkLogs.log(LogLevel.WARNING, LogCodes.STORIES, "showStories: no StoriesView is attached to present from", context = mapOf("code" to code)) + SdkLogs.log(LogLevel.WARNING, LogCodes.STORIES, "showStories: no StoriesView is attached to present from", logShopId, mapOf("code" to code)) return } requestStories( @@ -77,7 +81,7 @@ class StoriesManager @Inject constructor( showStories(looper, view, stories) } catch (e: JSONException) { - SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", logShopId, throwable = e) } } } @@ -132,7 +136,7 @@ class StoriesManager @Inject constructor( val effectiveCode = code ?: lastRequestedCode if (effectiveCode == null) { val message = "trackStory($event): no stories code given and no block loaded yet" - SdkLogs.log(LogLevel.WARNING, LogCodes.STORIES, message) + SdkLogs.log(LogLevel.WARNING, LogCodes.STORIES, message, logShopId) listener?.onError(CLIENT_VALIDATION_ERROR_CODE, message) return } @@ -147,7 +151,7 @@ class StoriesManager @Inject constructor( sendNetworkMethodUseCase.postAsync(TRACK_STORIES_METHOD, params, listener) } catch (e: JSONException) { - SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", logShopId, throwable = e) listener?.onError(CLIENT_VALIDATION_ERROR_CODE, e.message) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/viewAdapters/ProductsAdapter.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/viewAdapters/ProductsAdapter.kt index bee642ac..07c69139 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/viewAdapters/ProductsAdapter.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/viewAdapters/ProductsAdapter.kt @@ -2,7 +2,6 @@ package com.personalization.stories.viewAdapters -import com.personalization.logs.SdkLogs import com.personalization.logs.LogCodes import com.personalization.LogLevel import android.annotation.SuppressLint @@ -106,7 +105,7 @@ internal class ProductsAdapter( private fun setupClickListener(product: Product) { itemView.setOnClickListener { - SdkLogs.log(LogLevel.DEBUG, LogCodes.STORIES, "Story product clicked") + sdk.log(LogLevel.DEBUG, LogCodes.STORIES, "Story product clicked") handleProductClick(product) } } @@ -128,7 +127,7 @@ internal class ProductsAdapter( onCloseStories.invoke() } } catch (e: ActivityNotFoundException) { - SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) + sdk.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) } } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoryView.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoryView.kt index ca1b85da..78f0b979 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoryView.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoryView.kt @@ -2,7 +2,6 @@ package com.personalization.stories.views -import com.personalization.logs.SdkLogs import com.personalization.logs.LogCodes import com.personalization.LogLevel import android.annotation.SuppressLint @@ -225,7 +224,7 @@ internal class StoryView( } override fun onPlayerError(error: PlaybackException) { - SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Story video failed", context = mapOf("story" to story?.id), throwable = error) + sdk.log(LogLevel.ERROR, LogCodes.STORIES, "Story video failed", context = mapOf("story" to story?.id), throwable = error) val holder = currentHolder holder?.storyItem?.let { storyItem -> storyItem.reloadLayout.visibility = VISIBLE @@ -277,7 +276,7 @@ internal class StoryView( try { storiesProgressView.resume() } catch (e: IndexOutOfBoundsException) { - SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Story video could not start", throwable = e) + sdk.log(LogLevel.ERROR, LogCodes.STORIES, "Story video could not start", throwable = e) } } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/storyItem/StoryItemView.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/storyItem/StoryItemView.kt index 4b942008..0680111f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/storyItem/StoryItemView.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/storyItem/StoryItemView.kt @@ -2,7 +2,6 @@ package com.personalization.stories.views.storyItem -import com.personalization.logs.SdkLogs import com.personalization.logs.LogCodes import com.personalization.LogLevel import android.annotation.SuppressLint @@ -342,10 +341,10 @@ class StoryItemView( } storyStateListener.onStoryStateChanged(StoryState.CLOSE) } catch (e: ActivityNotFoundException) { - SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) + sdk.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) Toast.makeText(context, "Unknown error", Toast.LENGTH_SHORT).show() } catch (e: NullPointerException) { - SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) + sdk.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", throwable = e) Toast.makeText(context, "Unknown error", Toast.LENGTH_SHORT).show() } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Presenter.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Presenter.kt index c8eb607f..86ebc44b 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Presenter.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Presenter.kt @@ -30,6 +30,8 @@ import androidx.core.view.WindowInsetsCompat import androidx.fragment.app.FragmentActivity import com.personalization.InternalPersonalizationV3Api import com.personalization.R +import com.personalization.LogLevel +import com.personalization.popups.popupsV3Log import com.personalization.SDK import com.personalization.api.OnApiCallbackListener import com.personalization.api.params.ProfileParams @@ -198,7 +200,7 @@ internal class PopupV3Session( ForegroundActivity.addListener(this) val current = ForegroundActivity.get() if (current == null) { - SDK.debug("popups v3: popup ${popup.id} waits for an activity") + popupsV3Log(sdk, LogLevel.DEBUG, "popups v3: popup ${popup.id} waits for an activity") return } attach(current) @@ -232,7 +234,7 @@ internal class PopupV3Session( } catch (throwable: Throwable) { dialog = null (view.parent as? ViewGroup)?.removeView(view) - if (!shown) fail("no window: ${throwable.javaClass.simpleName}") else SDK.debug("popups v3: popup ${popup.id} could not come back: ${throwable.javaClass.simpleName}") + if (!shown) fail("no window: ${throwable.javaClass.simpleName}") else popupsV3Log(sdk, LogLevel.DEBUG, "popups v3: popup ${popup.id} could not come back", throwable = throwable) return } activity = WeakReference(host) @@ -725,7 +727,7 @@ internal class PopupV3ActionHandler( val handled = try { handler(link) } catch (throwable: Throwable) { - SDK.error("popups v3: the link handler threw", throwable) + popupsV3Log(sdk, LogLevel.ERROR, "popups v3: the link handler threw", throwable = throwable) false } if (handled) { @@ -739,9 +741,9 @@ internal class PopupV3ActionHandler( try { host.startActivity(intent) } catch (exception: ActivityNotFoundException) { - SDK.error("popups v3: no app can open the link") + popupsV3Log(sdk, LogLevel.ERROR, "popups v3: no app can open the link") } catch (exception: RuntimeException) { - SDK.error("popups v3: the link could not be opened", exception) + popupsV3Log(sdk, LogLevel.ERROR, "popups v3: the link could not be opened", throwable = exception) } } @@ -824,7 +826,7 @@ internal class PopupV3ActionHandler( val handled = try { handler(product) } catch (throwable: Throwable) { - SDK.error("popups v3: product handler failed on ${product.id}", throwable) + popupsV3Log(sdk, LogLevel.ERROR, "popups v3: product handler failed on ${product.id}", throwable = throwable) false } if (handled) { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt index 62f05f71..88a03b3d 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt @@ -1,6 +1,9 @@ package com.personalization.ui.widgets +import com.personalization.LogLevel import com.personalization.SDK +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import com.personalization.stories.StoriesManager import com.personalization.stories.models.Story import java.net.HttpURLConnection @@ -109,8 +112,8 @@ internal object StoriesApiV3 { runCatching { sdk.storiesManager.setTrackingSourceUseCase(StoriesManager.STORIES_SOURCE_TYPE, code) } whenDid(sdk) { val body = eventBody(shopId(sdk), sdk.getDid().orEmpty(), event, storyId, slideId) - val response = post(eventsUrl(domain, code), body) - if (response.code !in 200..299) SDK.error("stories v3: $event failed: ${response.code}") + val response = post(eventsUrl(domain, code), body, sdk.logShopId) + if (response.code !in 200..299) SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES_V3, "stories v3: $event failed: ${response.code}", sdk.logShopId) } } @@ -118,7 +121,7 @@ internal object StoriesApiV3 { private fun load(sdk: SDK, url: (String, String) -> String, onLoaded: (List) -> Unit, onFailed: (Int, String?) -> Unit) { whenDid(sdk) { val response = try { - call("GET", url(shopId(sdk), sdk.getDid().orEmpty()), null) + call("GET", url(shopId(sdk), sdk.getDid().orEmpty()), null, sdk.logShopId) } catch (throwable: Throwable) { MainThread.run { onFailed(StoriesLoader.ERROR_CODE, throwable.message) } return@whenDid @@ -149,7 +152,7 @@ internal object StoriesApiV3 { try { task() } catch (throwable: Throwable) { - SDK.error("stories v3: request failed", throwable) + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES_V3, "stories v3: request failed", sdk.logShopId, throwable = throwable) } } try { @@ -158,21 +161,24 @@ internal object StoriesApiV3 { if (sdk.getDid().isNullOrEmpty()) return } } catch (throwable: Throwable) { - SDK.error("stories v3: SDK is not ready", throwable) + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES_V3, "stories v3: SDK is not ready", sdk.logShopId, throwable = throwable) } runner(run) } fun shopId(sdk: SDK): String = sdk.getUserSettingsValueUseCase.getShopId() - /** POST с JSON-телом — события и опросы; зовётся не на главном потоке. */ - fun post(url: String, body: JSONObject): Response = call("POST", url, body.toString()) + /** + * POST с JSON-телом — события и опросы; зовётся не на главном потоке. [shopId] — магазин + * экземпляра: строка лога проходит его уровень. + */ + fun post(url: String, body: JSONObject, shopId: String? = null): Response = call("POST", url, body.toString(), shopId) /** GET — для других клиентов API v3 (попапы); зовётся не на главном потоке. */ - fun get(url: String): Response = call("GET", url, null) + fun get(url: String, shopId: String? = null): Response = call("GET", url, null, shopId) /** Запрос с логом, как у сети SDK: строка в logcat и в `networkLogger`. */ - private fun call(method: String, url: String, body: String?): Response { + private fun call(method: String, url: String, body: String?, shopId: String?): Response { val started = System.currentTimeMillis() val response = try { http.send(method, url, body) @@ -181,7 +187,7 @@ internal object StoriesApiV3 { throw throwable } // Method and path only: the query and the body carry did, the session and the user's answers. - SDK.debug("${response.code}: $method ${pathOf(url)}") + SdkLogs.log(LogLevel.DEBUG, LogCodes.SDK_LOG, "${response.code}: $method ${pathOf(url)}", shopId) SDK.networkLogger?.onHttpCall(method, url, body, response.code, response.body, System.currentTimeMillis() - started, null) return response } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesPollsApi.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesPollsApi.kt index 29af6db6..44b813cc 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesPollsApi.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesPollsApi.kt @@ -3,7 +3,10 @@ package com.personalization.ui.widgets import android.os.Handler import android.os.Looper import com.personalization.InternalPersonalizationV3Api +import com.personalization.LogLevel import com.personalization.SDK +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import java.util.concurrent.atomic.AtomicBoolean import org.json.JSONArray import org.json.JSONObject @@ -28,7 +31,7 @@ internal class StoriesPollsApi(private val sdk: SDK, private val domain: String) val claimed = AtomicBoolean(false) val timeout = Runnable { if (!claimed.compareAndSet(false, true)) return@Runnable - SDK.debug("stories poll: ${pathOf(url)} failed: no did in ${DID_TIMEOUT_MS / 1000} s") + SdkLogs.log(LogLevel.DEBUG, LogCodes.STORIES_V3, "stories poll: ${pathOf(url)} failed: no did in ${DID_TIMEOUT_MS / 1000} s", sdk.logShopId) callback(StoriesPollsResult.Failure(StoriesLoader.ERROR_CODE, "no did")) } handler.postDelayed(timeout, DID_TIMEOUT_MS) @@ -48,7 +51,7 @@ internal class StoriesPollsApi(private val sdk: SDK, private val domain: String) val json = identity() body.forEach { (key, value) -> json.put(key, if (value is Collection<*>) JSONArray(value) else value) } val response = try { - StoriesApiV3.post(url, json) + StoriesApiV3.post(url, json, sdk.logShopId) } catch (throwable: Throwable) { return failure(url, StoriesLoader.ERROR_CODE, throwable.message) } @@ -72,7 +75,7 @@ internal class StoriesPollsApi(private val sdk: SDK, private val domain: String) // Path only, without the answer: the query carries did and sid, an error answer often repeats them. private fun failure(url: String, code: Int, message: String?): StoriesPollsResult { - SDK.debug("stories poll: ${pathOf(url)} failed: $code") + SdkLogs.log(LogLevel.DEBUG, LogCodes.STORIES_V3, "stories poll: ${pathOf(url)} failed: $code", sdk.logShopId) return StoriesPollsResult.Failure(code, message) } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/logs/SdkLogsTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/logs/SdkLogsTest.kt index e9b772aa..662162e7 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/logs/SdkLogsTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/logs/SdkLogsTest.kt @@ -131,6 +131,73 @@ class SdkLogsTest { assertFalse(received.any { it.message.contains("hidden") }) } + @Test + fun `an exception reaches logcat and the handler as its class name only`() { + SdkLogs.register("shop", LogLevel.WARNING, "ShopTag") + + SdkLogs.log( + LogLevel.ERROR, + "decoding_failed", + "The answer did not parse", + shopId = "shop", + throwable = IllegalStateException("Value secret@example.com at email of type java.lang.String"), + ) + + val entry = ShadowLog.getLogsForTag("ShopTag").single() + assertEquals("[decoding_failed] The answer did not parse {exception=IllegalStateException}", entry.msg) + assertEquals(null, entry.throwable) + assertFalse(received.single().toString().contains("secret@example.com")) + } + + @Test + fun `a forced line is written whatever the level`() { + SdkLogs.register("shop", LogLevel.NONE, "ShopTag") + + SdkLogs.log(LogLevel.DEBUG, "popups_v3", "skipped by the level", shopId = "shop") + SdkLogs.log(LogLevel.DEBUG, "popups_v3", "asked for by the app", shopId = "shop", force = true) + + assertEquals(listOf("asked for by the app"), received.map { it.message }) + } + + @Test + fun `popups v3 debug writes the engine's lines at the default level`() { + val sdk = io.mockk.mockk(relaxed = true) + val popups = com.personalization.popups.PopupsV3(sdk) + io.mockk.every { sdk.popupsV3 } returns popups + + popups.log(LogLevel.DEBUG, "hidden: debug is off") + popups.debug = true + popups.log(LogLevel.DEBUG, "shown: debug is on") + + assertEquals(listOf("popups_v3" to "shown: debug is on"), received.map { it.code to it.message }) + } + + @Test + fun `a shop keeps its level while one of its instances is live`() { + val first = Any() + val second = Any() + SdkLogs.register("shop", LogLevel.DEBUG, "SDK", owner = first) + SdkLogs.register("shop", LogLevel.ERROR, "SDK", owner = second) + assertEquals(LogLevel.DEBUG, SdkLogs.levelFor("shop")) + + SdkLogs.unregister("shop", owner = first) + assertEquals(LogLevel.ERROR, SdkLogs.levelFor("shop")) + + SdkLogs.unregister("shop", owner = second) + // No instance left: the shop falls back to the default. + assertEquals(LogLevel.WARNING, SdkLogs.levelFor("shop")) + } + + @Test + fun `the level can be checked before the shop is known`() { + assertFalse(SdkLogs.isLoggable(LogLevel.DEBUG)) + SdkLogs.register("quiet", LogLevel.ERROR, "SDK") + SdkLogs.register("loud", LogLevel.DEBUG, "SDK") + + assertTrue(SdkLogs.isLoggable(LogLevel.DEBUG)) + assertFalse(SdkLogs.isLoggable(LogLevel.DEBUG, "quiet")) + } + @Test fun `log levels allow from their own level up`() { assertTrue(LogLevel.WARNING.allows(LogLevel.ERROR)) From 253eb6b7e26beba181bcc25be1ed3fd21f3b08c4 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 21:56:49 +0400 Subject: [PATCH 007/109] refactor(sdk): the core's main dispatcher is looked up on first use, not when CoreDispatchers.DEFAULT is made; handleInitError loses its unused message parameter; the stories polls take the log path from StoriesApiV3.pathOf instead of a copy --- .../kotlin/com/personalization/RegisterManager.kt | 6 +++--- .../com/personalization/core/CoreDispatchers.kt | 7 ++++++- .../com/personalization/ui/widgets/StoriesApiV3.kt | 3 ++- .../com/personalization/ui/widgets/StoriesPollsApi.kt | 6 ++---- .../test/kotlin/com/personalization/core/CoreFakes.kt | 2 +- .../com/personalization/core/CoreHarnessTest.kt | 11 +++++++++++ 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt index c87d4d3d..d092494c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt @@ -110,8 +110,9 @@ class RegisterManager @Inject constructor( handleInitSuccess(response, knownDevice) } + // The answer's body stays out of the log: a server often repeats the request's data in it. override fun onError(code: Int, msg: String?) { - handleInitError(code, msg, attempt, knownDevice) + handleInitError(code, attempt, knownDevice) } } ) @@ -166,8 +167,7 @@ class RegisterManager @Inject constructor( } } - // The answer's body stays out of the log: a server often repeats the request's data in it. - private fun handleInitError(code: Int, @Suppress("UNUSED_PARAMETER") msg: String?, attempt: Int, knownDevice: Boolean) { + private fun handleInitError(code: Int, attempt: Int, knownDevice: Boolean) { if (code in 500..Int.MAX_VALUE || code <= 0) { if (attempt < MAX_INIT_RETRIES) { log( diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/CoreDispatchers.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/CoreDispatchers.kt index f6f5ad69..3fda6d25 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/CoreDispatchers.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/CoreDispatchers.kt @@ -6,12 +6,17 @@ import kotlinx.coroutines.Dispatchers /** * Where the core runs its work: network and disk on [io], app callbacks on [main]. Tests pass test * dispatchers to run the core step by step. + * + * [main] is looked up on first use: [Dispatchers.Main] loads the platform's main dispatcher, which a + * process that never delivers a callback (a push in a cold start, a unit test) should not pay for. */ internal class CoreDispatchers( val io: CoroutineDispatcher = Dispatchers.IO, - val main: CoroutineDispatcher = Dispatchers.Main, + main: () -> CoroutineDispatcher = { Dispatchers.Main }, val default: CoroutineDispatcher = Dispatchers.Default, ) { + val main: CoroutineDispatcher by lazy(main) + companion object { val DEFAULT = CoreDispatchers() } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt index 88a03b3d..440a1654 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt @@ -192,7 +192,8 @@ internal object StoriesApiV3 { return response } - private fun pathOf(url: String): String = runCatching { URL(url).path }.getOrDefault("") + /** The path of [url] without the host and the query, for a log line: the query carries did and sid. */ + fun pathOf(url: String): String = runCatching { URL(url).path }.getOrDefault("") private fun sendHttp(method: String, url: String, body: String?): Response { val connection = URL(url).openConnection() as HttpURLConnection diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesPollsApi.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesPollsApi.kt index 44b813cc..6c6ab35b 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesPollsApi.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesPollsApi.kt @@ -31,7 +31,7 @@ internal class StoriesPollsApi(private val sdk: SDK, private val domain: String) val claimed = AtomicBoolean(false) val timeout = Runnable { if (!claimed.compareAndSet(false, true)) return@Runnable - SdkLogs.log(LogLevel.DEBUG, LogCodes.STORIES_V3, "stories poll: ${pathOf(url)} failed: no did in ${DID_TIMEOUT_MS / 1000} s", sdk.logShopId) + SdkLogs.log(LogLevel.DEBUG, LogCodes.STORIES_V3, "stories poll: ${StoriesApiV3.pathOf(url)} failed: no did in ${DID_TIMEOUT_MS / 1000} s", sdk.logShopId) callback(StoriesPollsResult.Failure(StoriesLoader.ERROR_CODE, "no did")) } handler.postDelayed(timeout, DID_TIMEOUT_MS) @@ -75,12 +75,10 @@ internal class StoriesPollsApi(private val sdk: SDK, private val domain: String) // Path only, without the answer: the query carries did and sid, an error answer often repeats them. private fun failure(url: String, code: Int, message: String?): StoriesPollsResult { - SdkLogs.log(LogLevel.DEBUG, LogCodes.STORIES_V3, "stories poll: ${pathOf(url)} failed: $code", sdk.logShopId) + SdkLogs.log(LogLevel.DEBUG, LogCodes.STORIES_V3, "stories poll: ${StoriesApiV3.pathOf(url)} failed: $code", sdk.logShopId) return StoriesPollsResult.Failure(code, message) } - private fun pathOf(url: String): String = runCatching { java.net.URL(url).path }.getOrDefault("") - /** `message` ответа с ошибкой, иначе начало тела. */ private fun message(body: String): String? = runCatching { JSONObject(body).optString("message") }.getOrNull()?.takeIf { it.isNotEmpty() } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/CoreFakes.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/CoreFakes.kt index 86d30312..24da3117 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/CoreFakes.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/CoreFakes.kt @@ -57,5 +57,5 @@ internal class FakeAppLifecycle(foreground: Boolean = true) : AppLifecycle { @OptIn(ExperimentalCoroutinesApi::class) internal fun testCoreDispatchers(scheduler: TestCoroutineScheduler = TestCoroutineScheduler()): CoreDispatchers { val dispatcher = StandardTestDispatcher(scheduler) - return CoreDispatchers(io = dispatcher, main = dispatcher, default = dispatcher) + return CoreDispatchers(io = dispatcher, main = { dispatcher }, default = dispatcher) } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/CoreHarnessTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/CoreHarnessTest.kt index 27e73239..78a2f8d4 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/CoreHarnessTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/CoreHarnessTest.kt @@ -78,6 +78,17 @@ class CoreHarnessTest { assertFalse(lifecycle.isForeground) } + @Test + fun `the main dispatcher is looked up on first use, not when the dispatchers are made`() { + var lookedUp = 0 + val dispatchers = CoreDispatchers(main = { lookedUp++; kotlinx.coroutines.Dispatchers.Unconfined }) + assertEquals(0, lookedUp) + + dispatchers.main + dispatchers.main + assertEquals(1, lookedUp) + } + @OptIn(ExperimentalCoroutinesApi::class) @Test fun `test dispatchers run the core's work when the test lets them`() = runTest { From af2e4d758e5e85921e3e4c202c0d31fb3d59f756 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 21:58:06 +0400 Subject: [PATCH 008/109] build: the `!!` check counts code only, not comments, strings or characters, and the baseline must stay exact: a file with fewer `!!` than allowed, a listed file that is gone and an unlisted file with `!!` all fail and say which line to change --- personalization-sdk/build.gradle | 108 +++++++++++++++--- personalization-sdk/force-unwrap-baseline.txt | 5 +- 2 files changed, 95 insertions(+), 18 deletions(-) diff --git a/personalization-sdk/build.gradle b/personalization-sdk/build.gradle index 12bfdfde..04735bd5 100644 --- a/personalization-sdk/build.gradle +++ b/personalization-sdk/build.gradle @@ -256,12 +256,13 @@ tasks.register('publishToMavenCentral') { } } -// No new `!!` in the SDK: a force unwrap on a path the app reaches crashes the app instead of -// failing one call. force-unwrap-baseline.txt lists the views that still have them; it may only -// shrink. Plain text scan, so it needs no compiler and runs as part of `check` in CI. +// No new `!!` in the SDK: a force unwrap on a path the app reaches crashes the app instead of failing one call. +// force-unwrap-baseline.txt lists the views that still have them and how many; it may only shrink, and it must +// stay exact, so a removed `!!` or file shows up as a stale line to delete. Plain text scan that skips +// comments, strings and characters (`"https://…"`, `"Hi!!"`), so it needs no compiler and runs in `check`. tasks.register('checkForceUnwraps') { group = 'verification' - description = 'Fails when a Kotlin source has more `!!` than force-unwrap-baseline.txt allows.' + description = 'Fails when the `!!` in the Kotlin sources differ from force-unwrap-baseline.txt.' def sourceRoot = file('src/main/kotlin') def baseline = file('force-unwrap-baseline.txt') def sources = fileTree(sourceRoot) { include '**/*.kt' } @@ -277,23 +278,98 @@ tasks.register('checkForceUnwraps') { allowed[parts[0]] = parts[1] as int } } - def violations = [] + def counts = [:] sources.each { File source -> def path = sourceRoot.toPath().relativize(source.toPath()).toString().replace(File.separator, '/') - int count = 0 - source.eachLine { line -> - def code = line.trim() - if (code.startsWith('//') || code.startsWith('*') || code.startsWith('/*')) return - int comment = code.indexOf('//') - if (comment >= 0) code = code.substring(0, comment) - count += code.findAll(/!!/).size() + counts[path] = ForceUnwraps.count(source.getText('UTF-8')) + } + def problems = [] + counts.each { path, count -> + if (!allowed.containsKey(path)) { + if (count > 0) problems << "${path}: ${count} `!!` in a file the baseline does not list; new code handles the null instead" + } else { + int limit = allowed[path] as int + if (count > limit) problems << "${path}: ${count} `!!`, ${limit} allowed" + if (count < limit) { + def fix = count == 0 ? 'remove its line' : "lower its count to ${count}" + problems << "${path}: ${count} `!!`, the baseline still allows ${limit}: ${fix} in force-unwrap-baseline.txt" + } } - int limit = allowed.getOrDefault(path, 0) as int - if (count > limit) violations << "${path}: ${count} `!!`, ${limit} allowed" } - if (!violations.isEmpty()) { - throw new GradleException("Force unwraps (`!!`) in the SDK — handle the null instead:\n" + violations.join('\n')) + allowed.keySet().findAll { !counts.containsKey(it) }.each { path -> + problems << "${path}: no such file any more: remove its line from force-unwrap-baseline.txt" + } + if (!problems.isEmpty()) { + throw new GradleException("Force unwraps (`!!`) in the SDK do not match the baseline:\n" + problems.join('\n')) + } + } +} + +/** Counts `!!` in Kotlin code, skipping comments, string and character literals but not string templates. */ +class ForceUnwraps { + static int count(String text) { + int count = 0 + int i = 0 + int n = text.length() + // Frames: [kind, brace depth]. The bottom one is the file's code; a code frame above it is a + // string template (`${…}`), closed by its own `}`. + def frames = new ArrayDeque() + frames.push(['code', 0]) + while (i < n) { + def frame = frames.peek() + char c = text.charAt(i) + switch (frame[0]) { + case 'code': + if (text.startsWith('//', i)) { + int end = text.indexOf('\n', i) + i = end < 0 ? n : end + } else if (text.startsWith('/*', i)) { + int depth = 1 + i += 2 + while (i < n && depth > 0) { + if (text.startsWith('/*', i)) { depth++; i += 2 } + else if (text.startsWith('*/', i)) { depth--; i += 2 } + else i++ + } + } else if (text.startsWith('"""', i)) { + frames.push(['raw', 0]); i += 3 + } else if (c == '"' as char) { + frames.push(['string', 0]); i++ + } else if (c == '\'' as char) { + int from = (i + 1 < n && text.charAt(i + 1) == '\\' as char) ? i + 3 : i + 2 + int close = text.indexOf("'", Math.min(from, n)) + i = close < 0 ? n : close + 1 + } else if (text.startsWith('!!', i)) { + count++; i += 2 + } else if (c == '{' as char) { + frame[1]++; i++ + } else if (c == '}' as char) { + if (frame[1] == 0 && frames.size() > 1) frames.pop() else frame[1]-- + i++ + } else { + i++ + } + break + case 'string': + if (c == '\\' as char) i += 2 + else if (c == '"' as char || c == '\n' as char) { frames.pop(); i++ } + else if (text.startsWith('${', i)) { frames.push(['code', 0]); i += 2 } + else i++ + break + case 'raw': + if (text.startsWith('"""', i)) { + i += 3 + while (i < n && text.charAt(i) == '"' as char) i++ + frames.pop() + } else if (text.startsWith('${', i)) { + frames.push(['code', 0]); i += 2 + } else { + i++ + } + break + } } + return count } } diff --git a/personalization-sdk/force-unwrap-baseline.txt b/personalization-sdk/force-unwrap-baseline.txt index aa1aab62..8181a6e1 100644 --- a/personalization-sdk/force-unwrap-baseline.txt +++ b/personalization-sdk/force-unwrap-baseline.txt @@ -1,6 +1,7 @@ -# Kotlin sources that may still have `!!`, and how many. The list may only shrink: +# Kotlin sources that may still have `!!`, and exactly how many. The list may only shrink: # a force unwrap on a path the app reaches crashes the app instead of failing one call. -# Checked by `./gradlew checkForceUnwraps`, which `check` runs. +# Removing a `!!` means lowering its count here (or removing the line at 0) in the same change. +# Checked by `./gradlew checkForceUnwraps`, which `check` runs; comments and strings are not counted. com/personalization/inAppNotification/view/component/dialog/alert/AlertDialog.kt 1 com/personalization/inAppNotification/view/component/dialog/bottom/BottomDialog.kt 1 com/personalization/inAppNotification/view/component/dialog/fullScreen/FullScreenDialog.kt 1 From 154a3a7cc0cf8db13295116b95a4d17ed0b948d7 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:00:23 +0400 Subject: [PATCH 009/109] fix(sdk): log codes follow the spec's table at its levels: a 4xx /init is init_failed (error), init gives up with a warning, an answered /init is init_succeeded, needReInitialization is device_id_reset; request_failed is an error on 4xx and a warning otherwise, the stories v3 client logs request and request_failed; the advertising id has its own codes; push token, decoding and popup_not_shown lines are warnings; a notification-only FCM message and a clicked push without type or code are push_dropped --- .../com/personalization/MessagingService.kt | 6 +- .../com/personalization/RegisterManager.kt | 17 +-- .../main/kotlin/com/personalization/SDK.kt | 4 +- .../errors/emptyField.error.kt | 2 +- .../personalization/errors/firebase.error.kt | 2 +- .../personalization/errors/network.error.kt | 2 +- .../personalization/errors/parsing.error.kt | 2 +- .../errors/response.error.kt.kt | 2 +- .../impl/InAppNotificationManagerImpl.kt | 10 +- .../helpers/NotificationClickProcessor.kt | 29 ++--- .../com/personalization/logs/SdkLogs.kt | 45 +++++-- .../personalization/push/PushTokenManager.kt | 15 ++- .../network/NetworkRepositoryImpl.kt | 6 +- .../ui/widgets/StoriesApiV3.kt | 10 +- .../com/personalization/logs/LogCodesTest.kt | 112 ++++++++++++++++++ .../ui/widgets/StoriesPollsApiTest.kt | 2 +- 16 files changed, 204 insertions(+), 62 deletions(-) create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/logs/LogCodesTest.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/MessagingService.kt b/personalization-sdk/src/main/kotlin/com/personalization/MessagingService.kt index d2b1fee9..4048fe9e 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/MessagingService.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/MessagingService.kt @@ -26,9 +26,9 @@ class MessagingService : FirebaseMessagingService() { Rees46.handlePush(remoteMessage.data, PushEventType.RECEIVED) } - // Check if message contains a notification payload. - if (remoteMessage.notification != null) { - SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_RECEIVED, "FCM message with a notification payload") + // SDK pushes are data messages; one with only a notification payload is not the SDK's. + if (remoteMessage.data.isEmpty() && remoteMessage.notification != null) { + SdkLogs.log(LogLevel.WARNING, LogCodes.PUSH_DROPPED, "FCM message with only a notification payload is not an SDK push") } } catch (throwable: Throwable) { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "An FCM message could not be handled", throwable = throwable) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt index d092494c..2b6526cf 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt @@ -7,7 +7,6 @@ import android.content.Context import android.provider.Settings import com.personalization.api.OnApiCallbackListener import com.personalization.api.managers.InAppNotificationManager -import com.personalization.errors.BaseInfoError import com.personalization.errors.JsonResponseErrorHandler import com.personalization.push.PushTokenManager import com.personalization.sdk.data.mappers.SdkInitializationMapper.mapToSdkInitResponse @@ -64,6 +63,9 @@ class RegisterManager @Inject constructor( this.autoSendPushToken = autoSendPushToken val did = getUserSettingsValueUseCase.getDid() + if (did.isNotEmpty() && needReInitialization) { + log(LogLevel.INFO, LogCodes.DEVICE_ID_RESET, "needReInitialization: /init asks for a new did") + } when { did.isEmpty() || needReInitialization -> initializeNewDevice() else -> { @@ -89,10 +91,7 @@ class RegisterManager @Inject constructor( private fun init(knownDevice: Boolean = false) { if (isTestDevice) { - BaseInfoError( - tag = TAG, - message = "Disable working on Google Play Pre-Launch report devices" - ).logError() + log(LogLevel.WARNING, LogCodes.SDK_LOG, "/init is not sent on a Google Play pre-launch report device") return } @@ -117,7 +116,7 @@ class RegisterManager @Inject constructor( } ) } catch (e: Exception) { - log(LogLevel.ERROR, LogCodes.INIT_FAILED, "/init could not be sent", throwable = e) + log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "/init could not be built", throwable = e) } } @@ -138,6 +137,7 @@ class RegisterManager @Inject constructor( if (!errorHandler.validateResponse()) { return } + log(LogLevel.INFO, LogCodes.INIT_SUCCEEDED, "/init succeeded") val initResponse = response?.mapToSdkInitResponse() val popUpData = initResponse?.popupDto if (popUpData != null) { @@ -182,7 +182,7 @@ class RegisterManager @Inject constructor( } } else { log( - LogLevel.ERROR, + LogCodes.failureLevel(code), LogCodes.INIT_FAILED, "/init failed after all retries", mapOf("status" to code, "attempts" to attempt + 1) @@ -191,7 +191,8 @@ class RegisterManager @Inject constructor( } } else { if (knownDevice) notifyInitFinished(popupsV3 = false) - log(LogLevel.ERROR, LogCodes.INIT_REFUSED, "The shop refused /init", mapOf("status" to code)) + // A 4xx is a failed /init, not a refused shop: the legacy path has no refused state. + log(LogCodes.failureLevel(code), LogCodes.INIT_FAILED, "/init failed", mapOf("status" to code)) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 5b1d35fc..9d2d6896 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -385,14 +385,14 @@ open class SDK { .toJson(), listener = object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { - SdkLogs.log(LogLevel.DEBUG, LogCodes.REQUEST, "Advertising id sent", shopId) + SdkLogs.log(LogLevel.DEBUG, LogCodes.ADVERTISING_ID_SENT, "Advertising id sent", shopId) } override fun onError(code: Int, msg: String?) { // Without the answer's body: an error answer often repeats the request's data. SdkLogs.log( LogLevel.WARNING, - LogCodes.REQUEST_FAILED, + LogCodes.ADVERTISING_ID_FAILED, "Advertising id was not sent", shopId, mapOf("status" to code) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/emptyField.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/emptyField.error.kt index 7444a6fa..eac0f927 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/emptyField.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/emptyField.error.kt @@ -11,6 +11,6 @@ class EmptyFieldError( ) { fun logError() { - SdkLogs.log(LogLevel.ERROR, LogCodes.INVALID_ARGUMENT, "Field is empty or null in $functionName : $message") + SdkLogs.log(LogLevel.WARNING, LogCodes.INVALID_ARGUMENT, "Field is empty or null in $functionName : $message") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/firebase.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/firebase.error.kt index d6185916..f5505970 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/firebase.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/firebase.error.kt @@ -10,6 +10,6 @@ class FirebaseError( ) { fun logError() { - SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_TOKEN_FAILED, "Failed to retrieve Firebase token: ${exception?.javaClass?.simpleName ?: "unknown error"}") + SdkLogs.log(LogLevel.WARNING, LogCodes.PUSH_TOKEN_FAILED, "Failed to retrieve Firebase token: ${exception?.javaClass?.simpleName ?: "unknown error"}") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/network.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/network.error.kt index 207c98e7..d19aa653 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/network.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/network.error.kt @@ -10,6 +10,6 @@ class NetworkError( ) { fun logError() { - SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_FAILED, "Error caught in $functionName : Network connection issue ") + SdkLogs.log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "Error caught in $functionName : Network connection issue ") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/parsing.error.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/parsing.error.kt index 51dc3c88..fdaaea48 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/parsing.error.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/parsing.error.kt @@ -11,6 +11,6 @@ class ParsingJsonError( ) { fun logError() { - SdkLogs.log(LogLevel.ERROR, LogCodes.DECODING_FAILED, "Error caught in $functionName: $message") + SdkLogs.log(LogLevel.WARNING, LogCodes.DECODING_FAILED, "Error caught in $functionName: $message") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/errors/response.error.kt.kt b/personalization-sdk/src/main/kotlin/com/personalization/errors/response.error.kt.kt index 1f1a8242..e9bf98bf 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/errors/response.error.kt.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/errors/response.error.kt.kt @@ -50,6 +50,6 @@ class JsonResponseErrorHandler( message: String? = this.message, exception: Exception? = null ) { - SdkLogs.log(LogLevel.ERROR, LogCodes.DECODING_FAILED, message ?: "Response is null or incorrect", throwable = exception) + SdkLogs.log(LogLevel.WARNING, LogCodes.DECODING_FAILED, message ?: "Response is null or incorrect", throwable = exception) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt index 33a848e8..70142da1 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt @@ -100,7 +100,7 @@ class InAppNotificationManagerImpl @Inject constructor( return } if (activity == null) { - log(LogLevel.DEBUG, LogCodes.POPUP_NOT_SHOWN, "Popup kept back by the presentation listener", context = mapOf("popup" to popupDto.id)) + log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Popup kept back by the presentation listener", context = mapOf("popup" to popupDto.id, "reason" to "listener")) return } activity.supportFragmentManager.takeIf { it.canShowDialog() } @@ -109,18 +109,18 @@ class InAppNotificationManagerImpl @Inject constructor( presentation.autoPresentation -> defaultTarget() else -> { - log(LogLevel.DEBUG, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown: automatic presentation is off", context = mapOf("popup" to popupDto.id)) + log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown: automatic presentation is off", context = mapOf("popup" to popupDto.id, "reason" to "auto_presentation_off")) return } } if (target == null) { - log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown: no activity that can take a dialog", context = mapOf("popup" to popupDto.id)) + log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown: no activity that can take a dialog", context = mapOf("popup" to popupDto.id, "reason" to "no_activity")) return } try { showDialog(target, extractDialogData(popupDto)) } catch (exception: Exception) { - log(LogLevel.ERROR, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown", context = mapOf("popup" to popupDto.id), throwable = exception) + log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown", context = mapOf("popup" to popupDto.id, "reason" to "exception"), throwable = exception) return } @@ -262,7 +262,7 @@ class InAppNotificationManagerImpl @Inject constructor( target: FragmentManager? ) { if (target == null) { - log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Dialog was not shown: no activity on screen to show it in") + log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Dialog was not shown: no activity on screen to show it in", context = mapOf("reason" to "no_activity")) return } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt index f22058ca..cd70c41a 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt @@ -1,8 +1,9 @@ package com.personalization.features.notification.domain.helpers import android.os.Bundle -import com.personalization.errors.EmptyFieldError -import com.personalization.errors.ParsingJsonError +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import com.personalization.features.notification.domain.data.NotificationDataExtractor import com.personalization.features.notification.domain.data.NotificationDataSender import javax.inject.Inject @@ -34,25 +35,17 @@ class NotificationClickProcessor @Inject constructor( ) onResult(type, code) } catch (e: JSONException) { - ParsingJsonError( - tag = this@NotificationClickProcessor.javaClass.name, - functionName = "processClick", - message = e.javaClass.simpleName - ).logError() + SdkLogs.log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "The click of a push could not be sent", throwable = e) } } private fun handleError(errorMessage: String, extras: Bundle?) { - EmptyFieldError( - tag = TAG, - functionName = FUNCTION_NAME, - // Not the extras themselves: they carry the push's content. - message = "$errorMessage; extras keys: ${extras?.keySet()?.sorted()}" - ).logError() - } - - companion object { - private const val TAG = "NotificationClickProcessor" - private const val FUNCTION_NAME = "processClick" + // Not the extras themselves: they carry the push's content. + SdkLogs.log( + LogLevel.WARNING, + LogCodes.PUSH_DROPPED, + "$errorMessage: the clicked push has no type or code", + context = mapOf("keys" to extras?.keySet()?.sorted()) + ) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt index 8eebf03c..90741d1b 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt @@ -126,32 +126,57 @@ internal object SdkLogs { } } -/** Stable codes of the SDK's log events. */ +/** + * Stable codes of the SDK's log events: the table "Коды" of the core's logging spec, the same on Android + * and iOS. A code is used only at its level in that table; a new code goes into the table first. + */ internal object LogCodes { - /** A free-text message written through [SDK.debug], [SDK.warn] or [SDK.error]. */ - const val SDK_LOG = "sdk_log" - + /** A request got an answer (`debug`); context: method, path, status. */ const val REQUEST = "request" + + /** An answer outside `2xx` or none (`error` on `4xx`, otherwise `warning`); context: method, path, status. */ const val REQUEST_FAILED = "request_failed" const val REQUEST_BUILD_FAILED = "request_build_failed" - const val DECODING_FAILED = "decoding_failed" - const val CALLBACK_FAILED = "callback_failed" const val INVALID_ARGUMENT = "invalid_argument" const val INVALID_CONFIG = "invalid_config" + const val DECODING_FAILED = "decoding_failed" + const val CALLBACK_FAILED = "callback_failed" + const val INIT_SUCCEEDED = "init_succeeded" + const val INIT_SAVED_ANSWER = "init_saved_answer" const val INIT_RETRY = "init_retry" - const val INIT_REFUSED = "init_refused" + + /** `/init` failed (`error` on `4xx`, otherwise `warning`). */ const val INIT_FAILED = "init_failed" + /** The shop refused and the core is in the `refused` state; the legacy path has no such state. */ + const val INIT_REFUSED = "init_refused" + const val DEVICE_ID_RESET = "device_id_reset" + const val STORAGE_FAILED = "storage_failed" + const val QUEUE_DROPPED = "queue_dropped" + const val PUSH_RECEIVED = "push_received" - const val PUSH_FAILED = "push_failed" const val PUSH_DROPPED = "push_dropped" + const val PUSH_FAILED = "push_failed" + const val PUSH_PERMISSION = "push_permission" + const val PUSH_PERMISSION_FAILED = "push_permission_failed" const val PUSH_TOKEN_SENT = "push_token_sent" + const val PUSH_TOKEN_SKIPPED = "push_token_skipped" const val PUSH_TOKEN_FAILED = "push_token_failed" + const val ADVERTISING_ID_SENT = "advertising_id_sent" + const val ADVERTISING_ID_FAILED = "advertising_id_failed" const val POPUP_NOT_SHOWN = "popup_not_shown" - const val POPUPS_V3 = "popups_v3" + const val CORE_FLAG_MISMATCH = "core_flag_mismatch" + + /** Lines of the older code without an event of their own go under the code of their area. */ + const val SDK_LOG = "sdk_log" + const val UI = "ui" const val STORIES = "stories" const val STORIES_V3 = "stories_v3" - const val UI = "ui" + const val POPUPS_V3 = "popups_v3" + + /** The level of a failed request or `/init` by its HTTP code: `error` on `4xx`, otherwise `warning`. */ + fun failureLevel(status: Int?): LogLevel = + if (status != null && status in 400..499) LogLevel.ERROR else LogLevel.WARNING } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt index c5e9fd69..78681e33 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt @@ -7,7 +7,6 @@ import android.content.Context import com.personalization.OnPushTokenListener import com.personalization.PushProvider import com.personalization.api.OnApiCallbackListener -import com.personalization.errors.BaseInfoError import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase import com.personalization.sdk.domain.usecases.preferences.GetPreferencesValueUseCase import com.personalization.sdk.domain.usecases.preferences.SavePreferencesValueUseCase @@ -94,10 +93,14 @@ class PushTokenManager @Inject constructor( ?.takeIf { it.isNotEmpty() } ?.let { token -> onTokenReceived(token, source.provider) } result.exceptionOrNull()?.let { error -> - BaseInfoError( - tag = TAG, - message = "${source.provider.id} token fetch error: ${error.javaClass.simpleName}" - ).logError() + SdkLogs.log( + LogLevel.WARNING, + LogCodes.PUSH_TOKEN_FAILED, + "Push token could not be fetched", + logShopId, + mapOf("provider" to source.provider.id), + error + ) } } } @@ -176,7 +179,7 @@ class PushTokenManager @Inject constructor( tokensInFlight.remove(inFlightKey(provider, token)) // Without the answer's body: an error answer often repeats the request's data. SdkLogs.log( - LogLevel.ERROR, + LogLevel.WARNING, LogCodes.PUSH_TOKEN_FAILED, "Push token was not sent", logShopId, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt index a295d692..7073c44e 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt @@ -272,16 +272,16 @@ class NetworkRepositoryImpl @Inject constructor( } responseBodyForLog = error // Without the body: an error answer often repeats the request's data. - log(LogLevel.ERROR, LogCodes.REQUEST_FAILED, "Request failed", networkMethod, responseCode) + log(LogCodes.failureLevel(responseCode), LogCodes.REQUEST_FAILED, "Request failed", networkMethod, responseCode) Outcome.Failure(responseCode, error) } } catch (e: ConnectException) { - log(LogLevel.ERROR, LogCodes.REQUEST_FAILED, "No connection to the server", networkMethod, throwable = e) + log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "No connection to the server", networkMethod, throwable = e) errorForLog = e.message responseCode = 504 Outcome.Failure(504, e.message) } catch (e: Exception) { - log(LogLevel.ERROR, LogCodes.REQUEST_FAILED, "Request ended without an answer", networkMethod, throwable = e) + log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "Request ended without an answer", networkMethod, throwable = e) errorForLog = e.message Outcome.Failure(-1, e.message) } finally { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt index 440a1654..8b31a9ba 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt @@ -183,11 +183,19 @@ internal object StoriesApiV3 { val response = try { http.send(method, url, body) } catch (throwable: Throwable) { + SdkLogs.log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "$method ${pathOf(url)}", shopId, mapOf("method" to method, "path" to pathOf(url)), throwable) SDK.networkLogger?.onHttpCall(method, url, body, -1, null, System.currentTimeMillis() - started, throwable.message) throw throwable } // Method and path only: the query and the body carry did, the session and the user's answers. - SdkLogs.log(LogLevel.DEBUG, LogCodes.SDK_LOG, "${response.code}: $method ${pathOf(url)}", shopId) + val success = response.code in 200..299 + SdkLogs.log( + if (success) LogLevel.DEBUG else LogCodes.failureLevel(response.code), + if (success) LogCodes.REQUEST else LogCodes.REQUEST_FAILED, + "${response.code}: $method ${pathOf(url)}", + shopId, + mapOf("method" to method, "path" to pathOf(url), "status" to response.code) + ) SDK.networkLogger?.onHttpCall(method, url, body, response.code, response.body, System.currentTimeMillis() - started, null) return response } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/logs/LogCodesTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/logs/LogCodesTest.kt new file mode 100644 index 00000000..15db4650 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/logs/LogCodesTest.kt @@ -0,0 +1,112 @@ +package com.personalization.logs + +import androidx.test.core.app.ApplicationProvider +import com.personalization.LogLevel +import com.personalization.Rees46 +import com.personalization.RegisterManager +import com.personalization.SdkLogMessage +import com.personalization.api.OnApiCallbackListener +import io.mockk.Runs +import io.mockk.every +import io.mockk.just +import io.mockk.mockk +import io.mockk.slot +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.lang.reflect.Modifier +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.Executor + +/** The SDK writes only the codes of the logging spec's table, each at its level there. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class LogCodesTest { + + private val received = CopyOnWriteArrayList() + private lateinit var previousExecutor: Executor + + @Before + fun setUp() { + SdkLogs.reset() + previousExecutor = SdkLogs.handlerExecutor + SdkLogs.handlerExecutor = Executor { it.run() } + Rees46.setLogHandler { received += it } + SdkLogs.register("shop", LogLevel.DEBUG, "SDK") + } + + @After + fun tearDown() { + SdkLogs.reset() + SdkLogs.handlerExecutor = previousExecutor + } + + @Test + fun `every code is in the table of the spec`() { + val codes = LogCodes::class.java.declaredFields + .filter { Modifier.isStatic(it.modifiers) && it.type == String::class.java } + .map { it.get(null) as String } + assertTrue(codes.isNotEmpty()) + assertEquals(emptyList(), codes - SPEC_CODES) + } + + @Test + fun `a failure is an error on a 4xx and a warning otherwise`() { + assertEquals(LogLevel.ERROR, LogCodes.failureLevel(403)) + assertEquals(LogLevel.WARNING, LogCodes.failureLevel(503)) + assertEquals(LogLevel.WARNING, LogCodes.failureLevel(-1)) + assertEquals(LogLevel.WARNING, LogCodes.failureLevel(null)) + } + + @Test + fun `a 4xx to init is a failed init, not a refused shop`() { + val init = startInit() + + init.onError(403, """{"message":"blocked"}""") + + assertEquals(listOf(LogCodes.INIT_FAILED to LogLevel.ERROR), received.map { it.code to it.level }) + assertEquals(403, received.single().context["status"]) + } + + @Test + fun `an answered init is logged as succeeded`() { + val init = startInit() + + init.onSuccess(JSONObject("""{"did":"D","seance":"S"}""")) + + assertEquals(listOf(LogCodes.INIT_SUCCEEDED to LogLevel.INFO), received.map { it.code to it.level }) + } + + private fun startInit(): OnApiCallbackListener { + val network = mockk(relaxed = true) + val settings = mockk(relaxed = true) + every { settings.getDid() } returns "" + every { settings.getShopId() } returns "shop" + val listener = slot() + every { network.get(eq("init"), any(), capture(listener)) } just Runs + val manager = RegisterManager(mockk(relaxed = true), settings, network, mockk(relaxed = true), mockk(relaxed = true), mockk(relaxed = true)) + val context = ApplicationProvider.getApplicationContext() + manager.initialize(context, context.contentResolver, autoSendPushToken = false) + received.clear() + return listener.captured + } + + private companion object { + /** The table "Коды" of the logging spec, with the area codes of the older code. */ + val SPEC_CODES = setOf( + "request", "request_failed", "request_build_failed", "invalid_argument", "invalid_config", + "decoding_failed", "callback_failed", "init_succeeded", "init_saved_answer", "init_retry", + "init_failed", "init_refused", "device_id_reset", "storage_failed", "queue_dropped", + "push_received", "push_dropped", "push_failed", "push_permission", "push_permission_failed", + "push_token_sent", "push_token_skipped", "push_token_failed", "advertising_id_sent", + "advertising_id_failed", "popup_not_shown", "core_flag_mismatch", "not_implemented", + "sdk_log", "ui", "stories", "stories_v3", "popups_v3", + ) + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/ui/widgets/StoriesPollsApiTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/ui/widgets/StoriesPollsApiTest.kt index 4a58d39b..bec5d4f4 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/ui/widgets/StoriesPollsApiTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/ui/widgets/StoriesPollsApiTest.kt @@ -108,7 +108,7 @@ class StoriesPollsApiTest { fun `a request is logged by method and path, without the identity and the answer`() { answer(body = mapOf("text" to "my own answer")) val logs = ShadowLog.getLogs().map { it.msg.orEmpty() } - assertTrue(logs.toString(), logs.any { it.endsWith("200: POST /api/v3/polls/mini_polls/gift_recipient/answers") }) + assertTrue(logs.toString(), logs.any { it.contains("[request] 200: POST /api/v3/polls/mini_polls/gift_recipient/answers") }) assertTrue(logs.toString(), logs.none { "did1" in it || "sid1" in it || "my own answer" in it }) } From 2b2bb55a001283c93d0de5624ee605fcef7f957e Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:01:22 +0400 Subject: [PATCH 010/109] fix(sdk): a new device's first requests carry its did: the requests that wait for a did are released after /init stored the server's did, not before /init was sent, when they went out with an empty did --- .../com/personalization/RegisterManager.kt | 9 +++-- .../personalization/RegisterManagerDidTest.kt | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt index 2b6526cf..2640cc66 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt @@ -85,7 +85,9 @@ class RegisterManager @Inject constructor( // handleInitSuccess persists the server's did. Keeps device identity consistent across // platforms and off Google's discouraged hardware-id APIs. Existing installs already hold a // non-empty did, so they keep it (this branch only runs when did is empty). - initializeSdk(seance = null) + // The requests waiting for a did stay queued: released now, they would go out with an empty + // did. handleInitSuccess releases them once the server's did is stored. + initializeSdk(seance = null, releaseQueue = false) init() } @@ -209,10 +211,11 @@ class RegisterManager @Inject constructor( SdkLogs.log(level, code, message, shopId, context, throwable) } - private fun initializeSdk(seance: String?) { + /** Starts the session; [releaseQueue] sends the requests that waited for a did, which must be stored by now. */ + private fun initializeSdk(seance: String?, releaseQueue: Boolean = true) { val finalSeance = seance ?: generateOrRetrieveSeance() updateUserSettingsValueUseCase.updateSid(value = finalSeance) - executeQueueTasksUseCase.invoke() + if (releaseQueue) executeQueueTasksUseCase.invoke() pushTokenManager.initialize(context = context, autoSendPushToken = autoSendPushToken) } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerDidTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerDidTest.kt index 23d73f47..5b145b0c 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerDidTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerDidTest.kt @@ -20,6 +20,7 @@ import io.mockk.just import io.mockk.mockk import io.mockk.slot import io.mockk.verify +import io.mockk.verifyOrder import org.json.JSONObject import org.junit.Before import org.junit.Test @@ -79,6 +80,39 @@ class RegisterManagerDidTest { verify(exactly = 1) { update.updateDid("SERVER_DID") } } + /** + * Requests made before a new device has a did wait in the network queue. Released before `/init` + * stored the server's did, they went out with `did=""`. + */ + @Test + fun `a new device releases the waiting requests only after the server's did is stored`() { + every { get.getDid() } returns "" + val listener = slot() + every { network.get(eq("init"), any(), capture(listener)) } just Runs + + manager.initialize(context, contentResolver, autoSendPushToken = false) + verify(exactly = 0) { queue.invoke() } + + listener.captured.onSuccess(JSONObject("""{"did":"SERVER_DID","seance":"SRV_SEANCE"}""")) + verifyOrder { + update.updateDid("SERVER_DID") + queue.invoke() + } + verify(exactly = 1) { queue.invoke() } + } + + @Test + fun `a new device whose init fails keeps the waiting requests queued`() { + every { get.getDid() } returns "" + val listener = slot() + every { network.get(eq("init"), any(), capture(listener)) } just Runs + + manager.initialize(context, contentResolver, autoSendPushToken = false) + listener.captured.onError(403, null) + + verify(exactly = 0) { queue.invoke() } + } + @Test fun `an existing install sends init for the popup and keeps its did and session`() { every { get.getDid() } returns "EXISTING_DID" From 397b98f75a15b1f52f2608c308b0ff203ece1c3c Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:02:36 +0400 Subject: [PATCH 011/109] build: minSdk 23 (Android 6.0) for the SDK and the demo app, as decision 0003 sets: current Firebase Messaging and media3 need it, and apps below it stay on the last release --- demo-app/build.gradle | 4 ++-- personalization-sdk/build.gradle | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/demo-app/build.gradle b/demo-app/build.gradle index 5c1d98ba..f1a075e2 100644 --- a/demo-app/build.gradle +++ b/demo-app/build.gradle @@ -38,8 +38,8 @@ android { defaultConfig { applicationId "com.personalization.demo" - // Compose requires 21. The SDK keeps minSdk 19 — it takes Compose as compileOnly. - minSdkVersion 21 + // The SDK's minimum. + minSdkVersion 23 targetSdkVersion 34 versionCode 1 versionName "1.0" diff --git a/personalization-sdk/build.gradle b/personalization-sdk/build.gradle index 04735bd5..b407288a 100644 --- a/personalization-sdk/build.gradle +++ b/personalization-sdk/build.gradle @@ -28,7 +28,8 @@ android { } } defaultConfig { - minSdkVersion 19 + // Android 6.0: what current Firebase Messaging and media3 need (decision 0003). + minSdkVersion 23 targetSdkVersion 34 buildConfigField "String", "VERSION_NAME", "\"" + version + "\"" multiDexEnabled true @@ -128,8 +129,7 @@ dependencies { // compileOnly mirrors the Huawei setup above: Compose stays off every consumer's classpath and // out of the published POM, so a View-based app pulls none of it. A host that wants the wrapper // already has Compose and declares it as `implementation` itself. compileOnly artifacts take no - // part in manifest merging either, which keeps the SDK's minSdk 19 intact even though Compose - // itself requires 21. + // part in manifest merging either. compileOnly platform('androidx.compose:compose-bom:2024.06.00') compileOnly 'androidx.compose.ui:ui' compileOnly 'androidx.compose.foundation:foundation' From 27dd53e9eb1f4514dc74f5153c466b4cc9699bbe Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:07:53 +0400 Subject: [PATCH 012/109] build: OkHttp 5.3 for the new core's client, with mockwebserver3 for its tests; its kotlin-stdlib 2.2 takes the SDK to Kotlin 2.1.21 and Dagger 2.57.2, whose processor reads that metadata, so apps need a Kotlin compiler of 2.1 or later; 5.4 and later are left out because they make apps compile against Android 36 or 37 --- build.gradle | 6 +++--- personalization-sdk/build.gradle | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 28f6e3ce..05bd3963 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '2.0.0' + ext.kotlin_version = '2.1.21' repositories { google() // Google's mirror of Maven Central first: JitPack builds get HTTP 429 from repo.maven.apache.org. @@ -19,8 +19,8 @@ buildscript { plugins { id 'ru.vyarus.pom' version '2.2.2' id "io.github.gradle-nexus.publish-plugin" version "2.0.0" - id 'org.jetbrains.kotlin.android' version '2.0.0' apply false - id 'org.jetbrains.kotlin.plugin.compose' version '2.0.0' apply false + id 'org.jetbrains.kotlin.android' version '2.1.21' apply false + id 'org.jetbrains.kotlin.plugin.compose' version '2.1.21' apply false } def versionFile = file("version.properties") diff --git a/personalization-sdk/build.gradle b/personalization-sdk/build.gradle index b407288a..d6a8b351 100644 --- a/personalization-sdk/build.gradle +++ b/personalization-sdk/build.gradle @@ -112,6 +112,10 @@ dependencies { // through lifecycle-process; declared because the SDK now relies on it directly. implementation 'androidx.startup:startup-runtime:1.1.1' implementation 'com.google.code.gson:gson:2.11.0' + // The new core's HTTP client (Rees46Config.coreV2): one connection pool for the process. 5.3 is the + // last line that compiles against any compileSdk: 5.4 asks apps for compileSdk 36, 5.5 for 37. Its + // kotlin-stdlib 2.2 is why apps and this SDK need a Kotlin compiler of 2.1 or later. + implementation 'com.squareup.okhttp3:okhttp:5.3.2' implementation 'org.jetbrains.kotlin:kotlin-stdlib' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4' @@ -137,7 +141,7 @@ dependencies { implementation 'com.github.bumptech.glide:glide:4.16.0' implementation 'androidx.media3:media3-exoplayer:1.4.1' implementation 'androidx.media3:media3-ui:1.4.1' - implementation 'com.google.dagger:dagger:2.51.1' + implementation 'com.google.dagger:dagger:2.57.2' // Instrumented tests only: as `implementation` they reached every host app's release build. androidTestImplementation("androidx.test:core:1.6.1") @@ -146,13 +150,14 @@ dependencies { implementation("com.google.android.gms:play-services-ads-identifier:17.0.0") - kapt 'com.google.dagger:dagger-compiler:2.48' + kapt 'com.google.dagger:dagger-compiler:2.57.2' testImplementation 'junit:junit:4.13.2' testImplementation 'io.mockk:mockk:1.13.11' testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4' testImplementation 'androidx.test:core:1.6.1' testImplementation 'org.robolectric:robolectric:4.12.1' + testImplementation 'com.squareup.okhttp3:mockwebserver3:5.3.2' // compileOnly dependencies are absent from the unit test classpath, and the Compose compiler // plugin refuses to compile the test variant without the runtime it can see. These mirror the // compileOnly declarations above and stay confined to tests, so the published POM is unaffected. From 45a33f66fe8692d29815b4b91b144672af9f03ee Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:11:33 +0400 Subject: [PATCH 013/109] refactor(core): the core's internal seams (clock, dispatchers, connectivity, app lifecycle) and their fakes move to com.personalization.core.internal, leaving com.personalization.core to the public types --- .../com/personalization/core/{ => internal}/AppLifecycle.kt | 2 +- .../kotlin/com/personalization/core/{ => internal}/Clock.kt | 2 +- .../personalization/core/{ => internal}/ConnectivityMonitor.kt | 2 +- .../com/personalization/core/{ => internal}/CoreDispatchers.kt | 2 +- .../com/personalization/core/{ => internal}/ContractFixtures.kt | 2 +- .../kotlin/com/personalization/core/{ => internal}/CoreFakes.kt | 2 +- .../com/personalization/core/{ => internal}/CoreHarnessTest.kt | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) rename personalization-sdk/src/main/kotlin/com/personalization/core/{ => internal}/AppLifecycle.kt (91%) rename personalization-sdk/src/main/kotlin/com/personalization/core/{ => internal}/Clock.kt (88%) rename personalization-sdk/src/main/kotlin/com/personalization/core/{ => internal}/ConnectivityMonitor.kt (90%) rename personalization-sdk/src/main/kotlin/com/personalization/core/{ => internal}/CoreDispatchers.kt (94%) rename personalization-sdk/src/test/kotlin/com/personalization/core/{ => internal}/ContractFixtures.kt (96%) rename personalization-sdk/src/test/kotlin/com/personalization/core/{ => internal}/CoreFakes.kt (98%) rename personalization-sdk/src/test/kotlin/com/personalization/core/{ => internal}/CoreHarnessTest.kt (98%) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/AppLifecycle.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/AppLifecycle.kt similarity index 91% rename from personalization-sdk/src/main/kotlin/com/personalization/core/AppLifecycle.kt rename to personalization-sdk/src/main/kotlin/com/personalization/core/internal/AppLifecycle.kt index 75f10855..94516ae4 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/AppLifecycle.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/AppLifecycle.kt @@ -1,4 +1,4 @@ -package com.personalization.core +package com.personalization.core.internal import com.personalization.Cancellable diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/Clock.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Clock.kt similarity index 88% rename from personalization-sdk/src/main/kotlin/com/personalization/core/Clock.kt rename to personalization-sdk/src/main/kotlin/com/personalization/core/internal/Clock.kt index a0edfa75..ffe6414b 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/Clock.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Clock.kt @@ -1,4 +1,4 @@ -package com.personalization.core +package com.personalization.core.internal /** * The time the core reads: the session window, retry pauses, the queue's limits, the source's diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/ConnectivityMonitor.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ConnectivityMonitor.kt similarity index 90% rename from personalization-sdk/src/main/kotlin/com/personalization/core/ConnectivityMonitor.kt rename to personalization-sdk/src/main/kotlin/com/personalization/core/internal/ConnectivityMonitor.kt index 56d73ce1..7428390c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/ConnectivityMonitor.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ConnectivityMonitor.kt @@ -1,4 +1,4 @@ -package com.personalization.core +package com.personalization.core.internal import com.personalization.Cancellable diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/CoreDispatchers.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreDispatchers.kt similarity index 94% rename from personalization-sdk/src/main/kotlin/com/personalization/core/CoreDispatchers.kt rename to personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreDispatchers.kt index 3fda6d25..30419a13 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/CoreDispatchers.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreDispatchers.kt @@ -1,4 +1,4 @@ -package com.personalization.core +package com.personalization.core.internal import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/ContractFixtures.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ContractFixtures.kt similarity index 96% rename from personalization-sdk/src/test/kotlin/com/personalization/core/ContractFixtures.kt rename to personalization-sdk/src/test/kotlin/com/personalization/core/internal/ContractFixtures.kt index ec6c8cdf..237f471f 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/ContractFixtures.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ContractFixtures.kt @@ -1,4 +1,4 @@ -package com.personalization.core +package com.personalization.core.internal import org.json.JSONArray import org.json.JSONObject diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/CoreFakes.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt similarity index 98% rename from personalization-sdk/src/test/kotlin/com/personalization/core/CoreFakes.kt rename to personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt index 24da3117..9f3056ce 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/CoreFakes.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt @@ -1,4 +1,4 @@ -package com.personalization.core +package com.personalization.core.internal import com.personalization.Cancellable import kotlinx.coroutines.ExperimentalCoroutinesApi diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/CoreHarnessTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHarnessTest.kt similarity index 98% rename from personalization-sdk/src/test/kotlin/com/personalization/core/CoreHarnessTest.kt rename to personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHarnessTest.kt index 78a2f8d4..d81227c3 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/CoreHarnessTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHarnessTest.kt @@ -1,4 +1,4 @@ -package com.personalization.core +package com.personalization.core.internal import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.launch From 5410800351c62c49100895af403cf899ead2f634 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:21:22 +0400 Subject: [PATCH 014/109] feat(core): SdkError, the failure type of the new API (notInitialized, shopBlocked, invalidArgument, network, timeout, http with the server's code, decoding, cancelled) with a log message each, SdkException for suspending calls, and one mapping to the old onError(code, msg): the HTTP code, 402 for a blocked shop, -1 without an answer --- .../com/personalization/core/SdkError.kt | 56 +++++++++++++++++++ .../core/internal/LegacyErrors.kt | 35 ++++++++++++ .../core/internal/LegacyErrorsTest.kt | 34 +++++++++++ 3 files changed, 125 insertions(+) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/SdkError.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyErrors.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/LegacyErrorsTest.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/SdkError.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/SdkError.kt new file mode 100644 index 00000000..046def93 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/SdkError.kt @@ -0,0 +1,56 @@ +package com.personalization.core + +/** + * Why a call of the SDK failed. Every failure reaches the app as one of these, never as an exception + * thrown into the app's code. + * + * Each error has a [message] for a log: English, without personal data and without the answer's body. + */ +sealed class SdkError { + + abstract val message: String + + /** The instance was not created or was released with `release()`. */ + data object NotInitialized : SdkError() { + override val message: String get() = "The SDK instance is not initialized or was released" + } + + /** The shop is refused: the server turned the SDK away. */ + data object ShopBlocked : SdkError() { + override val message: String get() = "The shop is blocked" + } + + /** A parameter of the call is wrong; [message] says which. */ + data class InvalidArgument(override val message: String) : SdkError() + + /** There is no network, or the connection was not established. */ + data object Network : SdkError() { + override val message: String get() = "No connection to the server" + } + + /** The answer did not come in time, or broke off in the middle. */ + data object Timeout : SdkError() { + override val message: String get() = "The answer did not come in time" + } + + /** + * The server answered outside `2xx` with [code]. [serverCode] is the machine-readable code of the + * answer, once the server sends one. + */ + data class Http(val code: Int, val serverCode: String? = null) : SdkError() { + override val message: String get() = "The server answered $code" + } + + /** A `2xx` answer that does not parse. */ + data object Decoding : SdkError() { + override val message: String get() = "The answer could not be read" + } + + /** The request was cancelled: the instance was released, or the app cancelled the call. */ + data object Cancelled : SdkError() { + override val message: String get() = "The request was cancelled" + } +} + +/** An [SdkError] as an exception, for the suspending methods. */ +class SdkException(val error: SdkError) : Exception(error.message) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyErrors.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyErrors.kt new file mode 100644 index 00000000..eee7837b --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyErrors.kt @@ -0,0 +1,35 @@ +package com.personalization.core.internal + +import com.personalization.core.SdkError + +/** What an old `OnApiCallbackListener.onError(code, msg)` receives. */ +internal data class LegacyError(val code: Int, val message: String?) + +/** + * The one mapping from [SdkError] to the `onError(Int, String?)` of the old methods, so they keep the + * codes apps know: the HTTP code for an answer outside `2xx`, `402` for a blocked shop, `-1` for a + * failure without an answer. + */ +internal object LegacyErrors { + + /** The code of a failure without an answer. */ + const val NO_ANSWER = -1 + + /** The code of a blocked shop: the server's own answer to a blocked shop. */ + const val SHOP_BLOCKED = 402 + + /** + * [errorBody] is the body of an answer outside `2xx`: the old callbacks got it as the message, and + * apps may show the server's text from it. It is never written to the log. + */ + fun of(error: SdkError, errorBody: String? = null): LegacyError = when (error) { + is SdkError.Http -> LegacyError(error.code, errorBody ?: error.message) + SdkError.ShopBlocked -> LegacyError(SHOP_BLOCKED, error.message) + SdkError.Network, + SdkError.Timeout, + SdkError.Decoding, + SdkError.NotInitialized, + SdkError.Cancelled, + is SdkError.InvalidArgument -> LegacyError(NO_ANSWER, error.message) + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/LegacyErrorsTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/LegacyErrorsTest.kt new file mode 100644 index 00000000..c9492718 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/LegacyErrorsTest.kt @@ -0,0 +1,34 @@ +package com.personalization.core.internal + +import com.personalization.core.SdkError +import org.junit.Assert.assertEquals +import org.junit.Test + +/** The old `onError(code, msg)` of every [SdkError]: the codes apps already handle. */ +class LegacyErrorsTest { + + @Test + fun `every error has the old code`() { + val expected = mapOf( + SdkError.Http(404) to 404, + SdkError.Http(500, serverCode = "internal") to 500, + SdkError.Network to -1, + SdkError.Timeout to -1, + SdkError.Decoding to -1, + SdkError.ShopBlocked to 402, + SdkError.NotInitialized to -1, + SdkError.Cancelled to -1, + SdkError.InvalidArgument("itemId is empty") to -1, + ) + + expected.forEach { (error, code) -> assertEquals(error.toString(), code, LegacyErrors.of(error).code) } + } + + @Test + fun `an http error gives the old callback the answer's body, the others their message`() { + assertEquals("""{"message":"no such card"}""", LegacyErrors.of(SdkError.Http(404), """{"message":"no such card"}""").message) + assertEquals(SdkError.Http(404).message, LegacyErrors.of(SdkError.Http(404)).message) + assertEquals(SdkError.Timeout.message, LegacyErrors.of(SdkError.Timeout).message) + assertEquals("itemId is empty", LegacyErrors.of(SdkError.InvalidArgument("itemId is empty")).message) + } +} From 7eddb7289de30bedbc5078e2c6b8f69065109f7c Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:22:46 +0400 Subject: [PATCH 015/109] feat(core): Rees46Config.coreV2 (off by default, one core per process: the first initialize or registerShops decides, a later instance asking for the other one logs core_flag_mismatch) and a Core per instance that the request funnel hands every request to; LegacyCore is today's path, CoreV2 sends through a new OkHttp client with a pool shared by the process: common fields in one place, 5 s to connect, 10 s to read and 5 s for /init, any 2xx a success and an empty one an empty object, a body that does not parse a decoding error, no answer network before the request reached the server and timeout after, a request without a network fails at once, release() cancels the instance's requests and fails later ones, no retries, method, path and code in the log; answers still reach the old callbacks as JSON, errors through the SdkError mapping --- .../src/main/AndroidManifest.xml | 2 + .../main/kotlin/com/personalization/Rees46.kt | 4 + .../com/personalization/Rees46Config.kt | 6 + .../main/kotlin/com/personalization/SDK.kt | 28 ++ .../internal/AndroidConnectivityMonitor.kt | 88 +++++ .../core/internal/CommonFields.kt | 36 ++ .../com/personalization/core/internal/Core.kt | 31 ++ .../core/internal/CoreHttpClient.kt | 290 ++++++++++++++ .../core/internal/CoreSelection.kt | 33 ++ .../personalization/core/internal/CoreV2.kt | 89 +++++ .../core/internal/HttpResult.kt | 18 + .../core/internal/LegacyCore.kt | 238 ++++++++++++ .../core/internal/LegacyDelivery.kt | 38 ++ .../network/NetworkRepositoryImpl.kt | 355 +++--------------- .../personalization/Rees46ConfigJavaTest.java | 10 + .../core/internal/CoreSelectionTest.kt | 61 +++ 16 files changed, 1016 insertions(+), 311 deletions(-) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/AndroidConnectivityMonitor.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/CommonFields.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreSelection.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/HttpResult.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyDelivery.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSelectionTest.kt diff --git a/personalization-sdk/src/main/AndroidManifest.xml b/personalization-sdk/src/main/AndroidManifest.xml index 48c9ef5e..8bb20826 100644 --- a/personalization-sdk/src/main/AndroidManifest.xml +++ b/personalization-sdk/src/main/AndroidManifest.xml @@ -3,6 +3,8 @@ + + + // A registration decides the process's core as an initialize does. + CoreSelection.resolve(config.coreV2, config.shopId) if (eagerInit) { initialize(appContext, config) } else { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt index d5bf3b54..88f4c1b2 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt @@ -18,6 +18,11 @@ package com.personalization * while no [SDK.popupPresentationListener] is set. * @property logLevel how much this shop's instance writes to the log; [LogLevel.WARNING] by default. * Messages that pass it also reach [Rees46.setLogHandler]. + * @property coreV2 runs the instance on the new core; off by default. The process has one core: the + * first [Rees46.initialize] or [Rees46.registerShops] decides it, and an instance that asks for the + * other one runs on the process's and logs `core_flag_mismatch`. So far the new core differs in its + * network client only: OkHttp, a request without a network fails at once, `release()` cancels the + * instance's requests. * * Constructors are overloaded for Java: every trailing parameter with a default may be left out, so * Java code written against the seven-argument constructor keeps compiling as parameters are added. @@ -32,4 +37,5 @@ data class Rees46Config @JvmOverloads constructor( val tag: String = "SDK", val enableAutoPopupPresentation: Boolean = true, val logLevel: LogLevel = LogLevel.WARNING, + val coreV2: Boolean = false, ) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 9d2d6896..d4fdddc6 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -25,6 +25,10 @@ import com.personalization.api.managers.SearchManager import com.personalization.api.managers.TrackEventManager import com.personalization.api.managers.TrackingApi import com.personalization.api.params.ProfileParams +import com.personalization.core.internal.AndroidConnectivityMonitor +import com.personalization.core.internal.Core +import com.personalization.core.internal.CoreSelection +import com.personalization.core.internal.CoreV2 import com.personalization.di.AppModule import com.personalization.di.DaggerSdkComponent import com.personalization.features.inAppNotification.impl.ForegroundActivity @@ -38,6 +42,7 @@ import com.personalization.sdk.domain.repositories.NPSRepository import com.personalization.sdk.domain.usecases.network.AddTaskToQueueUseCase import com.personalization.sdk.domain.usecases.network.InitNetworkUseCase import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase +import com.personalization.sdk.data.repositories.network.NetworkRepositoryImpl import com.personalization.sdk.domain.usecases.notification.GetAllNotificationsUseCase import com.personalization.sdk.domain.usecases.preferences.GetPreferencesValueUseCase import com.personalization.sdk.domain.usecases.preferences.InitPreferencesUseCase @@ -167,6 +172,14 @@ open class SDK { /** The level of this instance's log; [Rees46Config.logLevel] sets it for instances made through [Rees46]. */ internal var logLevel: LogLevel = LogLevel.WARNING + /** [Rees46Config.coreV2] of the instance; the process's core decides ([CoreSelection]). */ + internal var coreV2Requested: Boolean = false + + /** The instance's core, once it is initialized: the legacy one or [CoreV2]. */ + @Volatile + internal var core: Core? = null + private set + /** The shop this instance logs for, once it is initialized: its lines pass that shop's level. */ @Volatile internal var logShopId: String? = null @@ -201,6 +214,10 @@ open class SDK { @Inject lateinit var initNetworkUseCase: InitNetworkUseCase + /** The request funnel of this instance; its core is chosen at initialization. */ + @Inject + internal lateinit var networkRepository: NetworkRepositoryImpl + @Inject lateinit var getPreferencesValueUseCase: GetPreferencesValueUseCase @@ -270,6 +287,7 @@ open class SDK { internal fun initializeForPush(context: Context, config: Rees46Config) { enableAutoPopupPresentation = config.enableAutoPopupPresentation logLevel = config.logLevel + coreV2Requested = config.coreV2 initializeInternal( context = context, shopId = config.shopId, @@ -346,6 +364,11 @@ open class SDK { ) initNetworkUseCase(url = baseUrl) + core = if (CoreSelection.resolve(coreV2Requested, shopId)) { + networkRepository.useCoreV2(shopId, AndroidConnectivityMonitor(context)) + } else { + networkRepository.core + } // Popups v3 start from the /init answer; in their test mode /init counts test popups too. registerManager.onInitFinished = { flag -> @@ -644,9 +667,14 @@ open class SDK { * process-global messaging services. Call this for short-lived instances to avoid leaking * them through the active-instances registry. Apps that keep a single long-lived instance * for the whole process usually do not need it. + * + * On the new core ([Rees46Config.coreV2]) the instance's requests in flight are cancelled and + * end in `onError(-1)`, and so does any request made through it afterwards. */ fun release() { SdkRegistry.unregister(this) + // The new core cancels the instance's requests in flight; the legacy one cannot. + core?.release() // Only this instance's level goes: another live instance of the shop keeps logging at its own. logShopId?.let { SdkLogs.unregister(it, owner = this) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/AndroidConnectivityMonitor.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/AndroidConnectivityMonitor.kt new file mode 100644 index 00000000..fc4f2fc7 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/AndroidConnectivityMonitor.kt @@ -0,0 +1,88 @@ +package com.personalization.core.internal + +import android.content.Context +import android.net.ConnectivityManager +import android.net.Network +import android.net.NetworkCapabilities +import android.net.NetworkRequest +import com.personalization.Cancellable +import java.util.concurrent.CopyOnWriteArrayList + +/** + * The device's network as [ConnectivityManager] sees it. + * + * It answers "no network" only when it knows: without a default network, or one without internet. + * When it cannot tell (no permission, the system service fails), it answers "connected" and lets the + * request find out, so a doubt never fails a request that would have gone through. + */ +internal class AndroidConnectivityMonitor(context: Context) : ConnectivityMonitor { + + private val manager: ConnectivityManager? = context.getSystemService(ConnectivityManager::class.java) + private val listeners = CopyOnWriteArrayList<(Boolean) -> Unit>() + private val lock = Any() + private var callback: ConnectivityManager.NetworkCallback? = null + + @Volatile + private var lastKnown: Boolean? = null + + override val isConnected: Boolean + get() = try { + val manager = manager ?: return true + val network = manager.activeNetwork ?: return false + val capabilities = manager.getNetworkCapabilities(network) ?: return true + capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + } catch (e: RuntimeException) { + // SecurityException without ACCESS_NETWORK_STATE, or a failing system service. + true + } + + override fun addListener(listener: (connected: Boolean) -> Unit): Cancellable { + listeners += listener + startWatching() + return Cancellable { + listeners -= listener + if (listeners.isEmpty()) stopWatching() + } + } + + private fun startWatching(): Unit = synchronized(lock) { + val manager = manager ?: return + if (callback != null) return + lastKnown = isConnected + val watcher = object : ConnectivityManager.NetworkCallback() { + override fun onAvailable(network: Network) = changed() + override fun onLost(network: Network) = changed() + override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) = changed() + } + try { + val request = NetworkRequest.Builder().addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build() + manager.registerNetworkCallback(request, watcher) + callback = watcher + } catch (e: RuntimeException) { + // Without the permission there are no changes to hear; isConnected still answers. + } + } + + private fun stopWatching(): Unit = synchronized(lock) { + val watcher = callback ?: return + callback = null + try { + manager?.unregisterNetworkCallback(watcher) + } catch (e: RuntimeException) { + // Already unregistered. + } + } + + private fun changed() { + val connected = isConnected + if (lastKnown == connected) return + lastKnown = connected + listeners.forEach { listener -> + try { + listener(connected) + } catch (e: Exception) { + // A listener of the core; one failing must not keep the others from hearing. + } + } + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CommonFields.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CommonFields.kt new file mode 100644 index 00000000..b8fadef4 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CommonFields.kt @@ -0,0 +1,36 @@ +package com.personalization.core.internal + +import com.personalization.sdk.data.models.params.UserBasicParams +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import org.json.JSONObject + +/** + * The fields every request of a shop carries, added in one place for both cores: `shop_id`, `did`, + * `sid` and `seance` with one value (some endpoints read only the old name), `segment` (the A/B group) + * and `stream`. A `GET` sends them in the query, a `POST` in its JSON body. + * + * A clicked push is not among them: `from` and `code` on `/init` record one more click of the mailing + * on every launch, so the click travels as the `source` of `/push` events instead + * ([com.personalization.handlers.notifications.NotificationHandler]). + */ +internal object CommonFields { + + /** Adds the common fields to [params] and returns it. A non-blank `segment` or `stream` of the call wins. */ + fun addTo(params: JSONObject, settings: UserSettingsRepository): JSONObject { + val sid = settings.getSid() + params.put(UserBasicParams.SHOP_ID, settings.getShopId()) + params.put(UserBasicParams.DID, settings.getDid()) + params.put(UserBasicParams.SID, sid) + params.put(UserBasicParams.SEANCE, sid) + // Read even when the call has its own: the first read assigns the device's A/B group. + params.put(UserBasicParams.SEGMENT, ownOr(params, UserBasicParams.SEGMENT, settings.getSegmentForABTesting())) + params.put(UserBasicParams.STREAM, ownOr(params, UserBasicParams.STREAM, settings.getStream())) + return params + } + + /** The call's own non-blank [key] (a strict `trackPurchase` sets its own `segment`), otherwise [stored]. */ + private fun ownOr(params: JSONObject, key: String, stored: String): String { + if (!params.has(key) || params.isNull(key)) return stored + return params.get(key).toString().ifBlank { stored } + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt new file mode 100644 index 00000000..d6d94c19 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt @@ -0,0 +1,31 @@ +package com.personalization.core.internal + +import com.personalization.api.OnApiCallbackListener +import com.personalization.sdk.domain.models.NetworkMethod +import org.json.JSONObject + +/** + * The core of one SDK instance: how its requests travel. The request funnel + * ([com.personalization.sdk.data.repositories.network.NetworkRepositoryImpl], behind + * `SendNetworkMethodUseCase`) hands every request to the instance's core, so the namespaces are the + * same code for both cores. + * + * [LegacyCore] keeps today's paths. [CoreV2] is the new core, chosen by `Rees46Config.coreV2`; for now + * it has only the new client, while `/init`, the session and the requests waiting for a `did` stay the + * legacy ones for both cores. + */ +internal interface Core { + + /** Whether this is the new core. */ + val isV2: Boolean + + /** + * Sends one request. [params] carry the method's own fields; the core adds the common ones. The + * answer reaches [listener] once, the way the old methods get it: `onSuccess` with a `JSONObject` or + * `JSONArray`, or `onError(code, msg)`. + */ + fun send(method: NetworkMethod, params: JSONObject, listener: OnApiCallbackListener?) + + /** Stops the core: requests in flight are cancelled where the core can cancel them. */ + fun release() +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt new file mode 100644 index 00000000..5747878d --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt @@ -0,0 +1,290 @@ +package com.personalization.core.internal + +import com.personalization.LogLevel +import com.personalization.SDK +import com.personalization.core.SdkError +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import com.personalization.sdk.domain.models.NetworkMethod +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import kotlinx.coroutines.suspendCancellableCoroutine +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Dispatcher +import okhttp3.EventListener +import okhttp3.HttpUrl.Companion.toHttpUrl +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody +import okhttp3.Response +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject +import org.json.JSONTokener +import java.io.IOException +import java.net.InetSocketAddress +import java.net.Proxy +import java.util.concurrent.TimeUnit +import kotlin.coroutines.resume + +/** Timeouts of the new client, per request: one request's timeout never changes another's. */ +internal data class CoreTimeouts( + val connectMs: Long = 5_000, + val readMs: Long = 10_000, + val writeMs: Long = 10_000, + /** `/init` has its own, shorter read timeout: an instance waits for it before its first requests. */ + val initReadMs: Long = 5_000, +) + +/** + * What the process shares between the instances' clients: one connection pool and one dispatcher. + * + * The dispatcher lets 16 requests run at once to one host instead of OkHttp's 5: the old client had no + * limit, and a screen of recommendation blocks should not wait in line. OkHttp still recovers from a + * stale pooled connection by opening a new one, as `HttpURLConnection` does; that is not a retry of an + * answered request, which the client never makes. + */ +internal object CoreHttp { + private const val MAX_REQUESTS_PER_HOST = 16 + + val shared: OkHttpClient by lazy { + OkHttpClient.Builder() + .dispatcher(Dispatcher().apply { maxRequestsPerHost = MAX_REQUESTS_PER_HOST }) + .build() + } +} + +/** + * The new core's client: one per instance, over the process's [CoreHttp.shared] pool. It adds the + * common fields ([CommonFields]), sends, and classifies the outcome: + * - any `2xx` is a success; an empty body is an empty success; a body that is not a JSON object or + * array is `decoding`; + * - an answer outside `2xx` is `http(code)`, its body kept only for the old callbacks; + * - no answer is `network` when the request never reached the server (no network, no connection) and + * `timeout` once it did (the answer did not come in time or broke off midway); + * - a request cancelled with the instance is `cancelled`. + * + * Without a network a request fails at once with `network`. The client never retries; the log gets the + * method, the path and the code, never the query, a body or the answer. + */ +internal class CoreHttpClient( + private val baseUrl: () -> String, + private val settings: UserSettingsRepository, + private val connectivity: ConnectivityMonitor, + private val shopId: String?, + shared: OkHttpClient = CoreHttp.shared, + timeouts: CoreTimeouts = CoreTimeouts(), + private val clock: Clock = Clock.SYSTEM, +) { + + /** This instance's client; [OkHttpClient.newBuilder] keeps the shared pool and dispatcher. */ + internal val client: OkHttpClient = shared.newBuilder() + .connectTimeout(timeouts.connectMs, TimeUnit.MILLISECONDS) + .readTimeout(timeouts.readMs, TimeUnit.MILLISECONDS) + .writeTimeout(timeouts.writeMs, TimeUnit.MILLISECONDS) + .eventListenerFactory(PhaseListener) + .build() + + internal val initClient: OkHttpClient = client.newBuilder() + .readTimeout(timeouts.initReadMs, TimeUnit.MILLISECONDS) + .build() + + /** The client for [method]: `/init` reads with its own timeout. */ + internal fun clientFor(method: NetworkMethod): OkHttpClient = + if (method.method == INIT_PATH) initClient else client + + /** Sends [params] (the method's own fields) to [method]; cancelling the caller cancels the request. */ + suspend fun send(method: NetworkMethod, params: JSONObject): HttpResult { + if (!connectivity.isConnected) { + log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "${line(method)}: no network") { context(method, error = ERROR_NETWORK) } + return HttpResult.Failure(SdkError.Network) + } + val phase = Phase() + val built = try { + build(method, params, phase) + } catch (e: Exception) { + log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "${line(method)}: the request could not be built", e) { context(method) } + return HttpResult.Failure(SdkError.InvalidArgument("The request could not be built")) + } + val started = clock.nowMillis() + val exchange = clientFor(method).newCall(built.request).exchange(phase) + report(method, built, exchange, clock.nowMillis() - started) + return exchange.result + } + + private class Built(val request: Request, val body: String?) + + private fun build(method: NetworkMethod, params: JSONObject, phase: Phase): Built { + val fields = CommonFields.addTo(params, settings) + val url = (baseUrl() + method.method).toHttpUrl() + val builder = Request.Builder() + .header(USER_AGENT, SDK.userAgent()) + .tag(Phase::class.java, phase) + return when (method) { + is NetworkMethod.GET -> { + val query = url.newBuilder() + fields.keys().forEach { key -> query.addQueryParameter(key, fields.getString(key)) } + Built(builder.url(query.build()).get().build(), null) + } + is NetworkMethod.POST -> { + val body = fields.toString() + Built(builder.url(url).post(body.toRequestBody(JSON)).build(), body) + } + } + } + + /** The outcome of one exchange; the status and the texts are for the app's [com.personalization.NetworkLogger]. */ + private class Exchange( + val result: HttpResult, + val status: Int = NO_STATUS, + val responseText: String? = null, + val failure: IOException? = null, + ) + + private suspend fun Call.exchange(phase: Phase): Exchange = suspendCancellableCoroutine { continuation -> + continuation.invokeOnCancellation { cancel() } + enqueue(object : Callback { + override fun onFailure(call: Call, e: IOException) { + continuation.resume(Exchange(HttpResult.Failure(noAnswer(call, phase)), failure = e)) + } + + override fun onResponse(call: Call, response: Response) { + val exchange = try { + response.use { read(it) } + } catch (e: IOException) { + // The status came, the body broke off: no answer, not a success with part of a body. + Exchange(HttpResult.Failure(noAnswer(call, phase)), response.code, failure = e) + } catch (e: Exception) { + Exchange(HttpResult.Failure(SdkError.Decoding), response.code) + } + continuation.resume(exchange) + } + }) + } + + private fun read(response: Response): Exchange { + val status = response.code + if (status !in HTTP_SUCCESS) { + // The status is the answer: a body that fails to arrive does not replace it. + val errorBody = try { + response.body.string() + } catch (e: IOException) { + null + } + return Exchange(HttpResult.Failure(SdkError.Http(status), errorBody), status, errorBody) + } + val text = response.body.string() + if (text.isBlank()) return Exchange(HttpResult.Success(JSONObject()), status, text) + val json = try { + JSONTokener(text).nextValue() + } catch (e: JSONException) { + null + } + val result = if (json is JSONObject || json is JSONArray) HttpResult.Success(json) else HttpResult.Failure(SdkError.Decoding) + return Exchange(result, status, text) + } + + /** No answer: cancelled with the instance, `timeout` once the request reached the server, `network` before. */ + private fun noAnswer(call: Call, phase: Phase): SdkError = when { + call.isCanceled() -> SdkError.Cancelled + phase.requestSent -> SdkError.Timeout + else -> SdkError.Network + } + + /** Writes the outcome to the log by method, path and code, and hands the exchange to the app's network logger. */ + private fun report(method: NetworkMethod, built: Built, exchange: Exchange, durationMs: Long) { + val status = exchange.status.takeIf { it != NO_STATUS } + when (val result = exchange.result) { + is HttpResult.Success -> + log(LogLevel.DEBUG, LogCodes.REQUEST, "$status: ${line(method)}") { context(method, status) } + is HttpResult.Failure -> when (val error = result.error) { + is SdkError.Http -> + log(LogCodes.failureLevel(error.code), LogCodes.REQUEST_FAILED, "${error.code}: ${line(method)}") { context(method, status) } + SdkError.Decoding -> { + log(LogLevel.DEBUG, LogCodes.REQUEST, "$status: ${line(method)}") { context(method, status) } + log(LogLevel.WARNING, LogCodes.DECODING_FAILED, "The answer to ${line(method)} did not parse") { context(method, status) } + } + SdkError.Network, SdkError.Timeout -> { + val name = if (error == SdkError.Timeout) ERROR_TIMEOUT else ERROR_NETWORK + log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "${line(method)}: no answer", exchange.failure) { + context(method, status, name) + } + } + else -> Unit + } + } + val networkLogger = SDK.networkLogger ?: return + try { + networkLogger.onHttpCall( + method = method.type, + url = built.request.url.toString(), + requestBody = built.body, + responseCode = exchange.status, + responseBody = exchange.responseText, + durationMs = durationMs, + error = exchange.failure?.message + ) + } catch (e: Exception) { + log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "The app's NetworkLogger threw", e) { context(method) } + } + } + + /** Method and path only: the query and the body carry the user's data. */ + private fun line(method: NetworkMethod): String = "${method.type} ${method.method}" + + private fun context(method: NetworkMethod, status: Int? = null, error: String? = null): Map = + buildMap { + put("method", method.type) + put("path", method.method) + status?.let { put("status", it) } + error?.let { put("error", it) } + } + + /** The level is checked before the context is built. */ + private inline fun log( + level: LogLevel, + code: String, + message: String, + throwable: Throwable? = null, + context: () -> Map, + ) { + if (!SdkLogs.isLoggable(level, shopId)) return + SdkLogs.log(level, code, message, shopId, context(), throwable) + } + + internal companion object { + const val INIT_PATH = "init" + private const val USER_AGENT = "User-Agent" + private const val NO_STATUS = -1 + private const val ERROR_NETWORK = "network" + private const val ERROR_TIMEOUT = "timeout" + private val HTTP_SUCCESS = 200..299 + private val JSON = "application/json; charset=utf-8".toMediaType() + } +} + +/** Whether the request reached the server: a failure before it is `network`, after it `timeout`. */ +internal class Phase { + @Volatile + var requestSent: Boolean = false +} + +/** + * Follows each request of the core through its [Phase], found in the request's tags. A new connection + * attempt (OkHttp recovering from a stale pooled connection) starts the phase over. + */ +internal object PhaseListener : EventListener.Factory { + override fun create(call: Call): EventListener { + val phase = call.request().tag(Phase::class.java) ?: return EventListener.NONE + return object : EventListener() { + override fun connectStart(call: Call, inetSocketAddress: InetSocketAddress, proxy: Proxy) { + phase.requestSent = false + } + + override fun requestHeadersStart(call: Call) { + phase.requestSent = true + } + } + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreSelection.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreSelection.kt new file mode 100644 index 00000000..ef2fe2cf --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreSelection.kt @@ -0,0 +1,33 @@ +package com.personalization.core.internal + +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs + +/** + * Which core the process runs: decided by the first `initialize` or `register`, the same for every + * instance after it. An instance that asks for the other one gets the process's and a + * `core_flag_mismatch` warning: switching takes a new launch. + */ +internal object CoreSelection { + + private var coreV2: Boolean? = null + + /** The process's choice; the first call makes it from [requested]. */ + fun resolve(requested: Boolean, shopId: String?): Boolean { + val decided = synchronized(this) { coreV2 ?: requested.also { coreV2 = it } } + if (decided != requested) { + SdkLogs.log( + LogLevel.WARNING, + LogCodes.CORE_FLAG_MISMATCH, + "Rees46Config.coreV2 differs from the process's core; the instance runs on the process's", + shopId, + mapOf("requested" to requested, "process" to decided) + ) + } + return decided + } + + /** Test-only: the next call decides again. */ + internal fun reset() = synchronized(this) { coreV2 = null } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt new file mode 100644 index 00000000..9b6966a1 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt @@ -0,0 +1,89 @@ +package com.personalization.core.internal + +import com.personalization.LogLevel +import com.personalization.api.OnApiCallbackListener +import com.personalization.core.SdkError +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import com.personalization.sdk.domain.models.NetworkMethod +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.CoroutineExceptionHandler +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancel +import kotlinx.coroutines.launch +import org.json.JSONObject +import java.util.concurrent.atomic.AtomicBoolean + +/** + * The new core. For now it sends the instance's requests through [CoreHttpClient]; `/init`, the session + * and the requests waiting for a `did` stay the legacy ones until the core takes them over. + * + * Every request ends in exactly one callback, on a background thread as today: the answer, or the + * error mapped to the old `onError(code, msg)` by [LegacyErrors]. [release] cancels the requests in + * flight (`cancelled`), and a request made after it fails with `notInitialized`. + */ +internal class CoreV2( + private val client: CoreHttpClient, + private val shopId: String?, + dispatchers: CoreDispatchers = CoreDispatchers.DEFAULT, +) : Core { + + override val isV2: Boolean = true + + /** Nothing a request does may reach the app's process as an exception. */ + private val uncaught = CoroutineExceptionHandler { _, throwable -> + SdkLogs.log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "A request of the core failed unexpectedly", shopId, throwable = throwable) + } + + /** The instance's requests; [release] cancels them all. */ + private val requests = CoroutineScope(SupervisorJob() + dispatchers.io + uncaught) + + /** Answers that do not come from a running request: a cancelled one, one after [release]. */ + private val endings = CoroutineScope(SupervisorJob() + dispatchers.io + uncaught) + + private val delivery = LegacyDelivery { level, code, message, throwable -> + SdkLogs.log(level, code, message, shopId, throwable = throwable) + } + + @Volatile + private var released = false + + override fun send(method: NetworkMethod, params: JSONObject, listener: OnApiCallbackListener?) { + val answered = AtomicBoolean(false) + val answer = { result: HttpResult -> + if (answered.compareAndSet(false, true) && listener != null) deliver(listener, result) + } + if (released) { + endings.launch { answer(HttpResult.Failure(SdkError.NotInitialized)) } + return + } + val request = requests.launch { + val result = try { + client.send(method, params) + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + // The client turns every failure into a result; this is the net under it. + SdkLogs.log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "${method.type} ${method.method}: no answer", shopId, + mapOf("method" to method.type, "path" to method.method), e) + HttpResult.Failure(SdkError.Network) + } + answer(result) + } + // Cancelled before or while it ran: the request still gets its one answer. + request.invokeOnCompletion { cause -> + if (cause != null) endings.launch { answer(HttpResult.Failure(SdkError.Cancelled)) } + } + } + + override fun release() { + released = true + requests.cancel() + } + + private fun deliver(listener: OnApiCallbackListener, result: HttpResult) = when (result) { + is HttpResult.Success -> delivery.success(listener, result.body) + is HttpResult.Failure -> LegacyErrors.of(result.error, result.errorBody).let { delivery.failure(listener, it.code, it.message) } + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/HttpResult.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/HttpResult.kt new file mode 100644 index 00000000..dd3b6267 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/HttpResult.kt @@ -0,0 +1,18 @@ +package com.personalization.core.internal + +import com.personalization.core.SdkError +import org.json.JSONArray +import org.json.JSONObject + +/** The outcome of one request of the new client. */ +internal sealed interface HttpResult { + + /** A `2xx` answer: a [JSONObject] (empty for an empty answer) or a [JSONArray]. */ + class Success(val body: Any) : HttpResult + + /** + * [errorBody] is the body of an answer outside `2xx`, kept only for the old callbacks, which got it + * as their message. It is never written to the log. + */ + class Failure(val error: SdkError, val errorBody: String? = null) : HttpResult +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt new file mode 100644 index 00000000..f9fd12bb --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt @@ -0,0 +1,238 @@ +package com.personalization.core.internal + +import android.net.Uri +import com.personalization.LogLevel +import com.personalization.SDK +import com.personalization.api.OnApiCallbackListener +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import com.personalization.sdk.domain.models.NetworkMethod +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject +import org.json.JSONTokener +import java.io.BufferedReader +import java.io.BufferedWriter +import java.io.IOException +import java.io.InputStream +import java.io.InputStreamReader +import java.io.OutputStreamWriter +import java.net.ConnectException +import java.net.HttpURLConnection +import java.net.URL +import java.nio.charset.StandardCharsets + +/** + * Today's request path, unchanged: `HttpURLConnection` on a thread per request. Any `2xx` is a + * success, an empty answer or one that is not JSON succeeds with an empty object, no connection is + * `504` and a failure without an answer `-1`. It cannot cancel a request, so [release] does nothing. + */ +internal class LegacyCore( + private val settings: UserSettingsRepository, + private val baseUrl: () -> String, +) : Core { + + override val isV2: Boolean = false + + /** How long a request may wait for the next bytes of an answer; tests shorten it. */ + @Volatile + var readTimeoutMs: Int = READ_TIMEOUT_MS + + private val delivery = LegacyDelivery { level, code, message, throwable -> log(level, code, message, throwable = throwable) } + + override fun send(method: NetworkMethod, params: JSONObject, listener: OnApiCallbackListener?) { + try { + executeMethod(method, CommonFields.addTo(params, settings), listener) + } catch (e: JSONException) { + log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "A request could not be built", method, throwable = e) + } + } + + override fun release() = Unit + + private fun executeMethod( + networkMethod: NetworkMethod, + params: JSONObject, + listener: OnApiCallbackListener? + ) { + val thread = Thread { + val startTime = System.currentTimeMillis() + val requestBodyForLog = if (networkMethod is NetworkMethod.POST) params.toString() else null + var logUrl = "" + var responseCode = -1 + var responseBodyForLog: String? = null + var errorForLog: String? = null + var connection: HttpURLConnection? = null + // The outcome is decided first and delivered once the connection is done with, so a + // callback that throws cannot change it. + val outcome: Outcome = try { + logUrl = baseUrl() + networkMethod.method + val buildUri = build(networkMethod, params) + val url = getUrl(networkMethod, buildUri) + logUrl = url.toString() + connection = getConnection(networkMethod, url, params) + + connection.connect() + responseCode = connection.responseCode + + // Method and path only: the query string and the body carry the user's data. + log(LogLevel.DEBUG, LogCodes.REQUEST, "Request answered", networkMethod, responseCode) + + if (responseCode in HTTP_SUCCESS) { + // A body the read timeout cuts short throws here and ends in onError. + val responseText = readStream(connection.inputStream) + responseBodyForLog = responseText + successOutcome(responseText) + } else { + // Below 400 there is no error stream: a redirect the connection did not follow. + // The status is the answer, so a body that fails to arrive does not replace it. + val error = try { + connection.errorStream?.let(::readStream).orEmpty() + } catch (e: IOException) { + "" + } + responseBodyForLog = error + // Without the body: an error answer often repeats the request's data. + log(LogCodes.failureLevel(responseCode), LogCodes.REQUEST_FAILED, "Request failed", networkMethod, responseCode) + Outcome.Failure(responseCode, error) + } + } catch (e: ConnectException) { + log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "No connection to the server", networkMethod, throwable = e) + errorForLog = e.message + responseCode = 504 + Outcome.Failure(504, e.message) + } catch (e: Exception) { + log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "Request ended without an answer", networkMethod, throwable = e) + errorForLog = e.message + Outcome.Failure(-1, e.message) + } finally { + connection?.disconnect() + try { + SDK.networkLogger?.onHttpCall( + method = networkMethod.type, + url = logUrl, + requestBody = requestBodyForLog, + responseCode = responseCode, + responseBody = responseBodyForLog, + durationMs = System.currentTimeMillis() - startTime, + error = errorForLog + ) + } catch (e: Exception) { + log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "The app's NetworkLogger threw", networkMethod, throwable = e) + } + } + + if (listener != null) { + when (outcome) { + is Outcome.Success -> delivery.success(listener, outcome.body) + is Outcome.Failure -> delivery.failure(listener, outcome.code, outcome.message) + } + } + } + + thread.start() + } + + private sealed interface Outcome { + /** [body] is a [JSONObject] or a [JSONArray]. */ + class Success(val body: Any) : Outcome + class Failure(val code: Int, val message: String?) : Outcome + } + + /** Any 2xx is a success; an empty answer or one that is not JSON succeeds with an empty object. */ + private fun successOutcome(responseText: String): Outcome { + val json = try { + if (responseText.isEmpty()) null else JSONTokener(responseText).nextValue() + } catch (e: JSONException) { + null + } + return Outcome.Success(if (json is JSONObject || json is JSONArray) json else JSONObject()) + } + + private fun build( + networkMethod: NetworkMethod, + params: JSONObject + ): Uri { + + val builder = Uri.parse(baseUrl() + networkMethod.method).buildUpon() + + val it = params.keys() + while (it.hasNext()) { + val key = it.next() + builder.appendQueryParameter(key, params.getString(key)) + } + + return builder.build() + } + + private fun getUrl( + networkMethod: NetworkMethod, + buildUri: Uri + ): URL { + + return if (networkMethod is NetworkMethod.POST) { + URL(baseUrl() + networkMethod.method) + } else { + URL(buildUri.toString()) + } + } + + private fun getConnection( + networkMethod: NetworkMethod, + url: URL, + params: JSONObject + ): HttpURLConnection { + val connection = url.openConnection() as HttpURLConnection + connection.setRequestProperty("User-Agent", SDK.userAgent()) + connection.requestMethod = networkMethod.type + connection.connectTimeout = CONNECT_TIMEOUT_MS + connection.readTimeout = readTimeoutMs + + if (networkMethod is NetworkMethod.POST) { + connection.setRequestProperty("Content-Type", "application/json") + connection.doOutput = true + connection.doInput = true + val os = + BufferedWriter(OutputStreamWriter(connection.outputStream, StandardCharsets.UTF_8)) + os.write(params.toString()) + os.flush() + os.close() + } + + return connection + } + + /** The whole answer, its lines joined; a read that fails midway throws instead of returning a part. */ + private fun readStream(inputStream: InputStream): String = + BufferedReader(InputStreamReader(inputStream)).use { reader -> + buildString { reader.forEachLine { append(it) } } + } + + /** + * Writes to the log by method and path only: the query and the body carry the user's data. The + * level is checked first, so a line no shop writes reads no preferences and builds nothing. + */ + private fun log( + level: LogLevel, + code: String, + message: String, + method: NetworkMethod? = null, + status: Int? = null, + throwable: Throwable? = null, + ) { + if (!SdkLogs.isLoggable(level)) return + val context = buildMap { + method?.let { put("method", it.type); put("path", it.method) } + status?.let { put("status", it) } + } + val shopId = runCatching { settings.getShopId() }.getOrNull()?.takeIf { it.isNotEmpty() } + SdkLogs.log(level, code, message, shopId, context, throwable) + } + + private companion object { + const val CONNECT_TIMEOUT_MS = 5_000 + const val READ_TIMEOUT_MS = 10_000 + val HTTP_SUCCESS = 200..299 + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyDelivery.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyDelivery.kt new file mode 100644 index 00000000..5a9f91aa --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyDelivery.kt @@ -0,0 +1,38 @@ +package com.personalization.core.internal + +import com.personalization.LogLevel +import com.personalization.api.OnApiCallbackListener +import com.personalization.logs.LogCodes +import org.json.JSONArray +import org.json.JSONObject + +/** + * Hands an answer to a listener of the old methods. A callback that throws is logged and goes no + * further. A failed `onSuccess` ends in one `onError(-1)`: the SDK's own listeners parse the answer + * there and hand the model to the app, so without it a parse error would leave the app waiting. + */ +internal class LegacyDelivery( + private val log: (level: LogLevel, code: String, message: String, throwable: Throwable?) -> Unit, +) { + + /** [body] is a [JSONObject] or a [JSONArray]. */ + fun success(listener: OnApiCallbackListener, body: Any) { + try { + when (body) { + is JSONArray -> listener.onSuccess(body) + else -> listener.onSuccess(body as? JSONObject) + } + } catch (e: Exception) { + log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "onSuccess of a request threw", e) + failure(listener, LegacyErrors.NO_ANSWER, e.message) + } + } + + fun failure(listener: OnApiCallbackListener, code: Int, message: String?) { + try { + listener.onError(code, message) + } catch (e: Exception) { + log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "onError of a request threw", e) + } + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt index 7073c44e..1128a291 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt @@ -1,40 +1,43 @@ package com.personalization.sdk.data.repositories.network -import com.personalization.LogLevel -import com.personalization.logs.LogCodes -import com.personalization.logs.SdkLogs -import android.net.Uri -import com.personalization.SDK import com.personalization.api.OnApiCallbackListener -import com.personalization.sdk.data.models.params.UserBasicParams -import com.personalization.sdk.data.utils.QueryParamsUtils.addMultipleParams +import com.personalization.core.internal.ConnectivityMonitor +import com.personalization.core.internal.Core +import com.personalization.core.internal.CoreDispatchers +import com.personalization.core.internal.CoreHttpClient +import com.personalization.core.internal.CoreV2 +import com.personalization.core.internal.LegacyCore import com.personalization.sdk.domain.models.NetworkMethod import com.personalization.sdk.domain.repositories.NetworkRepository import com.personalization.sdk.domain.repositories.UserSettingsRepository -import org.json.JSONArray -import org.json.JSONException import org.json.JSONObject -import org.json.JSONTokener -import java.io.BufferedReader -import java.io.BufferedWriter -import java.io.IOException -import java.io.InputStream -import java.io.InputStreamReader -import java.io.OutputStreamWriter -import java.net.ConnectException -import java.net.HttpURLConnection -import java.net.URL -import java.nio.charset.StandardCharsets import javax.inject.Inject +import javax.inject.Singleton +/** + * The funnel every request of an instance goes through: the namespaces call it, it holds the requests + * that wait for a `did` and hands each request to the instance's [Core], legacy or new. + */ +@Singleton class NetworkRepositoryImpl @Inject constructor( private val userSettingsRepository: UserSettingsRepository, ) : NetworkRepository { private lateinit var baseUrl: String - /** How long a request may wait for the next bytes of an answer; tests shorten it. */ - internal var readTimeoutMs: Int = READ_TIMEOUT_MS + private val legacyCore = LegacyCore(userSettingsRepository) { baseUrl } + + /** The core the requests go to: the legacy one until [useCoreV2]. */ + @Volatile + internal var core: Core = legacyCore + private set + + /** How long a legacy request may wait for the next bytes of an answer; tests shorten it. */ + internal var readTimeoutMs: Int + get() = legacyCore.readTimeoutMs + set(value) { + legacyCore.readTimeoutMs = value + } /** * Requests made before the device has a `did`. Guarded by [queueLock] together with the `did` @@ -50,6 +53,22 @@ class NetworkRepositoryImpl @Inject constructor( this.baseUrl = baseUrl } + /** + * Sends this instance's requests through the new client from now on. `/init`, the session and the + * requests waiting for a `did` keep the legacy behaviour until the core takes them over. + */ + internal fun useCoreV2( + shopId: String, + connectivity: ConnectivityMonitor, + dispatchers: CoreDispatchers = CoreDispatchers.DEFAULT, + client: CoreHttpClient = CoreHttpClient( + baseUrl = { baseUrl }, + settings = userSettingsRepository, + connectivity = connectivity, + shopId = shopId, + ), + ): Core = CoreV2(client, shopId, dispatchers).also { core = it } + override fun post( method: String, params: JSONObject, @@ -81,13 +100,7 @@ class NetworkRepositoryImpl @Inject constructor( params: JSONObject, listener: OnApiCallbackListener? ) { - sendAsync { - sendSecretMethod( - networkMethod = NetworkMethod.POST(method), - params = params, - listener = listener - ) - } + postAsync(method, params, listener) } override fun get( @@ -121,13 +134,7 @@ class NetworkRepositoryImpl @Inject constructor( params: JSONObject, listener: OnApiCallbackListener? ) { - sendAsync { - sendSecretMethod( - networkMethod = NetworkMethod.GET(method), - params = params, - listener = listener - ) - } + getAsync(method, params, listener) } private fun sendAsync(sendFunction: () -> Unit) { @@ -154,286 +161,12 @@ class NetworkRepositoryImpl @Inject constructor( synchronized(queueLock) { queue.add(thread) } } - private fun sendSecretMethod( - networkMethod: NetworkMethod, - params: JSONObject, - listener: OnApiCallbackListener? - ) { - sendMethod( - networkMethod = networkMethod, - params = params, - listener = listener, - ) - } - private fun sendMethod( networkMethod: NetworkMethod, params: JSONObject, listener: OnApiCallbackListener?, ) { userSettingsRepository.updateSidLastActTime() - - try { - val newParams = addBasicQueryParams(params = params) - - executeMethod(networkMethod, newParams, listener) - } catch (e: JSONException) { - log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "A request could not be built", networkMethod, throwable = e) - } - } - - /** - * The common fields of every request. A clicked push is not among them: `from` and `code` on - * `/init` record one more click of the mailing on every launch, so the click travels as the - * `source` of `/push` events instead ([com.personalization.handlers.notifications.NotificationHandler]). - */ - private fun addBasicQueryParams(params: JSONObject): JSONObject { - addMultipleParams( - params = params, - paramsToAdd = mapOf( - UserBasicParams.SHOP_ID to userSettingsRepository.getShopId(), - UserBasicParams.DID to userSettingsRepository.getDid(), - UserBasicParams.SID to userSettingsRepository.getSid(), - UserBasicParams.SEANCE to userSettingsRepository.getSid(), - UserBasicParams.SEGMENT to resolveClientOrRepoString( - params = params, - key = UserBasicParams.SEGMENT, - repoValue = userSettingsRepository.getSegmentForABTesting(), - ), - UserBasicParams.STREAM to resolveClientOrRepoString( - params = params, - key = UserBasicParams.STREAM, - repoValue = userSettingsRepository.getStream(), - ), - ) - ) - return params - } - - /** - * If the request body already contains a non-blank [key] (e.g. strict `trackPurchase` override), keep it; - * otherwise use [repoValue]. - */ - private fun resolveClientOrRepoString( - params: JSONObject, - key: String, - repoValue: String, - ): String { - if (!params.has(key) || params.isNull(key)) { - return repoValue - } - val raw = params.get(key) - val asString = when (raw) { - is String -> raw - else -> raw.toString() - } - return asString.ifBlank { repoValue } - } - - private fun executeMethod( - networkMethod: NetworkMethod, - params: JSONObject, - listener: OnApiCallbackListener? - ) { - val thread = Thread { - val startTime = System.currentTimeMillis() - val requestBodyForLog = if (networkMethod is NetworkMethod.POST) params.toString() else null - var logUrl = baseUrl + networkMethod.method - var responseCode = -1 - var responseBodyForLog: String? = null - var errorForLog: String? = null - var connection: HttpURLConnection? = null - // The outcome is decided first and delivered once the connection is done with, so a - // callback that throws cannot change it. - val outcome: Outcome = try { - val buildUri = build(networkMethod, params) - val url = getUrl(networkMethod, buildUri) - logUrl = url.toString() - connection = getConnection(networkMethod, url, params) - - connection.connect() - responseCode = connection.responseCode - - // Method and path only: the query string and the body carry the user's data. - log(LogLevel.DEBUG, LogCodes.REQUEST, "Request answered", networkMethod, responseCode) - - if (responseCode in HTTP_SUCCESS) { - // A body the read timeout cuts short throws here and ends in onError. - val responseText = readStream(connection.inputStream) - responseBodyForLog = responseText - successOutcome(responseText) - } else { - // Below 400 there is no error stream: a redirect the connection did not follow. - // The status is the answer, so a body that fails to arrive does not replace it. - val error = try { - connection.errorStream?.let(::readStream).orEmpty() - } catch (e: IOException) { - "" - } - responseBodyForLog = error - // Without the body: an error answer often repeats the request's data. - log(LogCodes.failureLevel(responseCode), LogCodes.REQUEST_FAILED, "Request failed", networkMethod, responseCode) - Outcome.Failure(responseCode, error) - } - } catch (e: ConnectException) { - log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "No connection to the server", networkMethod, throwable = e) - errorForLog = e.message - responseCode = 504 - Outcome.Failure(504, e.message) - } catch (e: Exception) { - log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "Request ended without an answer", networkMethod, throwable = e) - errorForLog = e.message - Outcome.Failure(-1, e.message) - } finally { - connection?.disconnect() - try { - SDK.networkLogger?.onHttpCall( - method = networkMethod.type, - url = logUrl, - requestBody = requestBodyForLog, - responseCode = responseCode, - responseBody = responseBodyForLog, - durationMs = System.currentTimeMillis() - startTime, - error = errorForLog - ) - } catch (e: Exception) { - log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "The app's NetworkLogger threw", networkMethod, throwable = e) - } - } - - listener?.let { deliver(it, outcome) } - } - - thread.start() - } - - private sealed interface Outcome { - /** [body] is a [JSONObject] or a [JSONArray]. */ - class Success(val body: Any) : Outcome - class Failure(val code: Int, val message: String?) : Outcome - } - - /** Any 2xx is a success; an empty answer or one that is not JSON succeeds with an empty object. */ - private fun successOutcome(responseText: String): Outcome { - val json = try { - if (responseText.isEmpty()) null else JSONTokener(responseText).nextValue() - } catch (e: JSONException) { - null - } - return Outcome.Success(if (json is JSONObject || json is JSONArray) json else JSONObject()) - } - - /** - * A callback that throws is logged and must not take down the SDK's network thread. A failed - * onSuccess ends in one onError(-1): the SDK's own listeners parse the answer there and hand - * the model to the app, so without it a parse error would leave the app waiting for an answer. - */ - private fun deliver(listener: OnApiCallbackListener, outcome: Outcome) { - when (outcome) { - is Outcome.Success -> try { - when (val body = outcome.body) { - is JSONArray -> listener.onSuccess(body) - else -> listener.onSuccess(body as JSONObject) - } - } catch (e: Exception) { - log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "onSuccess of a request threw", throwable = e) - fail(listener, -1, e.message) - } - is Outcome.Failure -> fail(listener, outcome.code, outcome.message) - } - } - - private fun fail(listener: OnApiCallbackListener, code: Int, message: String?) { - try { - listener.onError(code, message) - } catch (e: Exception) { - log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "onError of a request threw", throwable = e) - } - } - - private fun build( - networkMethod: NetworkMethod, - params: JSONObject - ): Uri { - - val builder = Uri.parse(baseUrl + networkMethod.method).buildUpon() - - val it = params.keys() - while (it.hasNext()) { - val key = it.next() - builder.appendQueryParameter(key, params.getString(key)) - } - - return builder.build() - } - - private fun getUrl( - networkMethod: NetworkMethod, - buildUri: Uri - ): URL { - - return if (networkMethod is NetworkMethod.POST) { - URL(baseUrl + networkMethod.method) - } else { - URL(buildUri.toString()) - } - } - - private fun getConnection( - networkMethod: NetworkMethod, - url: URL, - params: JSONObject - ): HttpURLConnection { - val connection = url.openConnection() as HttpURLConnection - connection.setRequestProperty("User-Agent", SDK.userAgent()) - connection.requestMethod = networkMethod.type - connection.connectTimeout = CONNECT_TIMEOUT_MS - connection.readTimeout = readTimeoutMs - - if (networkMethod is NetworkMethod.POST) { - connection.setRequestProperty("Content-Type", "application/json") - connection.doOutput = true - connection.doInput = true - val os = - BufferedWriter(OutputStreamWriter(connection.outputStream, StandardCharsets.UTF_8)) - os.write(params.toString()) - os.flush() - os.close() - } - - return connection - } - - /** The whole answer, its lines joined; a read that fails midway throws instead of returning a part. */ - private fun readStream(inputStream: InputStream): String = - BufferedReader(InputStreamReader(inputStream)).use { reader -> - buildString { reader.forEachLine { append(it) } } - } - - /** - * Writes to the log by method and path only: the query and the body carry the user's data. The - * level is checked first, so a line no shop writes reads no preferences and builds nothing. - */ - private fun log( - level: LogLevel, - code: String, - message: String, - method: NetworkMethod? = null, - status: Int? = null, - throwable: Throwable? = null, - ) { - if (!SdkLogs.isLoggable(level)) return - val context = buildMap { - method?.let { put("method", it.type); put("path", it.method) } - status?.let { put("status", it) } - } - val shopId = runCatching { userSettingsRepository.getShopId() }.getOrNull()?.takeIf { it.isNotEmpty() } - SdkLogs.log(level, code, message, shopId, context, throwable) - } - - private companion object { - const val CONNECT_TIMEOUT_MS = 5_000 - const val READ_TIMEOUT_MS = 10_000 - val HTTP_SUCCESS = 200..299 + core.send(networkMethod, params, listener) } } diff --git a/personalization-sdk/src/test/java/com/personalization/Rees46ConfigJavaTest.java b/personalization-sdk/src/test/java/com/personalization/Rees46ConfigJavaTest.java index 9e4834fc..7983a173 100644 --- a/personalization-sdk/src/test/java/com/personalization/Rees46ConfigJavaTest.java +++ b/personalization-sdk/src/test/java/com/personalization/Rees46ConfigJavaTest.java @@ -1,6 +1,7 @@ package com.personalization; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Test; @@ -26,5 +27,14 @@ public void trailingDefaultsMayBeLeftOut() { assertEquals("api.rees46.ru", config.getApiDomain()); assertEquals("SDK", config.getTag()); + assertFalse(config.getCoreV2()); + } + + @Test + public void theNewCoreIsTheLastParameter() { + Rees46Config config = new Rees46Config( + "shop", "api.rees46.ru", "android", true, false, true, "SDK", true, LogLevel.WARNING, true); + + assertTrue(config.getCoreV2()); } } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSelectionTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSelectionTest.kt new file mode 100644 index 00000000..3a910a18 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSelectionTest.kt @@ -0,0 +1,61 @@ +package com.personalization.core.internal + +import com.personalization.LogLevel +import com.personalization.Rees46 +import com.personalization.SdkLogMessage +import com.personalization.logs.SdkLogs +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.Executor + +/** One core for the process: the first instance decides, the others follow and are told. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class CoreSelectionTest { + + private val received = CopyOnWriteArrayList() + private lateinit var previousExecutor: Executor + + @Before + fun setUp() { + CoreSelection.reset() + SdkLogs.reset() + previousExecutor = SdkLogs.handlerExecutor + SdkLogs.handlerExecutor = Executor { it.run() } + Rees46.setLogHandler { received += it } + } + + @After + fun tearDown() { + CoreSelection.reset() + SdkLogs.reset() + SdkLogs.handlerExecutor = previousExecutor + } + + @Test + fun `the first instance decides the core of the process`() { + assertTrue(CoreSelection.resolve(requested = true, shopId = "a")) + assertTrue(CoreSelection.resolve(requested = true, shopId = "b")) + assertTrue(received.isEmpty()) + } + + @Test + fun `an instance that asks for the other core gets the process's and a warning`() { + assertFalse(CoreSelection.resolve(requested = false, shopId = "a")) + + assertFalse(CoreSelection.resolve(requested = true, shopId = "b")) + + val warning = received.single() + assertEquals("core_flag_mismatch" to LogLevel.WARNING, warning.code to warning.level) + assertEquals("b", warning.shopId) + assertEquals(mapOf("requested" to true, "process" to false), warning.context) + } +} From cc542a89c9b391557ee75d2c917f1a364a46453c Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:22:50 +0400 Subject: [PATCH 016/109] test(core): the new client against mockwebserver3: the classification of 2xx, empty, unparsable and non-2xx answers, a body cut midway, the read timeouts of requests and /init, a refused connection, no network, no retries, the common fields of GET and POST, a log without data, cancel on release, the funnel switching cores, the did-gated queue on the new core and an instance made with coreV2 --- .../AndroidConnectivityMonitorTest.kt | 46 ++++ .../core/internal/CoreFunnelTest.kt | 158 +++++++++++ .../core/internal/CoreHttpClientTest.kt | 253 ++++++++++++++++++ .../core/internal/CoreV2Test.kt | 143 ++++++++++ 4 files changed, 600 insertions(+) create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/AndroidConnectivityMonitorTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/AndroidConnectivityMonitorTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/AndroidConnectivityMonitorTest.kt new file mode 100644 index 00000000..c43db9d2 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/AndroidConnectivityMonitorTest.kt @@ -0,0 +1,46 @@ +package com.personalization.core.internal + +import android.net.ConnectivityManager +import android.net.NetworkCapabilities +import androidx.test.core.app.ApplicationProvider +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.RobolectricTestRunner +import org.robolectric.shadows.ShadowNetworkCapabilities + +/** "No network" only when the system says so: no default network, or one without internet. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class AndroidConnectivityMonitorTest { + + private val context = ApplicationProvider.getApplicationContext() + private val manager = context.getSystemService(ConnectivityManager::class.java) + + @Test + fun `a default network with internet is connected`() { + val capabilities = ShadowNetworkCapabilities.newInstance() + shadowOf(capabilities).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + shadowOf(manager).setNetworkCapabilities(manager.activeNetwork, capabilities) + + assertTrue(AndroidConnectivityMonitor(context).isConnected) + } + + @Test + fun `a default network without internet is not`() { + shadowOf(manager).setNetworkCapabilities(manager.activeNetwork, ShadowNetworkCapabilities.newInstance()) + + assertFalse(AndroidConnectivityMonitor(context).isConnected) + } + + @Test + fun `no default network is not connected`() { + shadowOf(manager).setDefaultNetworkActive(false) + shadowOf(manager).setActiveNetworkInfo(null) + + assertFalse(AndroidConnectivityMonitor(context).isConnected) + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt new file mode 100644 index 00000000..426df2d1 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt @@ -0,0 +1,158 @@ +package com.personalization.core.internal + +import android.net.ConnectivityManager +import android.net.NetworkCapabilities +import androidx.test.core.app.ApplicationProvider +import com.personalization.Rees46 +import com.personalization.Rees46Config +import com.personalization.SDK +import com.personalization.SdkRegistry +import com.personalization.api.OnApiCallbackListener +import com.personalization.sdk.data.repositories.network.NetworkRepositoryImpl +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import io.mockk.every +import io.mockk.mockk +import mockwebserver3.MockResponse +import mockwebserver3.MockWebServer +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.ShadowNetworkCapabilities +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicReference + +/** + * The request funnel asks the instance's core: the legacy one by default, the new client once the + * instance runs on [CoreV2]. The did-gated queue stays the legacy one for both. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class CoreFunnelTest { + + private val server = MockWebServer() + private val did = AtomicReference("did-1") + private val settings = mockk(relaxed = true) { + every { getShopId() } returns "shop" + every { getDid() } answers { did.get() } + every { getSid() } returns "sid-1" + every { getSegmentForABTesting() } returns "A" + every { getStream() } returns "android" + } + private val repository = NetworkRepositoryImpl(settings) + + @Before + fun setUp() { + server.start() + repository.initialize(server.url("/").toString()) + CoreSelection.reset() + SdkRegistry.reset() + Rees46.reset() + } + + @After + fun tearDown() { + repository.core.release() + server.close() + CoreSelection.reset() + SdkRegistry.reset() + Rees46.reset() + } + + @Test + fun `the funnel sends through the legacy core until the instance takes the new one`() { + assertFalse(repository.core.isV2) + server.enqueue(MockResponse.Builder().code(200).body("{}").build()) + await { repository.get("products/get", JSONObject(), it) } + // HttpURLConnection on the JVM asks for no compression; OkHttp asks for gzip. + assertEquals(null, server.takeRequest().headers["Accept-Encoding"]) + + repository.useCoreV2("shop", FakeConnectivity(connected = true)) + assertTrue(repository.core.isV2) + server.enqueue(MockResponse.Builder().code(200).body("{}").build()) + await { repository.get("products/get", JSONObject(), it) } + assertEquals("gzip", server.takeRequest().headers["Accept-Encoding"]) + } + + @Test + fun `requests waiting for a did go through the new core once it comes`() { + repository.useCoreV2("shop", FakeConnectivity(connected = true)) + did.set("") + server.enqueue(MockResponse.Builder().code(200).body("{}").build()) + val answered = CountDownLatch(1) + repository.getAsync("products/get", JSONObject(), object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) = answered.countDown() + }) + Thread.sleep(200) + assertEquals(0, server.requestCount) + + did.set("did-2") + repository.executeQueueTasks() + + assertTrue(answered.await(5, TimeUnit.SECONDS)) + assertEquals("did-2", server.takeRequest().url.queryParameter("did")) + } + + @Test + fun `an instance made with coreV2 runs on the new core, its requests on the new client`() { + server.enqueue(MockResponse.Builder().code(200).body("""{"did":"server-did","seance":"S"}""").build()) + val context = ApplicationProvider.getApplicationContext() + // Robolectric's default network has no internet capability: give it one, as a device's has. + val manager = context.getSystemService(ConnectivityManager::class.java) + val capabilities = ShadowNetworkCapabilities.newInstance() + shadowOf(capabilities).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + shadowOf(manager).setNetworkCapabilities(manager.activeNetwork, capabilities) + + val sdk = Rees46.initialize( + context, + Rees46Config( + shopId = "core-shop", + apiDomain = server.url("/").toString().trimEnd('/'), + autoSendPushToken = false, + coreV2 = true, + ) + ) + + assertTrue(sdk.core is CoreV2) + val init = server.takeRequest(5, TimeUnit.SECONDS) + assertNotNull(init) + assertEquals("/init", init?.url?.encodedPath) + assertEquals("core-shop", init?.url?.queryParameter("shop_id")) + assertEquals("gzip", init?.headers?.get("Accept-Encoding")) + sdk.release() + } + + @Test + fun `an instance made without the flag keeps the legacy core`() { + server.enqueue(MockResponse.Builder().code(200).body("""{"did":"server-did","seance":"S"}""").build()) + val context = ApplicationProvider.getApplicationContext() + + val sdk: SDK = Rees46.initialize( + context, + Rees46Config(shopId = "legacy-shop", apiDomain = server.url("/").toString().trimEnd('/'), autoSendPushToken = false) + ) + + assertFalse(sdk.core?.isV2 ?: true) + val init = server.takeRequest(5, TimeUnit.SECONDS) + assertEquals("/init", init?.url?.encodedPath) + sdk.release() + } + + private fun await(send: (OnApiCallbackListener) -> Unit) { + val answered = CountDownLatch(1) + send(object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) = answered.countDown() + override fun onError(code: Int, msg: String?) = answered.countDown() + }) + assertTrue(answered.await(5, TimeUnit.SECONDS)) + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt new file mode 100644 index 00000000..9bdfc849 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt @@ -0,0 +1,253 @@ +package com.personalization.core.internal + +import com.personalization.LogLevel +import com.personalization.core.SdkError +import com.personalization.logs.SdkLogs +import com.personalization.sdk.domain.models.NetworkMethod +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import io.mockk.every +import io.mockk.mockk +import kotlinx.coroutines.runBlocking +import mockwebserver3.MockResponse +import mockwebserver3.MockWebServer +import mockwebserver3.SocketEffect +import org.json.JSONArray +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import org.robolectric.shadows.ShadowLog +import java.net.ServerSocket +import java.util.concurrent.TimeUnit + +/** The new core's client against a local server: what each outcome becomes, and what goes on the wire. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class CoreHttpClientTest { + + private val server = MockWebServer() + private val connectivity = FakeConnectivity(connected = true) + private val settings = mockk(relaxed = true) { + every { getShopId() } returns "shop" + every { getDid() } returns "did-1" + every { getSid() } returns "sid-1" + every { getSegmentForABTesting() } returns "A" + every { getStream() } returns "android" + } + + @Before + fun setUp() { + server.start() + ShadowLog.clear() + } + + @After + fun tearDown() { + server.close() + SdkLogs.reset() + } + + private fun client(timeouts: CoreTimeouts = CoreTimeouts(), baseUrl: String = server.url("/").toString()) = + CoreHttpClient({ baseUrl }, settings, connectivity, "shop", timeouts = timeouts) + + private fun send( + method: NetworkMethod = NetworkMethod.GET("products/get"), + params: JSONObject = JSONObject(), + client: CoreHttpClient = client(), + ): HttpResult = runBlocking { client.send(method, params) } + + private fun reply(code: Int, body: String = "") { + server.enqueue(MockResponse.Builder().code(code).body(body).build()) + } + + private fun HttpResult.error(): SdkError = (this as HttpResult.Failure).error + + private fun HttpResult.body(): Any = (this as HttpResult.Success).body + + @Test + fun `any 2xx is a success and an empty one an empty object`() { + reply(200, """{"id":1}""") + reply(201, "[1,2]") + reply(204) + reply(202, " ") + + assertEquals("""{"id":1}""", send().body().toString()) + assertEquals(JSONArray("[1,2]").toString(), (send().body() as JSONArray).toString()) + assertEquals("{}", send().body().toString()) + assertEquals("{}", send().body().toString()) + } + + @Test + fun `a 2xx that is not a JSON object or array is decoding`() { + reply(200, "maintenance") + reply(200, "\"ok\"") + reply(200, "42") + + repeat(3) { assertEquals(SdkError.Decoding, send().error()) } + } + + @Test + fun `an answer outside 2xx is http with its code, the body kept for the old callbacks`() { + listOf(304, 400, 402, 404, 422, 500, 503).forEach { code -> + reply(code, if (code == 304) "" else """{"message":"failed $code"}""") + val result = send() as HttpResult.Failure + assertEquals(SdkError.Http(code), result.error) + if (code != 304) assertEquals("""{"message":"failed $code"}""", result.errorBody) + } + } + + @Test + fun `a body that breaks off midway is a timeout, not a success with part of it`() { + server.enqueue( + MockResponse.Builder() + .code(200) + .body("""{"did":"abc","popup":{"id":7,"components":"${"x".repeat(4_000)}"}}""") + .onResponseBody(SocketEffect.ShutdownConnection) + .build() + ) + + assertEquals(SdkError.Timeout, send().error()) + } + + @Test + fun `an answer that does not come within the read timeout is a timeout`() { + server.enqueue(MockResponse.Builder().onResponseStart(SocketEffect.Stall).build()) + + val started = System.nanoTime() + val result = send(client = client(CoreTimeouts(readMs = 300))) + + assertEquals(SdkError.Timeout, result.error()) + assertTrue(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started) < 5_000) + } + + @Test + fun `init reads with its own timeout, other requests keep theirs`() { + val client = client(CoreTimeouts(readMs = 60_000, initReadMs = 300)) + server.enqueue(MockResponse.Builder().onResponseStart(SocketEffect.Stall).build()) + + val started = System.nanoTime() + assertEquals(SdkError.Timeout, send(NetworkMethod.GET("init"), client = client).error()) + assertTrue(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started) < 5_000) + assertEquals(60_000, client.clientFor(NetworkMethod.GET("products/get")).readTimeoutMillis) + } + + @Test + fun `the timeouts are 5 seconds to connect, 10 to read and 5 to read init`() { + val client = client() + + assertEquals(5_000, client.client.connectTimeoutMillis) + assertEquals(10_000, client.client.readTimeoutMillis) + assertEquals(10_000, client.client.writeTimeoutMillis) + assertEquals(5_000, client.clientFor(NetworkMethod.GET("init")).readTimeoutMillis) + assertEquals(5_000, client.clientFor(NetworkMethod.GET("init")).connectTimeoutMillis) + } + + @Test + fun `the instances' clients share one connection pool`() { + val first = client() + val second = client() + + assertTrue(first.client.connectionPool === second.client.connectionPool) + assertTrue(first.client.dispatcher === second.client.dispatcher) + assertTrue(first.client.connectionPool === CoreHttp.shared.connectionPool) + } + + @Test + fun `a server that refuses the connection is network`() { + val closedPort = ServerSocket(0).use { it.localPort } + + val result = send(client = client(baseUrl = "http://127.0.0.1:$closedPort/")) + + assertEquals(SdkError.Network, result.error()) + } + + @Test + fun `without a network a request fails at once and is never sent`() { + connectivity.set(false) + reply(200, "{}") + + assertEquals(SdkError.Network, send().error()) + assertEquals(0, server.requestCount) + } + + @Test + fun `the client does not retry`() { + reply(500, "{}") + server.enqueue(MockResponse.Builder().code(200).body("{\"a\":\"${"x".repeat(4_000)}\"}").onResponseBody(SocketEffect.ShutdownConnection).build()) + reply(200, "{}") + + assertEquals(SdkError.Http(500), send().error()) + assertEquals(SdkError.Timeout, send(NetworkMethod.POST("push")).error()) + + assertEquals(2, server.requestCount) + } + + @Test + fun `a GET carries the common fields in its query`() { + reply(200, "{}") + + send(NetworkMethod.GET("products/get"), JSONObject().put("item_id", "42")) + + val url = server.takeRequest().url + assertEquals("/products/get", url.encodedPath) + assertEquals("shop", url.queryParameter("shop_id")) + assertEquals("did-1", url.queryParameter("did")) + assertEquals("sid-1", url.queryParameter("sid")) + assertEquals("sid-1", url.queryParameter("seance")) + assertEquals("A", url.queryParameter("segment")) + assertEquals("android", url.queryParameter("stream")) + assertEquals("42", url.queryParameter("item_id")) + } + + @Test + fun `a POST carries the common fields in its JSON body`() { + reply(200, "{}") + + send(NetworkMethod.POST("push"), JSONObject().put("event", "view").put("segment", "B")) + + val request = server.takeRequest() + assertEquals("POST", request.method) + assertNull(request.url.query) + assertTrue(request.headers["Content-Type"].orEmpty().startsWith("application/json")) + val body = JSONObject(request.body?.utf8().orEmpty()) + assertEquals( + setOf("event", "shop_id", "did", "sid", "seance", "segment", "stream"), + body.keys().asSequence().toSet() + ) + assertEquals("sid-1", body.getString("seance")) + // A strict call's own segment wins over the device's. + assertEquals("B", body.getString("segment")) + } + + @Test + fun `the log has the method, the path and the code, never the query, a body or the answer`() { + SdkLogs.register("shop", LogLevel.DEBUG, "CoreTag") + reply(200, """{"email":"answer@example.com"}""") + reply(422, """{"error":"invalid phone +79990004455"}""") + + send(NetworkMethod.GET("profile"), JSONObject().put("email", "get@example.com")) + send(NetworkMethod.POST("profile/set"), JSONObject().put("phone", "+79990004455")) + + val log = ShadowLog.getLogsForTag("CoreTag").joinToString("\n") { it.msg } + assertTrue(log, log.contains("[request] 200: GET profile")) + assertTrue(log, log.contains("[request_failed] 422: POST profile/set {method=POST, path=profile/set, status=422}")) + listOf("get@example.com", "answer@example.com", "79990004455", "did-1", "sid-1").forEach { + assertFalse(log, log.contains(it)) + } + } + + @Test + fun `a request that cannot be built fails without being sent`() { + val result = send(client = client(baseUrl = "not a url")) + + assertTrue(result.error() is SdkError.InvalidArgument) + assertEquals(0, server.requestCount) + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt new file mode 100644 index 00000000..ebaef0ce --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt @@ -0,0 +1,143 @@ +package com.personalization.core.internal + +import com.personalization.api.OnApiCallbackListener +import com.personalization.sdk.domain.models.NetworkMethod +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import io.mockk.every +import io.mockk.mockk +import mockwebserver3.MockResponse +import mockwebserver3.MockWebServer +import mockwebserver3.SocketEffect +import org.json.JSONArray +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotEquals +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit + +/** The new core behind the old callbacks: one answer per request, the old codes, and release. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class CoreV2Test { + + private val server = MockWebServer() + private val connectivity = FakeConnectivity(connected = true) + private val settings = mockk(relaxed = true) { + every { getShopId() } returns "shop" + every { getDid() } returns "did-1" + every { getSid() } returns "sid-1" + } + private lateinit var core: CoreV2 + + @Before + fun setUp() { + server.start() + val client = CoreHttpClient({ server.url("/").toString() }, settings, connectivity, "shop") + core = CoreV2(client, "shop") + } + + @After + fun tearDown() { + core.release() + server.close() + } + + @Test + fun `answers come back as the old callbacks get them`() { + server.enqueue(MockResponse.Builder().code(200).body("""{"id":1}""").build()) + server.enqueue(MockResponse.Builder().code(200).body("[1]").build()) + server.enqueue(MockResponse.Builder().code(404).body("""{"message":"no such product"}""").build()) + server.enqueue(MockResponse.Builder().code(200).body("").build()) + + assertEquals(listOf("""success:{"id":1}"""), call()) + assertEquals(listOf("success:[1]"), call()) + assertEquals(listOf("""error:404:{"message":"no such product"}"""), call()) + assertEquals(listOf("error:-1"), call().map { it.substringBefore(":The") }) + } + + @Test + fun `without a network the old callback gets -1 at once`() { + connectivity.set(false) + + assertEquals(listOf("error:-1"), call().map { it.substringBefore(":No") }) + assertEquals(0, server.requestCount) + } + + @Test + fun `a failing onSuccess ends in one onError and nothing escapes`() { + server.enqueue(MockResponse.Builder().code(200).body("{}").build()) + + val recorder = Recorder(throwOnSuccess = true) + core.send(NetworkMethod.GET("products/get"), JSONObject(), recorder) + + assertEquals(listOf("success:{}", "error:-1"), recorder.await(expected = 2).map { it.substringBefore(":host") }) + } + + @Test + fun `release cancels the requests in flight and fails the ones after it`() { + server.enqueue(MockResponse.Builder().onResponseStart(SocketEffect.Stall).build()) + val inFlight = Recorder() + core.send(NetworkMethod.GET("products/get"), JSONObject(), inFlight) + server.takeRequest(5, TimeUnit.SECONDS) + + val started = System.nanoTime() + core.release() + + assertEquals(listOf("error:-1"), inFlight.await().map { it.substringBefore(":The") }) + assertTrue(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started) < 5_000) + + val after = Recorder() + core.send(NetworkMethod.GET("products/get"), JSONObject(), after) + assertEquals(listOf("error:-1"), after.await().map { it.substringBefore(":The") }) + assertEquals(1, server.requestCount) + } + + @Test + fun `a request without a listener still goes`() { + server.enqueue(MockResponse.Builder().code(200).body("{}").build()) + + core.send(NetworkMethod.POST("push"), JSONObject().put("event", "view"), null) + + val request = server.takeRequest(5, TimeUnit.SECONDS) + assertNotEquals(null, request) + } + + private fun call(): List { + val recorder = Recorder() + core.send(NetworkMethod.GET("products/get"), JSONObject(), recorder) + return recorder.await() + } + + private class Recorder(private val throwOnSuccess: Boolean = false) : OnApiCallbackListener() { + private val calls = CopyOnWriteArrayList() + private val latch = CountDownLatch(2) + + override fun onSuccess(response: JSONObject?) = record("success:$response", throwOnSuccess) + + override fun onSuccess(response: JSONArray) = record("success:$response", throwOnSuccess) + + override fun onError(code: Int, msg: String?) = record("error:$code" + (msg?.let { ":$it" } ?: ""), false) + + private fun record(call: String, fail: Boolean) { + calls += call + latch.countDown() + if (fail) throw IllegalStateException("host callback failed") + } + + /** Waits for [expected] calls, then a little longer for any extra one. */ + fun await(expected: Int = 1): List { + val deadline = System.currentTimeMillis() + 5_000 + while (calls.size < expected && System.currentTimeMillis() < deadline) Thread.sleep(10) + Thread.sleep(200) + return calls.toList() + } + } +} From 4832115190111ffdafcfee1669d79b82078ce8e6 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:24:19 +0400 Subject: [PATCH 017/109] fix(sdk): an app callback that throws is logged as callback_failed (the popups v3 link and product handlers, the stories viewer's shareUrlTransform), and needReInitialization logs no device_id_reset: the legacy path asks /init again but keeps the did --- .../src/main/kotlin/com/personalization/RegisterManager.kt | 3 --- .../com/personalization/ui/popups/PopupV3Presenter.kt | 6 ++++-- .../ui/widgets/PersonalizationStoriesViewer.kt | 5 ++++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt index 2640cc66..8a13330d 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt @@ -63,9 +63,6 @@ class RegisterManager @Inject constructor( this.autoSendPushToken = autoSendPushToken val did = getUserSettingsValueUseCase.getDid() - if (did.isNotEmpty() && needReInitialization) { - log(LogLevel.INFO, LogCodes.DEVICE_ID_RESET, "needReInitialization: /init asks for a new did") - } when { did.isEmpty() || needReInitialization -> initializeNewDevice() else -> { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Presenter.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Presenter.kt index 86ebc44b..e8e533db 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Presenter.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Presenter.kt @@ -31,6 +31,8 @@ import androidx.fragment.app.FragmentActivity import com.personalization.InternalPersonalizationV3Api import com.personalization.R import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import com.personalization.popups.popupsV3Log import com.personalization.SDK import com.personalization.api.OnApiCallbackListener @@ -727,7 +729,7 @@ internal class PopupV3ActionHandler( val handled = try { handler(link) } catch (throwable: Throwable) { - popupsV3Log(sdk, LogLevel.ERROR, "popups v3: the link handler threw", throwable = throwable) + SdkLogs.log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "popups v3: the link handler threw", sdk.logShopId, throwable = throwable) false } if (handled) { @@ -826,7 +828,7 @@ internal class PopupV3ActionHandler( val handled = try { handler(product) } catch (throwable: Throwable) { - popupsV3Log(sdk, LogLevel.ERROR, "popups v3: product handler failed on ${product.id}", throwable = throwable) + SdkLogs.log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "popups v3: the product handler threw", sdk.logShopId, mapOf("product" to product.id), throwable) false } if (handled) { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStoriesViewer.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStoriesViewer.kt index 60748053..16c76452 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStoriesViewer.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStoriesViewer.kt @@ -36,6 +36,9 @@ import com.bumptech.glide.request.transition.Transition import com.personalization.InternalPersonalizationV3Api import com.personalization.OnClickListener import com.personalization.SDK +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import com.personalization.stories.models.Story import com.personalization.ui.widgets.StoriesViewerState.Pause import java.lang.ref.WeakReference @@ -643,7 +646,7 @@ class PersonalizationStoriesViewer( track("share") val shared = shareUrlTransform?.let { transform -> runCatching { transform(url) }.getOrElse { - SDK.error("PersonalizationStoriesViewer: shareUrlTransform threw, the link is shared as it is", it) + SdkLogs.log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "PersonalizationStoriesViewer: shareUrlTransform threw, the link is shared as it is", sdk?.logShopId, throwable = it) url } } ?: url From 92e214e03404bc6592fdfc2dc6c694c208aa0caf Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:24:19 +0400 Subject: [PATCH 018/109] docs(core): Rees46Config.coreV2 says how the new core's answers differ: -1 for a failure without an answer where no connection used to be 504, and a 2xx whose body is not JSON fails instead of succeeding empty --- .../src/main/kotlin/com/personalization/Rees46Config.kt | 5 +++-- .../com/personalization/core/internal/CoreHttpClient.kt | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt index 88f4c1b2..885a3b59 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt @@ -21,8 +21,9 @@ package com.personalization * @property coreV2 runs the instance on the new core; off by default. The process has one core: the * first [Rees46.initialize] or [Rees46.registerShops] decides it, and an instance that asks for the * other one runs on the process's and logs `core_flag_mismatch`. So far the new core differs in its - * network client only: OkHttp, a request without a network fails at once, `release()` cancels the - * instance's requests. + * network client only: requests go through OkHttp, a request without a network fails at once, + * `release()` cancels the instance's requests, and a failure without an answer is `onError(-1)` (no + * connection used to be `504`), as is a `2xx` whose body is not JSON (it used to succeed empty). * * Constructors are overloaded for Java: every trailing parameter with a default may be left out, so * Java code written against the seven-argument constructor keeps compiling as parameters are added. diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt index 5747878d..b38c2a34 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt @@ -143,7 +143,7 @@ internal class CoreHttpClient( ) private suspend fun Call.exchange(phase: Phase): Exchange = suspendCancellableCoroutine { continuation -> - continuation.invokeOnCancellation { cancel() } + continuation.invokeOnCancellation { this@exchange.cancel() } enqueue(object : Callback { override fun onFailure(call: Call, e: IOException) { continuation.resume(Exchange(HttpResult.Failure(noAnswer(call, phase)), failure = e)) From ae472b397cbafdbe157e12683a70f730d08f2e3d Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:35:55 +0400 Subject: [PATCH 019/109] fix(sdk): a shop's log level registers on Android 6: the levels live in a snapshot replaced under a lock instead of ConcurrentHashMap.compute, which needs Android 7; the connectivity monitor walks its listeners with a plain loop --- .../internal/AndroidConnectivityMonitor.kt | 2 +- .../com/personalization/logs/SdkLogs.kt | 22 ++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/AndroidConnectivityMonitor.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/AndroidConnectivityMonitor.kt index fc4f2fc7..4a2b10d3 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/AndroidConnectivityMonitor.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/AndroidConnectivityMonitor.kt @@ -77,7 +77,7 @@ internal class AndroidConnectivityMonitor(context: Context) : ConnectivityMonito val connected = isConnected if (lastKnown == connected) return lastKnown = connected - listeners.forEach { listener -> + for (listener in listeners) { try { listener(connected) } catch (e: Exception) { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt index 90741d1b..7fd26804 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt @@ -5,7 +5,6 @@ import com.personalization.LogLevel import com.personalization.SDK import com.personalization.SdkLogHandler import com.personalization.SdkLogMessage -import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.Executor import java.util.concurrent.Executors @@ -22,8 +21,13 @@ internal object SdkLogs { /** One live instance of a shop and the level it asked for. */ private class Registration(val owner: Any, val level: LogLevel, val tag: String) - /** The live instances of each shop, the latest last. */ - private val shops = ConcurrentHashMap>() + /** + * The live instances of each shop, the latest last: an immutable snapshot that writers replace + * under [lock] and readers take without it. (`ConcurrentHashMap.compute` would need Android 7.0.) + */ + @Volatile + private var shops: Map> = emptyMap() + private val lock = Any() @Volatile var handler: SdkLogHandler? = null @@ -39,20 +43,22 @@ internal object SdkLogs { * are live, the shop logs at the most detailed of their levels, under the tag of the latest one. */ fun register(shopId: String, level: LogLevel, tag: String, owner: Any = shopId) { - shops.compute(shopId) { _, current -> - current.orEmpty().filterNot { it.owner == owner } + Registration(owner, level, tag) + synchronized(lock) { + val registrations = shops[shopId].orEmpty().filterNot { it.owner == owner } + Registration(owner, level, tag) + shops = shops + (shopId to registrations) } } /** [owner] is gone; the shop keeps a level while another of its instances is live. */ fun unregister(shopId: String, owner: Any = shopId) { - shops.computeIfPresent(shopId) { _, current -> - current.filterNot { it.owner == owner }.takeIf { it.isNotEmpty() } + synchronized(lock) { + val remaining = shops[shopId]?.filterNot { it.owner == owner } ?: return + shops = if (remaining.isEmpty()) shops - shopId else shops + (shopId to remaining) } } internal fun reset() { - shops.clear() + synchronized(lock) { shops = emptyMap() } handler = null } From f73b99bd2c92a1192f6c1db9296029535e67385b Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:35:55 +0400 Subject: [PATCH 020/109] build: lint 8.9 over AGP 8.5, whose lint cannot read the kotlin-stdlib 2.2 metadata that OkHttp 5 brings and reported every Kotlin forEach as the Java 8 one; the Compose runtime's CoroutineCreationDuringComposition check, which crashes on that metadata, is off --- gradle.properties | 3 +++ personalization-sdk/build.gradle | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/gradle.properties b/gradle.properties index 274c2163..8334e4a5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,6 +14,9 @@ org.gradle.daemon=true android.useAndroidX=true android.nonTransitiveRClass=false android.nonFinalResIds=false +# Lint of AGP 8.5 reads Kotlin metadata up to 2.1; OkHttp 5 brings kotlin-stdlib 2.2, and without a newer +# lint every Kotlin `forEach` would be reported as the Java 8 one (NewApi). Lint 8.9 reads 2.2. +android.experimental.lint.version=8.9.1 # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects diff --git a/personalization-sdk/build.gradle b/personalization-sdk/build.gradle index d6a8b351..1cf0d614 100644 --- a/personalization-sdk/build.gradle +++ b/personalization-sdk/build.gradle @@ -89,6 +89,13 @@ android { enable = false } + lint { + // The Compose runtime's lint check reads Kotlin metadata only up to 2.0 and crashes on the + // kotlin-stdlib 2.2 that OkHttp 5 brings; the SDK's one Compose file (StoriesWidget) launches + // no coroutine during composition. + disable 'CoroutineCreationDuringComposition' + } + namespace 'com.personalization' } From dcd13333344130270871fc2f8b4fef8cbed9f535 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:50:02 +0400 Subject: [PATCH 021/109] build: OkHttp 4.12 instead of 5 for the new core's client, so an app needs nothing new to build: its kotlin-stdlib is older than the 2.0.0 the SDK already brings and it adds no androidx.startup initializer; the SDK is back on Kotlin 2.0.0, Dagger 2.51.1 with its 2.48 compiler and the lint of AGP 8.5 with every check on, and the core's tests run on okhttp3's mockwebserver --- build.gradle | 6 ++-- gradle.properties | 3 -- personalization-sdk/build.gradle | 22 +++++--------- .../core/internal/CoreHttpClient.kt | 5 ++-- .../core/internal/CoreFunnelTest.kt | 22 +++++++------- .../core/internal/CoreHttpClientTest.kt | 29 +++++++++---------- .../core/internal/CoreV2Test.kt | 20 ++++++------- 7 files changed, 49 insertions(+), 58 deletions(-) diff --git a/build.gradle b/build.gradle index 05bd3963..28f6e3ce 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '2.1.21' + ext.kotlin_version = '2.0.0' repositories { google() // Google's mirror of Maven Central first: JitPack builds get HTTP 429 from repo.maven.apache.org. @@ -19,8 +19,8 @@ buildscript { plugins { id 'ru.vyarus.pom' version '2.2.2' id "io.github.gradle-nexus.publish-plugin" version "2.0.0" - id 'org.jetbrains.kotlin.android' version '2.1.21' apply false - id 'org.jetbrains.kotlin.plugin.compose' version '2.1.21' apply false + id 'org.jetbrains.kotlin.android' version '2.0.0' apply false + id 'org.jetbrains.kotlin.plugin.compose' version '2.0.0' apply false } def versionFile = file("version.properties") diff --git a/gradle.properties b/gradle.properties index 8334e4a5..274c2163 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,9 +14,6 @@ org.gradle.daemon=true android.useAndroidX=true android.nonTransitiveRClass=false android.nonFinalResIds=false -# Lint of AGP 8.5 reads Kotlin metadata up to 2.1; OkHttp 5 brings kotlin-stdlib 2.2, and without a newer -# lint every Kotlin `forEach` would be reported as the Java 8 one (NewApi). Lint 8.9 reads 2.2. -android.experimental.lint.version=8.9.1 # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects diff --git a/personalization-sdk/build.gradle b/personalization-sdk/build.gradle index 1cf0d614..50550324 100644 --- a/personalization-sdk/build.gradle +++ b/personalization-sdk/build.gradle @@ -89,13 +89,6 @@ android { enable = false } - lint { - // The Compose runtime's lint check reads Kotlin metadata only up to 2.0 and crashes on the - // kotlin-stdlib 2.2 that OkHttp 5 brings; the SDK's one Compose file (StoriesWidget) launches - // no coroutine during composition. - disable 'CoroutineCreationDuringComposition' - } - namespace 'com.personalization' } @@ -119,10 +112,11 @@ dependencies { // through lifecycle-process; declared because the SDK now relies on it directly. implementation 'androidx.startup:startup-runtime:1.1.1' implementation 'com.google.code.gson:gson:2.11.0' - // The new core's HTTP client (Rees46Config.coreV2): one connection pool for the process. 5.3 is the - // last line that compiles against any compileSdk: 5.4 asks apps for compileSdk 36, 5.5 for 37. Its - // kotlin-stdlib 2.2 is why apps and this SDK need a Kotlin compiler of 2.1 or later. - implementation 'com.squareup.okhttp3:okhttp:5.3.2' + // The new core's HTTP client (Rees46Config.coreV2): one connection pool for the process. 4.12, not 5: + // OkHttp 5 brings kotlin-stdlib 2.2, which asks apps for a Kotlin compiler of 2.1 or later, and adds an + // androidx.startup initializer to their manifest. 4.12 brings neither: its kotlin-stdlib is older than + // the SDK's own. + implementation 'com.squareup.okhttp3:okhttp:4.12.0' implementation 'org.jetbrains.kotlin:kotlin-stdlib' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4' @@ -148,7 +142,7 @@ dependencies { implementation 'com.github.bumptech.glide:glide:4.16.0' implementation 'androidx.media3:media3-exoplayer:1.4.1' implementation 'androidx.media3:media3-ui:1.4.1' - implementation 'com.google.dagger:dagger:2.57.2' + implementation 'com.google.dagger:dagger:2.51.1' // Instrumented tests only: as `implementation` they reached every host app's release build. androidTestImplementation("androidx.test:core:1.6.1") @@ -157,14 +151,14 @@ dependencies { implementation("com.google.android.gms:play-services-ads-identifier:17.0.0") - kapt 'com.google.dagger:dagger-compiler:2.57.2' + kapt 'com.google.dagger:dagger-compiler:2.48' testImplementation 'junit:junit:4.13.2' testImplementation 'io.mockk:mockk:1.13.11' testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4' testImplementation 'androidx.test:core:1.6.1' testImplementation 'org.robolectric:robolectric:4.12.1' - testImplementation 'com.squareup.okhttp3:mockwebserver3:5.3.2' + testImplementation 'com.squareup.okhttp3:mockwebserver:4.12.0' // compileOnly dependencies are absent from the unit test classpath, and the Compose compiler // plugin refuses to compile the test variant without the runtime it can see. These mirror the // compileOnly declarations above and stay confined to tests, so the published POM is unaffected. diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt index b38c2a34..604051ae 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt @@ -168,13 +168,14 @@ internal class CoreHttpClient( if (status !in HTTP_SUCCESS) { // The status is the answer: a body that fails to arrive does not replace it. val errorBody = try { - response.body.string() + response.body?.string() } catch (e: IOException) { null } return Exchange(HttpResult.Failure(SdkError.Http(status), errorBody), status, errorBody) } - val text = response.body.string() + // OkHttp gives every answer it hands over a body; without one the answer is empty. + val text = response.body?.string().orEmpty() if (text.isBlank()) return Exchange(HttpResult.Success(JSONObject()), status, text) val json = try { JSONTokener(text).nextValue() diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt index 426df2d1..69515caa 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt @@ -12,8 +12,8 @@ import com.personalization.sdk.data.repositories.network.NetworkRepositoryImpl import com.personalization.sdk.domain.repositories.UserSettingsRepository import io.mockk.every import io.mockk.mockk -import mockwebserver3.MockResponse -import mockwebserver3.MockWebServer +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer import org.json.JSONObject import org.junit.After import org.junit.Assert.assertEquals @@ -71,14 +71,14 @@ class CoreFunnelTest { @Test fun `the funnel sends through the legacy core until the instance takes the new one`() { assertFalse(repository.core.isV2) - server.enqueue(MockResponse.Builder().code(200).body("{}").build()) + server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) await { repository.get("products/get", JSONObject(), it) } // HttpURLConnection on the JVM asks for no compression; OkHttp asks for gzip. assertEquals(null, server.takeRequest().headers["Accept-Encoding"]) repository.useCoreV2("shop", FakeConnectivity(connected = true)) assertTrue(repository.core.isV2) - server.enqueue(MockResponse.Builder().code(200).body("{}").build()) + server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) await { repository.get("products/get", JSONObject(), it) } assertEquals("gzip", server.takeRequest().headers["Accept-Encoding"]) } @@ -87,7 +87,7 @@ class CoreFunnelTest { fun `requests waiting for a did go through the new core once it comes`() { repository.useCoreV2("shop", FakeConnectivity(connected = true)) did.set("") - server.enqueue(MockResponse.Builder().code(200).body("{}").build()) + server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) val answered = CountDownLatch(1) repository.getAsync("products/get", JSONObject(), object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) = answered.countDown() @@ -99,12 +99,12 @@ class CoreFunnelTest { repository.executeQueueTasks() assertTrue(answered.await(5, TimeUnit.SECONDS)) - assertEquals("did-2", server.takeRequest().url.queryParameter("did")) + assertEquals("did-2", checkNotNull(server.takeRequest().requestUrl).queryParameter("did")) } @Test fun `an instance made with coreV2 runs on the new core, its requests on the new client`() { - server.enqueue(MockResponse.Builder().code(200).body("""{"did":"server-did","seance":"S"}""").build()) + server.enqueue(MockResponse().setResponseCode(200).setBody("""{"did":"server-did","seance":"S"}""")) val context = ApplicationProvider.getApplicationContext() // Robolectric's default network has no internet capability: give it one, as a device's has. val manager = context.getSystemService(ConnectivityManager::class.java) @@ -125,15 +125,15 @@ class CoreFunnelTest { assertTrue(sdk.core is CoreV2) val init = server.takeRequest(5, TimeUnit.SECONDS) assertNotNull(init) - assertEquals("/init", init?.url?.encodedPath) - assertEquals("core-shop", init?.url?.queryParameter("shop_id")) + assertEquals("/init", init?.requestUrl?.encodedPath) + assertEquals("core-shop", init?.requestUrl?.queryParameter("shop_id")) assertEquals("gzip", init?.headers?.get("Accept-Encoding")) sdk.release() } @Test fun `an instance made without the flag keeps the legacy core`() { - server.enqueue(MockResponse.Builder().code(200).body("""{"did":"server-did","seance":"S"}""").build()) + server.enqueue(MockResponse().setResponseCode(200).setBody("""{"did":"server-did","seance":"S"}""")) val context = ApplicationProvider.getApplicationContext() val sdk: SDK = Rees46.initialize( @@ -143,7 +143,7 @@ class CoreFunnelTest { assertFalse(sdk.core?.isV2 ?: true) val init = server.takeRequest(5, TimeUnit.SECONDS) - assertEquals("/init", init?.url?.encodedPath) + assertEquals("/init", init?.requestUrl?.encodedPath) sdk.release() } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt index 9bdfc849..19693585 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt @@ -8,9 +8,9 @@ import com.personalization.sdk.domain.repositories.UserSettingsRepository import io.mockk.every import io.mockk.mockk import kotlinx.coroutines.runBlocking -import mockwebserver3.MockResponse -import mockwebserver3.MockWebServer -import mockwebserver3.SocketEffect +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import okhttp3.mockwebserver.SocketPolicy import org.json.JSONArray import org.json.JSONObject import org.junit.After @@ -64,7 +64,7 @@ class CoreHttpClientTest { ): HttpResult = runBlocking { client.send(method, params) } private fun reply(code: Int, body: String = "") { - server.enqueue(MockResponse.Builder().code(code).body(body).build()) + server.enqueue(MockResponse().setResponseCode(code).setBody(body)) } private fun HttpResult.error(): SdkError = (this as HttpResult.Failure).error @@ -106,11 +106,10 @@ class CoreHttpClientTest { @Test fun `a body that breaks off midway is a timeout, not a success with part of it`() { server.enqueue( - MockResponse.Builder() - .code(200) - .body("""{"did":"abc","popup":{"id":7,"components":"${"x".repeat(4_000)}"}}""") - .onResponseBody(SocketEffect.ShutdownConnection) - .build() + MockResponse() + .setResponseCode(200) + .setBody("""{"did":"abc","popup":{"id":7,"components":"${"x".repeat(4_000)}"}}""") + .setSocketPolicy(SocketPolicy.DISCONNECT_DURING_RESPONSE_BODY) ) assertEquals(SdkError.Timeout, send().error()) @@ -118,7 +117,7 @@ class CoreHttpClientTest { @Test fun `an answer that does not come within the read timeout is a timeout`() { - server.enqueue(MockResponse.Builder().onResponseStart(SocketEffect.Stall).build()) + server.enqueue(MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE)) val started = System.nanoTime() val result = send(client = client(CoreTimeouts(readMs = 300))) @@ -130,7 +129,7 @@ class CoreHttpClientTest { @Test fun `init reads with its own timeout, other requests keep theirs`() { val client = client(CoreTimeouts(readMs = 60_000, initReadMs = 300)) - server.enqueue(MockResponse.Builder().onResponseStart(SocketEffect.Stall).build()) + server.enqueue(MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE)) val started = System.nanoTime() assertEquals(SdkError.Timeout, send(NetworkMethod.GET("init"), client = client).error()) @@ -180,7 +179,7 @@ class CoreHttpClientTest { @Test fun `the client does not retry`() { reply(500, "{}") - server.enqueue(MockResponse.Builder().code(200).body("{\"a\":\"${"x".repeat(4_000)}\"}").onResponseBody(SocketEffect.ShutdownConnection).build()) + server.enqueue(MockResponse().setResponseCode(200).setBody("{\"a\":\"${"x".repeat(4_000)}\"}").setSocketPolicy(SocketPolicy.DISCONNECT_DURING_RESPONSE_BODY)) reply(200, "{}") assertEquals(SdkError.Http(500), send().error()) @@ -195,7 +194,7 @@ class CoreHttpClientTest { send(NetworkMethod.GET("products/get"), JSONObject().put("item_id", "42")) - val url = server.takeRequest().url + val url = checkNotNull(server.takeRequest().requestUrl) assertEquals("/products/get", url.encodedPath) assertEquals("shop", url.queryParameter("shop_id")) assertEquals("did-1", url.queryParameter("did")) @@ -214,9 +213,9 @@ class CoreHttpClientTest { val request = server.takeRequest() assertEquals("POST", request.method) - assertNull(request.url.query) + assertNull(request.requestUrl?.query) assertTrue(request.headers["Content-Type"].orEmpty().startsWith("application/json")) - val body = JSONObject(request.body?.utf8().orEmpty()) + val body = JSONObject(request.body.readUtf8()) assertEquals( setOf("event", "shop_id", "did", "sid", "seance", "segment", "stream"), body.keys().asSequence().toSet() diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt index ebaef0ce..0022a9ae 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt @@ -5,9 +5,9 @@ import com.personalization.sdk.domain.models.NetworkMethod import com.personalization.sdk.domain.repositories.UserSettingsRepository import io.mockk.every import io.mockk.mockk -import mockwebserver3.MockResponse -import mockwebserver3.MockWebServer -import mockwebserver3.SocketEffect +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import okhttp3.mockwebserver.SocketPolicy import org.json.JSONArray import org.json.JSONObject import org.junit.After @@ -52,10 +52,10 @@ class CoreV2Test { @Test fun `answers come back as the old callbacks get them`() { - server.enqueue(MockResponse.Builder().code(200).body("""{"id":1}""").build()) - server.enqueue(MockResponse.Builder().code(200).body("[1]").build()) - server.enqueue(MockResponse.Builder().code(404).body("""{"message":"no such product"}""").build()) - server.enqueue(MockResponse.Builder().code(200).body("").build()) + server.enqueue(MockResponse().setResponseCode(200).setBody("""{"id":1}""")) + server.enqueue(MockResponse().setResponseCode(200).setBody("[1]")) + server.enqueue(MockResponse().setResponseCode(404).setBody("""{"message":"no such product"}""")) + server.enqueue(MockResponse().setResponseCode(200).setBody("")) assertEquals(listOf("""success:{"id":1}"""), call()) assertEquals(listOf("success:[1]"), call()) @@ -73,7 +73,7 @@ class CoreV2Test { @Test fun `a failing onSuccess ends in one onError and nothing escapes`() { - server.enqueue(MockResponse.Builder().code(200).body("{}").build()) + server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) val recorder = Recorder(throwOnSuccess = true) core.send(NetworkMethod.GET("products/get"), JSONObject(), recorder) @@ -83,7 +83,7 @@ class CoreV2Test { @Test fun `release cancels the requests in flight and fails the ones after it`() { - server.enqueue(MockResponse.Builder().onResponseStart(SocketEffect.Stall).build()) + server.enqueue(MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE)) val inFlight = Recorder() core.send(NetworkMethod.GET("products/get"), JSONObject(), inFlight) server.takeRequest(5, TimeUnit.SECONDS) @@ -102,7 +102,7 @@ class CoreV2Test { @Test fun `a request without a listener still goes`() { - server.enqueue(MockResponse.Builder().code(200).body("{}").build()) + server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) core.send(NetworkMethod.POST("push"), JSONObject().put("event", "view"), null) From 8484f1e8df32ef330fd97750276631ce0d0de56a Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 22:54:49 +0400 Subject: [PATCH 022/109] fix(sdk): log codes follow the extended table: a push that is not the SDK's (a notification-only FCM message, a clicked notification without type or code) is push_dropped at debug with reason not_sdk_push, one no shop takes a warning with unknown_shop or ambiguous_shop; the steps before a push reaches its shop log under push and push_received only once a shop takes it; a token from FCM or HMS is push_token_received; a request release() cancels is request_cancelled; the offline fast-fail is request_failed with reason offline; a popup the app kept back or that automatic presentation left out is popup_not_shown at debug with app_declined or auto_presentation_off; a Google Play pre-launch device skips /init with init_skipped --- .../personalization/HmsMessagingService.kt | 2 +- .../com/personalization/MessagingService.kt | 10 +- .../main/kotlin/com/personalization/Rees46.kt | 30 ++- .../com/personalization/RegisterManager.kt | 2 +- .../main/kotlin/com/personalization/SDK.kt | 21 ++- .../core/internal/CoreHttpClient.kt | 7 +- .../personalization/core/internal/CoreV2.kt | 12 +- .../impl/InAppNotificationManagerImpl.kt | 5 +- .../helpers/NotificationClickProcessor.kt | 9 +- .../com/personalization/logs/SdkLogs.kt | 37 +++- .../personalization/push/PushTokenManager.kt | 2 + .../core/internal/CoreHttpClientTest.kt | 3 + .../core/internal/CoreV2Test.kt | 31 ++++ .../com/personalization/logs/LogCodesTest.kt | 171 +++++++++++++++++- 14 files changed, 312 insertions(+), 30 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/HmsMessagingService.kt b/personalization-sdk/src/main/kotlin/com/personalization/HmsMessagingService.kt index 8245aff6..cf891ea4 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/HmsMessagingService.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/HmsMessagingService.kt @@ -24,7 +24,7 @@ class HmsMessagingService : HmsMessageService() { // lazily-registered shop the push targets, so a cold-process push (app not running) reaches // the second shop instead of being dropped for not being live yet. if (data.isNotEmpty()) { - SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_RECEIVED, "HMS data message", data["shop_id"], mapOf("keys" to data.keys.sorted())) + SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH, "HMS data message", data["shop_id"], mapOf("keys" to data.keys.sorted())) Rees46.handlePush(data, PushEventType.RECEIVED) } } catch (throwable: Throwable) { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/MessagingService.kt b/personalization-sdk/src/main/kotlin/com/personalization/MessagingService.kt index 4048fe9e..3a14e8f0 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/MessagingService.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/MessagingService.kt @@ -2,6 +2,7 @@ package com.personalization import com.personalization.logs.SdkLogs import com.personalization.logs.LogCodes +import com.personalization.logs.LogReasons import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage @@ -19,7 +20,7 @@ class MessagingService : FirebaseMessagingService() { try { // Check if message contains a data payload. if (remoteMessage.data.isNotEmpty()) { - SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_RECEIVED, "FCM data message", remoteMessage.data["shop_id"], mapOf("keys" to remoteMessage.data.keys.sorted())) + SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH, "FCM data message", remoteMessage.data["shop_id"], mapOf("keys" to remoteMessage.data.keys.sorted())) // Route through the facade so a lazily-registered shop the push targets is materialized // on demand — the cold process FCM starts to deliver a data-only message only has the // eagerly-initialized shops live, so a lazy shop would otherwise be dropped at routing. @@ -28,7 +29,12 @@ class MessagingService : FirebaseMessagingService() { // SDK pushes are data messages; one with only a notification payload is not the SDK's. if (remoteMessage.data.isEmpty() && remoteMessage.notification != null) { - SdkLogs.log(LogLevel.WARNING, LogCodes.PUSH_DROPPED, "FCM message with only a notification payload is not an SDK push") + SdkLogs.log( + LogLevel.DEBUG, + LogCodes.PUSH_DROPPED, + "FCM message with only a notification payload is not an SDK push", + context = mapOf("reason" to LogReasons.NOT_SDK_PUSH) + ) } } catch (throwable: Throwable) { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "An FCM message could not be handled", throwable = throwable) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt index 723fc87d..ce227aa1 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt @@ -1,6 +1,7 @@ package com.personalization import com.personalization.logs.LogCodes +import com.personalization.logs.LogReasons import com.personalization.logs.SdkLogs import android.content.Context import android.os.Bundle @@ -204,6 +205,7 @@ object Rees46 { private fun receivePush(payload: Map) { val (resolution, shopId) = resolvePushTarget(payload[PreferencesPartition.SHOP_ID_FIELD]) ?: return + logHandedOver(shopId, PushEventType.RECEIVED) // Show first — the process-global listener needs no instance, so a pending shop's push still // appears. Then track the delivery on the shop's own network (light bring-up if it isn't live); // a tracking/init failure must never undo the shown notification. @@ -214,6 +216,7 @@ object Rees46 { private fun clickPush(payload: Map) { val (resolution, shopId) = resolvePushTarget(payload[PreferencesPartition.SHOP_ID_FIELD]) ?: return + logHandedOver(shopId, PushEventType.CLICKED) runCatching { targetInstance(resolution, shopId)?.notificationClicked(payload.toBundle()) } .onFailure { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "handlePush: the click was not tracked", shopId, throwable = it) } } @@ -232,20 +235,31 @@ object Rees46 { return when (resolution) { is InstanceResolver.Resolution.Existing -> resolution to resolution.shopId is InstanceResolver.Resolution.Pending -> resolution to resolution.shopId - InstanceResolver.Resolution.Ambiguous, + InstanceResolver.Resolution.Ambiguous -> { + logDropped(payloadShopId, LogReasons.AMBIGUOUS_SHOP) + null + } InstanceResolver.Resolution.NotRegistered -> { - SdkLogs.log( - LogLevel.WARNING, - LogCodes.PUSH_DROPPED, - "handlePush: push dropped — no shop resolves it", - payloadShopId, - mapOf("live" to SdkRegistry.shopIds().size, "pending" to pending.size) - ) + logDropped(payloadShopId, LogReasons.UNKNOWN_SHOP) null } } } + private fun logDropped(payloadShopId: String?, reason: String) { + SdkLogs.log( + LogLevel.WARNING, + LogCodes.PUSH_DROPPED, + "handlePush: push dropped — no shop resolves it", + payloadShopId, + mapOf("reason" to reason, "live" to SdkRegistry.shopIds().size, "pending" to pending.size) + ) + } + + private fun logHandedOver(shopId: String, event: PushEventType) { + SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_RECEIVED, "Push handed to its shop", shopId, mapOf("event" to event.name.lowercase())) + } + /** The live instance, or a pending shop brought up with a light push-context init. */ private fun targetInstance(resolution: InstanceResolver.Resolution, shopId: String): SDK? = when (resolution) { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt index 8a13330d..aee401dd 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt @@ -90,7 +90,7 @@ class RegisterManager @Inject constructor( private fun init(knownDevice: Boolean = false) { if (isTestDevice) { - log(LogLevel.WARNING, LogCodes.SDK_LOG, "/init is not sent on a Google Play pre-launch report device") + log(LogLevel.INFO, LogCodes.INIT_SKIPPED, "/init is not sent on a Google Play pre-launch report device", mapOf("reason" to "pre_launch_report")) return } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index d4fdddc6..d29c1817 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -6,6 +6,7 @@ import androidx.core.util.Consumer import androidx.fragment.app.FragmentManager import com.google.firebase.messaging.RemoteMessage import com.personalization.logs.LogCodes +import com.personalization.logs.LogReasons import com.personalization.logs.SdkLogs import com.personalization.Params.InternalParameter import com.personalization.Params.TrackEvent @@ -1214,7 +1215,7 @@ open class SDK { if (!isSdkInitialized) { log( LogLevel.WARNING, - LogCodes.SDK_LOG, + LogCodes.PUSH, "notificationReceived() called before SDK.initialize(); ignoring. " + "Initialize SDK.instance in Application.onCreate so push delivery works in a cold process." ) @@ -1370,18 +1371,22 @@ open class SDK { * @param data the push data payload (title/body/icon/…) */ fun onMessage(data: Map) { - val shopId = PushTargetResolver.resolve( - payloadShopId = data[PreferencesPartition.SHOP_ID_FIELD], - liveShopIds = SdkRegistry.shopIds() - ) + val payloadShopId = data[PreferencesPartition.SHOP_ID_FIELD] + val liveShopIds = SdkRegistry.shopIds() + val shopId = PushTargetResolver.resolve(payloadShopId = payloadShopId, liveShopIds = liveShopIds) val target = shopId?.let { SdkRegistry.byShopId(it) } if (target == null || shopId == null) { - warn( - "onMessage: push dropped — no shop resolves it " + - "(shop_id=${data[PreferencesPartition.SHOP_ID_FIELD]}, live=${SdkRegistry.shopIds()})." + val reason = if (payloadShopId == null && liveShopIds.size > 1) LogReasons.AMBIGUOUS_SHOP else LogReasons.UNKNOWN_SHOP + SdkLogs.log( + LogLevel.WARNING, + LogCodes.PUSH_DROPPED, + "onMessage: push dropped — no shop resolves it", + payloadShopId, + mapOf("reason" to reason, "live" to liveShopIds.size) ) return } + SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_RECEIVED, "Push handed to its shop", shopId, mapOf("event" to "received")) // Show via the process-global listener, then track received on the instance. SdkRegistry.dispatchMessage(shopId = shopId, data = data.toNotificationData()) target.receiveMessage(data = data) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt index 604051ae..c658f521 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt @@ -4,6 +4,7 @@ import com.personalization.LogLevel import com.personalization.SDK import com.personalization.core.SdkError import com.personalization.logs.LogCodes +import com.personalization.logs.LogReasons import com.personalization.logs.SdkLogs import com.personalization.sdk.domain.models.NetworkMethod import com.personalization.sdk.domain.repositories.UserSettingsRepository @@ -97,7 +98,9 @@ internal class CoreHttpClient( /** Sends [params] (the method's own fields) to [method]; cancelling the caller cancels the request. */ suspend fun send(method: NetworkMethod, params: JSONObject): HttpResult { if (!connectivity.isConnected) { - log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "${line(method)}: no network") { context(method, error = ERROR_NETWORK) } + log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "${line(method)}: no network") { + context(method) + ("reason" to LogReasons.OFFLINE) + } return HttpResult.Failure(SdkError.Network) } val phase = Phase() @@ -212,6 +215,8 @@ internal class CoreHttpClient( context(method, status, name) } } + SdkError.Cancelled -> + log(LogLevel.DEBUG, LogCodes.REQUEST_CANCELLED, "${line(method)}: cancelled") { context(method) } else -> Unit } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt index 9b6966a1..cc381c8f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt @@ -51,8 +51,11 @@ internal class CoreV2( override fun send(method: NetworkMethod, params: JSONObject, listener: OnApiCallbackListener?) { val answered = AtomicBoolean(false) + // Delivers the result unless the request has had its answer; true when this one was it. val answer = { result: HttpResult -> - if (answered.compareAndSet(false, true) && listener != null) deliver(listener, result) + answered.compareAndSet(false, true).also { first -> + if (first && listener != null) deliver(listener, result) + } } if (released) { endings.launch { answer(HttpResult.Failure(SdkError.NotInitialized)) } @@ -73,7 +76,12 @@ internal class CoreV2( } // Cancelled before or while it ran: the request still gets its one answer. request.invokeOnCompletion { cause -> - if (cause != null) endings.launch { answer(HttpResult.Failure(SdkError.Cancelled)) } + if (cause != null) endings.launch { + if (answer(HttpResult.Failure(SdkError.Cancelled))) { + SdkLogs.log(LogLevel.DEBUG, LogCodes.REQUEST_CANCELLED, "${method.type} ${method.method}: cancelled", shopId, + mapOf("method" to method.type, "path" to method.method)) + } + } } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt index 70142da1..672e5543 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt @@ -2,6 +2,7 @@ package com.personalization.features.inAppNotification.impl import com.personalization.logs.SdkLogs import com.personalization.logs.LogCodes +import com.personalization.logs.LogReasons import com.personalization.LogLevel import android.app.Activity import android.content.Context @@ -100,7 +101,7 @@ class InAppNotificationManagerImpl @Inject constructor( return } if (activity == null) { - log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Popup kept back by the presentation listener", context = mapOf("popup" to popupDto.id, "reason" to "listener")) + log(LogLevel.DEBUG, LogCodes.POPUP_NOT_SHOWN, "Popup kept back by the presentation listener", context = mapOf("popup" to popupDto.id, "reason" to LogReasons.APP_DECLINED)) return } activity.supportFragmentManager.takeIf { it.canShowDialog() } @@ -109,7 +110,7 @@ class InAppNotificationManagerImpl @Inject constructor( presentation.autoPresentation -> defaultTarget() else -> { - log(LogLevel.WARNING, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown: automatic presentation is off", context = mapOf("popup" to popupDto.id, "reason" to "auto_presentation_off")) + log(LogLevel.DEBUG, LogCodes.POPUP_NOT_SHOWN, "Popup was not shown: automatic presentation is off", context = mapOf("popup" to popupDto.id, "reason" to LogReasons.AUTO_PRESENTATION_OFF)) return } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt index cd70c41a..e46090e0 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/domain/helpers/NotificationClickProcessor.kt @@ -3,6 +3,7 @@ package com.personalization.features.notification.domain.helpers import android.os.Bundle import com.personalization.LogLevel import com.personalization.logs.LogCodes +import com.personalization.logs.LogReasons import com.personalization.logs.SdkLogs import com.personalization.features.notification.domain.data.NotificationDataExtractor import com.personalization.features.notification.domain.data.NotificationDataSender @@ -40,12 +41,14 @@ class NotificationClickProcessor @Inject constructor( } private fun handleError(errorMessage: String, extras: Bundle?) { - // Not the extras themselves: they carry the push's content. + // Every SDK push has a type and a code: a click without them is another sender's notification, + // which an app that hands every click to the SDK passes on as well. Not the extras themselves: + // they carry the push's content. SdkLogs.log( - LogLevel.WARNING, + LogLevel.DEBUG, LogCodes.PUSH_DROPPED, "$errorMessage: the clicked push has no type or code", - context = mapOf("keys" to extras?.keySet()?.sorted()) + context = mapOf("reason" to LogReasons.NOT_SDK_PUSH, "keys" to extras?.keySet()?.sorted()) ) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt index 7fd26804..6dbb74f4 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt @@ -140,9 +140,16 @@ internal object LogCodes { /** A request got an answer (`debug`); context: method, path, status. */ const val REQUEST = "request" - /** An answer outside `2xx` or none (`error` on `4xx`, otherwise `warning`); context: method, path, status. */ + /** + * An answer outside `2xx` or none (`error` on `4xx`, otherwise `warning`); context: method, path, status. + * A request refused at once for want of a network has [LogReasons.OFFLINE]. + */ const val REQUEST_FAILED = "request_failed" const val REQUEST_BUILD_FAILED = "request_build_failed" + + /** A request cancelled by `release()` (`debug`); context: method, path. */ + const val REQUEST_CANCELLED = "request_cancelled" + const val INVALID_ARGUMENT = "invalid_argument" const val INVALID_CONFIG = "invalid_config" const val DECODING_FAILED = "decoding_failed" @@ -155,15 +162,28 @@ internal object LogCodes { /** `/init` failed (`error` on `4xx`, otherwise `warning`). */ const val INIT_FAILED = "init_failed" + /** `/init` deliberately not sent (`info`), as on a Google Play pre-launch report device; context: reason. */ + const val INIT_SKIPPED = "init_skipped" + /** The shop refused and the core is in the `refused` state; the legacy path has no such state. */ const val INIT_REFUSED = "init_refused" const val DEVICE_ID_RESET = "device_id_reset" const val STORAGE_FAILED = "storage_failed" const val QUEUE_DROPPED = "queue_dropped" + /** A push handed to its shop (`debug`); the steps before that go under [PUSH]. */ const val PUSH_RECEIVED = "push_received" + + /** + * A push not handled; context: reason. `debug` for one that is not the SDK's ([LogReasons.NOT_SDK_PUSH]), + * `warning` for one no shop takes ([LogReasons.UNKNOWN_SHOP], [LogReasons.AMBIGUOUS_SHOP]). + */ const val PUSH_DROPPED = "push_dropped" const val PUSH_FAILED = "push_failed" + + /** A push token came from FCM or HMS (`debug`), without the token itself. */ + const val PUSH_TOKEN_RECEIVED = "push_token_received" + const val PUSH_PERMISSION = "push_permission" const val PUSH_PERMISSION_FAILED = "push_permission_failed" const val PUSH_TOKEN_SENT = "push_token_sent" @@ -172,6 +192,10 @@ internal object LogCodes { const val ADVERTISING_ID_SENT = "advertising_id_sent" const val ADVERTISING_ID_FAILED = "advertising_id_failed" + /** + * A popup not shown; context: reason. `debug` when the app kept it back ([LogReasons.APP_DECLINED], + * [LogReasons.AUTO_PRESENTATION_OFF]), otherwise `warning`. + */ const val POPUP_NOT_SHOWN = "popup_not_shown" const val CORE_FLAG_MISMATCH = "core_flag_mismatch" @@ -181,8 +205,19 @@ internal object LogCodes { const val STORIES = "stories" const val STORIES_V3 = "stories_v3" const val POPUPS_V3 = "popups_v3" + const val PUSH = "push" /** The level of a failed request or `/init` by its HTTP code: `error` on `4xx`, otherwise `warning`. */ fun failureLevel(status: Int?): LogLevel = if (status != null && status in 400..499) LogLevel.ERROR else LogLevel.WARNING } + +/** Values of `context.reason` that the logging spec names. */ +internal object LogReasons { + const val OFFLINE = "offline" + const val NOT_SDK_PUSH = "not_sdk_push" + const val UNKNOWN_SHOP = "unknown_shop" + const val AMBIGUOUS_SHOP = "ambiguous_shop" + const val APP_DECLINED = "app_declined" + const val AUTO_PRESENTATION_OFF = "auto_presentation_off" +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt index 78681e33..4401d30c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt @@ -112,6 +112,8 @@ class PushTokenManager @Inject constructor( */ fun onTokenReceived(token: String, provider: PushProvider) { if (token.isEmpty() || !::context.isInitialized) return + // Never the token itself. + SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_TOKEN_RECEIVED, "Push token received", logShopId, mapOf("provider" to provider.id)) val currentDate = Date().time if (shouldSendToken(provider, token, currentDate) && claimInFlight(provider, token)) { diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt index 19693585..8786dc08 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt @@ -169,11 +169,14 @@ class CoreHttpClientTest { @Test fun `without a network a request fails at once and is never sent`() { + SdkLogs.register("shop", LogLevel.DEBUG, "CoreTag") connectivity.set(false) reply(200, "{}") assertEquals(SdkError.Network, send().error()) assertEquals(0, server.requestCount) + val log = ShadowLog.getLogsForTag("CoreTag").joinToString("\n") { it.msg } + assertTrue(log, log.contains("[request_failed] GET products/get: no network {method=GET, path=products/get, reason=offline}")) } @Test diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt index 0022a9ae..ffce0536 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt @@ -1,6 +1,11 @@ package com.personalization.core.internal +import com.personalization.LogLevel +import com.personalization.SdkLogHandler +import com.personalization.SdkLogMessage import com.personalization.api.OnApiCallbackListener +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import com.personalization.sdk.domain.models.NetworkMethod import com.personalization.sdk.domain.repositories.UserSettingsRepository import io.mockk.every @@ -21,6 +26,7 @@ import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.CountDownLatch +import java.util.concurrent.Executor import java.util.concurrent.TimeUnit /** The new core behind the old callbacks: one answer per request, the old codes, and release. */ @@ -48,8 +54,12 @@ class CoreV2Test { fun tearDown() { core.release() server.close() + SdkLogs.reset() + SdkLogs.handlerExecutor = defaultExecutor } + private val defaultExecutor = SdkLogs.handlerExecutor + @Test fun `answers come back as the old callbacks get them`() { server.enqueue(MockResponse().setResponseCode(200).setBody("""{"id":1}""")) @@ -100,6 +110,27 @@ class CoreV2Test { assertEquals(1, server.requestCount) } + @Test + fun `a request release cancels is logged at debug by method and path`() { + val logged = CopyOnWriteArrayList() + SdkLogs.handlerExecutor = Executor { it.run() } + SdkLogs.register("shop", LogLevel.DEBUG, "SDK") + SdkLogs.handler = SdkLogHandler { logged += it } + server.enqueue(MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE)) + val inFlight = Recorder() + core.send(NetworkMethod.GET("products/get"), JSONObject().put("email", "a@example.com"), inFlight) + server.takeRequest(5, TimeUnit.SECONDS) + + core.release() + inFlight.await() + + val cancelled = logged.filter { it.code == LogCodes.REQUEST_CANCELLED } + assertEquals(1, cancelled.size) + assertEquals(LogLevel.DEBUG, cancelled.single().level) + assertEquals(mapOf("method" to "GET", "path" to "products/get"), cancelled.single().context) + assertTrue(logged.none { "a@example.com" in it.message || "a@example.com" in it.context.toString() }) + } + @Test fun `a request without a listener still goes`() { server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/logs/LogCodesTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/logs/LogCodesTest.kt index 15db4650..83b1cf33 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/logs/LogCodesTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/logs/LogCodesTest.kt @@ -1,11 +1,32 @@ package com.personalization.logs +import android.os.Bundle +import android.os.Looper +import android.provider.Settings import androidx.test.core.app.ApplicationProvider +import com.google.firebase.messaging.RemoteMessage import com.personalization.LogLevel +import com.personalization.MessagingService +import com.personalization.PushEventType +import com.personalization.PushProvider import com.personalization.Rees46 import com.personalization.RegisterManager +import com.personalization.SDK import com.personalization.SdkLogMessage +import com.personalization.SdkRegistry import com.personalization.api.OnApiCallbackListener +import com.personalization.features.inAppNotification.impl.ForegroundActivity +import com.personalization.features.inAppNotification.impl.InAppNotificationManagerImpl +import com.personalization.features.notification.domain.data.NotificationDataExtractor +import com.personalization.features.notification.domain.data.NotificationDataSender +import com.personalization.features.notification.domain.helpers.NotificationClickProcessor +import com.personalization.push.PushTokenManager +import com.personalization.sdk.data.models.dto.popUp.CloseAction +import com.personalization.sdk.data.models.dto.popUp.Components +import com.personalization.sdk.data.models.dto.popUp.PopupActions +import com.personalization.sdk.data.models.dto.popUp.PopupDto +import com.personalization.sdk.data.models.dto.popUp.Position +import dagger.Lazy import io.mockk.Runs import io.mockk.every import io.mockk.just @@ -19,6 +40,7 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config import java.lang.reflect.Modifier import java.util.concurrent.CopyOnWriteArrayList @@ -35,6 +57,8 @@ class LogCodesTest { @Before fun setUp() { SdkLogs.reset() + SdkRegistry.reset() + Rees46.reset() previousExecutor = SdkLogs.handlerExecutor SdkLogs.handlerExecutor = Executor { it.run() } Rees46.setLogHandler { received += it } @@ -44,9 +68,15 @@ class LogCodesTest { @After fun tearDown() { SdkLogs.reset() + SdkRegistry.reset() + Rees46.reset() SdkLogs.handlerExecutor = previousExecutor + val context = ApplicationProvider.getApplicationContext() + Settings.System.putString(context.contentResolver, "firebase.test.lab", null) } + private fun codes(): List> = received.map { it.code to it.level } + @Test fun `every code is in the table of the spec`() { val codes = LogCodes::class.java.declaredFields @@ -83,6 +113,144 @@ class LogCodesTest { assertEquals(listOf(LogCodes.INIT_SUCCEEDED to LogLevel.INFO), received.map { it.code to it.level }) } + @Test + fun `init is skipped at info on a Google Play pre-launch report device`() { + val context = ApplicationProvider.getApplicationContext() + Settings.System.putString(context.contentResolver, "firebase.test.lab", "true") + val network = mockk(relaxed = true) + val settings = mockk(relaxed = true) + every { settings.getDid() } returns "" + every { settings.getShopId() } returns "shop" + val manager = RegisterManager(mockk(relaxed = true), settings, network, mockk(relaxed = true), mockk(relaxed = true), mockk(relaxed = true)) + + manager.initialize(context, context.contentResolver, autoSendPushToken = false) + + assertEquals(listOf(LogCodes.INIT_SKIPPED to LogLevel.INFO), codes()) + io.mockk.verify(exactly = 0) { network.get(eq("init"), any(), any()) } + } + + @Test + fun `a notification-only FCM message is dropped at debug as not the SDK's`() { + val message = mockk(relaxed = true) + every { message.data } returns emptyMap() + every { message.notification } returns mockk(relaxed = true) + + MessagingService().onMessageReceived(message) + + assertEquals(listOf(LogCodes.PUSH_DROPPED to LogLevel.DEBUG), codes()) + assertEquals(LogReasons.NOT_SDK_PUSH, received.single().context["reason"]) + } + + @Test + fun `a clicked notification without type or code is dropped at debug as not the SDK's`() { + NotificationClickProcessor(NotificationDataExtractor(), NotificationDataSender()) + .processClick(Bundle().apply { putString("title", "another sender") }, { _, _ -> }, { _, _ -> }) + + assertEquals(listOf(LogCodes.PUSH_DROPPED to LogLevel.DEBUG), codes()) + assertEquals(LogReasons.NOT_SDK_PUSH, received.single().context["reason"]) + } + + @Test + fun `a push no shop takes is a warning that says why`() { + Rees46.handlePush(mapOf("shop_id" to "nobody", "type" to "bulk"), PushEventType.RECEIVED) + SdkRegistry.register("shop-a", SDK()) + SdkRegistry.register("shop-b", SDK()) + Rees46.handlePush(mapOf("type" to "bulk"), PushEventType.RECEIVED) + SDK.onMessage(mapOf("shop_id" to "nobody")) + SDK.onMessage(mapOf("type" to "bulk")) + + assertEquals(List(4) { LogCodes.PUSH_DROPPED to LogLevel.WARNING }, codes()) + assertEquals( + listOf(LogReasons.UNKNOWN_SHOP, LogReasons.AMBIGUOUS_SHOP, LogReasons.UNKNOWN_SHOP, LogReasons.AMBIGUOUS_SHOP), + received.map { it.context["reason"] } + ) + } + + @Test + fun `a data message is a push step until its shop takes it`() { + SdkRegistry.register("shop", SDK()) + val message = mockk(relaxed = true) + every { message.data } returns mapOf("shop_id" to "shop", "type" to "bulk", "id" to "1") + every { message.notification } returns null + + MessagingService().onMessageReceived(message) + + // The bare instance has not been initialized, so its delivery is not tracked: one more push step. + assertEquals( + listOf(LogCodes.PUSH to LogLevel.DEBUG, LogCodes.PUSH_RECEIVED to LogLevel.DEBUG, LogCodes.PUSH to LogLevel.WARNING), + codes() + ) + assertEquals("shop", received[1].shopId) + } + + @Test + fun `a push token from a provider is logged at debug without the token`() { + val context = ApplicationProvider.getApplicationContext() + val tokens = PushTokenManager(mockk(relaxed = true), mockk(relaxed = true), mockk(relaxed = true)) + tokens.logShopId = "shop" + tokens.initialize(context, autoSendPushToken = false) + received.clear() + + tokens.onTokenReceived("secret-token", PushProvider.FCM) + + assertEquals(listOf(LogCodes.PUSH_TOKEN_RECEIVED to LogLevel.DEBUG), codes()) + assertEquals("fcm", received.single().context["provider"]) + assertTrue(received.none { "secret-token" in it.message || "secret-token" in it.context.toString() }) + } + + @Test + fun `a popup the app keeps back is debug, one that cannot be shown a warning`() { + val context = ApplicationProvider.getApplicationContext() + ForegroundActivity.install(context) + val settings = mockk(relaxed = true) + every { settings.getShopId() } returns "shop" + val manager = InAppNotificationManagerImpl(context, settings, Lazy { mockk(relaxed = true) }) + + // A popup goes on the main thread and reads the settings there. + manager.presentation.listener = { null } + manager.shopPopUp(popup(1)) + shadowOf(Looper.getMainLooper()).idle() + manager.presentation.listener = null + manager.presentation.autoPresentation = false + manager.shopPopUp(popup(2)) + shadowOf(Looper.getMainLooper()).idle() + manager.presentation.autoPresentation = true + manager.shopPopUp(popup(3)) // no activity on screen + shadowOf(Looper.getMainLooper()).idle() + + assertEquals( + listOf( + LogCodes.POPUP_NOT_SHOWN to LogLevel.DEBUG, + LogCodes.POPUP_NOT_SHOWN to LogLevel.DEBUG, + LogCodes.POPUP_NOT_SHOWN to LogLevel.WARNING, + ), + codes() + ) + assertEquals( + listOf(LogReasons.APP_DECLINED, LogReasons.AUTO_PRESENTATION_OFF, "no_activity"), + received.map { it.context["reason"] } + ) + } + + private fun popup(id: Int) = PopupDto( + id = id, + channels = emptyList(), + position = Position.CENTERED, + delay = 0, + html = "", + components = Components( + text = "Body", + image = "", + button = "", + header = "Title", + textEnabled = "true", + imageEnabled = "false", + headerEnabled = "true" + ), + webPushSystem = false, + popupActions = PopupActions(link = null, close = CloseAction("Close"), pushSubscribe = null) + ) + private fun startInit(): OnApiCallbackListener { val network = mockk(relaxed = true) val settings = mockk(relaxed = true) @@ -106,7 +274,8 @@ class LogCodesTest { "push_received", "push_dropped", "push_failed", "push_permission", "push_permission_failed", "push_token_sent", "push_token_skipped", "push_token_failed", "advertising_id_sent", "advertising_id_failed", "popup_not_shown", "core_flag_mismatch", "not_implemented", - "sdk_log", "ui", "stories", "stories_v3", "popups_v3", + "request_cancelled", "init_skipped", "push_token_received", + "sdk_log", "ui", "stories", "stories_v3", "popups_v3", "push", ) } } From ba071e3bb8c9792964bbb251caaa65d1c3979791 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 23:13:01 +0400 Subject: [PATCH 023/109] feat(core): storage for the new core: a device-level store (personalization_sdk.device) takes the A/B group of the first shop that has one, the push tokens and the advertising id, while each shop's keys keep being written so the legacy path reads what it read; a shop without a group gets the device's; each shop's partition holds one-shot migration flags, the refused state, the last /init answer (read and write, in noBackupFilesDir) and its queue's directory; Rees46Config.storageKey prefixes the partition (_personalization_sdk_), and the file named exactly that key, the whole store of SDK.initialize(preferencesKey) in earlier releases, is read into it once and kept; the file all shops shared before partitions goes into each shop at most once; a did is committed before the write returns, and disk failures are storage_failed with context.op --- .../personalization/PreferencesPartition.kt | 6 +- .../main/kotlin/com/personalization/Rees46.kt | 2 + .../com/personalization/Rees46Config.kt | 14 +- .../main/kotlin/com/personalization/SDK.kt | 34 ++-- .../core/internal/DeviceStorage.kt | 70 ++++++++ .../core/internal/ShopStorage.kt | 154 ++++++++++++++++++ .../core/internal/StorageKeys.kt | 71 ++++++++ .../core/internal/StorageLayout.kt | 37 +++++ .../core/internal/StorageMigrations.kt | 133 +++++++++++++++ .../preferences/PreferencesDataSource.kt | 3 +- .../preferences/PreferencesDataSourceImpl.kt | 109 +++++-------- .../TrackingSourceDataSourceImpl.kt | 7 +- .../UserSettings.repository.impl.kt | 22 +-- .../InitializeAdvertisingIdUseCase.kt | 7 +- 14 files changed, 571 insertions(+), 98 deletions(-) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/DeviceStorage.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageLayout.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageMigrations.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/PreferencesPartition.kt b/personalization-sdk/src/main/kotlin/com/personalization/PreferencesPartition.kt index 1ed49046..3cc749c9 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/PreferencesPartition.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/PreferencesPartition.kt @@ -4,9 +4,9 @@ package com.personalization * Naming of the SDK's SharedPreferences partitions. * * Release 2 of multi-instance: storage is partitioned per `shop_id` so instances do not share one - * file. Transparent to hosts — a host that leaves `SDK.initialize(preferencesKey)` at its default - * gets a shop-scoped partition plus a one-time migration out of the old shared file, so an existing - * install keeps its `did`/`sid`. A host that passes its own `preferencesKey` keeps using it verbatim. + * file. Transparent to hosts — an existing install's data is read once into the shop's partition, + * so it keeps its `did`/`sid`. With an app's `storageKey` (formerly `SDK.initialize(preferencesKey)`) + * the partition name gets that prefix; see [com.personalization.core.internal.StorageLayout]. */ internal object PreferencesPartition { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt index ce227aa1..c901f860 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt @@ -47,6 +47,8 @@ object Rees46 { shopId = config.shopId, apiDomain = config.apiDomain, tag = config.tag, + // The prefix of the shop's partition; a file of the same name is read once as its old data. + preferencesKey = config.storageKey ?: PreferencesPartition.LEGACY_KEY, stream = config.stream, autoSendPushToken = config.autoSendPushToken, needReInitialization = config.needReInitialization, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt index 885a3b59..46f02be6 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt @@ -4,8 +4,9 @@ package com.personalization * Configuration for one SDK instance (one shop). * * The unified initialization input shared by [Rees46.initialize] and [Rees46.registerShops]. Mirrors - * the parameters of the legacy `SDK.initialize`, minus the Android `Context` (passed separately) and - * the preferences key (storage is partitioned per [shopId] automatically — see [PreferencesPartition]). + * the parameters of the legacy `SDK.initialize`, minus the Android `Context` (passed separately); its + * `preferencesKey` is [storageKey] here. Storage is partitioned per [shopId] automatically — see + * [PreferencesPartition]. * * @property shopId the shop key; also the partition key and the id used by [Rees46.getInstance]. * @property apiDomain API host the instance talks to (a region points here). @@ -24,6 +25,14 @@ package com.personalization * network client only: requests go through OkHttp, a request without a network fails at once, * `release()` cancels the instance's requests, and a failure without an answer is `onError(-1)` (no * connection used to be `504`), as is a `2xx` whose body is not JSON (it used to succeed empty). + * @property storageKey a prefix for the shop's storage, when the app needs its own split of it: the + * shop's `SharedPreferences` file becomes `_personalization_sdk_` instead of + * `personalization_sdk_`. A file named exactly [storageKey] (the whole store of + * `SDK.initialize(preferencesKey = …)` in earlier releases) is read into it once and left as it is, + * so an app moving its `preferencesKey` here keeps its customers. The device-level data (A/B group, + * push token, advertising id) is the app's and has no prefix. Android's Auto Backup restores these + * files on the user's new device, the `did` among them; see [SDK.deleteUserCredentials] for the files + * an app excludes from its backup rules to avoid that. * * Constructors are overloaded for Java: every trailing parameter with a default may be left out, so * Java code written against the seven-argument constructor keeps compiling as parameters are added. @@ -39,4 +48,5 @@ data class Rees46Config @JvmOverloads constructor( val enableAutoPopupPresentation: Boolean = true, val logLevel: LogLevel = LogLevel.WARNING, val coreV2: Boolean = false, + val storageKey: String? = null, ) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index d29c1817..d6a25241 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -30,6 +30,9 @@ import com.personalization.core.internal.AndroidConnectivityMonitor import com.personalization.core.internal.Core import com.personalization.core.internal.CoreSelection import com.personalization.core.internal.CoreV2 +import com.personalization.core.internal.ShopStorage +import com.personalization.core.internal.StorageLayout +import com.personalization.core.internal.StorageMigrations import com.personalization.di.AppModule import com.personalization.di.DaggerSdkComponent import com.personalization.features.inAppNotification.impl.ForegroundActivity @@ -181,6 +184,11 @@ open class SDK { internal var core: Core? = null private set + /** The shop's partition, once the instance is initialized. */ + @Volatile + internal var storage: ShopStorage? = null + private set + /** The shop this instance logs for, once it is initialized: its lines pass that shop's level. */ @Volatile internal var logShopId: String? = null @@ -245,6 +253,9 @@ open class SDK { /** * @param shopId Shop key + * @param preferencesKey the prefix of the shop's storage, as [Rees46Config.storageKey]. Earlier + * releases kept everything in one file of this name; it is read once into the shop's partition and + * left as it is. */ @Deprecated( message = "Use Rees46.initialize(context, Rees46Config(...)) — the unified, multi-instance " + @@ -270,7 +281,7 @@ open class SDK { shopId = shopId, apiDomain = apiDomain, tag = tag, - preferencesKey = preferencesKey, + storageKey = preferencesKey.takeUnless { it == PreferencesPartition.LEGACY_KEY }, stream = stream, autoSendPushToken = autoSendPushToken, needReInitialization = needReInitialization, @@ -294,7 +305,7 @@ open class SDK { shopId = config.shopId, apiDomain = config.apiDomain, tag = config.tag, - preferencesKey = PreferencesPartition.LEGACY_KEY, + storageKey = config.storageKey, stream = config.stream, autoSendPushToken = false, needReInitialization = false, @@ -308,7 +319,7 @@ open class SDK { shopId: String, apiDomain: String, tag: String, - preferencesKey: String, + storageKey: String?, stream: String, autoSendPushToken: Boolean, needReInitialization: Boolean, @@ -342,18 +353,17 @@ open class SDK { onPushTokenListener?.let { pushTokenManager.setOnPushTokenListener(it) } - // A host that leaves preferencesKey at its default gets a per-shop partition instead of the - // single shared file, plus a one-time migration out of the legacy file so an existing - // install keeps its did/sid. A host that passes its own key keeps using it verbatim, with no - // derivation and no migration. - val usingDefaultPartition = preferencesKey == PreferencesPartition.LEGACY_KEY - val effectivePreferencesKey = - if (usingDefaultPartition) PreferencesPartition.keyFor(shopId) else preferencesKey + // The shop's own partition, prefixed by the app's storageKey if it has one. Before /init, the + // data of older releases is read into it once (the file of a custom preferencesKey, or the file + // all shops shared before partitions), and the device-level store gets the A/B group, push + // tokens and advertising id. Nothing older is deleted. + val layout = StorageLayout(shopId, storageKey) + StorageMigrations.run(context, layout) + storage = ShopStorage(context, layout) initPreferencesUseCase.invoke( context = context, - preferencesKey = effectivePreferencesKey, - legacyPreferencesKey = if (usingDefaultPartition) PreferencesPartition.LEGACY_KEY else null, + preferencesKey = layout.partitionName, shopId = shopId ) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/DeviceStorage.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/DeviceStorage.kt new file mode 100644 index 00000000..d8bd1f2e --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/DeviceStorage.kt @@ -0,0 +1,70 @@ +package com.personalization.core.internal + +import android.content.Context +import android.content.SharedPreferences + +/** + * What belongs to the device rather than to a shop or a customer: the A/B group, the push tokens and + * the advertising id. One file for the app, shared by every shop and every [StorageLayout.prefix]. + * + * Each shop's partition keeps its own copy under the keys the legacy path reads ([StorageKeys]), so + * nothing the legacy path reads changes: the device level is written beside it. + * + * The A/B group is the first shop's: a device has one group, and the group a shop already sends keeps + * being sent ([adoptSegment]). + */ +internal class DeviceStorage(context: Context) { + + private val preferences: SharedPreferences = + context.applicationContext.getSharedPreferences(NAME, Context.MODE_PRIVATE) + + /** The device's A/B group, or null before any shop had one. */ + fun segment(): String? = preferences.getString(SEGMENT, null)?.takeIf { it.isNotEmpty() } + + /** + * The device's A/B group: the one it has, or [candidate] if it has none yet. The first caller in + * the process wins, so two shops starting together agree on one group. + */ + fun adoptSegment(candidate: String): String = synchronized(LOCK) { + segment() ?: candidate.also { preferences.edit().putString(SEGMENT, it).apply() } + } + + /** The last push token of [provider] (`fcm`, `hms`) the device has, or null. */ + fun pushToken(provider: String): String? = preferences.getString(StorageKeys.pushToken(provider), null)?.takeIf { it.isNotEmpty() } + + fun savePushToken(provider: String, token: String) { + if (token.isEmpty()) return + preferences.edit().putString(StorageKeys.pushToken(provider), token).apply() + } + + /** Takes [token] only while the device has no token of [provider]: a newer one is never replaced by a copy. */ + fun adoptPushToken(provider: String, token: String) = synchronized(LOCK) { + if (pushToken(provider) == null) savePushToken(provider, token) + } + + /** The advertising id the device last reported, or null. */ + fun advertisingId(): String? = preferences.getString(StorageKeys.ADVERTISING_ID, null)?.takeIf { it.isNotEmpty() } + + fun saveAdvertisingId(id: String) { + if (id.isEmpty()) return + preferences.edit().putString(StorageKeys.ADVERTISING_ID, id).apply() + } + + /** Takes [id] only while the device has none. */ + fun adoptAdvertisingId(id: String) = synchronized(LOCK) { + if (advertisingId() == null) saveAdvertisingId(id) + } + + internal companion object { + /** + * The file of the device level. The dot keeps it apart from every partition: a shop id's dots + * become underscores in a partition's name. + */ + const val NAME = "personalization_sdk.device" + + const val SEGMENT = StorageKeys.SEGMENT + + /** One lock for the process: the instances of every shop share the file. */ + private val LOCK = Any() + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt new file mode 100644 index 00000000..4cff2823 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt @@ -0,0 +1,154 @@ +package com.personalization.core.internal + +import android.content.Context +import android.content.SharedPreferences +import android.util.AtomicFile +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import org.json.JSONException +import org.json.JSONObject +import java.io.File +import java.io.IOException + +/** + * A shop's partition: its `SharedPreferences` file ([StorageLayout.partitionName]) and its directory + * under `noBackupFilesDir`. Beside what the legacy path keeps there ([StorageKeys]) it holds the + * core's state: the last successful `/init` answer, the refused state and the queue's directory. + * + * The saved answer and the queue are files in `noBackupFilesDir`: Android's Auto Backup never copies + * them to another device, and a large answer stays out of the preferences file that every request + * rewrites. A disk failure is logged as `storage_failed` with the operation in `context.op`; nothing + * here throws into the caller. + */ +internal class ShopStorage(context: Context, val layout: StorageLayout) { + + private val appContext: Context = context.applicationContext + + val preferences: SharedPreferences = appContext.getSharedPreferences(layout.partitionName, Context.MODE_PRIVATE) + + /** The shop's files. */ + val directory: File = File(File(appContext.noBackupFilesDir, ROOT_DIRECTORY), layout.partitionName) + + private val initAnswerFile = AtomicFile(File(directory, INIT_ANSWER_FILE)) + private val fileLock = Any() + + /** The last successful `/init` answer, or null when there is none or it cannot be read. */ + fun initAnswer(): JSONObject? = synchronized(fileLock) { + if (!initAnswerFile.baseFile.exists()) return null + try { + JSONObject(String(initAnswerFile.readFully(), Charsets.UTF_8)) + } catch (e: IOException) { + failed(OP_READ_INIT_ANSWER, e) + null + } catch (e: JSONException) { + // A file cut short: the answer is gone, the next /init writes a new one. + failed(OP_READ_INIT_ANSWER, e) + initAnswerFile.delete() + null + } + } + + /** Replaces the saved `/init` answer; false when the disk refused it. */ + fun saveInitAnswer(answer: JSONObject): Boolean = synchronized(fileLock) { + if (!ensureDirectory(OP_WRITE_INIT_ANSWER)) return false + val stream = try { + initAnswerFile.startWrite() + } catch (e: IOException) { + failed(OP_WRITE_INIT_ANSWER, e) + return false + } + try { + stream.write(answer.toString().toByteArray(Charsets.UTF_8)) + initAnswerFile.finishWrite(stream) + true + } catch (e: IOException) { + initAnswerFile.failWrite(stream) + failed(OP_WRITE_INIT_ANSWER, e) + false + } + } + + /** Whether the shop refused the SDK; it stays refused across launches until a `/init` succeeds. */ + var isRefused: Boolean + get() = preferences.getBoolean(StorageKeys.REFUSED, false) + set(value) { + preferences.edit().putBoolean(StorageKeys.REFUSED, value).apply() + } + + /** The directory of the shop's event queue, created if needed; null when the disk refused it. */ + fun queueDirectory(): File? { + val queue = File(directory, QUEUE_DIRECTORY) + if (queue.isDirectory) return queue + return try { + if (queue.mkdirs() || queue.isDirectory) queue else null.also { failed(OP_CREATE_QUEUE, null) } + } catch (e: SecurityException) { + failed(OP_CREATE_QUEUE, e) + null + } + } + + /** + * Forgets the customer: [StorageKeys.USER_CREDENTIALS] and the saved `/init` answer, and with + * [includeQueue] the queue's directory. The A/B group, the push tokens, the advertising id, the + * stories' state and the migration flags stay. Written with `commit()`: a crash right after must + * not bring the customer back. False when part of it did not reach the disk. + */ + fun deleteUserCredentials(includeQueue: Boolean): Boolean { + var complete = attempt(OP_DELETE_CREDENTIALS) { + val editor = preferences.edit() + StorageKeys.USER_CREDENTIALS.forEach { editor.remove(it) } + editor.commit() + } + complete = attempt(OP_DELETE_INIT_ANSWER) { + synchronized(fileLock) { + // Also drops a write left half-done, so it cannot come back as the answer. + initAnswerFile.delete() + !initAnswerFile.baseFile.exists() + } + } && complete + if (includeQueue) { + complete = attempt(OP_DELETE_QUEUE) { + val queue = File(directory, QUEUE_DIRECTORY) + !queue.exists() || queue.deleteRecursively() + } && complete + } + return complete + } + + /** Runs [block]; a false result or an exception is logged under [op]. */ + private inline fun attempt(op: String, block: () -> Boolean): Boolean { + val done = try { + block() + } catch (e: Exception) { + failed(op, e) + return false + } + if (!done) failed(op, null) + return done + } + + private fun ensureDirectory(op: String): Boolean { + if (directory.isDirectory || directory.mkdirs() || directory.isDirectory) return true + failed(op, null) + return false + } + + private fun failed(op: String, throwable: Throwable?) { + SdkLogs.log(LogLevel.WARNING, LogCodes.STORAGE_FAILED, "Storage operation failed: $op", layout.shopId, mapOf("op" to op), throwable) + } + + internal companion object { + /** Under `noBackupFilesDir`: one directory per partition. */ + const val ROOT_DIRECTORY = "personalization_sdk" + const val INIT_ANSWER_FILE = "init.json" + const val QUEUE_DIRECTORY = "queue" + + const val OP_READ_INIT_ANSWER = "read_init_answer" + const val OP_WRITE_INIT_ANSWER = "write_init_answer" + const val OP_CREATE_QUEUE = "create_queue_directory" + const val OP_DELETE_CREDENTIALS = "delete_user_credentials" + const val OP_DELETE_INIT_ANSWER = "delete_init_answer" + const val OP_DELETE_QUEUE = "delete_queue" + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt new file mode 100644 index 00000000..96998cb7 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt @@ -0,0 +1,71 @@ +package com.personalization.core.internal + +/** + * The keys of a shop's partition, in one place: the legacy path reads and writes them, the migrations + * copy them and [ShopStorage.deleteUserCredentials] knows which of them belong to the customer. + * + * Every key that has shipped keeps its name and meaning: an installed app has data under it. + */ +internal object StorageKeys { + + // What the shipped SDK stores per shop. `LegacyCore` reads these. + const val DID = "did" + const val SID = "sid" + const val SID_LAST_ACT = "sid_last_act" + const val SHOP_ID = "shop_id" + const val SEGMENT = "segment_ab_testing" + const val STREAM = "stream" + const val ADVERTISING_ID = "google_ad_id" + const val TRACKING_SOURCE_TYPE = "tracking_source_type" + const val TRACKING_SOURCE_CODE = "tracking_source_code" + const val TRACKING_SOURCE_SAVED_AT = "tracking_source_saved_at" + + /** The push token of a provider (`fcm`, `hms`) as last sent from this shop. */ + fun pushToken(provider: String): String = "push_token_$provider" + + /** When the shop last sent the push token of a provider. */ + fun pushTokenDate(provider: String): String = "push_token_date_$provider" + + /** The token keys of releases before the provider was part of the key; still read as a fallback. */ + val LEGACY_PUSH_TOKEN: Map = mapOf("fcm" to "token", "hms" to "hms_token") + + /** The send-date keys of releases before the provider was part of the key; still read as a fallback. */ + val LEGACY_PUSH_TOKEN_DATE: Map = mapOf("fcm" to "last_push_token_date", "hms" to "last_hms_push_token_date") + + /** The providers whose token the SDK stores. */ + val PUSH_PROVIDERS: List = listOf("fcm", "hms") + + // The core's own state. + /** The shop refused the SDK (`402`, `403`, `404` or another `4xx` on `/init`). */ + const val REFUSED = "refused" + + // One-shot migrations: each marks itself here once it has run, so it never runs twice for a shop. + /** The old full-name file (`preferencesKey` of `SDK.initialize`) was read into the partition. */ + const val MIGRATED_NAMED_FILE = "migrated_named_file" + + /** The shared file of the releases before per-shop partitions was read into the partition. */ + const val MIGRATED_LEGACY_FILE = "migrated_legacy_file" + + /** The shop's A/B group, push tokens and advertising id were offered to the device-level store. */ + const val MIGRATED_DEVICE_LEVEL = "migrated_device_level" + + val MIGRATION_FLAGS: Set = setOf(MIGRATED_NAMED_FILE, MIGRATED_LEGACY_FILE, MIGRATED_DEVICE_LEVEL) + + /** + * What `deleteUserCredentials()` wipes from the partition: the customer's identity and session, + * the source `tracking.setSource` set, the dates the push token was sent (so the token goes to the + * next customer), and the refused state. The A/B group, the push tokens, the advertising id, the + * stream, the shop id and the migration flags stay: they belong to the device or the app. + */ + val USER_CREDENTIALS: List = buildList { + add(DID) + add(SID) + add(SID_LAST_ACT) + add(TRACKING_SOURCE_TYPE) + add(TRACKING_SOURCE_CODE) + add(TRACKING_SOURCE_SAVED_AT) + PUSH_PROVIDERS.forEach { add(pushTokenDate(it)) } + addAll(LEGACY_PUSH_TOKEN_DATE.values) + add(REFUSED) + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageLayout.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageLayout.kt new file mode 100644 index 00000000..5baa4fd0 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageLayout.kt @@ -0,0 +1,37 @@ +package com.personalization.core.internal + +import com.personalization.PreferencesPartition + +/** + * Where a shop's data lives, and where the data of older releases is read from. + * + * The partition is the shop's own `SharedPreferences` file: `personalization_sdk_`, or with a + * [storageKey] `_personalization_sdk_`. Its files (the saved `/init` answer, the + * queue) live in a directory of the same name under `noBackupFilesDir`. + * + * Older releases are read once into a fresh partition ([StorageMigrations]): + * - with a [storageKey], the file named exactly that: what `SDK.initialize(preferencesKey = …)` used + * as the whole store, one file with no per-shop partition; + * - without one, the file shared by every shop before partitions ([PreferencesPartition.LEGACY_KEY]). + */ +internal class StorageLayout(val shopId: String, storageKey: String?) { + + /** The app's prefix, or null for the SDK's default layout. */ + val prefix: String? = storageKey?.takeIf { it.isNotBlank() && it != PreferencesPartition.LEGACY_KEY } + + /** The name of the shop's `SharedPreferences` file. */ + val partitionName: String = + prefix?.let { sanitize(it) + "_" + PreferencesPartition.keyFor(shopId) } ?: PreferencesPartition.keyFor(shopId) + + /** The old full-name file, read once into the partition. */ + val namedFile: String? = prefix?.takeIf { it != partitionName } + + /** The file of the releases before partitions, read once into a default partition. */ + val legacyFile: String? = if (prefix == null) PreferencesPartition.LEGACY_KEY else null + + private companion object { + val UNSAFE_FILENAME_CHARS = Regex("[^A-Za-z0-9_-]") + + fun sanitize(value: String): String = value.replace(UNSAFE_FILENAME_CHARS, "_") + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageMigrations.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageMigrations.kt new file mode 100644 index 00000000..9d919394 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageMigrations.kt @@ -0,0 +1,133 @@ +package com.personalization.core.internal + +import android.content.Context +import android.content.SharedPreferences +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs + +/** + * The one-shot migrations of a shop's partition, run on every start before `/init`. Each runs at most + * once per shop: it marks itself with a flag in the partition ([StorageKeys.MIGRATION_FLAGS]), in the + * same write as what it copied. The sources are only read, never changed or deleted. + * + * 1. **The old full-name file** ([StorageLayout.namedFile]): what `SDK.initialize(preferencesKey = …)` + * used as the whole store. Read into a partition that has no data yet, whatever shop it last + * belonged to: two shops that shared such a file shared its `did`, and keep sharing it. + * 2. **The file shared by every shop before partitions** ([StorageLayout.legacyFile]): read into a + * default partition that has no data yet, when it belongs to the shop or names no shop at all (a + * very old install). A file without a shop goes to each shop that starts with an empty partition, + * as before; the flag keeps a shop from taking it again after its data was deleted. + * 3. **The device level** ([DeviceStorage]): the shop offers its A/B group, push tokens and advertising + * id, which the device takes while it has none of its own. A shop without an A/B group takes the + * device's, so every new shop of the device sends the same group. + * + * The writes go through `apply()`: the copy and its flag are one write, so a crash loses both or + * neither, and the next start runs the migration again. + */ +internal object StorageMigrations { + + /** Runs what has not run yet for [layout]'s shop. A failure is logged and never stops the start. */ + fun run(context: Context, layout: StorageLayout, device: DeviceStorage = DeviceStorage(context)) { + val shopId = layout.shopId + val partition = open(context, layout.partitionName, shopId, OP_OPEN_PARTITION) ?: return + layout.namedFile?.let { name -> + step(shopId, OP_MIGRATE_NAMED_FILE) { + copyOnce(context, name, partition, StorageKeys.MIGRATED_NAMED_FILE, shopId, belongsTo = { true }) + } + } + layout.legacyFile?.let { name -> step(shopId, OP_MIGRATE_LEGACY_FILE) { migrateLegacyFile(context, name, partition, shopId) } } + step(shopId, OP_MIGRATE_DEVICE_LEVEL) { adoptDeviceLevel(partition, device) } + } + + /** Step 2 alone, for a [partition] opened by name ([com.personalization.sdk.data.repositories.preferences.PreferencesDataSource]). */ + fun migrateLegacyFile(context: Context, legacyName: String, partition: SharedPreferences, shopId: String) { + copyOnce(context, legacyName, partition, StorageKeys.MIGRATED_LEGACY_FILE, shopId) { source -> + val owner = source.getString(StorageKeys.SHOP_ID, "").orEmpty() + owner.isEmpty() || owner == shopId + } + } + + /** Whether [partition] holds anything besides migration flags. */ + internal fun hasData(partition: SharedPreferences): Boolean = + partition.all.keys.any { it !in StorageKeys.MIGRATION_FLAGS } + + private fun copyOnce( + context: Context, + sourceName: String, + partition: SharedPreferences, + flag: String, + shopId: String, + belongsTo: (SharedPreferences) -> Boolean, + ) { + if (partition.getBoolean(flag, false)) return + val editor = partition.edit() + // A partition that already has data keeps it: it is newer than any file of an older release. + if (!hasData(partition)) { + val source = open(context, sourceName, shopId, OP_OPEN_SOURCE) + if (source != null && belongsTo(source)) { + source.all.forEach { (key, value) -> if (key !in StorageKeys.MIGRATION_FLAGS) editor.putAny(key, value) } + } + } + editor.putBoolean(flag, true).apply() + } + + private fun adoptDeviceLevel(partition: SharedPreferences, device: DeviceStorage) { + if (partition.getBoolean(StorageKeys.MIGRATED_DEVICE_LEVEL, false)) return + val editor = partition.edit() + val segment = partition.getString(StorageKeys.SEGMENT, null)?.takeIf { it.isNotEmpty() } + if (segment != null) { + // The shop keeps sending its own group; the device takes it if it is the first. + device.adoptSegment(segment) + } else { + editor.putString(StorageKeys.SEGMENT, device.adoptSegment(randomSegment())) + } + StorageKeys.PUSH_PROVIDERS.forEach { provider -> + val token = partition.getString(StorageKeys.pushToken(provider), null)?.takeIf { it.isNotEmpty() } + ?: StorageKeys.LEGACY_PUSH_TOKEN[provider]?.let { partition.getString(it, null) }?.takeIf { it.isNotEmpty() } + token?.let { device.adoptPushToken(provider, it) } + } + partition.getString(StorageKeys.ADVERTISING_ID, null)?.takeIf { it.isNotEmpty() }?.let { device.adoptAdvertisingId(it) } + editor.putBoolean(StorageKeys.MIGRATED_DEVICE_LEVEL, true).apply() + } + + /** The two groups of the server's A/B tests, picked at random once per device. */ + internal fun randomSegment(): String = arrayOf("A", "B").random() + + private fun open(context: Context, name: String, shopId: String, op: String): SharedPreferences? = try { + context.applicationContext.getSharedPreferences(name, Context.MODE_PRIVATE) + } catch (e: Exception) { + // A name with a path separator, an unreadable file: the start goes on without it. + failed(shopId, op, e) + null + } + + private inline fun step(shopId: String, op: String, block: () -> Unit) { + try { + block() + } catch (e: Exception) { + failed(shopId, op, e) + } + } + + private fun failed(shopId: String, op: String, throwable: Throwable) { + SdkLogs.log(LogLevel.WARNING, LogCodes.STORAGE_FAILED, "Storage operation failed: $op", shopId, mapOf("op" to op), throwable) + } + + private fun SharedPreferences.Editor.putAny(key: String, value: Any?) { + when (value) { + is String -> putString(key, value) + is Long -> putLong(key, value) + is Int -> putInt(key, value) + is Float -> putFloat(key, value) + is Boolean -> putBoolean(key, value) + is Set<*> -> putStringSet(key, value.filterIsInstance().toSet()) + } + } + + const val OP_OPEN_PARTITION = "open_partition" + const val OP_OPEN_SOURCE = "open_old_storage" + const val OP_MIGRATE_NAMED_FILE = "migrate_named_file" + const val OP_MIGRATE_LEGACY_FILE = "migrate_legacy_file" + const val OP_MIGRATE_DEVICE_LEVEL = "migrate_device_level" +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt index a8945e76..6fc42fb1 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt @@ -7,7 +7,8 @@ interface PreferencesDataSource { /** * Opens the preferences partition named [preferencesKey]. When [legacyPreferencesKey] and * [shopId] are provided, a one-time migration copies the legacy shared file into the partition - * if it is still empty and the legacy file belongs to [shopId]. + * if it is still empty and the legacy file belongs to [shopId] or names no shop; a flag in the + * partition keeps it from running again. The legacy file is never changed. */ fun initialize( context: Context, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceImpl.kt index 45e268a5..f015ccc2 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceImpl.kt @@ -2,26 +2,25 @@ package com.personalization.sdk.data.repositories.preferences import android.content.Context import android.content.SharedPreferences -import com.personalization.PreferencesPartition +import com.personalization.LogLevel +import com.personalization.core.internal.DeviceStorage +import com.personalization.core.internal.StorageKeys +import com.personalization.core.internal.StorageMigrations +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import javax.inject.Inject import javax.inject.Singleton private const val DEFAULT_TOKEN = "" private const val DEFAULT_LAST_PUSH_TOKEN_DATE = 0L -private const val PUSH_TOKEN_KEY_PREFIX = "push_token_" -private const val PUSH_TOKEN_DATE_KEY_PREFIX = "push_token_date_" - -// Legacy keys kept for read-only fallback so existing installs are not re-registered. -private val LEGACY_TOKEN_KEYS = mapOf("fcm" to "token", "hms" to "hms_token") -private val LEGACY_TOKEN_DATE_KEYS = - mapOf("fcm" to "last_push_token_date", "hms" to "last_hms_push_token_date") - @Singleton class PreferencesDataSourceImpl @Inject constructor() : PreferencesDataSource { private var sharedPreferences: SharedPreferences? = null private var preferencesKey: String? = null + private var device: DeviceStorage? = null + private var shopId: String? = null override fun initialize( context: Context, @@ -31,78 +30,43 @@ class PreferencesDataSourceImpl @Inject constructor() : PreferencesDataSource { ) { val preferences = context.getSharedPreferences(preferencesKey, Context.MODE_PRIVATE) if (legacyPreferencesKey != null && shopId != null) { - migrateLegacyIfNeeded( - context = context, - legacyPreferencesKey = legacyPreferencesKey, - target = preferences, - shopId = shopId - ) + // One-time copy of the pre-partitioning shared file, so an existing single-instance + // install keeps its did/sid/segment/tokens; see StorageMigrations for the guards. + try { + StorageMigrations.migrateLegacyFile(context, legacyPreferencesKey, preferences, shopId) + } catch (e: Exception) { + SdkLogs.log(LogLevel.WARNING, LogCodes.STORAGE_FAILED, "Storage operation failed: ${StorageMigrations.OP_MIGRATE_LEGACY_FILE}", + shopId, mapOf("op" to StorageMigrations.OP_MIGRATE_LEGACY_FILE), e) + } } this.sharedPreferences = preferences this.preferencesKey = preferencesKey - } - - /** - * One-time copy of the pre-partitioning shared preferences into this shop's partition, so an - * existing single-instance install keeps its `did`/`sid`/segment/tokens after upgrading to - * per-shop storage. - * - * Guards keep it safe and idempotent: - * - runs only while the partition is still empty, so it never overwrites data the SDK already - * wrote (and re-running after a completed migration is a no-op); - * - copies the legacy file only when it belongs to this shop — its stored `shop_id` matches, or - * is absent (a very old install). A legacy file left by a different shop is not pulled in, so - * that shop's partition stays clean under multi-instance. - */ - private fun migrateLegacyIfNeeded( - context: Context, - legacyPreferencesKey: String, - target: SharedPreferences, - shopId: String - ) { - if (target.all.isNotEmpty()) return - - val legacy = context.getSharedPreferences(legacyPreferencesKey, Context.MODE_PRIVATE) - val legacyEntries = legacy.all - if (legacyEntries.isEmpty()) return - - val legacyShopId = legacy.getString(PreferencesPartition.SHOP_ID_FIELD, "").orEmpty() - if (legacyShopId.isNotEmpty() && legacyShopId != shopId) return - - with(target.edit()) { - for ((key, value) in legacyEntries) { - when (value) { - is String -> putString(key, value) - is Long -> putLong(key, value) - is Int -> putInt(key, value) - is Float -> putFloat(key, value) - is Boolean -> putBoolean(key, value) - is Set<*> -> putStringSet(key, value.filterIsInstance().toSet()) - } - } - apply() - } + this.device = DeviceStorage(context) + this.shopId = shopId } override fun getPushToken(provider: String): String { - val value = getValue(PUSH_TOKEN_KEY_PREFIX + provider, DEFAULT_TOKEN) + val value = getValue(StorageKeys.pushToken(provider), DEFAULT_TOKEN) if (value.isNotEmpty()) return value - val legacyKey = LEGACY_TOKEN_KEYS[provider] ?: return value + val legacyKey = StorageKeys.LEGACY_PUSH_TOKEN[provider] ?: return value return getValue(legacyKey, DEFAULT_TOKEN) } - override fun savePushToken(provider: String, value: String) = - saveValue(PUSH_TOKEN_KEY_PREFIX + provider, value) + /** The shop's copy, which the legacy path reads, and the device's, which belongs to every shop. */ + override fun savePushToken(provider: String, value: String) { + saveValue(StorageKeys.pushToken(provider), value) + device?.savePushToken(provider, value) + } override fun getLastPushTokenDate(provider: String): Long { - val value = getValue(PUSH_TOKEN_DATE_KEY_PREFIX + provider, DEFAULT_LAST_PUSH_TOKEN_DATE) + val value = getValue(StorageKeys.pushTokenDate(provider), DEFAULT_LAST_PUSH_TOKEN_DATE) if (value != DEFAULT_LAST_PUSH_TOKEN_DATE) return value - val legacyKey = LEGACY_TOKEN_DATE_KEYS[provider] ?: return value + val legacyKey = StorageKeys.LEGACY_PUSH_TOKEN_DATE[provider] ?: return value return getValue(legacyKey, DEFAULT_LAST_PUSH_TOKEN_DATE) } override fun saveLastPushTokenDate(provider: String, value: Long) = - saveValue(PUSH_TOKEN_DATE_KEY_PREFIX + provider, value) + saveValue(StorageKeys.pushTokenDate(provider), value) override fun getValue(field: String, defaultValue: String): String { return sharedPreferences?.getString(field, defaultValue) ?: defaultValue @@ -112,6 +76,11 @@ class PreferencesDataSourceImpl @Inject constructor() : PreferencesDataSource { return sharedPreferences?.getLong(field, defaultValue) ?: defaultValue } + /** + * Writes [value] under [field]. The `did` is committed before this returns: the server handed it + * out once, and a crash before a background write reached the disk would make the next launch a + * new device. Everything else is written in the background. + */ override fun saveValue(field: String, value: T) { val putEditor = sharedPreferences?.let { sharedPreferences -> with(sharedPreferences.edit()) { @@ -124,12 +93,20 @@ class PreferencesDataSourceImpl @Inject constructor() : PreferencesDataSource { else -> null } } - } + } ?: return - putEditor?.apply() + if (field != StorageKeys.DID) { + putEditor.apply() + } else if (!putEditor.commit()) { + SdkLogs.log(LogLevel.WARNING, LogCodes.STORAGE_FAILED, "Storage operation failed: $OP_WRITE_DID", shopId, mapOf("op" to OP_WRITE_DID)) + } } override fun removeValue(field: String) { sharedPreferences?.edit()?.remove(field)?.apply() } + + private companion object { + const val OP_WRITE_DID = "write_did" + } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceDataSourceImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceDataSourceImpl.kt index e9a68517..52dd3414 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceDataSourceImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceDataSourceImpl.kt @@ -1,5 +1,6 @@ package com.personalization.sdk.data.repositories.trackingSource +import com.personalization.core.internal.StorageKeys import com.personalization.sdk.data.repositories.preferences.PreferencesDataSource import com.personalization.sdk.domain.models.StoredTrackingSource import javax.inject.Inject @@ -38,9 +39,9 @@ class TrackingSourceDataSourceImpl @Inject constructor( } private companion object { - const val TYPE_FIELD = "tracking_source_type" - const val CODE_FIELD = "tracking_source_code" - const val SAVED_AT_FIELD = "tracking_source_saved_at" + const val TYPE_FIELD = StorageKeys.TRACKING_SOURCE_TYPE + const val CODE_FIELD = StorageKeys.TRACKING_SOURCE_CODE + const val SAVED_AT_FIELD = StorageKeys.TRACKING_SOURCE_SAVED_AT const val NEVER_SAVED = 0L const val EMPTY = "" diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/userSettings/UserSettings.repository.impl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/userSettings/UserSettings.repository.impl.kt index d8a8ff9f..f9fbdd9b 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/userSettings/UserSettings.repository.impl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/userSettings/UserSettings.repository.impl.kt @@ -1,5 +1,6 @@ package com.personalization.sdk.data.repositories.userSettings +import com.personalization.core.internal.StorageKeys import com.personalization.sdk.data.repositories.preferences.PreferencesDataSource import com.personalization.sdk.data.utils.AdvertisingUtils import com.personalization.sdk.domain.repositories.UserSettingsRepository @@ -12,13 +13,13 @@ private const val DEFAULT_SHOP_ID = "" private const val DEFAULT_STREAM = "android" private const val DEFAULT_SEGMENT = "" -private const val DID_KEY = "did" -private const val SID_KEY = "sid" -private const val SID_LAST_ACT_KEY = "sid_last_act" -private const val SHOP_ID_KEY = "shop_id" -private const val SEGMENT_AB_TESTING_KEY = "segment_ab_testing" -private const val STREAM_KEY = "stream" -private const val ADVERTISING_ID_KEY = "google_ad_id" +private const val DID_KEY = StorageKeys.DID +private const val SID_KEY = StorageKeys.SID +private const val SID_LAST_ACT_KEY = StorageKeys.SID_LAST_ACT +private const val SHOP_ID_KEY = StorageKeys.SHOP_ID +private const val SEGMENT_AB_TESTING_KEY = StorageKeys.SEGMENT +private const val STREAM_KEY = StorageKeys.STREAM +private const val ADVERTISING_ID_KEY = StorageKeys.ADVERTISING_ID class UserSettingsRepositoryImpl @Inject constructor( private val preferencesDataSource: PreferencesDataSource, @@ -78,9 +79,10 @@ class UserSettingsRepositoryImpl @Inject constructor( } // The A/B segment is a sticky per-device bucket: assigned once, then reused for the device's - // lifetime (and kept as-is when migrated from a previous install). It is assigned-and-persisted - // lazily on first read rather than overwritten on every init — re-rolling it each launch would - // flip the device between A and B and discard a migrated segment. + // lifetime (and kept as-is when migrated from a previous install). The start gives a shop without + // one the device's group (StorageMigrations); the random pick here is the fallback for a partition + // opened without it. It is never overwritten on init — re-rolling it each launch would flip the + // device between A and B and discard a migrated segment. override fun getSegmentForABTesting(): String { val stored = preferencesDataSource.getValue( field = SEGMENT_AB_TESTING_KEY, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/userSettings/InitializeAdvertisingIdUseCase.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/userSettings/InitializeAdvertisingIdUseCase.kt index 892521ff..acd5975d 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/userSettings/InitializeAdvertisingIdUseCase.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/userSettings/InitializeAdvertisingIdUseCase.kt @@ -1,16 +1,21 @@ package com.personalization.sdk.domain.usecases.userSettings +import android.content.Context +import com.personalization.core.internal.DeviceStorage import com.personalization.sdk.domain.repositories.AdvertisingRepository import com.personalization.sdk.domain.repositories.UserSettingsRepository import javax.inject.Inject internal class InitializeAdvertisingIdUseCase @Inject constructor( private val advertisingRepository: AdvertisingRepository, - private val userSettingsRepository: UserSettingsRepository + private val userSettingsRepository: UserSettingsRepository, + private val context: Context, ) { + /** Reads the advertising id and keeps it in the shop's partition, as before, and at the device level. */ suspend fun invoke(): String { val advertisingId = advertisingRepository.fetchAdvertisingId() userSettingsRepository.updateAdvertisingId(advertisingId) + DeviceStorage(context).saveAdvertisingId(advertisingId) return advertisingId } } From c4c5c3510f6fdfe5eb165c5b233b439b93c89811 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 23:13:07 +0400 Subject: [PATCH 024/109] feat(core): deleteUserCredentials(), a suspend function and a Java form with a CompletionListener called once on the main thread, forgets the shop's customer on both cores: did, sid and the last activity, the saved /init answer, the tracking source and the push token's send dates, so the token goes to the next customer, and on the new core the queue's directory; the stories' state, the device's push token, A/B group and advertising id stay; it logs device_id_reset, keeps the instance registered and asks /init again without a did, ignoring the answer to an /init sent before and fetching the push token only once the new did is stored; an instance never initialized gets notInitialized --- .../com/personalization/RegisterManager.kt | 40 ++++++++-- .../main/kotlin/com/personalization/SDK.kt | 76 +++++++++++++++++++ .../core/CompletionListener.kt | 10 +++ .../com/personalization/core/internal/Core.kt | 6 ++ .../personalization/core/internal/CoreV2.kt | 3 + .../core/internal/LegacyCore.kt | 3 + 6 files changed, 130 insertions(+), 8 deletions(-) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/CompletionListener.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt index aee401dd..b397c906 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt @@ -46,6 +46,13 @@ class RegisterManager @Inject constructor( private lateinit var context: Context private lateinit var contentResolver: ContentResolver + /** + * Which start the `/init` in flight belongs to. [restartAsNewDevice] begins a new one, and an + * answer to an `/init` of an earlier start is dropped: it carries the forgotten customer's did. + */ + @Volatile + private var generation: Int = 0 + private val isTestDevice: Boolean get() = IS_TEST_DEVICE_FIELD == Settings.System.getString( contentResolver, @@ -76,6 +83,19 @@ class RegisterManager @Inject constructor( } } + /** + * Starts over as a device the server has not seen, after `deleteUserCredentials()` wiped the did: + * `/init` goes out without a did and the server hands out a new one, which the requests waiting + * for a did then carry. The push token waits for it too: sent before, it would be registered + * without a did. An answer to an `/init` sent before is ignored. Nothing happens before [initialize]. + */ + internal fun restartAsNewDevice() { + if (!::context.isInitialized) return + generation++ + initializeSdk(seance = null, releaseQueue = false, fetchPushTokens = false) + init() + } + private fun initializeNewDevice() { // Do NOT seed the did with a hardware id (Settings.Secure.ANDROID_ID). Like the iOS, React // Native and web SDKs, send an empty did on the first /init and let the backend assign one — @@ -97,7 +117,8 @@ class RegisterManager @Inject constructor( sendInitRequestWithRetry(knownDevice = knownDevice) } - private fun sendInitRequestWithRetry(attempt: Int = 0, knownDevice: Boolean = false) { + private fun sendInitRequestWithRetry(attempt: Int = 0, knownDevice: Boolean = false, start: Int = generation) { + if (start != generation) return try { val params = buildInitParams() sendNetworkMethodUseCase.get( @@ -105,12 +126,12 @@ class RegisterManager @Inject constructor( params = params, listener = object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { - handleInitSuccess(response, knownDevice) + if (start == generation) handleInitSuccess(response, knownDevice) } // The answer's body stays out of the log: a server often repeats the request's data in it. override fun onError(code: Int, msg: String?) { - handleInitError(code, attempt, knownDevice) + if (start == generation) handleInitError(code, attempt, knownDevice, start) } } ) @@ -166,7 +187,7 @@ class RegisterManager @Inject constructor( } } - private fun handleInitError(code: Int, attempt: Int, knownDevice: Boolean) { + private fun handleInitError(code: Int, attempt: Int, knownDevice: Boolean, start: Int) { if (code in 500..Int.MAX_VALUE || code <= 0) { if (attempt < MAX_INIT_RETRIES) { log( @@ -177,7 +198,7 @@ class RegisterManager @Inject constructor( ) CoroutineScope(Dispatchers.Main).launch { delay(RETRY_DELAY_MILLISECONDS * (attempt + 1)) - sendInitRequestWithRetry(attempt + 1, knownDevice) + sendInitRequestWithRetry(attempt + 1, knownDevice, start) } } else { log( @@ -208,12 +229,15 @@ class RegisterManager @Inject constructor( SdkLogs.log(level, code, message, shopId, context, throwable) } - /** Starts the session; [releaseQueue] sends the requests that waited for a did, which must be stored by now. */ - private fun initializeSdk(seance: String?, releaseQueue: Boolean = true) { + /** + * Starts the session; [releaseQueue] sends the requests that waited for a did, which must be stored + * by now, and [fetchPushTokens] asks the providers for their tokens. + */ + private fun initializeSdk(seance: String?, releaseQueue: Boolean = true, fetchPushTokens: Boolean = true) { val finalSeance = seance ?: generateOrRetrieveSeance() updateUserSettingsValueUseCase.updateSid(value = finalSeance) if (releaseQueue) executeQueueTasksUseCase.invoke() - pushTokenManager.initialize(context = context, autoSendPushToken = autoSendPushToken) + if (fetchPushTokens) pushTokenManager.initialize(context = context, autoSendPushToken = autoSendPushToken) } private fun generateOrRetrieveSeance(): String { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index d6a25241..cef0cea5 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -26,8 +26,12 @@ import com.personalization.api.managers.SearchManager import com.personalization.api.managers.TrackEventManager import com.personalization.api.managers.TrackingApi import com.personalization.api.params.ProfileParams +import com.personalization.core.CompletionListener +import com.personalization.core.SdkError +import com.personalization.core.SdkException import com.personalization.core.internal.AndroidConnectivityMonitor import com.personalization.core.internal.Core +import com.personalization.core.internal.CoreDispatchers import com.personalization.core.internal.CoreSelection import com.personalization.core.internal.CoreV2 import com.personalization.core.internal.ShopStorage @@ -57,9 +61,12 @@ import com.personalization.sdk.domain.usecases.userSettings.InitializeAdvertisin import com.personalization.stories.StoriesManager import com.personalization.stories.views.StoriesView import com.personalization.utils.DomainFormattingUtils.formatApiDomain +import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import java.util.Locale import javax.inject.Inject import org.json.JSONException @@ -189,6 +196,13 @@ open class SDK { internal var storage: ShopStorage? = null private set + /** Where the calls made for Java and for code outside coroutines run; nothing they do reaches the app as an exception. */ + private val calls: CoroutineScope by lazy { + CoroutineScope(SupervisorJob() + CoreDispatchers.DEFAULT.io + CoroutineExceptionHandler { _, throwable -> + log(LogLevel.ERROR, LogCodes.SDK_LOG, "A call of the instance failed unexpectedly", throwable = throwable) + }) + } + /** The shop this instance logs for, once it is initialized: its lines pass that shop's level. */ @Volatile internal var logShopId: String? = null @@ -690,6 +704,68 @@ open class SDK { logShopId?.let { SdkLogs.unregister(it, owner = this) } } + /** + * Forgets this shop's customer on this device: the `did`, the session (`sid` and the time of the last + * activity), the saved `/init` answer, the source [tracking] `setSource` set, the dates the push + * token was sent (so the token goes to the next customer) and, on the new core, the queue of events + * not sent yet. The stories' state, the device's push token, its A/B group and its advertising id + * stay: they belong to the device, not to the customer. Logs `device_id_reset`. + * + * The instance stays registered and asks `/init` again without a `did`; the server hands out a new + * one, and requests made meanwhile wait for it. Other shops of the app keep their customers. + * + * Android's Auto Backup (on by default for apps targeting Android 6.0 and later) copies the app's + * `SharedPreferences`, the SDK's among them, and restores them on the user's new device: the `did` + * of the backed-up customer comes back there. A library cannot change the app's backup rules; an + * app that wants every device to start without one excludes the SDK's files in its own + * `fullBackupContent` / `dataExtractionRules`: `personalization_sdk_.xml` (with a + * [Rees46Config.storageKey], `_personalization_sdk_.xml`) and + * `personalization_sdk.device.xml` in the `sharedpref` domain. The saved `/init` answer and the + * queue are kept in `noBackupFilesDir` and never leave the device. + * + * @throws SdkException with [SdkError.NotInitialized] when the instance was never initialized. + */ + suspend fun deleteUserCredentials() { + val core = core + val storage = storage + if (core == null || storage == null) throw SdkException(SdkError.NotInitialized) + withContext(CoreDispatchers.DEFAULT.io) { core.deleteUserCredentials(storage) } + log( + LogLevel.INFO, + LogCodes.DEVICE_ID_RESET, + "The customer's data was deleted; /init asks for a new did", + mapOf("reason" to "delete_user_credentials") + ) + try { + registerManager.restartAsNewDevice() + } catch (e: Exception) { + // The customer is forgotten either way; the next launch asks /init. + log(LogLevel.ERROR, LogCodes.SDK_LOG, "/init could not be restarted after the deletion", throwable = e) + } + } + + /** + * [deleteUserCredentials] for Java and for code outside coroutines: [listener] gets null once the + * customer is forgotten, or the error ([SdkError.NotInitialized]), once, on the main thread. + */ + fun deleteUserCredentials(listener: CompletionListener) { + calls.launch { + val error = try { + deleteUserCredentials() + null + } catch (e: SdkException) { + e.error + } + withContext(CoreDispatchers.DEFAULT.main) { + try { + listener.onComplete(error) + } catch (e: Exception) { + log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "The app's CompletionListener threw", throwable = e) + } + } + } + } + /** Writes to the SDK's log under this instance's shop, so the shop's [Rees46Config.logLevel] applies. */ internal fun log( level: LogLevel, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/CompletionListener.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/CompletionListener.kt new file mode 100644 index 00000000..68e9b6b0 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/CompletionListener.kt @@ -0,0 +1,10 @@ +package com.personalization.core + +/** + * The end of a call that returns nothing, for Java and for code outside coroutines. Called once, on the + * main thread. + */ +fun interface CompletionListener { + /** [error] is null when the call succeeded. */ + fun onComplete(error: SdkError?) +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt index d6d94c19..27d17ce2 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt @@ -28,4 +28,10 @@ internal interface Core { /** Stops the core: requests in flight are cancelled where the core can cancel them. */ fun release() + + /** + * Forgets the customer in [storage]: what [ShopStorage.deleteUserCredentials] wipes, and whatever + * the core keeps of its own. Runs on a background thread; false when part of it did not reach the disk. + */ + fun deleteUserCredentials(storage: ShopStorage): Boolean } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt index cc381c8f..65e4ebee 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt @@ -90,6 +90,9 @@ internal class CoreV2( requests.cancel() } + /** The events in the queue were the customer's: its directory goes with the credentials. */ + override fun deleteUserCredentials(storage: ShopStorage): Boolean = storage.deleteUserCredentials(includeQueue = true) + private fun deliver(listener: OnApiCallbackListener, result: HttpResult) = when (result) { is HttpResult.Success -> delivery.success(listener, result.body) is HttpResult.Failure -> LegacyErrors.of(result.error, result.errorBody).let { delivery.failure(listener, it.code, it.message) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt index f9fd12bb..1847a09d 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt @@ -51,6 +51,9 @@ internal class LegacyCore( override fun release() = Unit + /** The legacy path has no queue: the partition's credentials and the saved answer go. */ + override fun deleteUserCredentials(storage: ShopStorage): Boolean = storage.deleteUserCredentials(includeQueue = false) + private fun executeMethod( networkMethod: NetworkMethod, params: JSONObject, From e808a915992112d8d5d9d50b890b64404fc6a1b2 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 23:13:13 +0400 Subject: [PATCH 025/109] test(core): upgrades from the layouts installs have on disk (the 2.4x shared file with and without a shop, a partition of a later release, a custom preferencesKey file, a storageKey of its own, a key that cannot name a file) and a second start that repeats nothing; the device level (the first shop's A/B group, the tokens and advertising id offered once, a later token written for both); the saved /init answer, the refused state and the queue's directory; what deleteUserCredentials wipes and keeps on each core, the token going to the next customer, the /init after it through the SDK, a stale answer dropped, the listener form and an instance never initialized; Rees46Config.storageKey from Java --- .../personalization/Rees46ConfigJavaTest.java | 12 +- .../RegisterManagerRestartTest.kt | 92 ++++++ .../com/personalization/SdkStorageTest.kt | 235 +++++++++++++++ .../core/internal/ShopStorageTest.kt | 223 +++++++++++++++ .../core/internal/StorageUpgradeTest.kt | 269 ++++++++++++++++++ 5 files changed, 830 insertions(+), 1 deletion(-) create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerRestartTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/StorageUpgradeTest.kt diff --git a/personalization-sdk/src/test/java/com/personalization/Rees46ConfigJavaTest.java b/personalization-sdk/src/test/java/com/personalization/Rees46ConfigJavaTest.java index 7983a173..04495fcd 100644 --- a/personalization-sdk/src/test/java/com/personalization/Rees46ConfigJavaTest.java +++ b/personalization-sdk/src/test/java/com/personalization/Rees46ConfigJavaTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import org.junit.Test; @@ -31,10 +32,19 @@ public void trailingDefaultsMayBeLeftOut() { } @Test - public void theNewCoreIsTheLastParameter() { + public void theNewCoreKeepsItsPlace() { Rees46Config config = new Rees46Config( "shop", "api.rees46.ru", "android", true, false, true, "SDK", true, LogLevel.WARNING, true); assertTrue(config.getCoreV2()); + assertNull(config.getStorageKey()); + } + + @Test + public void theStorageKeyIsTheLastParameter() { + Rees46Config config = new Rees46Config( + "shop", "api.rees46.ru", "android", true, false, true, "SDK", true, LogLevel.WARNING, false, "host"); + + assertEquals("host", config.getStorageKey()); } } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerRestartTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerRestartTest.kt new file mode 100644 index 00000000..ccfd3abc --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerRestartTest.kt @@ -0,0 +1,92 @@ +package com.personalization + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import com.personalization.api.OnApiCallbackListener +import com.personalization.push.PushTokenManager +import com.personalization.sdk.domain.usecases.network.ExecuteQueueTasksUseCase +import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase +import com.personalization.sdk.domain.usecases.userSettings.GetUserSettingsValueUseCase +import com.personalization.sdk.domain.usecases.userSettings.UpdateUserSettingsValueUseCase +import io.mockk.Runs +import io.mockk.every +import io.mockk.just +import io.mockk.mockk +import io.mockk.verify +import org.json.JSONObject +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.util.concurrent.CopyOnWriteArrayList + +/** After `deleteUserCredentials()` the instance starts over as a new device. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class RegisterManagerRestartTest { + + private val update = mockk(relaxed = true) + private val get = mockk(relaxed = true) + private val network = mockk(relaxed = true) + private val queue = mockk(relaxed = true) + private val pushTokens = mockk(relaxed = true) + private val inits = CopyOnWriteArrayList() + private lateinit var manager: RegisterManager + private lateinit var context: Context + + @Before + fun setUp() { + context = ApplicationProvider.getApplicationContext() + every { network.get(eq("init"), any(), any()) } answers { inits += thirdArg() } + every { pushTokens.initialize(any(), any()) } just Runs + manager = RegisterManager(update, get, network, queue, mockk(relaxed = true), pushTokens) + } + + @Test + fun `the new did is asked for before the push token is fetched again`() { + every { get.getDid() } returns "old-did" + manager.initialize(context, context.contentResolver, autoSendPushToken = true) + // A known device starts its session from storage: the waiting requests go and the token is fetched. + verify(exactly = 1) { queue.invoke() } + verify(exactly = 1) { pushTokens.initialize(any(), true) } + + every { get.getDid() } returns "" + manager.restartAsNewDevice() + + // Without a did nothing waiting goes out, and the token is not fetched. + assertEquals(2, inits.size) + verify(exactly = 1) { queue.invoke() } + verify(exactly = 1) { pushTokens.initialize(any(), any()) } + + inits[1].onSuccess(JSONObject("""{"did":"new-did","seance":"S2"}""")) + + verify(exactly = 1) { update.updateDid("new-did") } + verify(exactly = 2) { queue.invoke() } + verify(exactly = 2) { pushTokens.initialize(any(), true) } + } + + @Test + fun `an answer to the init sent before the restart is dropped`() { + every { get.getDid() } returns "" + manager.initialize(context, context.contentResolver, autoSendPushToken = false) + manager.restartAsNewDevice() + + inits[0].onSuccess(JSONObject("""{"did":"stale-did","seance":"S1"}""")) + inits[0].onError(500, null) + inits[1].onSuccess(JSONObject("""{"did":"fresh-did","seance":"S2"}""")) + + verify(exactly = 0) { update.updateDid("stale-did") } + verify(exactly = 1) { update.updateDid("fresh-did") } + // The dropped error schedules no retry of the old start. + assertEquals(2, inits.size) + } + + @Test + fun `a restart before initialize does nothing`() { + manager.restartAsNewDevice() + + assertEquals(0, inits.size) + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt new file mode 100644 index 00000000..19b9850c --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt @@ -0,0 +1,235 @@ +package com.personalization + +import android.content.Context +import android.os.Looper +import androidx.test.core.app.ApplicationProvider +import com.personalization.core.CompletionListener +import com.personalization.core.SdkError +import com.personalization.core.SdkException +import com.personalization.core.internal.CoreSelection +import com.personalization.core.internal.DeviceStorage +import com.personalization.core.internal.ShopStorage +import com.personalization.core.internal.StorageLayout +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import kotlinx.coroutines.runBlocking +import okhttp3.mockwebserver.Dispatcher +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import okhttp3.mockwebserver.RecordedRequest +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Assert.fail +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import java.io.File +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.Executor +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicInteger + +/** + * Storage through the SDK as an app starts it: the partition [Rees46Config.storageKey] picks, the old + * `preferencesKey` file read into it, and [SDK.deleteUserCredentials] with the `/init` that follows. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class SdkStorageTest { + + private lateinit var context: Context + private val server = MockWebServer() + private val inits = CopyOnWriteArrayList() + private val initCount = AtomicInteger() + private val logged = CopyOnWriteArrayList() + private val defaultExecutor = SdkLogs.handlerExecutor + + /** The answer to the n-th `/init` (from 1), given the `did` it carried. */ + @Volatile + private var initAnswer: (n: Int, did: String) -> MockResponse = { _, did -> + MockResponse().setBody(JSONObject().put("did", did.ifEmpty { "new-did" }).put("seance", "S").toString()) + } + + @Before + fun setUp() { + context = ApplicationProvider.getApplicationContext() + CoreSelection.reset() + SdkRegistry.reset() + Rees46.reset() + SdkLogs.reset() + SdkLogs.handlerExecutor = Executor { it.run() } + SdkLogs.handler = SdkLogHandler { logged += it } + listOf(StorageLayout(SHOP, null), StorageLayout(SHOP, HOST_KEY)).forEach { + context.getSharedPreferences(it.partitionName, Context.MODE_PRIVATE).edit().clear().commit() + } + listOf(HOST_KEY, PreferencesPartition.LEGACY_KEY, DeviceStorage.NAME).forEach { + context.getSharedPreferences(it, Context.MODE_PRIVATE).edit().clear().commit() + } + File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() + server.dispatcher = object : Dispatcher() { + override fun dispatch(request: RecordedRequest): MockResponse { + val url = request.requestUrl ?: return MockResponse().setResponseCode(400) + if (url.encodedPath != "/init") return MockResponse().setBody("{}") + val did = url.queryParameter("did").orEmpty() + inits += did + return initAnswer(initCount.incrementAndGet(), did) + } + } + server.start() + } + + @After + fun tearDown() { + SdkRegistry.all().forEach { it.release() } + server.shutdown() + CoreSelection.reset() + SdkRegistry.reset() + Rees46.reset() + SdkLogs.reset() + SdkLogs.handlerExecutor = defaultExecutor + } + + private fun config(storageKey: String? = null, coreV2: Boolean = false) = Rees46Config( + shopId = SHOP, + apiDomain = server.url("/").toString().trimEnd('/'), + autoSendPushToken = false, + logLevel = LogLevel.DEBUG, + coreV2 = coreV2, + storageKey = storageKey, + ) + + private fun awaitInits(count: Int) { + val deadline = System.currentTimeMillis() + 5_000 + while (inits.size < count && System.currentTimeMillis() < deadline) Thread.sleep(10) + assertTrue("expected $count /init, got $inits", inits.size >= count) + } + + private fun awaitDid(sdk: SDK, expected: String) { + val deadline = System.currentTimeMillis() + 5_000 + while (sdk.getDid() != expected && System.currentTimeMillis() < deadline) Thread.sleep(10) + assertEquals(expected, sdk.getDid()) + } + + @Test + fun `a storageKey prefixes the shop's partition and reads the file of that name once`() { + // The whole store of SDK.initialize(preferencesKey = "host_prefs") in an earlier release. + context.getSharedPreferences(HOST_KEY, Context.MODE_PRIVATE).edit() + .putString("did", "host-did").putString("sid", "host-sid").putString("shop_id", SHOP).commit() + + val sdk = Rees46.initialize(context, config(storageKey = HOST_KEY)) + + assertEquals("host_prefs_personalization_sdk_shop-s", sdk.storage?.layout?.partitionName) + assertEquals("host-did", sdk.getDid()) + awaitInits(1) + assertEquals("host-did", inits.single()) + assertEquals("host-did", context.getSharedPreferences(HOST_KEY, Context.MODE_PRIVATE).getString("did", null)) + } + + @Test + @Suppress("DEPRECATION") + fun `the deprecated preferencesKey opens the same partition as the storageKey`() { + context.getSharedPreferences(HOST_KEY, Context.MODE_PRIVATE).edit().putString("did", "host-did").commit() + + val sdk = SDK() + sdk.initialize(context, SHOP, apiDomain = server.url("/").toString().trimEnd('/'), preferencesKey = HOST_KEY, autoSendPushToken = false) + + assertEquals(StorageLayout(SHOP, HOST_KEY).partitionName, sdk.storage?.layout?.partitionName) + assertEquals("host-did", sdk.getDid()) + } + + @Test + fun `deleting the customer forgets the did, logs it and asks init for a new one`() { + val partition = context.getSharedPreferences(StorageLayout(SHOP, null).partitionName, Context.MODE_PRIVATE) + partition.edit().putString("did", "old-did").putString("sid", "old-sid").putLong("sid_last_act", System.currentTimeMillis()).commit() + val sdk = Rees46.initialize(context, config()) + awaitInits(1) + sdk.storage?.saveInitAnswer(JSONObject().put("did", "old-did")) + + runBlocking { sdk.deleteUserCredentials() } + + awaitInits(2) + assertEquals(listOf("old-did", ""), inits) + awaitDid(sdk, "new-did") + assertTrue(sdk.getSid() != "old-sid") + assertEquals(null, sdk.storage?.initAnswer()) + val reset = logged.single { it.code == LogCodes.DEVICE_ID_RESET } + assertEquals(LogLevel.INFO, reset.level) + assertEquals(SHOP, reset.shopId) + assertEquals("delete_user_credentials", reset.context["reason"]) + // The instance stays registered. + assertTrue(Rees46.getInstance(SHOP) === sdk) + } + + @Test + fun `an init answer for the customer before the deletion is not taken`() { + // A new device: its first /init is slow, and the deletion starts over before it answers. + initAnswer = { n, _ -> + val did = if (n == 1) "stale-did" else "fresh-did" + MockResponse().setBody("""{"did":"$did","seance":"S$n"}""").apply { if (n == 1) setHeadersDelay(1_000, TimeUnit.MILLISECONDS) } + } + val sdk = Rees46.initialize(context, config()) + awaitInits(1) + + runBlocking { sdk.deleteUserCredentials() } + + awaitDid(sdk, "fresh-did") + Thread.sleep(1_500) + assertEquals("fresh-did", sdk.getDid()) + } + + @Test + fun `the listener form ends once on the main thread`() { + val sdk = Rees46.initialize(context, config()) + val ends = CopyOnWriteArrayList>() + + sdk.deleteUserCredentials(CompletionListener { error -> ends += error to (Looper.myLooper() == Looper.getMainLooper()) }) + + val deadline = System.currentTimeMillis() + 5_000 + while (ends.isEmpty() && System.currentTimeMillis() < deadline) { + shadowOf(Looper.getMainLooper()).idle() + Thread.sleep(10) + } + shadowOf(Looper.getMainLooper()).idle() + assertEquals(listOf>(null to true), ends) + } + + @Test + fun `an instance never initialized has nothing to delete`() { + try { + runBlocking { SDK().deleteUserCredentials() } + fail("expected SdkException") + } catch (e: SdkException) { + assertEquals(SdkError.NotInitialized, e.error) + } + + val ends = CopyOnWriteArrayList() + SDK().deleteUserCredentials(CompletionListener { ends += it }) + val deadline = System.currentTimeMillis() + 5_000 + while (ends.isEmpty() && System.currentTimeMillis() < deadline) { + shadowOf(Looper.getMainLooper()).idle() + Thread.sleep(10) + } + assertEquals(listOf(SdkError.NotInitialized), ends) + } + + @Test + fun `on the new core the deletion takes the queue too`() { + val sdk = Rees46.initialize(context, config(coreV2 = true)) + val queue = sdk.storage?.queueDirectory() + File(queue, "event").writeText("{}") + + runBlocking { sdk.deleteUserCredentials() } + + assertTrue(queue?.exists() == false) + } + + private companion object { + const val SHOP = "shop-s" + const val HOST_KEY = "host_prefs" + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt new file mode 100644 index 00000000..2995daaf --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt @@ -0,0 +1,223 @@ +package com.personalization.core.internal + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import com.personalization.LogLevel +import com.personalization.PreferencesPartition +import com.personalization.PushProvider +import com.personalization.SdkLogMessage +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import com.personalization.push.PushTokenManager +import com.personalization.sdk.data.repositories.preferences.PreferencesDataSourceImpl +import com.personalization.sdk.data.repositories.preferences.PreferencesRepositoryImpl +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase +import com.personalization.sdk.domain.usecases.preferences.GetPreferencesValueUseCase +import com.personalization.sdk.domain.usecases.preferences.SavePreferencesValueUseCase +import io.mockk.mockk +import io.mockk.verify +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.io.File +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.Executor + +/** A shop's partition: the core's state in it, and what forgetting the customer takes away. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class ShopStorageTest { + + private lateinit var context: Context + private val layout = StorageLayout(SHOP, null) + private val logged = CopyOnWriteArrayList() + private val defaultExecutor = SdkLogs.handlerExecutor + + @Before + fun setUp() { + context = ApplicationProvider.getApplicationContext() + listOf(layout.partitionName, DeviceStorage.NAME, PreferencesPartition.keyFor(SHOP) + "_stories") + .forEach { context.getSharedPreferences(it, Context.MODE_PRIVATE).edit().clear().commit() } + File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() + SdkLogs.reset() + SdkLogs.handlerExecutor = Executor { it.run() } + SdkLogs.register(SHOP, LogLevel.DEBUG, "SDK") + SdkLogs.handler = com.personalization.SdkLogHandler { logged += it } + } + + @After + fun tearDown() { + SdkLogs.reset() + SdkLogs.handlerExecutor = defaultExecutor + } + + private fun storage() = ShopStorage(context, layout) + + @Test + fun `the last init answer is kept in a file of the shop, outside the preferences and the backup`() { + assertNull(storage().initAnswer()) + + storage().saveInitAnswer(JSONObject("""{"did":"d1","seance":"s1","popup":{"id":1}}""")) + storage().saveInitAnswer(JSONObject("""{"did":"d1","seance":"s2"}""")) + + assertEquals("s2", storage().initAnswer()?.getString("seance")) + val file = File(context.noBackupFilesDir, "personalization_sdk/personalization_sdk_shop-1/init.json") + assertTrue(file.isFile) + assertFalse(storage().preferences.all.values.any { it.toString().contains("seance") }) + } + + @Test + fun `an answer that cannot be read is gone and the failure is logged without the answer`() { + storage().saveInitAnswer(JSONObject("""{"did":"secret-did"}""")) + File(storage().directory, ShopStorage.INIT_ANSWER_FILE).writeText("""{"did":"secret-""") + + assertNull(storage().initAnswer()) + assertNull(storage().initAnswer()) + + val failure = logged.single { it.code == LogCodes.STORAGE_FAILED } + assertEquals(LogLevel.WARNING, failure.level) + assertEquals(ShopStorage.OP_READ_INIT_ANSWER, failure.context["op"]) + assertFalse(logged.any { "secret" in it.message || "secret" in it.context.toString() }) + } + + @Test + fun `the refused state survives a restart until it is cleared`() { + assertFalse(storage().isRefused) + + storage().isRefused = true + assertTrue(storage().isRefused) + + storage().isRefused = false + assertFalse(storage().isRefused) + } + + @Test + fun `the queue has a directory of the shop, made on first use`() { + val queue = storage().queueDirectory() + + assertNotNull(queue) + assertTrue(queue?.isDirectory == true) + assertEquals(File(context.noBackupFilesDir, "personalization_sdk/personalization_sdk_shop-1/queue"), queue) + assertEquals(queue, storage().queueDirectory()) + // Another shop, another directory. + assertEquals( + File(context.noBackupFilesDir, "personalization_sdk/host_personalization_sdk_shop-1/queue"), + ShopStorage(context, StorageLayout(SHOP, "host")).queueDirectory() + ) + } + + @Test + fun `forgetting the customer wipes the credentials and keeps what belongs to the device`() { + seedShop() + val storage = storage() + storage.saveInitAnswer(JSONObject("""{"did":"did-1"}""")) + storage.isRefused = true + val queue = storage.queueDirectory() + File(queue, "event-1").writeText("{}") + + assertTrue(LegacyCore(mockk(relaxed = true)) { "" }.deleteUserCredentials(storage)) + + val left = storage.preferences.all + listOf( + "did", "sid", "sid_last_act", "tracking_source_type", "tracking_source_code", "tracking_source_saved_at", + "push_token_date_fcm", "push_token_date_hms", "last_push_token_date", "last_hms_push_token_date", "refused", + ).forEach { assertFalse(it, left.containsKey(it)) } + assertEquals( + mapOf( + "shop_id" to SHOP, + "stream" to "android", + "segment_ab_testing" to "B", + "google_ad_id" to "ad-1", + "push_token_fcm" to "fcm-1", + "token" to "fcm-old", + "push_token_hms" to "hms-1", + StorageKeys.MIGRATED_LEGACY_FILE to true, + ), + left + ) + assertNull(storage.initAnswer()) + // The legacy core has no queue: its directory is not its to delete. + assertTrue(File(queue, "event-1").isFile) + // The stories' state and the device level are untouched. + assertEquals(setOf("slide-1"), context.getSharedPreferences(PreferencesPartition.keyFor(SHOP) + "_stories", Context.MODE_PRIVATE).getStringSet("7", null)) + assertEquals("A", DeviceStorage(context).segment()) + assertEquals("fcm-device", DeviceStorage(context).pushToken("fcm")) + } + + @Test + fun `on the new core the queue goes with the customer`() { + seedShop() + val storage = storage() + val queue = storage.queueDirectory() + File(queue, "event-1").writeText("{}") + val core = CoreV2(CoreHttpClient({ "https://example.invalid/" }, mockk(relaxed = true), FakeConnectivity(), SHOP), SHOP) + + assertTrue(core.deleteUserCredentials(storage)) + core.release() + + assertFalse(queue?.exists() == true) + assertFalse(storage.preferences.contains("did")) + } + + @Test + fun `after the wipe the push token goes to the next customer`() { + seedShop() + val source = PreferencesDataSourceImpl().apply { initialize(context, layout.partitionName, shopId = SHOP) } + val repository = PreferencesRepositoryImpl(source) + val network = mockk(relaxed = true) + val tokens = PushTokenManager(network, GetPreferencesValueUseCase(repository), SavePreferencesValueUseCase(repository)) + tokens.initialize(context, autoSendPushToken = true) + + // Sent this week to the customer before: not sent again. + tokens.onTokenReceived("fcm-1", PushProvider.FCM) + verify(exactly = 0) { network.post("mobile_push_tokens", any(), any()) } + + storage().deleteUserCredentials(includeQueue = false) + tokens.onTokenReceived("fcm-1", PushProvider.FCM) + + verify(exactly = 1) { network.post("mobile_push_tokens", any(), any()) } + } + + /** A shop of a running install: its customer, the device's data and the stories' state. */ + private fun seedShop() { + val now = System.currentTimeMillis() + storage().preferences.edit() + .putString("did", "did-1") + .putString("sid", "sid-1") + .putLong("sid_last_act", now) + .putString("shop_id", SHOP) + .putString("stream", "android") + .putString("segment_ab_testing", "B") + .putString("google_ad_id", "ad-1") + .putString("push_token_fcm", "fcm-1") + .putString("token", "fcm-old") + .putString("push_token_hms", "hms-1") + .putLong("push_token_date_fcm", now) + .putLong("push_token_date_hms", now) + .putLong("last_push_token_date", now) + .putLong("last_hms_push_token_date", now) + .putString("tracking_source_type", "bulk") + .putString("tracking_source_code", "c1") + .putLong("tracking_source_saved_at", now) + .putBoolean(StorageKeys.MIGRATED_LEGACY_FILE, true) + .commit() + DeviceStorage(context).adoptSegment("A") + DeviceStorage(context).savePushToken("fcm", "fcm-device") + context.getSharedPreferences(PreferencesPartition.keyFor(SHOP) + "_stories", Context.MODE_PRIVATE) + .edit().putStringSet("7", setOf("slide-1")).commit() + } + + private companion object { + const val SHOP = "shop-1" + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/StorageUpgradeTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/StorageUpgradeTest.kt new file mode 100644 index 00000000..cb909cb7 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/StorageUpgradeTest.kt @@ -0,0 +1,269 @@ +package com.personalization.core.internal + +import android.content.Context +import android.content.SharedPreferences +import androidx.test.core.app.ApplicationProvider +import com.personalization.PreferencesPartition +import com.personalization.sdk.data.repositories.preferences.PreferencesDataSourceImpl +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** + * Upgrades from the layouts that installed apps have on disk: the 2.4x file shared by every shop, with + * and without a shop in it; a per-shop partition of a later release; the whole store of a custom + * `preferencesKey`. Real Robolectric `SharedPreferences`: the migrations copy between files. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class StorageUpgradeTest { + + private lateinit var context: Context + + @Before + fun setUp() { + context = ApplicationProvider.getApplicationContext() + // Robolectric keeps SharedPreferences between tests in the same process. + (listOf(PreferencesPartition.LEGACY_KEY, HOST_KEY, DeviceStorage.NAME, StorageLayout(SHOP_A, "dir/prefs").partitionName) + + listOf(SHOP_A, SHOP_B, SHOP_C).flatMap { listOf(StorageLayout(it, null).partitionName, StorageLayout(it, HOST_KEY).partitionName) }) + .forEach { prefs(it).edit().clear().commit() } + } + + private fun prefs(name: String): SharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE) + + private fun partition(shop: String, storageKey: String? = null) = prefs(StorageLayout(shop, storageKey).partitionName) + + private fun start(shop: String, storageKey: String? = null) = StorageMigrations.run(context, StorageLayout(shop, storageKey)) + + /** What a 2.4x install wrote to its one file; without [shop] the file of an install older still. */ + private fun seed24x(name: String = PreferencesPartition.LEGACY_KEY, shop: String? = SHOP_A, did: String = "did-24x") { + prefs(name).edit().apply { + shop?.let { putString("shop_id", it) } + putString("did", did) + putString("sid", "sid-24x") + putLong("sid_last_act", 1_600_000_000_000L) + putString("segment_ab_testing", "B") + putString("stream", "android") + putString("google_ad_id", "ad-24x") + putString("token", "fcm-24x") + putLong("last_push_token_date", 1_600_000_000_001L) + putString("tracking_source_type", "bulk") + }.commit() + } + + @Test + fun `a 2_4x file of the shop moves into its partition and stays where it was`() { + seed24x() + + start(SHOP_A) + + val partition = partition(SHOP_A) + assertEquals("did-24x", partition.getString("did", null)) + assertEquals("sid-24x", partition.getString("sid", null)) + assertEquals(1_600_000_000_000L, partition.getLong("sid_last_act", 0)) + assertEquals("fcm-24x", partition.getString("token", null)) + assertEquals(1_600_000_000_001L, partition.getLong("last_push_token_date", 0)) + assertEquals("bulk", partition.getString("tracking_source_type", null)) + assertTrue(partition.getBoolean(StorageKeys.MIGRATED_LEGACY_FILE, false)) + // The source is only read. + assertEquals("did-24x", prefs(PreferencesPartition.LEGACY_KEY).getString("did", null)) + assertEquals(SHOP_A, prefs(PreferencesPartition.LEGACY_KEY).getString("shop_id", null)) + } + + @Test + fun `a 2_4x file of another shop is left out`() { + seed24x(shop = SHOP_A) + + start(SHOP_B) + + val partition = partition(SHOP_B) + assertNull(partition.getString("did", null)) + assertTrue(partition.getBoolean(StorageKeys.MIGRATED_LEGACY_FILE, false)) + } + + @Test + fun `a 2_4x file without a shop goes to each shop that starts empty, and to each at most once`() { + seed24x(shop = null) + + start(SHOP_A) + start(SHOP_B) + + // As before: such a file has no owner, so every shop starting empty takes it. + assertEquals("did-24x", partition(SHOP_A).getString("did", null)) + assertEquals("did-24x", partition(SHOP_B).getString("did", null)) + + // The customer of shop A is forgotten; the old file still has its did and is never taken again. + ShopStorage(context, StorageLayout(SHOP_A, null)).deleteUserCredentials(includeQueue = false) + partition(SHOP_A).edit().apply { partition(SHOP_A).all.keys.filterNot { it in StorageKeys.MIGRATION_FLAGS }.forEach { remove(it) } }.commit() + start(SHOP_A) + + assertNull(partition(SHOP_A).getString("did", null)) + assertEquals("did-24x", partition(SHOP_B).getString("did", null)) + } + + @Test + fun `an existing partition keeps its data and the old file is not read into it`() { + partition(SHOP_A).edit().putString("did", "did-partition").putString("shop_id", SHOP_A).putString("segment_ab_testing", "A").commit() + seed24x(shop = SHOP_A, did = "did-stale") + + start(SHOP_A) + + val partition = partition(SHOP_A) + assertEquals("did-partition", partition.getString("did", null)) + assertNull(partition.getString("sid", null)) + assertEquals("A", partition.getString("segment_ab_testing", null)) + assertTrue(partition.getBoolean(StorageKeys.MIGRATED_LEGACY_FILE, false)) + } + + @Test + fun `a custom preferencesKey file moves into the prefixed partition, whatever shop it last named`() { + // Two shops shared the host's one file; the last to start wrote its shop id there. + seed24x(name = HOST_KEY, shop = SHOP_B, did = "did-host") + seed24x(shop = SHOP_A, did = "did-legacy") + + start(SHOP_A, HOST_KEY) + start(SHOP_B, HOST_KEY) + + assertEquals("host_prefs_personalization_sdk_shop-a", StorageLayout(SHOP_A, HOST_KEY).partitionName) + // Each keeps the did it read before: shops that shared it go on sharing it, nothing worse. + assertEquals("did-host", partition(SHOP_A, HOST_KEY).getString("did", null)) + assertEquals("did-host", partition(SHOP_B, HOST_KEY).getString("did", null)) + assertTrue(partition(SHOP_A, HOST_KEY).getBoolean(StorageKeys.MIGRATED_NAMED_FILE, false)) + // The prefixed layout does not read the file all shops shared before partitions. + assertFalse(partition(SHOP_A, HOST_KEY).contains(StorageKeys.MIGRATED_LEGACY_FILE)) + // The host's file is left as it was. + assertEquals("did-host", prefs(HOST_KEY).getString("did", null)) + assertEquals(SHOP_B, prefs(HOST_KEY).getString("shop_id", null)) + } + + @Test + fun `a second start repeats no migration`() { + seed24x(shop = SHOP_A) + start(SHOP_A) + val device = DeviceStorage(context) + val first = partition(SHOP_A).all + + // The sources change after the first start (nothing writes them any more, but a migration must not care). + seed24x(shop = SHOP_A, did = "did-changed") + prefs(DeviceStorage.NAME).edit().remove(StorageKeys.pushToken("fcm")).commit() + start(SHOP_A) + + assertEquals(first, partition(SHOP_A).all) + assertNull(device.pushToken("fcm")) + } + + @Test + fun `a storageKey gives the shop a partition of its own, apart from the default one`() { + partition(SHOP_A).edit().putString("did", "did-default").commit() + + start(SHOP_A, HOST_KEY) + + assertNull(partition(SHOP_A, HOST_KEY).getString("did", null)) + assertEquals("did-default", partition(SHOP_A).getString("did", null)) + } + + @Test + fun `the layout names the partition, its old files and its directory`() { + val default = StorageLayout("357382bf/66.ac", null) + assertEquals("personalization_sdk_357382bf_66_ac", default.partitionName) + assertEquals(PreferencesPartition.LEGACY_KEY, default.legacyFile) + assertNull(default.namedFile) + + val prefixed = StorageLayout("shop", "com.host/app") + assertEquals("com_host_app_personalization_sdk_shop", prefixed.partitionName) + assertEquals("com.host/app", prefixed.namedFile) + assertNull(prefixed.legacyFile) + + // The default of SDK.initialize(preferencesKey) and a blank key are no prefix. + assertEquals(default.partitionName, StorageLayout("357382bf/66.ac", PreferencesPartition.LEGACY_KEY).partitionName) + assertEquals("personalization_sdk_shop", StorageLayout("shop", " ").partitionName) + } + + @Test + fun `a custom key that cannot name a file costs its migration, not the start`() { + start(SHOP_A, "dir/prefs") + + assertTrue(partition(SHOP_A, "dir/prefs").getBoolean(StorageKeys.MIGRATED_NAMED_FILE, false)) + assertTrue(partition(SHOP_A, "dir/prefs").getBoolean(StorageKeys.MIGRATED_DEVICE_LEVEL, false)) + } + + @Test + fun `the first shop's A_B group becomes the device's, and each shop keeps sending its own`() { + // Two shops of a release with per-shop groups, which happened to differ. + partition(SHOP_A).edit().putString("did", "a").putString("segment_ab_testing", "B").commit() + partition(SHOP_B).edit().putString("did", "b").putString("segment_ab_testing", "A").commit() + + start(SHOP_A) + start(SHOP_B) + + assertEquals("B", DeviceStorage(context).segment()) + assertEquals("B", partition(SHOP_A).getString("segment_ab_testing", null)) + assertEquals("A", partition(SHOP_B).getString("segment_ab_testing", null)) + } + + @Test + fun `a new shop takes the device's group, and a device without one gets it from its first shop`() { + start(SHOP_A) + val group = DeviceStorage(context).segment() + assertTrue(group == "A" || group == "B") + assertEquals(group, partition(SHOP_A).getString("segment_ab_testing", null)) + + repeat(2) { start(SHOP_B) } + start(SHOP_C) + + assertEquals(group, partition(SHOP_B).getString("segment_ab_testing", null)) + assertEquals(group, partition(SHOP_C).getString("segment_ab_testing", null)) + assertEquals(group, DeviceStorage(context).segment()) + } + + @Test + fun `the shops' push tokens and advertising id go to the device, the first one kept`() { + partition(SHOP_A).edit() + .putString("token", "fcm-old-key") + .putString(StorageKeys.pushToken("hms"), "hms-a") + .putString("google_ad_id", "ad-a") + .commit() + partition(SHOP_B).edit() + .putString(StorageKeys.pushToken("fcm"), "fcm-b") + .putString("google_ad_id", "ad-b") + .commit() + + start(SHOP_A) + start(SHOP_B) + + val device = DeviceStorage(context) + assertEquals("fcm-old-key", device.pushToken("fcm")) + assertEquals("hms-a", device.pushToken("hms")) + assertEquals("ad-a", device.advertisingId()) + // The shops' own copies are what the legacy path reads; they stay. + assertEquals("fcm-b", partition(SHOP_B).getString(StorageKeys.pushToken("fcm"), null)) + assertEquals("ad-b", partition(SHOP_B).getString("google_ad_id", null)) + } + + @Test + fun `a token sent later is written for the shop and for the device`() { + start(SHOP_A) + val source = PreferencesDataSourceImpl().apply { + initialize(context = context, preferencesKey = StorageLayout(SHOP_A, null).partitionName, shopId = SHOP_A) + } + + source.savePushToken("fcm", "fcm-new") + + assertEquals("fcm-new", source.getPushToken("fcm")) + assertEquals("fcm-new", partition(SHOP_A).getString(StorageKeys.pushToken("fcm"), null)) + assertEquals("fcm-new", DeviceStorage(context).pushToken("fcm")) + } + + private companion object { + const val SHOP_A = "shop-a" + const val SHOP_B = "shop-b" + const val SHOP_C = "shop-c" + const val HOST_KEY = "host_prefs" + } +} From 9491ffaba468fa9ee17e86b2bac54a7cfa3bff4b Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 23:50:37 +0400 Subject: [PATCH 026/109] =?UTF-8?q?fix(sdk):=20the=20User-Agent=20is=20upp?= =?UTF-8?q?er-cased=20without=20the=20device's=20locale,=20so=20under=20Tu?= =?UTF-8?q?rkish=20or=20Azerbaijani=20the=20flavor=20keeps=20an=20ASCII=20?= =?UTF-8?q?I=20instead=20of=20=C4=B0,=20which=20OkHttp=20refuses=20in=20a?= =?UTF-8?q?=20header=20and=20which=20failed=20every=20request=20of=20the?= =?UTF-8?q?=20new=20core?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/kotlin/com/personalization/SDK.kt | 12 ++++-- .../com/personalization/UserAgentTest.kt | 38 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/UserAgentTest.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index cef0cea5..0465f365 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -1387,9 +1387,15 @@ open class SDK { val instance: SDK get() = SdkRegistry.currentOrLazy() - fun userAgent(): String { - return PERSONALIZATION_SDK + BuildConfig.FLAVOR.uppercase(Locale.getDefault()) + ", v" + BuildConfig.VERSION_NAME - } + fun userAgent(): String = userAgent(BuildConfig.FLAVOR, BuildConfig.VERSION_NAME) + + /** + * The flavor is upper-cased without the device's locale: under Turkish or Azerbaijani the `i` + * of `personaclick` would become `İ`, which no HTTP header may carry, and every request of the + * new client would fail. + */ + internal fun userAgent(flavor: String, version: String): String = + PERSONALIZATION_SDK + flavor.uppercase(Locale.ROOT) + ", v" + version /** * Writes [message] to the SDK's log at [LogLevel.DEBUG] (see [Rees46Config.logLevel]). diff --git a/personalization-sdk/src/test/kotlin/com/personalization/UserAgentTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/UserAgentTest.kt new file mode 100644 index 00000000..134f5aa3 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/UserAgentTest.kt @@ -0,0 +1,38 @@ +package com.personalization + +import okhttp3.Headers +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Test +import java.util.Locale + +/** The User-Agent stays a valid header whatever the device's language. */ +class UserAgentTest { + + private val defaultLocale = Locale.getDefault() + + @After + fun tearDown() = Locale.setDefault(defaultLocale) + + @Test + fun `under Turkish the flavor keeps its dotless capital I`() { + listOf(Locale("tr", "TR"), Locale("az", "AZ")).forEach { locale -> + Locale.setDefault(locale) + + val agent = SDK.userAgent("personaclick", "2.49.0") + + assertEquals("Personalizatio SDK PERSONACLICK, v2.49.0", agent) + // OkHttp refuses a header with a character outside ASCII; this one goes. + assertEquals(agent, Headers.Builder().add("User-Agent", agent).build()["User-Agent"]) + } + } + + @Test + fun `the agent of this build is ASCII`() { + Locale.setDefault(Locale("tr", "TR")) + + val agent = SDK.userAgent() + + assertEquals(agent, Headers.Builder().add("User-Agent", agent).build()["User-Agent"]) + } +} From 6972771519cd7841c470bd7caa35612ca563851d Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 23:50:54 +0400 Subject: [PATCH 027/109] fix(sdk): deleteUserCredentials() after review: the restart begins before the wipe and an /init answer writes the did only under the lock that begins it, so an answer landing during the wipe can no longer bring the forgotten did back, and each restart has a start of its own; the did, the sid and seance also go from the stores a migration reads (the store of releases before partitions when it is the shop's, the old preferencesKey file), so a restore that lost the partition cannot read them back, and the shop's recent searches and the widgets' cached answers go too; a wipe that does not reach the disk logs storage_failed at error instead of device_id_reset and still ends normally; the Java form takes a CompletionListener that gets an SdkResult, as the contract has them; the old preferencesKey file gives the partition the SDK's own keys only; the dead legacy-file branch of the preferences data source and the second copy of the file-name pattern are gone, and the docs list every file of the SDK for an app's backup rules and say that a new storageKey on an existing install starts every customer anew --- .../personalization/PreferencesPartition.kt | 5 +- .../com/personalization/Rees46Config.kt | 11 +- .../com/personalization/RegisterManager.kt | 67 ++++++---- .../main/kotlin/com/personalization/SDK.kt | 101 ++++++++++----- .../core/CompletionListener.kt | 10 +- .../com/personalization/core/SdkResult.kt | 9 ++ .../core/internal/ShopStorage.kt | 19 +++ .../core/internal/StorageKeys.kt | 23 ++++ .../core/internal/StorageLayout.kt | 8 +- .../core/internal/StorageMigrations.kt | 28 +++-- .../com/personalization/logs/SdkLogs.kt | 2 + .../preferences/PreferencesDataSource.kt | 7 +- .../preferences/PreferencesDataSourceImpl.kt | 13 +- .../preferences/PreferencesRepositoryImpl.kt | 2 - .../repositories/PreferencesRepository.kt | 1 - .../preferences/InitPreferencesUseCase.kt | 2 - .../ui/widgets/SearchSupport.kt | 14 ++- .../RegisterManagerRestartTest.kt | 46 ++++++- .../com/personalization/SdkStorageTest.kt | 116 +++++++++++++++--- .../SdkUpgradeMigrationTest.kt | 11 +- .../PreferencesDataSourceMigrationTest.kt | 7 +- .../TrackingSourceDataSourceImplTest.kt | 1 - .../TrackingSourceMultiInstanceTest.kt | 3 +- 23 files changed, 376 insertions(+), 130 deletions(-) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/SdkResult.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/PreferencesPartition.kt b/personalization-sdk/src/main/kotlin/com/personalization/PreferencesPartition.kt index 3cc749c9..976783ab 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/PreferencesPartition.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/PreferencesPartition.kt @@ -30,5 +30,8 @@ internal object PreferencesPartition { * Stable, filename-safe SharedPreferences name for [shopId]'s partition. Stable forever: * changing the scheme would orphan every existing install's storage. */ - fun keyFor(shopId: String): String = PREFIX + shopId.replace(UNSAFE_FILENAME_CHARS, "_") + fun keyFor(shopId: String): String = PREFIX + sanitize(shopId) + + /** [value] as a part of a file name: every character but letters, digits, `_` and `-` becomes `_`. */ + fun sanitize(value: String): String = value.replace(UNSAFE_FILENAME_CHARS, "_") } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt index 46f02be6..fd65fef4 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt @@ -29,10 +29,13 @@ package com.personalization * shop's `SharedPreferences` file becomes `_personalization_sdk_` instead of * `personalization_sdk_`. A file named exactly [storageKey] (the whole store of * `SDK.initialize(preferencesKey = …)` in earlier releases) is read into it once and left as it is, - * so an app moving its `preferencesKey` here keeps its customers. The device-level data (A/B group, - * push token, advertising id) is the app's and has no prefix. Android's Auto Backup restores these - * files on the user's new device, the `did` among them; see [SDK.deleteUserCredentials] for the files - * an app excludes from its backup rules to avoid that. + * so an app moving its `preferencesKey` here keeps its customers; only the SDK's own keys are taken + * from that file, which may be the app's own preferences. Set on an install that already runs the SDK + * without it, or changed later, it opens a new, empty partition: every customer becomes a new device + * with a new `did`, unless the value equals the `preferencesKey` the app passed before. The + * device-level data (A/B group, push token, advertising id) is the app's and has no prefix. Android's + * Auto Backup restores these files on the user's new device, the `did` among them; see + * [SDK.deleteUserCredentials] for the files an app excludes from its backup rules to avoid that. * * Constructors are overloaded for Java: every trailing parameter with a default may be left out, so * Java code written against the seven-argument constructor keeps compiling as parameters are added. diff --git a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt index b397c906..281d6b00 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt @@ -1,6 +1,7 @@ package com.personalization import com.personalization.logs.LogCodes +import com.personalization.logs.LogReasons import com.personalization.logs.SdkLogs import android.content.ContentResolver import android.content.Context @@ -21,6 +22,7 @@ import kotlinx.coroutines.launch import org.json.JSONObject import java.security.SecureRandom import java.util.TimeZone +import java.util.concurrent.atomic.AtomicInteger import javax.inject.Inject class RegisterManager @Inject constructor( @@ -47,11 +49,18 @@ class RegisterManager @Inject constructor( private lateinit var contentResolver: ContentResolver /** - * Which start the `/init` in flight belongs to. [restartAsNewDevice] begins a new one, and an - * answer to an `/init` of an earlier start is dropped: it carries the forgotten customer's did. + * Which start the `/init` in flight belongs to. [beginRestart] begins a new one, and an answer to an + * `/init` of an earlier start is dropped: it carries the forgotten customer's did. Each restart + * gets a number of its own. */ - @Volatile - private var generation: Int = 0 + private val generation = AtomicInteger(0) + + /** + * Held while an answer writes the did and the session, and while a restart begins: an answer + * either lands before the restart (and the wipe after it removes what it wrote) or sees the new + * start and writes nothing. + */ + private val answerLock = Any() private val isTestDevice: Boolean get() = IS_TEST_DEVICE_FIELD == Settings.System.getString( @@ -84,16 +93,21 @@ class RegisterManager @Inject constructor( } /** - * Starts over as a device the server has not seen, after `deleteUserCredentials()` wiped the did: - * `/init` goes out without a did and the server hands out a new one, which the requests waiting - * for a did then carry. The push token waits for it too: sent before, it would be registered - * without a did. An answer to an `/init` sent before is ignored. Nothing happens before [initialize]. + * Begins a new start before `deleteUserCredentials()` wipes the did: from now on an answer to an + * `/init` sent before is dropped. Returns the start for [restartAsNewDevice]. + */ + internal fun beginRestart(): Int = synchronized(answerLock) { generation.incrementAndGet() } + + /** + * Starts over as a device the server has not seen, once the did is wiped: `/init` goes out without + * a did and the server hands out a new one, which the requests waiting for a did then carry. The + * push token waits for it too: sent before, it would be registered without a did. Nothing happens + * before [initialize], or when a later [beginRestart] took over from [start]. */ - internal fun restartAsNewDevice() { - if (!::context.isInitialized) return - generation++ + internal fun restartAsNewDevice(start: Int) { + if (!::context.isInitialized || start != generation.get()) return initializeSdk(seance = null, releaseQueue = false, fetchPushTokens = false) - init() + init(start = start) } private fun initializeNewDevice() { @@ -108,17 +122,17 @@ class RegisterManager @Inject constructor( init() } - private fun init(knownDevice: Boolean = false) { + private fun init(knownDevice: Boolean = false, start: Int = generation.get()) { if (isTestDevice) { - log(LogLevel.INFO, LogCodes.INIT_SKIPPED, "/init is not sent on a Google Play pre-launch report device", mapOf("reason" to "pre_launch_report")) + log(LogLevel.INFO, LogCodes.INIT_SKIPPED, "/init is not sent on a Google Play pre-launch report device", mapOf("reason" to LogReasons.PRE_LAUNCH_REPORT)) return } - sendInitRequestWithRetry(knownDevice = knownDevice) + sendInitRequestWithRetry(knownDevice = knownDevice, start = start) } - private fun sendInitRequestWithRetry(attempt: Int = 0, knownDevice: Boolean = false, start: Int = generation) { - if (start != generation) return + private fun sendInitRequestWithRetry(attempt: Int = 0, knownDevice: Boolean = false, start: Int) { + if (start != generation.get()) return try { val params = buildInitParams() sendNetworkMethodUseCase.get( @@ -126,12 +140,12 @@ class RegisterManager @Inject constructor( params = params, listener = object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { - if (start == generation) handleInitSuccess(response, knownDevice) + if (start == generation.get()) handleInitSuccess(response, knownDevice, start) } // The answer's body stays out of the log: a server often repeats the request's data in it. override fun onError(code: Int, msg: String?) { - if (start == generation) handleInitError(code, attempt, knownDevice, start) + if (start == generation.get()) handleInitError(code, attempt, knownDevice, start) } } ) @@ -148,7 +162,7 @@ class RegisterManager @Inject constructor( } } - private fun handleInitSuccess(response: JSONObject?, knownDevice: Boolean) { + private fun handleInitSuccess(response: JSONObject?, knownDevice: Boolean, start: Int) { val errorHandler = JsonResponseErrorHandler( tag = TAG, response = response @@ -174,8 +188,13 @@ class RegisterManager @Inject constructor( val did = errorHandler.getRequiredField(fieldName = "did") ?: return val seance = errorHandler.getRequiredField(fieldName = "seance") ?: return - updateUserSettingsValueUseCase.updateDid(did) - initializeSdk(seance) + synchronized(answerLock) { + // A restart that began while this answer was on its way: the did belongs to the forgotten customer. + if (start != generation.get()) return + updateUserSettingsValueUseCase.updateDid(did) + updateUserSettingsValueUseCase.updateSid(value = seance) + } + sessionStarted(releaseQueue = true, fetchPushTokens = true) notifyInitFinished(popupsV3) } @@ -236,6 +255,10 @@ class RegisterManager @Inject constructor( private fun initializeSdk(seance: String?, releaseQueue: Boolean = true, fetchPushTokens: Boolean = true) { val finalSeance = seance ?: generateOrRetrieveSeance() updateUserSettingsValueUseCase.updateSid(value = finalSeance) + sessionStarted(releaseQueue, fetchPushTokens) + } + + private fun sessionStarted(releaseQueue: Boolean, fetchPushTokens: Boolean) { if (releaseQueue) executeQueueTasksUseCase.invoke() if (fetchPushTokens) pushTokenManager.initialize(context = context, autoSendPushToken = autoSendPushToken) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 0465f365..cd00b751 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -29,6 +29,7 @@ import com.personalization.api.params.ProfileParams import com.personalization.core.CompletionListener import com.personalization.core.SdkError import com.personalization.core.SdkException +import com.personalization.core.SdkResult import com.personalization.core.internal.AndroidConnectivityMonitor import com.personalization.core.internal.Core import com.personalization.core.internal.CoreDispatchers @@ -60,6 +61,8 @@ import com.personalization.sdk.domain.usecases.userSettings.InitUserSettingsUseC import com.personalization.sdk.domain.usecases.userSettings.InitializeAdvertisingIdUseCase import com.personalization.stories.StoriesManager import com.personalization.stories.views.StoriesView +import com.personalization.ui.widgets.RecentSearches +import com.personalization.ui.widgets.ResponseCache import com.personalization.utils.DomainFormattingUtils.formatApiDomain import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope @@ -196,9 +199,12 @@ open class SDK { internal var storage: ShopStorage? = null private set + /** Where the instance's own work runs: tests pass test dispatchers before the first call. */ + internal var dispatchers: CoreDispatchers = CoreDispatchers.DEFAULT + /** Where the calls made for Java and for code outside coroutines run; nothing they do reaches the app as an exception. */ private val calls: CoroutineScope by lazy { - CoroutineScope(SupervisorJob() + CoreDispatchers.DEFAULT.io + CoroutineExceptionHandler { _, throwable -> + CoroutineScope(SupervisorJob() + dispatchers.io + CoroutineExceptionHandler { _, throwable -> log(LogLevel.ERROR, LogCodes.SDK_LOG, "A call of the instance failed unexpectedly", throwable = throwable) }) } @@ -707,21 +713,35 @@ open class SDK { /** * Forgets this shop's customer on this device: the `did`, the session (`sid` and the time of the last * activity), the saved `/init` answer, the source [tracking] `setSource` set, the dates the push - * token was sent (so the token goes to the next customer) and, on the new core, the queue of events - * not sent yet. The stories' state, the device's push token, its A/B group and its advertising id - * stay: they belong to the device, not to the customer. Logs `device_id_reset`. + * token was sent (so the token goes to the next customer), the shop's recent searches, the answers + * the widgets keep in memory and, on the new core, the queue of events not sent yet. The `did` and + * the session also go from the files older releases kept them in, so a later start cannot read them + * back. The stories' state, the device's push token, its A/B group and its advertising id stay: they + * belong to the device, not to the customer. Logs `device_id_reset`. * * The instance stays registered and asks `/init` again without a `did`; the server hands out a new - * one, and requests made meanwhile wait for it. Other shops of the app keep their customers. + * one, and requests made meanwhile wait for it, the push token among them. An answer to an `/init` + * sent before is dropped. Other shops of the app keep their customers. + * + * When part of the data does not reach the disk, the call still ends normally: there is no error for + * storage. The failure is logged as `storage_failed` (and not as `device_id_reset`), and the next + * call tries again. * * Android's Auto Backup (on by default for apps targeting Android 6.0 and later) copies the app's * `SharedPreferences`, the SDK's among them, and restores them on the user's new device: the `did` * of the backed-up customer comes back there. A library cannot change the app's backup rules; an * app that wants every device to start without one excludes the SDK's files in its own - * `fullBackupContent` / `dataExtractionRules`: `personalization_sdk_.xml` (with a - * [Rees46Config.storageKey], `_personalization_sdk_.xml`) and - * `personalization_sdk.device.xml` in the `sharedpref` domain. The saved `/init` answer and the - * queue are kept in `noBackupFilesDir` and never leave the device. + * `fullBackupContent` / `dataExtractionRules`, all in the `sharedpref` domain: + * - `personalization_sdk_.xml`, the shop's partition (with a [Rees46Config.storageKey], + * `_personalization_sdk_.xml`); + * - `personalization_sdk.device.xml`, the device's A/B group, push token and advertising id; + * - `DEFAULT_STORAGE_KEY.xml`, the store of releases before per-shop partitions, and `.xml`, + * the store an app named with `preferencesKey`: read once into the partition; + * - `personalization_sdk__stories.xml`, the stories seen; + * - `personalization_sdk__popups_v3.xml`, when popups v3 were shown; + * - `personalization_ui_recent_searches.xml`, the search widget's recent searches. + * + * The saved `/init` answer and the queue are kept in `noBackupFilesDir` and never leave the device. * * @throws SdkException with [SdkError.NotInitialized] when the instance was never initialized. */ @@ -729,15 +749,32 @@ open class SDK { val core = core val storage = storage if (core == null || storage == null) throw SdkException(SdkError.NotInitialized) - withContext(CoreDispatchers.DEFAULT.io) { core.deleteUserCredentials(storage) } - log( - LogLevel.INFO, - LogCodes.DEVICE_ID_RESET, - "The customer's data was deleted; /init asks for a new did", - mapOf("reason" to "delete_user_credentials") - ) + // From here on an answer to an /init sent before is dropped: one landing during the wipe would + // write the forgotten did back. + val restart = registerManager.beginRestart() + val complete = withContext(dispatchers.io) { + val stored = core.deleteUserCredentials(storage) + val searches = RecentSearches.forget(context, storage.layout.shopId) + ResponseCache.shared.clear() + stored && searches + } + if (complete) { + log( + LogLevel.INFO, + LogCodes.DEVICE_ID_RESET, + "The customer's data was deleted; /init asks for a new did", + mapOf("reason" to LogReasons.DELETE_USER_CREDENTIALS) + ) + } else { + log( + LogLevel.ERROR, + LogCodes.STORAGE_FAILED, + "The customer's data was not all deleted", + mapOf("op" to ShopStorage.OP_DELETE_CREDENTIALS) + ) + } try { - registerManager.restartAsNewDevice() + registerManager.restartAsNewDevice(restart) } catch (e: Exception) { // The customer is forgotten either way; the next launch asks /init. log(LogLevel.ERROR, LogCodes.SDK_LOG, "/init could not be restarted after the deletion", throwable = e) @@ -745,23 +782,29 @@ open class SDK { } /** - * [deleteUserCredentials] for Java and for code outside coroutines: [listener] gets null once the - * customer is forgotten, or the error ([SdkError.NotInitialized]), once, on the main thread. + * [deleteUserCredentials] for Java and for code outside coroutines: [listener] gets + * [SdkResult.Success] once the customer is forgotten, or [SdkResult.Failure] with the error + * ([SdkError.NotInitialized]), once, on the main thread. */ - fun deleteUserCredentials(listener: CompletionListener) { + fun deleteUserCredentials(listener: CompletionListener) { calls.launch { - val error = try { + val result: SdkResult = try { deleteUserCredentials() - null + SdkResult.Success(Unit) } catch (e: SdkException) { - e.error + SdkResult.Failure(e.error) } - withContext(CoreDispatchers.DEFAULT.main) { - try { - listener.onComplete(error) - } catch (e: Exception) { - log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "The app's CompletionListener threw", throwable = e) - } + complete(listener, result) + } + } + + /** Hands [result] to the app's [listener] on the main thread; an exception it throws is logged. */ + private suspend fun complete(listener: CompletionListener, result: SdkResult) { + withContext(dispatchers.main) { + try { + listener.onComplete(result) + } catch (e: Exception) { + log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "The app's CompletionListener threw", throwable = e) } } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/CompletionListener.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/CompletionListener.kt index 68e9b6b0..957c0957 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/CompletionListener.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/CompletionListener.kt @@ -1,10 +1,10 @@ package com.personalization.core /** - * The end of a call that returns nothing, for Java and for code outside coroutines. Called once, on the - * main thread. + * The end of a call for Java and for code outside coroutines: the second form of a suspending method, + * with this listener as its last, required parameter. Called once, on the main thread; an exception it + * throws is logged as `callback_failed` and goes no further. */ -fun interface CompletionListener { - /** [error] is null when the call succeeded. */ - fun onComplete(error: SdkError?) +fun interface CompletionListener { + fun onComplete(result: SdkResult) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/SdkResult.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/SdkResult.kt new file mode 100644 index 00000000..8cf95ac4 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/SdkResult.kt @@ -0,0 +1,9 @@ +package com.personalization.core + +/** What a [CompletionListener] receives: the value of a call that succeeded, or why it failed. */ +sealed interface SdkResult { + + data class Success(val value: T) : SdkResult + + data class Failure(val error: SdkError) : SdkResult +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt index 4cff2823..480ea759 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt @@ -93,6 +93,10 @@ internal class ShopStorage(context: Context, val layout: StorageLayout) { * [includeQueue] the queue's directory. The A/B group, the push tokens, the advertising id, the * stories' state and the migration flags stay. Written with `commit()`: a crash right after must * not bring the customer back. False when part of it did not reach the disk. + * + * The stores a migration reads lose the customer too ([StorageKeys.SOURCE_CREDENTIALS], nothing + * else of them): a partition restored without its migration flags, or lost, would otherwise read + * the forgotten `did` back from them. */ fun deleteUserCredentials(includeQueue: Boolean): Boolean { var complete = attempt(OP_DELETE_CREDENTIALS) { @@ -100,6 +104,9 @@ internal class ShopStorage(context: Context, val layout: StorageLayout) { StorageKeys.USER_CREDENTIALS.forEach { editor.remove(it) } editor.commit() } + listOfNotNull(layout.namedFile, layout.legacyFile).forEach { name -> + complete = attempt(OP_DELETE_SOURCE_CREDENTIALS) { forgetInSource(name) } && complete + } complete = attempt(OP_DELETE_INIT_ANSWER) { synchronized(fileLock) { // Also drops a write left half-done, so it cannot come back as the answer. @@ -116,6 +123,17 @@ internal class ShopStorage(context: Context, val layout: StorageLayout) { return complete } + /** Removes the customer's identity from the store [name] of an older release, when it is this shop's. */ + private fun forgetInSource(name: String): Boolean { + val source = appContext.getSharedPreferences(name, Context.MODE_PRIVATE) + // The store of releases before partitions may be another shop's: its customer is not this one. + if (name == layout.legacyFile && !StorageMigrations.belongsTo(source, layout.shopId)) return true + if (StorageKeys.SOURCE_CREDENTIALS.none(source::contains)) return true + val editor = source.edit() + StorageKeys.SOURCE_CREDENTIALS.forEach { editor.remove(it) } + return editor.commit() + } + /** Runs [block]; a false result or an exception is logged under [op]. */ private inline fun attempt(op: String, block: () -> Boolean): Boolean { val done = try { @@ -150,5 +168,6 @@ internal class ShopStorage(context: Context, val layout: StorageLayout) { const val OP_DELETE_CREDENTIALS = "delete_user_credentials" const val OP_DELETE_INIT_ANSWER = "delete_init_answer" const val OP_DELETE_QUEUE = "delete_queue" + const val OP_DELETE_SOURCE_CREDENTIALS = "delete_old_storage_credentials" } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt index 96998cb7..125cb70f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt @@ -20,6 +20,9 @@ internal object StorageKeys { const val TRACKING_SOURCE_CODE = "tracking_source_code" const val TRACKING_SOURCE_SAVED_AT = "tracking_source_saved_at" + /** The old name of the `sid`, which some stores of older releases and other SDKs kept. */ + const val SEANCE = "seance" + /** The push token of a provider (`fcm`, `hms`) as last sent from this shop. */ fun pushToken(provider: String): String = "push_token_$provider" @@ -51,6 +54,25 @@ internal object StorageKeys { val MIGRATION_FLAGS: Set = setOf(MIGRATED_NAMED_FILE, MIGRATED_LEGACY_FILE, MIGRATED_DEVICE_LEVEL) + /** + * Every key the SDK has kept in a shop's store: what a migration may take from a file that may be + * the app's own preferences (the old `preferencesKey`), leaving the app's keys where they are. + */ + val SDK_KEYS: Set = buildSet { + addAll(listOf(DID, SID, SID_LAST_ACT, SHOP_ID, SEGMENT, STREAM, ADVERTISING_ID)) + addAll(listOf(TRACKING_SOURCE_TYPE, TRACKING_SOURCE_CODE, TRACKING_SOURCE_SAVED_AT)) + PUSH_PROVIDERS.forEach { add(pushToken(it)); add(pushTokenDate(it)) } + addAll(LEGACY_PUSH_TOKEN.values) + addAll(LEGACY_PUSH_TOKEN_DATE.values) + } + + /** + * What `deleteUserCredentials()` wipes from the stores a migration reads (the store of releases + * before partitions, the old `preferencesKey` file): the customer's identity and session, and + * nothing else of those files. + */ + val SOURCE_CREDENTIALS: List = listOf(DID, SID, SEANCE) + /** * What `deleteUserCredentials()` wipes from the partition: the customer's identity and session, * the source `tracking.setSource` set, the dates the push token was sent (so the token goes to the @@ -60,6 +82,7 @@ internal object StorageKeys { val USER_CREDENTIALS: List = buildList { add(DID) add(SID) + add(SEANCE) add(SID_LAST_ACT) add(TRACKING_SOURCE_TYPE) add(TRACKING_SOURCE_CODE) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageLayout.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageLayout.kt index 5baa4fd0..799a570f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageLayout.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageLayout.kt @@ -21,17 +21,11 @@ internal class StorageLayout(val shopId: String, storageKey: String?) { /** The name of the shop's `SharedPreferences` file. */ val partitionName: String = - prefix?.let { sanitize(it) + "_" + PreferencesPartition.keyFor(shopId) } ?: PreferencesPartition.keyFor(shopId) + prefix?.let { PreferencesPartition.sanitize(it) + "_" + PreferencesPartition.keyFor(shopId) } ?: PreferencesPartition.keyFor(shopId) /** The old full-name file, read once into the partition. */ val namedFile: String? = prefix?.takeIf { it != partitionName } /** The file of the releases before partitions, read once into a default partition. */ val legacyFile: String? = if (prefix == null) PreferencesPartition.LEGACY_KEY else null - - private companion object { - val UNSAFE_FILENAME_CHARS = Regex("[^A-Za-z0-9_-]") - - fun sanitize(value: String): String = value.replace(UNSAFE_FILENAME_CHARS, "_") - } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageMigrations.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageMigrations.kt index 9d919394..eedb257f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageMigrations.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageMigrations.kt @@ -9,11 +9,13 @@ import com.personalization.logs.SdkLogs /** * The one-shot migrations of a shop's partition, run on every start before `/init`. Each runs at most * once per shop: it marks itself with a flag in the partition ([StorageKeys.MIGRATION_FLAGS]), in the - * same write as what it copied. The sources are only read, never changed or deleted. + * same write as what it copied. The sources are only read here; `deleteUserCredentials()` later wipes + * the customer's identity from them ([StorageKeys.SOURCE_CREDENTIALS]), so it cannot come back. * * 1. **The old full-name file** ([StorageLayout.namedFile]): what `SDK.initialize(preferencesKey = …)` * used as the whole store. Read into a partition that has no data yet, whatever shop it last - * belonged to: two shops that shared such a file shared its `did`, and keep sharing it. + * belonged to: two shops that shared such a file shared its `did`, and keep sharing it. Only the + * SDK's own keys ([StorageKeys.SDK_KEYS]) are taken: the app may have named its own preferences. * 2. **The file shared by every shop before partitions** ([StorageLayout.legacyFile]): read into a * default partition that has no data yet, when it belongs to the shop or names no shop at all (a * very old install). A file without a shop goes to each shop that starts with an empty partition, @@ -33,19 +35,21 @@ internal object StorageMigrations { val partition = open(context, layout.partitionName, shopId, OP_OPEN_PARTITION) ?: return layout.namedFile?.let { name -> step(shopId, OP_MIGRATE_NAMED_FILE) { - copyOnce(context, name, partition, StorageKeys.MIGRATED_NAMED_FILE, shopId, belongsTo = { true }) + copyOnce(context, name, partition, StorageKeys.MIGRATED_NAMED_FILE, shopId, keys = StorageKeys.SDK_KEYS, belongsTo = { true }) } } layout.legacyFile?.let { name -> step(shopId, OP_MIGRATE_LEGACY_FILE) { migrateLegacyFile(context, name, partition, shopId) } } step(shopId, OP_MIGRATE_DEVICE_LEVEL) { adoptDeviceLevel(partition, device) } } - /** Step 2 alone, for a [partition] opened by name ([com.personalization.sdk.data.repositories.preferences.PreferencesDataSource]). */ - fun migrateLegacyFile(context: Context, legacyName: String, partition: SharedPreferences, shopId: String) { - copyOnce(context, legacyName, partition, StorageKeys.MIGRATED_LEGACY_FILE, shopId) { source -> - val owner = source.getString(StorageKeys.SHOP_ID, "").orEmpty() - owner.isEmpty() || owner == shopId - } + private fun migrateLegacyFile(context: Context, legacyName: String, partition: SharedPreferences, shopId: String) { + copyOnce(context, legacyName, partition, StorageKeys.MIGRATED_LEGACY_FILE, shopId, keys = null) { source -> belongsTo(source, shopId) } + } + + /** Whether the store of releases before partitions is [shopId]'s: it names that shop, or no shop at all. */ + internal fun belongsTo(source: SharedPreferences, shopId: String): Boolean { + val owner = source.getString(StorageKeys.SHOP_ID, "").orEmpty() + return owner.isEmpty() || owner == shopId } /** Whether [partition] holds anything besides migration flags. */ @@ -58,6 +62,8 @@ internal object StorageMigrations { partition: SharedPreferences, flag: String, shopId: String, + /** The keys to take; null takes the whole file, the SDK's own. */ + keys: Set?, belongsTo: (SharedPreferences) -> Boolean, ) { if (partition.getBoolean(flag, false)) return @@ -66,7 +72,9 @@ internal object StorageMigrations { if (!hasData(partition)) { val source = open(context, sourceName, shopId, OP_OPEN_SOURCE) if (source != null && belongsTo(source)) { - source.all.forEach { (key, value) -> if (key !in StorageKeys.MIGRATION_FLAGS) editor.putAny(key, value) } + source.all.forEach { (key, value) -> + if (key !in StorageKeys.MIGRATION_FLAGS && (keys == null || key in keys)) editor.putAny(key, value) + } } } editor.putBoolean(flag, true).apply() diff --git a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt index 6dbb74f4..6d6f093f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt @@ -220,4 +220,6 @@ internal object LogReasons { const val AMBIGUOUS_SHOP = "ambiguous_shop" const val APP_DECLINED = "app_declined" const val AUTO_PRESENTATION_OFF = "auto_presentation_off" + const val DELETE_USER_CREDENTIALS = "delete_user_credentials" + const val PRE_LAUNCH_REPORT = "pre_launch_report" } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt index 6fc42fb1..24008e31 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt @@ -5,15 +5,12 @@ import android.content.Context interface PreferencesDataSource { /** - * Opens the preferences partition named [preferencesKey]. When [legacyPreferencesKey] and - * [shopId] are provided, a one-time migration copies the legacy shared file into the partition - * if it is still empty and the legacy file belongs to [shopId] or names no shop; a flag in the - * partition keeps it from running again. The legacy file is never changed. + * Opens the preferences partition named [preferencesKey] of [shopId]'s instance. The data of older + * releases is read into it before, by [com.personalization.core.internal.StorageMigrations]. */ fun initialize( context: Context, preferencesKey: String, - legacyPreferencesKey: String? = null, shopId: String? = null ) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceImpl.kt index f015ccc2..63f2465f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceImpl.kt @@ -5,7 +5,6 @@ import android.content.SharedPreferences import com.personalization.LogLevel import com.personalization.core.internal.DeviceStorage import com.personalization.core.internal.StorageKeys -import com.personalization.core.internal.StorageMigrations import com.personalization.logs.LogCodes import com.personalization.logs.SdkLogs import javax.inject.Inject @@ -25,20 +24,10 @@ class PreferencesDataSourceImpl @Inject constructor() : PreferencesDataSource { override fun initialize( context: Context, preferencesKey: String, - legacyPreferencesKey: String?, shopId: String? ) { + // The data of older releases is read into the partition before this, by StorageMigrations. val preferences = context.getSharedPreferences(preferencesKey, Context.MODE_PRIVATE) - if (legacyPreferencesKey != null && shopId != null) { - // One-time copy of the pre-partitioning shared file, so an existing single-instance - // install keeps its did/sid/segment/tokens; see StorageMigrations for the guards. - try { - StorageMigrations.migrateLegacyFile(context, legacyPreferencesKey, preferences, shopId) - } catch (e: Exception) { - SdkLogs.log(LogLevel.WARNING, LogCodes.STORAGE_FAILED, "Storage operation failed: ${StorageMigrations.OP_MIGRATE_LEGACY_FILE}", - shopId, mapOf("op" to StorageMigrations.OP_MIGRATE_LEGACY_FILE), e) - } - } this.sharedPreferences = preferences this.preferencesKey = preferencesKey this.device = DeviceStorage(context) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesRepositoryImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesRepositoryImpl.kt index 105beee8..ce3bf341 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesRepositoryImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesRepositoryImpl.kt @@ -11,12 +11,10 @@ class PreferencesRepositoryImpl @Inject constructor( override fun initialize( context: Context, preferencesKey: String, - legacyPreferencesKey: String?, shopId: String? ) = preferencesDataSource.initialize( context = context, preferencesKey = preferencesKey, - legacyPreferencesKey = legacyPreferencesKey, shopId = shopId ) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/repositories/PreferencesRepository.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/repositories/PreferencesRepository.kt index f186faf8..15e1ff21 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/repositories/PreferencesRepository.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/repositories/PreferencesRepository.kt @@ -7,7 +7,6 @@ interface PreferencesRepository { fun initialize( context: Context, preferencesKey: String, - legacyPreferencesKey: String? = null, shopId: String? = null ) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/preferences/InitPreferencesUseCase.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/preferences/InitPreferencesUseCase.kt index fa5db6a9..9f1e0de8 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/preferences/InitPreferencesUseCase.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/preferences/InitPreferencesUseCase.kt @@ -11,13 +11,11 @@ class InitPreferencesUseCase @Inject constructor( fun invoke( context: Context, preferencesKey: String, - legacyPreferencesKey: String? = null, shopId: String? = null ) { preferencesRepository.initialize( context = context, preferencesKey = preferencesKey, - legacyPreferencesKey = legacyPreferencesKey, shopId = shopId ) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/SearchSupport.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/SearchSupport.kt index fe6482fe..9e22c21c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/SearchSupport.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/SearchSupport.kt @@ -64,8 +64,18 @@ internal class RecentSearches(context: Context, private val shopKey: String, pri return items } - private companion object { - const val FILE = "personalization_ui_recent_searches" + companion object { + private const val FILE = "personalization_ui_recent_searches" + + /** + * Forgets the recent searches of [shopKey] (`deleteUserCredentials()`): they are the + * customer's. False when the disk refused it. + */ + internal fun forget(context: Context, shopKey: String): Boolean { + val preferences = context.applicationContext.getSharedPreferences(FILE, Context.MODE_PRIVATE) + if (!preferences.contains(shopKey)) return true + return preferences.edit().remove(shopKey).commit() + } } } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerRestartTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerRestartTest.kt index ccfd3abc..dc3a6014 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerRestartTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/RegisterManagerRestartTest.kt @@ -53,7 +53,7 @@ class RegisterManagerRestartTest { verify(exactly = 1) { pushTokens.initialize(any(), true) } every { get.getDid() } returns "" - manager.restartAsNewDevice() + manager.restartAsNewDevice(manager.beginRestart()) // Without a did nothing waiting goes out, and the token is not fetched. assertEquals(2, inits.size) @@ -71,7 +71,7 @@ class RegisterManagerRestartTest { fun `an answer to the init sent before the restart is dropped`() { every { get.getDid() } returns "" manager.initialize(context, context.contentResolver, autoSendPushToken = false) - manager.restartAsNewDevice() + manager.restartAsNewDevice(manager.beginRestart()) inits[0].onSuccess(JSONObject("""{"did":"stale-did","seance":"S1"}""")) inits[0].onError(500, null) @@ -83,9 +83,49 @@ class RegisterManagerRestartTest { assertEquals(2, inits.size) } + /** + * The window of `deleteUserCredentials()`: the restart begins, an answer to the earlier `/init` + * lands while the did is being wiped, and only then is `/init` sent again. The late answer must not + * write the forgotten did back. + */ + @Test + fun `an answer landing between the start of a restart and its init writes nothing`() { + every { get.getDid() } returns "" + manager.initialize(context, context.contentResolver, autoSendPushToken = false) + + val start = manager.beginRestart() + inits[0].onSuccess(JSONObject("""{"did":"stale-did","seance":"S1"}""")) + // The wipe runs here. + manager.restartAsNewDevice(start) + inits[1].onSuccess(JSONObject("""{"did":"fresh-did","seance":"S2"}""")) + + verify(exactly = 0) { update.updateDid("stale-did") } + verify(exactly = 0) { update.updateSid("S1") } + verify(exactly = 1) { update.updateDid("fresh-did") } + } + + @Test + fun `two restarts never share a start, and only the later one asks init`() { + every { get.getDid() } returns "" + manager.initialize(context, context.contentResolver, autoSendPushToken = false) + + val starts = java.util.Collections.synchronizedSet(HashSet()) + val threads = List(8) { Thread { repeat(50) { starts += manager.beginRestart() } } } + threads.forEach { it.start() } + threads.forEach { it.join() } + assertEquals(400, starts.size) + + val first = manager.beginRestart() + val second = manager.beginRestart() + manager.restartAsNewDevice(first) + assertEquals(1, inits.size) + manager.restartAsNewDevice(second) + assertEquals(2, inits.size) + } + @Test fun `a restart before initialize does nothing`() { - manager.restartAsNewDevice() + manager.restartAsNewDevice(manager.beginRestart()) assertEquals(0, inits.size) } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt index 19b9850c..b17ae385 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt @@ -6,13 +6,18 @@ import androidx.test.core.app.ApplicationProvider import com.personalization.core.CompletionListener import com.personalization.core.SdkError import com.personalization.core.SdkException +import com.personalization.core.SdkResult import com.personalization.core.internal.CoreSelection import com.personalization.core.internal.DeviceStorage import com.personalization.core.internal.ShopStorage import com.personalization.core.internal.StorageLayout +import com.personalization.core.internal.StorageMigrations +import com.personalization.core.internal.testCoreDispatchers import com.personalization.logs.LogCodes import com.personalization.logs.SdkLogs +import com.personalization.ui.widgets.RecentSearches import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.test.TestCoroutineScheduler import okhttp3.mockwebserver.Dispatcher import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockWebServer @@ -38,6 +43,7 @@ import java.util.concurrent.atomic.AtomicInteger * Storage through the SDK as an app starts it: the partition [Rees46Config.storageKey] picks, the old * `preferencesKey` file read into it, and [SDK.deleteUserCredentials] with the `/init` that follows. */ +@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class) @RunWith(RobolectricTestRunner::class) @Config(sdk = [28]) class SdkStorageTest { @@ -185,17 +191,28 @@ class SdkStorageTest { @Test fun `the listener form ends once on the main thread`() { val sdk = Rees46.initialize(context, config()) - val ends = CopyOnWriteArrayList>() + val scheduler = TestCoroutineScheduler() + sdk.dispatchers = testCoreDispatchers(scheduler) + val ends = mutableListOf, Boolean>>() - sdk.deleteUserCredentials(CompletionListener { error -> ends += error to (Looper.myLooper() == Looper.getMainLooper()) }) + sdk.deleteUserCredentials(CompletionListener { result -> ends += result to (Looper.myLooper() == Looper.getMainLooper()) }) + scheduler.advanceUntilIdle() - val deadline = System.currentTimeMillis() + 5_000 - while (ends.isEmpty() && System.currentTimeMillis() < deadline) { - shadowOf(Looper.getMainLooper()).idle() - Thread.sleep(10) - } - shadowOf(Looper.getMainLooper()).idle() - assertEquals(listOf>(null to true), ends) + assertEquals(listOf, Boolean>>(SdkResult.Success(Unit) to true), ends) + } + + @Test + fun `a listener that throws is logged and called once`() { + val sdk = Rees46.initialize(context, config()) + val scheduler = TestCoroutineScheduler() + sdk.dispatchers = testCoreDispatchers(scheduler) + var calls = 0 + + sdk.deleteUserCredentials(CompletionListener { calls++; throw IllegalStateException("app") }) + scheduler.advanceUntilIdle() + + assertEquals(1, calls) + assertEquals(LogLevel.ERROR, logged.single { it.code == LogCodes.CALLBACK_FAILED }.level) } @Test @@ -207,14 +224,81 @@ class SdkStorageTest { assertEquals(SdkError.NotInitialized, e.error) } - val ends = CopyOnWriteArrayList() - SDK().deleteUserCredentials(CompletionListener { ends += it }) - val deadline = System.currentTimeMillis() + 5_000 - while (ends.isEmpty() && System.currentTimeMillis() < deadline) { - shadowOf(Looper.getMainLooper()).idle() - Thread.sleep(10) + val scheduler = TestCoroutineScheduler() + val ends = mutableListOf>() + SDK().apply { dispatchers = testCoreDispatchers(scheduler) }.deleteUserCredentials(CompletionListener { ends += it }) + scheduler.advanceUntilIdle() + + assertEquals(listOf>(SdkResult.Failure(SdkError.NotInitialized)), ends) + } + + @Test + fun `the forgotten did is wiped from the store of older releases and does not come back`() { + // A 2.4x install: the store all shops shared, with the app's segment beside the customer. + context.getSharedPreferences(PreferencesPartition.LEGACY_KEY, Context.MODE_PRIVATE).edit() + .putString("shop_id", SHOP).putString("did", "legacy-did").putString("sid", "legacy-sid") + .putString("seance", "legacy-seance").putString("segment_ab_testing", "B").commit() + val sdk = Rees46.initialize(context, config()) + assertEquals("legacy-did", sdk.getDid()) + + runBlocking { sdk.deleteUserCredentials() } + + val legacy = context.getSharedPreferences(PreferencesPartition.LEGACY_KEY, Context.MODE_PRIVATE) + assertEquals(setOf("shop_id", "segment_ab_testing"), legacy.all.keys) + // A restore that brought the old store back but not the partition: the start reads nothing old. + val partition = context.getSharedPreferences(StorageLayout(SHOP, null).partitionName, Context.MODE_PRIVATE) + partition.edit().clear().commit() + StorageMigrations.run(context, StorageLayout(SHOP, null)) + assertEquals(null, partition.getString("did", null)) + assertEquals("B", partition.getString("segment_ab_testing", null)) + } + + @Test + fun `the old preferencesKey file gives the SDK's keys only and loses only the customer`() { + context.getSharedPreferences(HOST_KEY, Context.MODE_PRIVATE).edit() + .putString("did", "host-did").putString("sid", "host-sid").putString("app_theme", "dark").commit() + val sdk = Rees46.initialize(context, config(storageKey = HOST_KEY)) + assertEquals(null, sdk.storage?.preferences?.getString("app_theme", null)) + + runBlocking { sdk.deleteUserCredentials() } + + assertEquals(mapOf("app_theme" to "dark"), context.getSharedPreferences(HOST_KEY, Context.MODE_PRIVATE).all) + } + + @Test + fun `deleting the customer forgets the shop's recent searches and keeps another shop's`() { + RecentSearches(context, SHOP, 10).add("red shoes") + RecentSearches(context, "other-shop", 10).add("blue hat") + val sdk = Rees46.initialize(context, config()) + + runBlocking { sdk.deleteUserCredentials() } + + assertEquals(emptyList(), RecentSearches(context, SHOP, 10).load()) + assertEquals(listOf("blue hat"), RecentSearches(context, "other-shop", 10).load()) + RecentSearches(context, "other-shop", 10).clear() + } + + @Test + fun `a wipe that does not reach the disk is storage_failed, not device_id_reset, and still ends`() { + val sdk = Rees46.initialize(context, config()) + val storage = checkNotNull(sdk.storage) + storage.saveInitAnswer(JSONObject().put("did", "d")) + // The answer's file cannot be deleted from a directory the SDK may not write. + assertTrue(storage.directory.setWritable(false)) + try { + val scheduler = TestCoroutineScheduler() + sdk.dispatchers = testCoreDispatchers(scheduler) + val ends = mutableListOf>() + + sdk.deleteUserCredentials(CompletionListener { ends += it }) + scheduler.advanceUntilIdle() + + assertEquals(listOf>(SdkResult.Success(Unit)), ends) + assertTrue(logged.none { it.code == LogCodes.DEVICE_ID_RESET }) + assertTrue(logged.any { it.code == LogCodes.STORAGE_FAILED && it.level == LogLevel.ERROR }) + } finally { + storage.directory.setWritable(true) } - assertEquals(listOf(SdkError.NotInitialized), ends) } @Test diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkUpgradeMigrationTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkUpgradeMigrationTest.kt index a65070b6..47968458 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkUpgradeMigrationTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkUpgradeMigrationTest.kt @@ -3,6 +3,8 @@ package com.personalization import android.content.Context import androidx.test.core.app.ApplicationProvider import com.personalization.api.OnApiCallbackListener +import com.personalization.core.internal.StorageLayout +import com.personalization.core.internal.StorageMigrations import com.personalization.sdk.data.repositories.preferences.PreferencesDataSourceImpl import com.personalization.sdk.data.repositories.userSettings.UserSettingsRepositoryImpl import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase @@ -63,16 +65,17 @@ class SdkUpgradeMigrationTest { editor.commit() } - /** The real migration into [shop]'s partition, the exact call `SDK.initialize` makes for the default. */ - private fun migratedPartition(shop: String): PreferencesDataSourceImpl = - PreferencesDataSourceImpl().apply { + /** The real migration into [shop]'s partition and the partition opened, as `SDK.initialize` does for the default. */ + private fun migratedPartition(shop: String): PreferencesDataSourceImpl { + StorageMigrations.run(context, StorageLayout(shop, null)) + return PreferencesDataSourceImpl().apply { initialize( context = context, preferencesKey = PreferencesPartition.keyFor(shop), - legacyPreferencesKey = legacyKey, shopId = shop ) } + } @Test fun `a single-shop upgrade keeps did and sid at the settings layer the SDK reads`() { diff --git a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceMigrationTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceMigrationTest.kt index 03908b73..90f42948 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceMigrationTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceMigrationTest.kt @@ -3,6 +3,8 @@ package com.personalization.sdk.data.repositories.preferences import android.content.Context import androidx.test.core.app.ApplicationProvider import com.personalization.PreferencesPartition +import com.personalization.core.internal.StorageLayout +import com.personalization.core.internal.StorageMigrations import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test @@ -45,12 +47,13 @@ class PreferencesDataSourceMigrationTest { editor.commit() } + /** What a start does: the migrations, then the partition opened. */ private fun initPartition(shop: String = shopId): PreferencesDataSourceImpl { + StorageMigrations.run(context, StorageLayout(shop, null)) return PreferencesDataSourceImpl().apply { initialize( context = context, preferencesKey = PreferencesPartition.keyFor(shop), - legacyPreferencesKey = legacyKey, shopId = shop ) } @@ -122,7 +125,7 @@ class PreferencesDataSourceMigrationTest { ) val source = PreferencesDataSourceImpl().apply { - // No legacyPreferencesKey/shopId — the host opted into its own partition. + // A partition opened by name without a start's migrations reads nothing older. initialize(context = context, preferencesKey = "host_custom_prefs") } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceDataSourceImplTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceDataSourceImplTest.kt index e0b82923..c44fa21f 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceDataSourceImplTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceDataSourceImplTest.kt @@ -115,7 +115,6 @@ class TrackingSourceDataSourceImplTest { override fun initialize( context: android.content.Context, preferencesKey: String, - legacyPreferencesKey: String?, shopId: String? ) = Unit diff --git a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceMultiInstanceTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceMultiInstanceTest.kt index 0bf60f18..06a76bed 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceMultiInstanceTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceMultiInstanceTest.kt @@ -48,7 +48,6 @@ class TrackingSourceMultiInstanceTest { preferences.initialize( context = context, preferencesKey = PreferencesPartition.keyFor(shopId), - legacyPreferencesKey = null, shopId = shopId ) return TrackingSourceDataSourceImpl(preferences) @@ -113,7 +112,7 @@ class TrackingSourceMultiInstanceTest { context.getSharedPreferences("host_own_key", Context.MODE_PRIVATE).edit().clear().commit() fun hostStore(): TrackingSourceDataSourceImpl { val preferences = PreferencesDataSourceImpl() - preferences.initialize(context, "host_own_key", null, null) + preferences.initialize(context, "host_own_key", null) return TrackingSourceDataSourceImpl(preferences) } From 008dbbe8850fa708f62767d768093c7453ebb3c8 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 23:50:59 +0400 Subject: [PATCH 028/109] fix(sdk): a push token never goes out without a did: it waits with the requests that wait for one until the did is stored, on a new install before /init answers and after deleteUserCredentials(), whether the app set it, a provider delivered it or onNewToken brought it, on both cores; the app's OnPushTokenListener still hears of it at once --- .../personalization/push/PushTokenManager.kt | 8 +- .../com/personalization/PushTokenDidTest.kt | 99 +++++++++++++++++++ .../personalization/PushTokenManagerTest.kt | 42 ++++---- .../com/personalization/SdkStorageTest.kt | 25 +++++ .../core/internal/ShopStorageTest.kt | 4 +- 5 files changed, 154 insertions(+), 24 deletions(-) create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/PushTokenDidTest.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt index 4401d30c..3df7ec71 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt @@ -24,6 +24,11 @@ import javax.inject.Singleton * ([onTokenReceived]), or pushed manually by the host ([sendToken]) — funnels through here: * it is deduplicated, persisted per provider, sent to the rees46 backend, and reported to the * host via [OnPushTokenListener]. Adding a provider only requires a new [PushTokenSource]. + * + * A token never goes to the backend without a `did`: registered without one it would belong to no + * device. Until the device has a `did` (a new install before `/init` answers, or the window after + * `deleteUserCredentials()`) the request waits in the instance's queue and goes once the `did` is + * stored; the host's listener hears of the token at once all the same. */ @Singleton class PushTokenManager @Inject constructor( @@ -162,7 +167,8 @@ class PushTokenManager @Inject constructor( TOKEN_FIELD to token, PUSH_PROVIDER_FIELD to provider.serverValue ) - sendNetworkMethodUseCase.post( + // Through the queue of requests that wait for a did, never straight to the network. + sendNetworkMethodUseCase.postAsync( MOBILE_PUSH_TOKENS, JSONObject(params), object : OnApiCallbackListener() { diff --git a/personalization-sdk/src/test/kotlin/com/personalization/PushTokenDidTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/PushTokenDidTest.kt new file mode 100644 index 00000000..3435976b --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/PushTokenDidTest.kt @@ -0,0 +1,99 @@ +package com.personalization + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import com.personalization.push.PushTokenManager +import com.personalization.sdk.data.repositories.network.NetworkRepositoryImpl +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase +import com.personalization.sdk.domain.usecases.preferences.GetPreferencesValueUseCase +import com.personalization.sdk.domain.usecases.preferences.SavePreferencesValueUseCase +import io.mockk.every +import io.mockk.mockk +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicReference + +/** + * A push token never reaches the server without a `did`: it waits with the requests that wait for one + * and goes once the `did` is stored, whichever way it came (the app's `setPushToken`, a provider's + * token). + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class PushTokenDidTest { + + private val server = MockWebServer() + private val did = AtomicReference("") + private val settings = mockk(relaxed = true) { + every { getShopId() } returns "shop" + every { getDid() } answers { did.get() } + every { getSid() } returns "sid" + every { getSegmentForABTesting() } returns "A" + every { getStream() } returns "android" + } + private val repository = NetworkRepositoryImpl(settings) + private val preferences = mockk(relaxed = true) { + every { getPushToken(any()) } returns "" + every { getLastPushTokenDate(any()) } returns 0L + } + private val manager = PushTokenManager(SendNetworkMethodUseCase(repository), preferences, mockk(relaxed = true)) + + @Before + fun setUp() { + server.start() + repository.initialize(server.url("/").toString()) + server.enqueue(MockResponse().setBody("{}")) + } + + @After + fun tearDown() = server.shutdown() + + @Test + fun `the app's token waits for the did and goes with it`() { + manager.sendToken("app-token", PushProvider.FCM, null) + + assertNull(server.takeRequest(300, TimeUnit.MILLISECONDS)) + + did.set("new-did") + repository.executeQueueTasks() + + val body = JSONObject(checkNotNull(server.takeRequest(5, TimeUnit.SECONDS)).body.readUtf8()) + assertEquals("app-token", body.getString("token")) + assertEquals("new-did", body.getString("did")) + } + + @Test + fun `a provider's token waits for the did as well`() { + manager.initialize(ApplicationProvider.getApplicationContext(), autoSendPushToken = true) + + manager.onTokenReceived("provider-token", PushProvider.HMS) + + assertNull(server.takeRequest(300, TimeUnit.MILLISECONDS)) + did.set("new-did") + repository.executeQueueTasks() + val body = JSONObject(checkNotNull(server.takeRequest(5, TimeUnit.SECONDS)).body.readUtf8()) + assertEquals("provider-token", body.getString("token")) + assertEquals("new-did", body.getString("did")) + } + + @Test + fun `with a did the token goes at once`() { + did.set("known-did") + + manager.sendToken("app-token", PushProvider.FCM, null) + + val body = JSONObject(checkNotNull(server.takeRequest(5, TimeUnit.SECONDS)).body.readUtf8()) + assertEquals("known-did", body.getString("did")) + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/PushTokenManagerTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/PushTokenManagerTest.kt index 9c812a18..7ff9f585 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/PushTokenManagerTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/PushTokenManagerTest.kt @@ -53,11 +53,11 @@ class PushTokenManagerTest { @Test fun `sendToken sends push_provider firebase for FCM`() { val paramsSlot = slot() - every { sendNetworkMethodUseCase.post(any(), capture(paramsSlot), any()) } just Runs + every { sendNetworkMethodUseCase.postAsync(any(), capture(paramsSlot), any()) } just Runs pushTokenManager.sendToken("fcm-token-123", PushProvider.FCM, null) - verify(exactly = 1) { sendNetworkMethodUseCase.post("mobile_push_tokens", any(), any()) } + verify(exactly = 1) { sendNetworkMethodUseCase.postAsync("mobile_push_tokens", any(), any()) } val body = paramsSlot.captured assertEquals("fcm-token-123", body.getString("token")) assertEquals("android", body.getString("platform")) @@ -67,7 +67,7 @@ class PushTokenManagerTest { @Test fun `sendToken sends push_provider huawei for HMS`() { val paramsSlot = slot() - every { sendNetworkMethodUseCase.post(any(), capture(paramsSlot), any()) } just Runs + every { sendNetworkMethodUseCase.postAsync(any(), capture(paramsSlot), any()) } just Runs pushTokenManager.sendToken("hms-token-1", PushProvider.HMS, null) @@ -79,7 +79,7 @@ class PushTokenManagerTest { @Test fun `sendToken forwards backend success to caller listener and persists token`() { val listener = mockk(relaxed = true) - every { sendNetworkMethodUseCase.post(any(), any(), any()) } answers { + every { sendNetworkMethodUseCase.postAsync(any(), any(), any()) } answers { thirdArg().onSuccess(null as JSONObject?) } @@ -94,11 +94,11 @@ class PushTokenManagerTest { fun `onTokenReceived sends push_provider huawei for new HMS token`() { pushTokenManager.initialize(context, autoSendPushToken = true) val paramsSlot = slot() - every { sendNetworkMethodUseCase.post(any(), capture(paramsSlot), any()) } just Runs + every { sendNetworkMethodUseCase.postAsync(any(), capture(paramsSlot), any()) } just Runs pushTokenManager.onTokenReceived("hms-token-456", PushProvider.HMS) - verify(exactly = 1) { sendNetworkMethodUseCase.post("mobile_push_tokens", any(), any()) } + verify(exactly = 1) { sendNetworkMethodUseCase.postAsync("mobile_push_tokens", any(), any()) } val body = paramsSlot.captured assertEquals("hms-token-456", body.getString("token")) assertEquals("huawei", body.getString("push_provider")) @@ -112,7 +112,7 @@ class PushTokenManagerTest { pushTokenManager.onTokenReceived("hms-token-same", PushProvider.HMS) - verify(exactly = 0) { sendNetworkMethodUseCase.post("mobile_push_tokens", any(), any()) } + verify(exactly = 0) { sendNetworkMethodUseCase.postAsync("mobile_push_tokens", any(), any()) } } @Test @@ -122,11 +122,11 @@ class PushTokenManagerTest { every { getPreferencesValueUseCase.getLastPushTokenDate(PushProvider.HMS) } returns eightDaysAgo pushTokenManager.initialize(context, autoSendPushToken = true) val paramsSlot = slot() - every { sendNetworkMethodUseCase.post(any(), capture(paramsSlot), any()) } just Runs + every { sendNetworkMethodUseCase.postAsync(any(), capture(paramsSlot), any()) } just Runs pushTokenManager.onTokenReceived("hms-token-old", PushProvider.HMS) - verify(exactly = 1) { sendNetworkMethodUseCase.post("mobile_push_tokens", any(), any()) } + verify(exactly = 1) { sendNetworkMethodUseCase.postAsync("mobile_push_tokens", any(), any()) } assertEquals("hms-token-old", paramsSlot.captured.getString("token")) } @@ -136,7 +136,7 @@ class PushTokenManagerTest { pushTokenManager.onTokenReceived("hms-token", PushProvider.HMS) - verify(exactly = 0) { sendNetworkMethodUseCase.post("mobile_push_tokens", any(), any()) } + verify(exactly = 0) { sendNetworkMethodUseCase.postAsync("mobile_push_tokens", any(), any()) } } @Test @@ -144,7 +144,7 @@ class PushTokenManagerTest { var notified: Pair? = null pushTokenManager.setOnPushTokenListener { token, provider -> notified = token to provider } pushTokenManager.initialize(context, autoSendPushToken = true) - every { sendNetworkMethodUseCase.post(any(), any(), any()) } just Runs + every { sendNetworkMethodUseCase.postAsync(any(), any(), any()) } just Runs pushTokenManager.onTokenReceived("hms-token-789", PushProvider.HMS) @@ -158,12 +158,12 @@ class PushTokenManagerTest { fun `onTokenReceived sends once when the same token arrives twice before the response`() { pushTokenManager.initialize(context, autoSendPushToken = true) // post() never invokes its callback: the first request is still on the wire. - every { sendNetworkMethodUseCase.post(any(), any(), any()) } just Runs + every { sendNetworkMethodUseCase.postAsync(any(), any(), any()) } just Runs pushTokenManager.onTokenReceived("fcm-token-race", PushProvider.FCM) pushTokenManager.onTokenReceived("fcm-token-race", PushProvider.FCM) - verify(exactly = 1) { sendNetworkMethodUseCase.post("mobile_push_tokens", any(), any()) } + verify(exactly = 1) { sendNetworkMethodUseCase.postAsync("mobile_push_tokens", any(), any()) } } @Test @@ -171,7 +171,7 @@ class PushTokenManagerTest { val notified = mutableListOf() pushTokenManager.setOnPushTokenListener { token, _ -> notified.add(token) } pushTokenManager.initialize(context, autoSendPushToken = true) - every { sendNetworkMethodUseCase.post(any(), any(), any()) } just Runs + every { sendNetworkMethodUseCase.postAsync(any(), any(), any()) } just Runs pushTokenManager.onTokenReceived("fcm-token-race", PushProvider.FCM) pushTokenManager.onTokenReceived("fcm-token-race", PushProvider.FCM) @@ -182,45 +182,45 @@ class PushTokenManagerTest { @Test fun `the claim is per provider so the same token value is sent for FCM and HMS`() { pushTokenManager.initialize(context, autoSendPushToken = true) - every { sendNetworkMethodUseCase.post(any(), any(), any()) } just Runs + every { sendNetworkMethodUseCase.postAsync(any(), any(), any()) } just Runs pushTokenManager.onTokenReceived("shared-token", PushProvider.FCM) pushTokenManager.onTokenReceived("shared-token", PushProvider.HMS) - verify(exactly = 2) { sendNetworkMethodUseCase.post("mobile_push_tokens", any(), any()) } + verify(exactly = 2) { sendNetworkMethodUseCase.postAsync("mobile_push_tokens", any(), any()) } } @Test fun `a failed send releases the claim so the next delivery retries`() { pushTokenManager.initialize(context, autoSendPushToken = true) - every { sendNetworkMethodUseCase.post(any(), any(), any()) } answers { + every { sendNetworkMethodUseCase.postAsync(any(), any(), any()) } answers { thirdArg().onError(500, "server error") } pushTokenManager.onTokenReceived("fcm-token-retry", PushProvider.FCM) pushTokenManager.onTokenReceived("fcm-token-retry", PushProvider.FCM) - verify(exactly = 2) { sendNetworkMethodUseCase.post("mobile_push_tokens", any(), any()) } + verify(exactly = 2) { sendNetworkMethodUseCase.postAsync("mobile_push_tokens", any(), any()) } } @Test fun `a new token is sent after the previous one completed`() { pushTokenManager.initialize(context, autoSendPushToken = true) - every { sendNetworkMethodUseCase.post(any(), any(), any()) } answers { + every { sendNetworkMethodUseCase.postAsync(any(), any(), any()) } answers { thirdArg().onSuccess(null as JSONObject?) } pushTokenManager.onTokenReceived("fcm-token-first", PushProvider.FCM) pushTokenManager.onTokenReceived("fcm-token-second", PushProvider.FCM) - verify(exactly = 2) { sendNetworkMethodUseCase.post("mobile_push_tokens", any(), any()) } + verify(exactly = 2) { sendNetworkMethodUseCase.postAsync("mobile_push_tokens", any(), any()) } } @Test fun `fcm and hms tokens carry different push_provider values`() { pushTokenManager.initialize(context, autoSendPushToken = true) val capturedParams = mutableListOf() - every { sendNetworkMethodUseCase.post(any(), any(), any()) } answers { + every { sendNetworkMethodUseCase.postAsync(any(), any(), any()) } answers { capturedParams.add(secondArg()) } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt index b17ae385..3aa4a72d 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt @@ -35,6 +35,8 @@ import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config import java.io.File import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.Executor import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicInteger @@ -52,6 +54,7 @@ class SdkStorageTest { private val server = MockWebServer() private val inits = CopyOnWriteArrayList() private val initCount = AtomicInteger() + private val tokens = LinkedBlockingQueue() private val logged = CopyOnWriteArrayList() private val defaultExecutor = SdkLogs.handlerExecutor @@ -80,6 +83,7 @@ class SdkStorageTest { server.dispatcher = object : Dispatcher() { override fun dispatch(request: RecordedRequest): MockResponse { val url = request.requestUrl ?: return MockResponse().setResponseCode(400) + if (url.encodedPath == "/mobile_push_tokens") tokens += JSONObject(request.body.readUtf8()) if (url.encodedPath != "/init") return MockResponse().setBody("{}") val did = url.queryParameter("did").orEmpty() inits += did @@ -301,6 +305,27 @@ class SdkStorageTest { } } + @Test + fun `after the deletion the push token waits for the new did`() { + val secondInit = CountDownLatch(1) + initAnswer = { n, _ -> + if (n == 2) secondInit.await(5, TimeUnit.SECONDS) + MockResponse().setBody("""{"did":"did-$n","seance":"S$n"}""") + } + val sdk = Rees46.initialize(context, config()) + awaitDid(sdk, "did-1") + + runBlocking { sdk.deleteUserCredentials() } + sdk.setPushToken("app-token", PushProvider.FCM) + + // Held while the new /init has not answered: no token without a did. + assertEquals(null, tokens.poll(300, TimeUnit.MILLISECONDS)) + secondInit.countDown() + val token = checkNotNull(tokens.poll(5, TimeUnit.SECONDS)) + assertEquals("app-token", token.getString("token")) + assertEquals("did-2", token.getString("did")) + } + @Test fun `on the new core the deletion takes the queue too`() { val sdk = Rees46.initialize(context, config(coreV2 = true)) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt index 2995daaf..0db1826a 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt @@ -180,12 +180,12 @@ class ShopStorageTest { // Sent this week to the customer before: not sent again. tokens.onTokenReceived("fcm-1", PushProvider.FCM) - verify(exactly = 0) { network.post("mobile_push_tokens", any(), any()) } + verify(exactly = 0) { network.postAsync("mobile_push_tokens", any(), any()) } storage().deleteUserCredentials(includeQueue = false) tokens.onTokenReceived("fcm-1", PushProvider.FCM) - verify(exactly = 1) { network.post("mobile_push_tokens", any(), any()) } + verify(exactly = 1) { network.postAsync("mobile_push_tokens", any(), any()) } } /** A shop of a running install: its customer, the device's data and the stories' state. */ From ff18eeae35cc8aac5057c4fd4de47ecb5b9cdc50 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Fri, 13 Nov 2026 23:51:55 +0400 Subject: [PATCH 029/109] fix(sdk): log lines after review: an exception reaches the log as its class only, never its text or a stack trace (a popups v3 element that failed to render, a popup image, a popup link no app opened, a notification image); the lines of popups v3 elements, StoriesView, StoriesWidget and the stories viewer carry their shop where it is known, and the popup lines are written whatever the level while PopupsV3.debug is on; core_flag_mismatch is logged once per shop, not again when a registered shop makes its instance; an unexpected failure of the new core's request is sdk_log, not callback_failed; a failed stories v3 event is logged once, as request_failed --- .../core/internal/CoreSelection.kt | 16 +++++++++---- .../personalization/core/internal/CoreV2.kt | 2 +- .../impl/InAppNotificationManagerImpl.kt | 3 ++- .../service/NotificationService.kt | 6 ++++- .../view/component/image/ImageView.kt | 11 +++++++-- .../stories/compose/StoriesWidget.kt | 5 +++- .../stories/views/StoriesView.kt | 12 +++++++--- .../ui/popups/PopupV3Elements.kt | 24 ++++++++++++++----- .../widgets/PersonalizationStoriesViewer.kt | 10 +++++--- .../ui/widgets/StoriesApiV3.kt | 4 ++-- .../core/internal/CoreSelectionTest.kt | 12 ++++++++++ 11 files changed, 81 insertions(+), 24 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreSelection.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreSelection.kt index ef2fe2cf..a7b8e211 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreSelection.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreSelection.kt @@ -7,16 +7,21 @@ import com.personalization.logs.SdkLogs /** * Which core the process runs: decided by the first `initialize` or `register`, the same for every * instance after it. An instance that asks for the other one gets the process's and a - * `core_flag_mismatch` warning: switching takes a new launch. + * `core_flag_mismatch` warning, once per shop (a registration and the instance it makes later ask + * twice): switching takes a new launch. */ internal object CoreSelection { private var coreV2: Boolean? = null + private val warned = HashSet() /** The process's choice; the first call makes it from [requested]. */ fun resolve(requested: Boolean, shopId: String?): Boolean { - val decided = synchronized(this) { coreV2 ?: requested.also { coreV2 = it } } - if (decided != requested) { + val (decided, warn) = synchronized(this) { + val decided = coreV2 ?: requested.also { coreV2 = it } + decided to (decided != requested && warned.add(shopId)) + } + if (warn) { SdkLogs.log( LogLevel.WARNING, LogCodes.CORE_FLAG_MISMATCH, @@ -29,5 +34,8 @@ internal object CoreSelection { } /** Test-only: the next call decides again. */ - internal fun reset() = synchronized(this) { coreV2 = null } + internal fun reset() = synchronized(this) { + coreV2 = null + warned.clear() + } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt index 65e4ebee..6f8c0482 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt @@ -33,7 +33,7 @@ internal class CoreV2( /** Nothing a request does may reach the app's process as an exception. */ private val uncaught = CoroutineExceptionHandler { _, throwable -> - SdkLogs.log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "A request of the core failed unexpectedly", shopId, throwable = throwable) + SdkLogs.log(LogLevel.ERROR, LogCodes.SDK_LOG, "A request of the core failed unexpectedly", shopId, throwable = throwable) } /** The instance's requests; [release] cancels them all. */ diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt index 672e5543..3766f2d3 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/inAppNotification/impl/InAppNotificationManagerImpl.kt @@ -340,7 +340,8 @@ class InAppNotificationManagerImpl @Inject constructor( } context.startActivity(intent) } catch (exception: Exception) { - exception.printStackTrace() + // The class only: the text names the link. + log(LogLevel.WARNING, LogCodes.UI, "No app opened the popup's link", throwable = exception) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/service/NotificationService.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/service/NotificationService.kt index db9313db..63af571f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/service/NotificationService.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/service/NotificationService.kt @@ -13,6 +13,9 @@ import com.personalization.features.notification.service.mapper.toNotificationDa import com.personalization.resources.NotificationResources.NOTIFICATION_LOADING_DATA_ERROR import com.personalization.resources.NotificationResources.NOTIFICATION_LOADING_IMAGE_ERROR import com.personalization.sdk.data.models.dto.notification.NotificationData +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import java.io.IOException import javax.inject.Inject import kotlinx.coroutines.CoroutineScope @@ -124,7 +127,8 @@ class NotificationService : Service() { /* resId = */ NOTIFICATION_LOADING_IMAGE_ERROR ) ) - ioException.printStackTrace() + // The class only: the text names the image's URL. + SdkLogs.log(LogLevel.WARNING, LogCodes.PUSH, "A notification image failed to load", throwable = ioException) } finally { stopSelf(startId) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/inAppNotification/view/component/image/ImageView.kt b/personalization-sdk/src/main/kotlin/com/personalization/inAppNotification/view/component/image/ImageView.kt index 9f862637..71736323 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/inAppNotification/view/component/image/ImageView.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/inAppNotification/view/component/image/ImageView.kt @@ -10,6 +10,9 @@ import com.bumptech.glide.load.engine.GlideException import com.bumptech.glide.request.RequestListener import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.target.Target +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs class ImageView @JvmOverloads constructor( context: Context, @@ -31,7 +34,7 @@ class ImageView @JvmOverloads constructor( target: Target, isFirstResource: Boolean ): Boolean { - exception?.printStackTrace() + exception?.let { failed("The popup image failed to load", it) } return false } @@ -48,7 +51,11 @@ class ImageView @JvmOverloads constructor( ) .into(this) } catch (exception: Exception) { - exception.printStackTrace() + failed("The popup image could not be requested", exception) } } + + /** The class of the failure only: its text carries the image's URL. */ + private fun failed(message: String, throwable: Throwable) = + SdkLogs.log(LogLevel.WARNING, LogCodes.UI, message, throwable = throwable) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/compose/StoriesWidget.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/compose/StoriesWidget.kt index 518f43d0..7b3eccfd 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/compose/StoriesWidget.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/compose/StoriesWidget.kt @@ -1,5 +1,8 @@ package com.personalization.stories.compose +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.produceState @@ -51,7 +54,7 @@ fun StoriesWidget( val handle = try { Rees46.awaitInstance(shopId) { value = it } } catch (throwable: Throwable) { - SDK.error("StoriesWidget: cannot resolve an SDK for shopId=$shopId", throwable) + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "StoriesWidget: cannot resolve an SDK for shopId=$shopId", shopId, throwable = throwable) Cancellable.NOOP } awaitDispose { handle.cancel() } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoriesView.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoriesView.kt index 8176e767..fed46e26 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoriesView.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoriesView.kt @@ -1,5 +1,8 @@ package com.personalization.stories.views +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import android.annotation.SuppressLint import android.annotation.TargetApi import android.content.Context @@ -109,7 +112,7 @@ class StoriesView : ConstraintLayout, ClickListener { typedArray.getString(R.styleable.StoriesView_product_banner_tap_default_message) ?: "Copied" this.shopId = typedArray.getString(R.styleable.StoriesView_shop_id) if (code == null) { - SDK.error("Code is set incorrectly") + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "StoriesView: app:code is not set", shopId) return } this.code = code @@ -149,10 +152,13 @@ class StoriesView : ConstraintLayout, ClickListener { instanceHandle = try { Rees46.awaitInstance(shopId) { resolved -> attach(resolved) } } catch (throwable: Throwable) { - SDK.error( + SdkLogs.log( + LogLevel.ERROR, + LogCodes.STORIES, "StoriesView: cannot resolve an SDK for shopId=$shopId — set app:shop_id to pick a " + "shop when several are registered. The block will not load.", - throwable + shopId, + throwable = throwable ) null } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Elements.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Elements.kt index d9aa1e8c..50862c54 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Elements.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/popups/PopupV3Elements.kt @@ -27,7 +27,11 @@ import com.bumptech.glide.Glide import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy import com.bumptech.glide.request.target.CustomTarget import com.bumptech.glide.request.transition.Transition +import com.personalization.LogLevel import com.personalization.SDK +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import com.personalization.popups.popupsV3Log import com.personalization.popups.PopupV3Design import com.personalization.popups.PopupV3Element import com.personalization.popups.PopupV3Links @@ -83,7 +87,10 @@ internal class PopupV3RenderContext( val screenHeightPx: Int, /** Фон рамки из дизайна. */ val frameBackground: Int = Color.WHITE, - val log: (String) -> Unit = SDK::debug + /** Строка попапов v3 под магазином [sdk]; с `PopupsV3.debug` — при любом уровне лога. */ + val log: (String) -> Unit = { message -> + if (sdk != null) popupsV3Log(sdk, LogLevel.DEBUG, message) else SdkLogs.log(LogLevel.DEBUG, LogCodes.POPUPS_V3, message) + } ) { fun dp(value: Double): Int = context.popupDp(value) fun dpF(value: Double): Float = context.popupDpF(value) @@ -169,7 +176,8 @@ internal object PopupV3Renderer { val content = try { renderer.render(element, ctx) } catch (throwable: Throwable) { - ctx.log("popups v3: element ${element.id} (${element.type}) failed: ${throwable.message}") + // The class only: an exception's text may carry the popup's data. + ctx.log("popups v3: element ${element.id} (${element.type}) failed: ${SdkLogs.exceptionName(throwable)}") null } ?: return null val box = PopupV3Specs.box(element) @@ -272,7 +280,7 @@ internal object PopupV3Renderer { val spec = PopupV3Specs.image(element, ctx.texts) if (!spec.show) return null val url = PopupV3Links.image(ctx.runtime(spec.url)) - val view = PopupV3ImageView(ctx.context, spec, ctx.screenHeightPx) + val view = PopupV3ImageView(ctx.context, spec, ctx.screenHeightPx, ctx.log) view.contentDescription = spec.alt view.scaleType = when (spec.fit) { "contain" -> ImageView.ScaleType.FIT_CENTER @@ -843,8 +851,12 @@ internal object PopupV3Renderer { * у `auto` высоты до загрузки нет. */ @SuppressLint("ViewConstructor") -internal class PopupV3ImageView(context: Context, private val spec: PopupV3Specs.Image, private val screenHeightPx: Int) : - AppCompatImageView(context) { +internal class PopupV3ImageView( + context: Context, + private val spec: PopupV3Specs.Image, + private val screenHeightPx: Int, + private val log: (String) -> Unit, +) : AppCompatImageView(context) { override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { val width = MeasureSpec.getSize(widthMeasureSpec) @@ -872,7 +884,7 @@ internal class PopupV3ImageView(context: Context, private val spec: PopupV3Specs override fun onLoadCleared(placeholder: Drawable?) = setImageDrawable(placeholder) override fun onLoadFailed(errorDrawable: Drawable?) { - SDK.debug("popups v3: an image failed to load") + log("popups v3: an image failed to load") } }) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStoriesViewer.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStoriesViewer.kt index 16c76452..dfc92b5f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStoriesViewer.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/PersonalizationStoriesViewer.kt @@ -631,7 +631,7 @@ class PersonalizationStoriesViewer( try { (activity ?: hostContext).startActivity(intent) } catch (_: android.content.ActivityNotFoundException) { - SDK.error("PersonalizationStoriesViewer: no app can open the link") + logError("PersonalizationStoriesViewer: no app can open the link") } } @@ -659,7 +659,7 @@ class PersonalizationStoriesViewer( try { (activity ?: hostContext).startActivity(chooser) } catch (throwable: RuntimeException) { - SDK.error("PersonalizationStoriesViewer: the link could not be shared", throwable) + logError("PersonalizationStoriesViewer: the link could not be shared", throwable) endShare() return } @@ -910,7 +910,7 @@ class PersonalizationStoriesViewer( } override fun onPlayerError(error: androidx.media3.common.PlaybackException) { - SDK.error("PersonalizationStoriesViewer: video failed", error) + logError("PersonalizationStoriesViewer: video failed", error) playerSlide?.let(::onMediaFailed) } @@ -975,6 +975,10 @@ class PersonalizationStoriesViewer( com.bumptech.glide.Glide.with(hostContext.applicationContext).load(url).preload() } + /** An error of the viewer under its shop, so the shop's log level applies. */ + private fun logError(message: String, throwable: Throwable? = null) = + SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, message, sdk?.logShopId ?: shopId, throwable = throwable) + private inner class StoriesAdapter : androidx.recyclerview.widget.RecyclerView.Adapter() { override fun getItemCount(): Int = stories.size diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt index 8b31a9ba..3040e94d 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt @@ -112,8 +112,8 @@ internal object StoriesApiV3 { runCatching { sdk.storiesManager.setTrackingSourceUseCase(StoriesManager.STORIES_SOURCE_TYPE, code) } whenDid(sdk) { val body = eventBody(shopId(sdk), sdk.getDid().orEmpty(), event, storyId, slideId) - val response = post(eventsUrl(domain, code), body, sdk.logShopId) - if (response.code !in 200..299) SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES_V3, "stories v3: $event failed: ${response.code}", sdk.logShopId) + // A failed answer is logged by the call itself, as request_failed. + post(eventsUrl(domain, code), body, sdk.logShopId) } } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSelectionTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSelectionTest.kt index 3a910a18..c58598a4 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSelectionTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSelectionTest.kt @@ -58,4 +58,16 @@ class CoreSelectionTest { assertEquals("b", warning.shopId) assertEquals(mapOf("requested" to true, "process" to false), warning.context) } + + @Test + fun `a registration and the instance it makes later warn once`() { + CoreSelection.resolve(requested = false, shopId = "a") + + // Rees46.register asks for the shop, and its instance asks again when it is made. + CoreSelection.resolve(requested = true, shopId = "b") + CoreSelection.resolve(requested = true, shopId = "b") + CoreSelection.resolve(requested = true, shopId = "c") + + assertEquals(listOf("b", "c"), received.map { it.shopId }) + } } From 46aa31c200b88c5d58ba6054a0fd03e959a69013 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 00:12:29 +0400 Subject: [PATCH 030/109] feat(core): initialization and the instance (stage 3). Both cores: SDK.state (SdkState: notInitialized, initializing, ready, retrying, refused) with onStateChange listeners called on the main thread once per change, a throwing one logged as callback_failed; shopId, deviceId and sessionId, with getDid()/getSid() returning "" when there is none, also before initialization; Rees46Config.deviceId, a did the app brings, taken while the shop has none; a second Rees46.initialize for a shop returns its instance (a different config logs invalid_config, already_initialized) and calls from several threads make one; Rees46.register and Rees46.instance, with registerShops and getInstance kept as deprecated delegates; UnknownShopIdException and AmbiguousShopException share Rees46Error, still an IllegalStateException; release() stops the instance and unregisters it, later calls fail with notInitialized (onError(-1), a request waiting for a did included, deleteUserCredentials throws and deletes nothing), and a new initialize makes a fresh instance. The legacy core goes from initializing to ready once the did and session are in place, at once for a known device, and is never retrying or refused. The new core has its own /init: a shop with a did (stored, from the config or from the saved answer) is ready at once while /init goes in the background; no answer, a 2xx without a did, 5xx, 408, 410, 417, 426 and 429 are retrying with pauses of 1 s doubling to 5 min, spread by 20 %, or the Retry-After of a 429 or 503, and without a did and a network /init waits for the network; 402, 403, 404 and other 4xx refuse the shop, kept on disk until a later launch's /init succeeds, and every request then fails with shopBlocked (onError(402)); each successful answer is saved, its seance becomes the sid and a stale sid is not sent; a request waits for the instance within its read timeout; needReInitialization really gets a new did; /init carries the configured stream, tz with summer time and the advertising id only with Rees46Config.sendAdvertisingId; it goes again on returning to the foreground after two hours and through an internal checkShop() at most every 10 minutes; logs init_succeeded, init_retry, init_failed, init_refused, init_saved_answer and init_skipped --- .../demo/MultiInstanceE2ETest.kt | 26 +- .../personalization/demo/DemoApplication.kt | 4 +- .../com/personalization/demo/MainActivity.kt | 2 +- .../personalization/demo/MultiInstancePane.kt | 8 +- .../personalization/demo/game/GameActivity.kt | 2 +- .../personalization/demo/shop/ShopActivity.kt | 2 +- .../com/personalization/InstanceResolver.kt | 2 +- .../main/kotlin/com/personalization/Rees46.kt | 106 ++-- .../com/personalization/Rees46Config.kt | 29 +- .../com/personalization/RegisterManager.kt | 55 +- .../main/kotlin/com/personalization/SDK.kt | 353 ++++++++++--- .../personalization/SdkInstanceException.kt | 18 +- .../kotlin/com/personalization/SdkRegistry.kt | 2 +- .../api/managers/TrackingApi.kt | 2 +- .../com/personalization/core/SdkState.kt | 28 ++ .../personalization/core/internal/Backoff.kt | 31 ++ .../core/internal/CoreHttpClient.kt | 26 +- .../personalization/core/internal/CoreInit.kt | 475 ++++++++++++++++++ .../personalization/core/internal/CoreV2.kt | 28 +- .../core/internal/HttpResult.kt | 5 +- .../core/internal/InstanceState.kt | 83 +++ .../core/internal/LegacyCore.kt | 19 +- .../core/internal/ProcessAppLifecycle.kt | 69 +++ .../core/internal/ShopStorage.kt | 22 + .../com/personalization/logs/SdkLogs.kt | 3 + .../network/NetworkRepositoryImpl.kt | 26 +- .../stories/compose/StoriesWidget.kt | 2 +- .../stories/views/StoriesView.kt | 2 +- .../personalization/Rees46ConfigJavaTest.java | 14 +- .../core/internal/CoreFunnelTest.kt | 11 +- 30 files changed, 1254 insertions(+), 201 deletions(-) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/SdkState.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/Backoff.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/InstanceState.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/ProcessAppLifecycle.kt diff --git a/demo-app/src/androidTest/java/com/personalization/demo/MultiInstanceE2ETest.kt b/demo-app/src/androidTest/java/com/personalization/demo/MultiInstanceE2ETest.kt index 1129f812..ad3f2501 100644 --- a/demo-app/src/androidTest/java/com/personalization/demo/MultiInstanceE2ETest.kt +++ b/demo-app/src/androidTest/java/com/personalization/demo/MultiInstanceE2ETest.kt @@ -66,8 +66,8 @@ class MultiInstanceE2ETest { @Test fun bothShops_resolveToDistinctInstances() { - val a = Rees46.getInstance(shopA) - val b = Rees46.getInstance(shopB) // materializes the lazily-registered shop B + val a = Rees46.instance(shopA) + val b = Rees46.instance(shopB) // materializes the lazily-registered shop B assertNotNull(a) assertNotNull(b) assertNotSame("Each shop must have its own SDK instance", a, b) @@ -84,19 +84,19 @@ class MultiInstanceE2ETest { @Test fun defaultGetInstance_isAmbiguousWithTwoShopsLive() { - Rees46.getInstance(shopB) // make both live - assertThrowsOf { Rees46.getInstance() } + Rees46.instance(shopB) // make both live + assertThrowsOf { Rees46.instance() } } @Test fun getInstance_forUnknownShop_throwsUnknownShopId() { - assertThrowsOf { Rees46.getInstance("nope-not-a-shop") } + assertThrowsOf { Rees46.instance("nope-not-a-shop") } } @Test fun handlePush_routesToTheNamedShopOnly() { - val a = Rees46.getInstance(shopA) - val b = Rees46.getInstance(shopB) + val a = Rees46.instance(shopA) + val b = Rees46.instance(shopB) val toA = mutableListOf() val toB = mutableListOf() a.setOnMessageListener { toA.add(it) } @@ -113,8 +113,8 @@ class MultiInstanceE2ETest { @Test fun handlePush_withUnknownShop_isDropped() { - val a = Rees46.getInstance(shopA) - val b = Rees46.getInstance(shopB) + val a = Rees46.instance(shopA) + val b = Rees46.instance(shopB) val toA = mutableListOf() val toB = mutableListOf() a.setOnMessageListener { toA.add(it) } @@ -130,7 +130,7 @@ class MultiInstanceE2ETest { @Test fun handlePush_clicked_tracksTheClickOnTheNamedShop() { - Rees46.getInstance(shopB) // ensure B is live and its session is ready + Rees46.instance(shopB) // ensure B is live and its session is ready HttpLogStore.clear() // CLICKED goes through notificationClicked, whose extractor reads NOTIFICATION_TYPE/ @@ -161,7 +161,7 @@ class MultiInstanceE2ETest { // used by no other test, so on a fresh process it is genuinely pending here. val lazyShop = "mi-materialize-on-push" val context = InstrumentationRegistry.getInstrumentation().targetContext - Rees46.registerShops( + Rees46.register( context = context, configs = listOf(Rees46Config(shopId = lazyShop, autoSendPushToken = false)), eagerInit = false @@ -196,8 +196,8 @@ class MultiInstanceE2ETest { /** Both shops live, then poll until the server has assigned each a did/sid (async /init). */ private fun awaitSessions(timeoutMs: Long = 10_000): Sessions { - val a = Rees46.getInstance(shopA) - val b = Rees46.getInstance(shopB) + val a = Rees46.instance(shopA) + val b = Rees46.instance(shopB) val deadline = System.currentTimeMillis() + timeoutMs var s = read(a, b) while (System.currentTimeMillis() < deadline && diff --git a/demo-app/src/main/java/com/personalization/demo/DemoApplication.kt b/demo-app/src/main/java/com/personalization/demo/DemoApplication.kt index 034bc6fb..3b60f6af 100644 --- a/demo-app/src/main/java/com/personalization/demo/DemoApplication.kt +++ b/demo-app/src/main/java/com/personalization/demo/DemoApplication.kt @@ -44,7 +44,7 @@ class DemoApplication : MultiDexApplication() { // Official initialization (per docs): initialize the shop once in Application.onCreate through // the Rees46 entry point so it is registered and ready in every process start — including the // cold process FCM starts just to deliver a push. Reach it anywhere with - // Rees46.getInstance(shopId). + // Rees46.instance(shopId). try { sdk = Rees46.initialize( context = applicationContext, @@ -55,7 +55,7 @@ class DemoApplication : MultiDexApplication() { // Second shop for the Multi-instance tab: registered but not initialized here. It comes // to life lazily the first time that tab resolves it (getInstance / a StoriesWidget with // its shopId) — demonstrating per-shop lazy materialization alongside the eager default. - Rees46.registerShops( + Rees46.register( context = applicationContext, configs = listOf(Rees46Config(shopId = BuildConfig.SHOP_ID_2)), eagerInit = false, diff --git a/demo-app/src/main/java/com/personalization/demo/MainActivity.kt b/demo-app/src/main/java/com/personalization/demo/MainActivity.kt index 9666c3dd..60033752 100644 --- a/demo-app/src/main/java/com/personalization/demo/MainActivity.kt +++ b/demo-app/src/main/java/com/personalization/demo/MainActivity.kt @@ -173,7 +173,7 @@ class MainActivity : AppCompatActivity() { // The shop is initialized once in DemoApplication.onCreate (Application) so it is ready in // every process — including the cold process FCM/HMS starts to deliver a push. Reach that same // registered instance by its shopId (do NOT create a new SDK()). - sdk = Rees46.getInstance(BuildConfig.SHOP_ID) + sdk = Rees46.instance(BuildConfig.SHOP_ID) // Show the registered push provider(s) + token (FCM and/or HMS) in the header. observePushTokens() diff --git a/demo-app/src/main/java/com/personalization/demo/MultiInstancePane.kt b/demo-app/src/main/java/com/personalization/demo/MultiInstancePane.kt index 22d6cf67..85d78ed4 100644 --- a/demo-app/src/main/java/com/personalization/demo/MultiInstancePane.kt +++ b/demo-app/src/main/java/com/personalization/demo/MultiInstancePane.kt @@ -55,9 +55,9 @@ fun MultiInstancePane( // Re-reading the sessions after B's async /init lands: bumping this recomposes the cards. var refreshToken by remember { mutableStateOf(0) } - val shopA = remember(refreshToken) { runCatching { Rees46.getInstance(shopIdA) }.getOrNull() } + val shopA = remember(refreshToken) { runCatching { Rees46.instance(shopIdA) }.getOrNull() } // Resolving B by its id materializes the lazy registration — this is where shop B is born. - val shopB = remember(refreshToken) { runCatching { Rees46.getInstance(shopIdB) }.getOrNull() } + val shopB = remember(refreshToken) { runCatching { Rees46.instance(shopIdB) }.getOrNull() } // The global Rees46.setOnMessageListener (DemoApplication) already posts a notification for every // shop. This extra, screen-scoped subscriber shows the (deprecated) per-instance @@ -115,10 +115,10 @@ fun MultiInstancePane( // Fail-fast contracts, exercised live. Text(text = stringResource(R.string.mi_contracts_title), fontWeight = FontWeight.Bold) - Button(onClick = { log(runContract { Rees46.getInstance() }) }) { + Button(onClick = { log(runContract { Rees46.instance() }) }) { Text(text = stringResource(R.string.mi_btn_ambiguous)) } - Button(onClick = { log(runContract { Rees46.getInstance("nope") }) }) { + Button(onClick = { log(runContract { Rees46.instance("nope") }) }) { Text(text = stringResource(R.string.mi_btn_unknown)) } diff --git a/demo-app/src/main/java/com/personalization/demo/game/GameActivity.kt b/demo-app/src/main/java/com/personalization/demo/game/GameActivity.kt index 4e801875..0b600697 100644 --- a/demo-app/src/main/java/com/personalization/demo/game/GameActivity.kt +++ b/demo-app/src/main/java/com/personalization/demo/game/GameActivity.kt @@ -143,7 +143,7 @@ class GameActivity : AppCompatActivity() { ) /** `null` — the shop did not initialize. */ - private fun shopSdk(): SDK? = runCatching { Rees46.getInstance(BuildConfig.SHOP_ID) }.getOrNull() + private fun shopSdk(): SDK? = runCatching { Rees46.instance(BuildConfig.SHOP_ID) }.getOrNull() /** Opens the game of the game link [link] over the screen of [context]. */ fun start(context: Context, link: String) { diff --git a/demo-app/src/main/java/com/personalization/demo/shop/ShopActivity.kt b/demo-app/src/main/java/com/personalization/demo/shop/ShopActivity.kt index 142ab511..590b75f8 100644 --- a/demo-app/src/main/java/com/personalization/demo/shop/ShopActivity.kt +++ b/demo-app/src/main/java/com/personalization/demo/shop/ShopActivity.kt @@ -56,7 +56,7 @@ class ShopActivity : AppCompatActivity() { statusBar.onSystemBars { top, _ -> statusBar.updateLayoutParams { height = top } } shop = DemoShop( context = this, - sdk = Rees46.getInstance(BuildConfig.SHOP_ID), + sdk = Rees46.instance(BuildConfig.SHOP_ID), shopId = BuildConfig.SHOP_ID, storiesCode = getString(R.string.shop_stories_code), container = container diff --git a/personalization-sdk/src/main/kotlin/com/personalization/InstanceResolver.kt b/personalization-sdk/src/main/kotlin/com/personalization/InstanceResolver.kt index bc8ed39c..2ae8cbb5 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/InstanceResolver.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/InstanceResolver.kt @@ -1,7 +1,7 @@ package com.personalization /** - * Pure decision logic behind [Rees46.getInstance]: given the requested `shopId` (or none) and the + * Pure decision logic behind [Rees46.instance]: given the requested `shopId` (or none) and the * sets of live and pending shops, decides which instance to return, whether one must be lazily * materialized, or which error to raise. Kept side-effect-free so the resolution rules can be tested * without constructing or initializing an SDK. diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt index c901f860..0ecd9dc2 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt @@ -14,16 +14,16 @@ import java.util.concurrent.ConcurrentHashMap * Public entry point for the SDK — the unified, multi-instance API. * * A host no longer keeps its own reference to the SDK: initialize (or register) shops here and reach - * them by `shopId` through [getInstance]. One instance per shop, each with isolated storage and state. + * them by `shopId` through [instance]. One instance per shop, each with isolated storage and state. * * ```kotlin * // Single shop: * Rees46.initialize(context, Rees46Config(shopId = "SHOP_ID")) - * Rees46.getInstance().track(...) + * Rees46.instance().tracking.productView("37") * * // Several shops, initialized lazily on first use: - * Rees46.registerShops(context, listOf(Rees46Config("shop-a"), Rees46Config("shop-b"))) - * Rees46.getInstance("shop-a").track(...) + * Rees46.register(context, listOf(Rees46Config("shop-a"), Rees46Config("shop-b"))) + * Rees46.instance("shop-a").tracking.productView("37") * ``` */ object Rees46 { @@ -32,39 +32,42 @@ object Rees46 { private val pending = ConcurrentHashMap() + /** Making an instance takes it: two calls for a shop never make two instances. */ + private val lock = Any() + /** - * Initializes an SDK instance for [config] immediately and returns it. The instance is registered, - * so it is also reachable via [getInstance]. Any pending registration for the same shop is cleared. + * Initializes an SDK instance for [config] and returns it at once; `/init` goes in the background + * and [SDK.state] follows it. The instance is registered, so it is also reachable via [instance]. + * Any pending registration for the same shop is cleared. + * + * A shop that has a live instance gets that instance back, not a second one; when [config] differs + * from the one it was made with, `invalid_config` (`already_initialized`) is logged and the instance + * keeps its own. After [SDK.release] a call makes a fresh instance. Calls from several threads at + * once make one instance. */ - @Suppress("DEPRECATION") - fun initialize(context: Context, config: Rees46Config): SDK { + fun initialize(context: Context, config: Rees46Config): SDK = synchronized(lock) { + val existing = SdkRegistry.byShopId(config.shopId) + if (existing != null && !existing.pushOnly) { + existing.keepConfig(config) + pending.remove(config.shopId) + return existing + } val sdk = SDK() - sdk.enableAutoPopupPresentation = config.enableAutoPopupPresentation - sdk.logLevel = config.logLevel - sdk.coreV2Requested = config.coreV2 - sdk.initialize( - context = context, - shopId = config.shopId, - apiDomain = config.apiDomain, - tag = config.tag, - // The prefix of the shop's partition; a file of the same name is read once as its old data. - preferencesKey = config.storageKey ?: PreferencesPartition.LEGACY_KEY, - stream = config.stream, - autoSendPushToken = config.autoSendPushToken, - needReInitialization = config.needReInitialization, - addTrailingSlash = config.addTrailingSlash - ) + sdk.initializeWith(context, config) + // A shop brought up only to track a push gives way to the full instance. + if (existing != null && existing !== sdk) existing.release() + // Removed once the instance is registered: a lookup meanwhile still finds the shop. pending.remove(config.shopId) - return sdk + sdk } /** * Registers [configs] without initializing them. Initialization happens lazily on the first - * [getInstance] for a shop — the region case, where only the current region is needed. Pass + * [instance] for a shop — the region case, where only the current region is needed. Pass * [eagerInit] = true to initialize every shop up front — the super-shop case, where instances must * stay consistent. */ - fun registerShops( + fun register( context: Context, configs: List, eagerInit: Boolean = false @@ -84,6 +87,24 @@ object Rees46 { } } + /** The former name of [register]. */ + @Deprecated( + message = "Renamed to register.", + replaceWith = ReplaceWith("Rees46.register(context, configs, eagerInit)", "com.personalization.Rees46") + ) + fun registerShops( + context: Context, + configs: List, + eagerInit: Boolean = false + ) = register(context, configs, eagerInit) + + /** The former name of [instance]. */ + @Deprecated( + message = "Renamed to instance.", + replaceWith = ReplaceWith("Rees46.instance(shopId)", "com.personalization.Rees46") + ) + fun getInstance(shopId: String? = null): SDK = instance(shopId) + /** * Returns the SDK instance for [shopId], initializing a pending registration on first use. With no * [shopId], returns the single instance when exactly one shop is registered. @@ -91,7 +112,7 @@ object Rees46 { * @throws AmbiguousShopException when [shopId] is null and more than one shop is registered. * @throws UnknownShopIdException when the shop is unknown — nothing registered, or no such shop. */ - fun getInstance(shopId: String? = null): SDK { + fun instance(shopId: String? = null): SDK { val resolution = InstanceResolver.resolve( requestedShopId = shopId, liveShopIds = SdkRegistry.shopIds(), @@ -108,12 +129,12 @@ object Rees46 { if (shopId != null) { missingMessage(shopId) } else { - "No shop has been registered. Call Rees46.initialize(...) or Rees46.registerShops(...) first." + "No shop has been registered. Call Rees46.initialize(...) or Rees46.register(...) first." } ) InstanceResolver.Resolution.Ambiguous -> throw AmbiguousShopException( - "More than one shop is registered — call Rees46.getInstance(shopId) with an explicit id. " + + "More than one shop is registered — call Rees46.instance(shopId) with an explicit id. " + "Registered: ${registeredShopIds()}." ) } @@ -173,7 +194,7 @@ object Rees46 { LogCodes.INVALID_ARGUMENT, "Rees46.awaitInstance: no shop with this id is initialized or registered. " + "Waiting for it to register — if it never is (typo, or a missing " + - "Rees46.initialize/registerShops), onReady will never fire.", + "Rees46.initialize/register), onReady will never fire.", shopId ) } @@ -225,7 +246,7 @@ object Rees46 { /** * Resolves the push target as ([InstanceResolver.Resolution] to shopId), or null when the push - * should be dropped. Mirrors [getInstance] resolution (live wins over pending; a single registered + * should be dropped. Mirrors [instance] resolution (live wins over pending; a single registered * shop resolves with no `shop_id`) but drops instead of throwing on an unknown or ambiguous target. */ private fun resolvePushTarget(payloadShopId: String?): Pair? { @@ -275,9 +296,10 @@ object Rees46 { * initialized has no did and the backend never sent it a push, so this is not reached for real * deliveries. */ - private fun materializeForPush(shopId: String): SDK? { - val pendingInit = pending.remove(shopId) ?: return SdkRegistry.byShopId(shopId) - return SDK().apply { initializeForPush(pendingInit.context, pendingInit.config) } + private fun materializeForPush(shopId: String): SDK? = synchronized(lock) { + SdkRegistry.byShopId(shopId)?.let { return it } + val pendingInit = pending[shopId] ?: return null + SDK().apply { initializeForPush(pendingInit.context, pendingInit.config) }.also { pending.remove(shopId) } } /** @@ -307,20 +329,16 @@ object Rees46 { private fun Map.toBundle(): Bundle = Bundle().also { bundle -> forEach { (key, value) -> bundle.putString(key, value) } } - /** Initializes a pending registration for [shopId], tolerating a lost race with another caller. */ - private fun materialize(shopId: String): SDK { - val pendingInit = pending.remove(shopId) - return if (pendingInit != null) { - initialize(pendingInit.context, pendingInit.config) - } else { - SdkRegistry.byShopId(shopId) - ?: throw UnknownShopIdException(missingMessage(shopId)) - } + /** Initializes a pending registration for [shopId]; one that another caller initialized meanwhile is returned. */ + private fun materialize(shopId: String): SDK = synchronized(lock) { + SdkRegistry.byShopId(shopId)?.let { return it } + val pendingInit = pending[shopId] ?: throw UnknownShopIdException(missingMessage(shopId)) + initialize(pendingInit.context, pendingInit.config) } private fun missingMessage(shopId: String): String = "No shop is registered for shopId=$shopId. " + - "Call Rees46.initialize(...) or Rees46.registerShops(...) first." + "Call Rees46.initialize(...) or Rees46.register(...) first." private fun registeredShopIds(): List = (SdkRegistry.shopIds() + pending.keys).sorted() diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt index fd65fef4..3b5770f4 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt @@ -3,12 +3,12 @@ package com.personalization /** * Configuration for one SDK instance (one shop). * - * The unified initialization input shared by [Rees46.initialize] and [Rees46.registerShops]. Mirrors + * The unified initialization input shared by [Rees46.initialize] and [Rees46.register]. Mirrors * the parameters of the legacy `SDK.initialize`, minus the Android `Context` (passed separately); its * `preferencesKey` is [storageKey] here. Storage is partitioned per [shopId] automatically — see * [PreferencesPartition]. * - * @property shopId the shop key; also the partition key and the id used by [Rees46.getInstance]. + * @property shopId the shop key; also the partition key and the id used by [Rees46.instance]. * @property apiDomain API host the instance talks to (a region points here). * @property stream traffic stream label sent with every request. * @property autoSendPushToken whether the SDK registers push tokens with the backend on its own. @@ -20,11 +20,24 @@ package com.personalization * @property logLevel how much this shop's instance writes to the log; [LogLevel.WARNING] by default. * Messages that pass it also reach [Rees46.setLogHandler]. * @property coreV2 runs the instance on the new core; off by default. The process has one core: the - * first [Rees46.initialize] or [Rees46.registerShops] decides it, and an instance that asks for the - * other one runs on the process's and logs `core_flag_mismatch`. So far the new core differs in its - * network client only: requests go through OkHttp, a request without a network fails at once, - * `release()` cancels the instance's requests, and a failure without an answer is `onError(-1)` (no - * connection used to be `504`), as is a `2xx` whose body is not JSON (it used to succeed empty). + * first [Rees46.initialize] or [Rees46.register] decides it, and an instance that asks for the other + * one runs on the process's and logs `core_flag_mismatch`. The new core differs in: + * - its network client: requests go through OkHttp, a request without a network fails at once, + * `release()` cancels the instance's requests, and a failure without an answer is `onError(-1)` (no + * connection used to be `504`), as is a `2xx` whose body is not JSON (it used to succeed empty); + * - its `/init`: repeated with growing pauses until it gets an answer; a shop that answers `402`, + * `403`, `404` or another `4xx` is refused ([com.personalization.core.SdkState.REFUSED]) until a + * later launch's `/init` succeeds, and every request of a refused shop fails with `onError(402)`; + * - a request waits until the instance is ready, within its own read timeout, instead of going out + * without a `did`: a new device's first requests fail with `onError(-1)` if `/init` does not answer + * in time; + * - [needReInitialization] really gives the device a new `did`; [stream] goes with `/init` too; the + * advertising id is read and sent only with [sendAdvertisingId]. + * @property deviceId a `did` the app already keeps, from another SDK or an earlier version of it: taken + * only while the shop has no `did` of its own, and sent with the next `/init`. Ignored with + * [needReInitialization]. + * @property sendAdvertisingId sends the advertising id with `/init`; off by default, and then the new + * core does not read it. The legacy core keeps sending it as before, whatever this says. * @property storageKey a prefix for the shop's storage, when the app needs its own split of it: the * shop's `SharedPreferences` file becomes `_personalization_sdk_` instead of * `personalization_sdk_`. A file named exactly [storageKey] (the whole store of @@ -52,4 +65,6 @@ data class Rees46Config @JvmOverloads constructor( val logLevel: LogLevel = LogLevel.WARNING, val coreV2: Boolean = false, val storageKey: String? = null, + val deviceId: String? = null, + val sendAdvertisingId: Boolean = false, ) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt index 281d6b00..02e47b60 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/RegisterManager.kt @@ -8,6 +8,7 @@ import android.content.Context import android.provider.Settings import com.personalization.api.OnApiCallbackListener import com.personalization.api.managers.InAppNotificationManager +import com.personalization.core.SdkState import com.personalization.errors.JsonResponseErrorHandler import com.personalization.push.PushTokenManager import com.personalization.sdk.data.mappers.SdkInitializationMapper.mapToSdkInitResponse @@ -45,6 +46,17 @@ class RegisterManager @Inject constructor( /** Extra query parameters of `/init` (popups v3 test mode). */ internal var extraInitParams: (() -> Map)? = null + /** + * The instance's state on the legacy path: [SdkState.READY] once the device has its did and session + * (at once for a known device, after `/init` for a new one), [SdkState.INITIALIZING] before. A + * failed `/init` leaves it where it is; the legacy path is never retrying or refused. + */ + internal var onStateChange: ((SdkState) -> Unit)? = null + + /** Set by [stop]: nothing more goes out and no answer is taken. */ + @Volatile + private var stopped = false + private lateinit var context: Context private lateinit var contentResolver: ContentResolver @@ -63,10 +75,7 @@ class RegisterManager @Inject constructor( private val answerLock = Any() private val isTestDevice: Boolean - get() = IS_TEST_DEVICE_FIELD == Settings.System.getString( - contentResolver, - FIREBASE_TEST_LAB - ) + get() = isPreLaunchDevice(contentResolver) fun initialize( context: Context, @@ -80,9 +89,13 @@ class RegisterManager @Inject constructor( val did = getUserSettingsValueUseCase.getDid() when { - did.isEmpty() || needReInitialization -> initializeNewDevice() + did.isEmpty() || needReInitialization -> { + state(SdkState.INITIALIZING) + initializeNewDevice() + } else -> { initializeSdk(null) + state(SdkState.READY) // A known device asks the server too, on every launch as the iOS and React Native SDKs // do: the /init answer is where the server hands out the popup for the visit. Only the // popup is taken from it — the did and the session stay as stored, so nobody is @@ -96,7 +109,11 @@ class RegisterManager @Inject constructor( * Begins a new start before `deleteUserCredentials()` wipes the did: from now on an answer to an * `/init` sent before is dropped. Returns the start for [restartAsNewDevice]. */ - internal fun beginRestart(): Int = synchronized(answerLock) { generation.incrementAndGet() } + internal fun beginRestart(): Int = synchronized(answerLock) { + // The did is about to go: requests and the state wait for the next one from now on. + state(SdkState.INITIALIZING) + generation.incrementAndGet() + } /** * Starts over as a device the server has not seen, once the did is wiped: `/init` goes out without @@ -105,7 +122,8 @@ class RegisterManager @Inject constructor( * before [initialize], or when a later [beginRestart] took over from [start]. */ internal fun restartAsNewDevice(start: Int) { - if (!::context.isInitialized || start != generation.get()) return + if (stopped || !::context.isInitialized || start != generation.get()) return + state(SdkState.INITIALIZING) initializeSdk(seance = null, releaseQueue = false, fetchPushTokens = false) init(start = start) } @@ -131,8 +149,20 @@ class RegisterManager @Inject constructor( sendInitRequestWithRetry(knownDevice = knownDevice, start = start) } + /** The instance was released: no `/init` goes out any more and no answer is taken. */ + internal fun stop() { + synchronized(answerLock) { + stopped = true + generation.incrementAndGet() + } + } + + private fun state(state: SdkState) { + if (!stopped) onStateChange?.invoke(state) + } + private fun sendInitRequestWithRetry(attempt: Int = 0, knownDevice: Boolean = false, start: Int) { - if (start != generation.get()) return + if (stopped || start != generation.get()) return try { val params = buildInitParams() sendNetworkMethodUseCase.get( @@ -193,6 +223,8 @@ class RegisterManager @Inject constructor( if (start != generation.get()) return updateUserSettingsValueUseCase.updateDid(did) updateUserSettingsValueUseCase.updateSid(value = seance) + // Under the lock: a restart beginning right after this cannot be overtaken by it. + state(SdkState.READY) } sessionStarted(releaseQueue = true, fetchPushTokens = true) notifyInitFinished(popupsV3) @@ -282,6 +314,13 @@ class RegisterManager @Inject constructor( companion object { private const val TAG = "RegisterManager" + /** A Google Play pre-launch report device (Firebase Test Lab): `/init` is not sent there. */ + internal fun isPreLaunchDevice(contentResolver: ContentResolver): Boolean = try { + IS_TEST_DEVICE_FIELD == Settings.System.getString(contentResolver, FIREBASE_TEST_LAB) + } catch (e: Exception) { + false + } + private const val SOURCE = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" private const val FIREBASE_TEST_LAB = "firebase.test.lab" private const val SESSION_CODE_EXPIRE = 2 * 3600 * 1000L diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index cd00b751..70a8e816 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -30,11 +30,19 @@ import com.personalization.core.CompletionListener import com.personalization.core.SdkError import com.personalization.core.SdkException import com.personalization.core.SdkResult +import com.personalization.core.SdkState import com.personalization.core.internal.AndroidConnectivityMonitor +import com.personalization.core.internal.ConnectivityMonitor import com.personalization.core.internal.Core import com.personalization.core.internal.CoreDispatchers +import com.personalization.core.internal.CoreHttpClient +import com.personalization.core.internal.CoreInit import com.personalization.core.internal.CoreSelection import com.personalization.core.internal.CoreV2 +import com.personalization.core.internal.InitHooks +import com.personalization.core.internal.InitSetup +import com.personalization.core.internal.InstanceState +import com.personalization.core.internal.ProcessAppLifecycle import com.personalization.core.internal.ShopStorage import com.personalization.core.internal.StorageLayout import com.personalization.core.internal.StorageMigrations @@ -47,7 +55,11 @@ import com.personalization.features.notification.presentation.helpers.Notificati import com.personalization.handlers.notifications.NotificationHandler import com.personalization.popups.PopupsV3 import com.personalization.push.PushTokenManager +import com.personalization.sdk.data.mappers.SdkInitializationMapper.mapToSdkInitResponse +import com.personalization.sdk.data.utils.AdvertisingUtils +import com.personalization.sdk.domain.models.NetworkMethod import com.personalization.sdk.domain.repositories.NPSRepository +import com.personalization.sdk.domain.repositories.UserSettingsRepository import com.personalization.sdk.domain.usecases.network.AddTaskToQueueUseCase import com.personalization.sdk.domain.usecases.network.InitNetworkUseCase import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase @@ -95,9 +107,32 @@ open class SDK { @Volatile private var isSdkInitialized: Boolean = false + /** Set by [release]: every call through the instance fails with `notInitialized` until it is initialized again. */ + @Volatile + private var released: Boolean = false + + /** Serializes initializing this object: two calls never initialize it twice. */ + private val initLock = Any() + + /** The config the instance was initialized with. */ + @Volatile + internal var config: Rees46Config? = null + private set + + /** Brought up only to track a push ([initializeForPush]): [Rees46.initialize] replaces it. */ + @Volatile + internal var pushOnly: Boolean = false + private set + + /** The instance's [SdkState] and the app's listeners of it, for both cores. */ + internal val instanceState: InstanceState by lazy { InstanceState(shopId = { logShopId }, dispatchers = dispatchers) } + @Inject internal lateinit var initializeAdvertisingIdUseCase: InitializeAdvertisingIdUseCase + @Inject + internal lateinit var userSettingsRepository: UserSettingsRepository + @Inject lateinit var notificationHandler: NotificationHandler @@ -183,6 +218,34 @@ open class SDK { applyPopupPresentation() } + /** + * Where the instance's initialization stands; [SdkState.NOT_INITIALIZED] before it is initialized + * and after [release]. On the legacy core it only goes from [SdkState.INITIALIZING] to + * [SdkState.READY]. + */ + val state: SdkState + get() = instanceState.current + + /** + * Calls [listener] on the main thread with every later change of [state], once per change, until + * the returned handle is cancelled. May be set before the instance is initialized. An exception + * the listener throws is logged as `callback_failed`. [release] tells it [SdkState.NOT_INITIALIZED] + * and drops it. + */ + fun onStateChange(listener: (SdkState) -> Unit): Cancellable = instanceState.addListener(listener) + + /** The shop of this instance; empty before it is initialized. */ + val shopId: String + get() = config?.shopId ?: logShopId.orEmpty() + + /** The device's `did` at this shop; null before the server hands one out, or before initialization. */ + val deviceId: String? + get() = if (::userSettingsRepository.isInitialized) userSettingsRepository.getDid().ifEmpty { null } else null + + /** The `sid` of the current session; null while there is none, or before initialization. */ + val sessionId: String? + get() = if (::userSettingsRepository.isInitialized) userSettingsRepository.getSid().ifEmpty { null } else null + /** The level of this instance's log; [Rees46Config.logLevel] sets it for instances made through [Rees46]. */ internal var logLevel: LogLevel = LogLevel.WARNING @@ -276,10 +339,13 @@ open class SDK { * @param preferencesKey the prefix of the shop's storage, as [Rees46Config.storageKey]. Earlier * releases kept everything in one file of this name; it is read once into the shop's partition and * left as it is. + * + * On an object already initialized for [shopId] it does nothing (and logs `invalid_config` when the + * parameters differ); after [release] it initializes the object again. */ @Deprecated( message = "Use Rees46.initialize(context, Rees46Config(...)) — the unified, multi-instance " + - "entry point. Reach instances with Rees46.getInstance(shopId).", + "entry point. Reach instances with Rees46.instance(shopId).", replaceWith = ReplaceWith( "Rees46.initialize(context, Rees46Config(shopId = shopId))", "com.personalization.Rees46", @@ -296,73 +362,110 @@ open class SDK { autoSendPushToken: Boolean = true, needReInitialization: Boolean = false, addTrailingSlash: Boolean = true - ) = initializeInternal( - context = context, - shopId = shopId, - apiDomain = apiDomain, - tag = tag, - storageKey = preferencesKey.takeUnless { it == PreferencesPartition.LEGACY_KEY }, - stream = stream, - autoSendPushToken = autoSendPushToken, - needReInitialization = needReInitialization, - addTrailingSlash = addTrailingSlash, - sendProfileOnInit = true + ) = initializeWith( + context, + Rees46Config( + shopId = shopId, + apiDomain = apiDomain, + stream = stream, + autoSendPushToken = autoSendPushToken, + needReInitialization = needReInitialization, + addTrailingSlash = addTrailingSlash, + tag = tag, + enableAutoPopupPresentation = enableAutoPopupPresentation, + logLevel = logLevel, + coreV2 = coreV2Requested, + storageKey = preferencesKey.takeUnless { it == PreferencesPartition.LEGACY_KEY }, + ) + ) + + /** + * Initializes this object for [config]: the entry of [Rees46.initialize] and of the legacy + * [initialize]. An object live for the same shop is left as it is: it keeps the config it was made + * with, and a different [config] is logged as `invalid_config` (`already_initialized`). + */ + internal fun initializeWith(context: Context, config: Rees46Config) = start( + context, + config, + autoSendPushToken = config.autoSendPushToken, + needReInitialization = config.needReInitialization, + pushOnly = false, ) /** - * Light bring-up for handling a push in a (possibly cold) background process: keeps the persisted - * did (no `/init`), does not re-register the push token (it is already on the server — that is why - * the push arrived), and skips the GAID + profile/set call. Just enough to track the delivery and - * reach the message listener. Used by [Rees46.handlePush] to materialize a lazily-registered shop - * without the full startup work. The public [initialize] contract is unchanged. + * Light bring-up for handling a push in a (possibly cold) background process: does not re-register + * the push token (it is already on the server — that is why the push arrived) and skips the GAID + + * profile/set call. Just enough to track the delivery and reach the message listener. Used by + * [Rees46.handlePush] to materialize a lazily-registered shop without the full startup work. */ - internal fun initializeForPush(context: Context, config: Rees46Config) { + internal fun initializeForPush(context: Context, config: Rees46Config) = start( + context, + config, + autoSendPushToken = false, + needReInitialization = false, + pushOnly = true, + ) + + private fun start( + context: Context, + config: Rees46Config, + autoSendPushToken: Boolean, + needReInitialization: Boolean, + pushOnly: Boolean, + ): Unit = synchronized(initLock) { + val current = this.config + if (current != null && isLive && current.shopId == config.shopId) { + keepConfig(config) + return + } enableAutoPopupPresentation = config.enableAutoPopupPresentation logLevel = config.logLevel coreV2Requested = config.coreV2 - initializeInternal( - context = context, - shopId = config.shopId, - apiDomain = config.apiDomain, - tag = config.tag, - storageKey = config.storageKey, - stream = config.stream, - autoSendPushToken = false, - needReInitialization = false, - addTrailingSlash = config.addTrailingSlash, - sendProfileOnInit = false + initializeInternal(context, config, autoSendPushToken, needReInitialization, sendProfileOnInit = !pushOnly) + this.pushOnly = pushOnly + this.config = config + } + + /** Initialized and not released. */ + private val isLive: Boolean get() = isSdkInitialized && !released + + /** A later initialization of a live shop: the instance stays as it was made, a different config is logged. */ + internal fun keepConfig(config: Rees46Config) { + if (this.config == config) return + SdkLogs.log( + LogLevel.WARNING, + LogCodes.INVALID_CONFIG, + "The shop is initialized already; its instance keeps the config it was made with", + config.shopId, + mapOf("reason" to LogReasons.ALREADY_INITIALIZED) ) } private fun initializeInternal( context: Context, - shopId: String, - apiDomain: String, - tag: String, - storageKey: String?, - stream: String, + config: Rees46Config, autoSendPushToken: Boolean, needReInitialization: Boolean, - addTrailingSlash: Boolean, // When false, skip the background GAID + profile/set call (the push-context bring-up). sendProfileOnInit: Boolean ) { + val shopId = config.shopId + // Initialized before (another shop, or released): the old start sends and takes nothing more. + stopCurrent() val sdkComponent = DaggerSdkComponent.factory().create( appModule = AppModule(applicationContext = context) ) - val baseUrl = apiDomain?.let { - formatApiDomain( - apiDomain = it, - addTrailingSlash = addTrailingSlash - ) - } + val baseUrl = formatApiDomain( + apiDomain = config.apiDomain, + addTrailingSlash = config.addTrailingSlash + ) sdkComponent.inject(sdk = this) this.context = context - TAG = tag - SdkLogs.register(shopId = shopId, level = logLevel, tag = tag, owner = this) + TAG = config.tag + SdkLogs.register(shopId = shopId, level = logLevel, tag = config.tag, owner = this) logShopId = shopId pushTokenManager.logShopId = shopId storiesManager.logShopId = shopId @@ -377,9 +480,9 @@ open class SDK { // data of older releases is read into it once (the file of a custom preferencesKey, or the file // all shops shared before partitions), and the device-level store gets the A/B group, push // tokens and advertising id. Nothing older is deleted. - val layout = StorageLayout(shopId, storageKey) + val layout = StorageLayout(shopId, config.storageKey) StorageMigrations.run(context, layout) - storage = ShopStorage(context, layout) + val storage = ShopStorage(context, layout).also { this.storage = it } initPreferencesUseCase.invoke( context = context, @@ -391,32 +494,22 @@ open class SDK { initUserSettingsUseCase.invoke( shopId = shopId, - stream = stream + stream = config.stream ) initNetworkUseCase(url = baseUrl) - core = if (CoreSelection.resolve(coreV2Requested, shopId)) { - networkRepository.useCoreV2(shopId, AndroidConnectivityMonitor(context)) + released = false + val coreV2 = CoreSelection.resolve(coreV2Requested, shopId) + if (coreV2) { + val connectivity = AndroidConnectivityMonitor(context) + val core = networkRepository.useCoreV2(shopId, connectivity, dispatchers) + this.core = core + startCoreV2(core, connectivity, context, config, storage, autoSendPushToken, needReInitialization) } else { - networkRepository.core + core = networkRepository.core + startLegacy(context, config, autoSendPushToken, needReInitialization) } - // Popups v3 start from the /init answer; in their test mode /init counts test popups too. - registerManager.onInitFinished = { flag -> - popupsV3InitFlag = flag - popupsV3?.onInit(flag) - } - registerManager.extraInitParams = { - if (popupsV3?.testMode == true) mapOf(POPUPS_V3_TEST_MODE to "true") else emptyMap() - } - - registerManager.initialize( - context = context, - contentResolver = context.contentResolver, - autoSendPushToken = autoSendPushToken, - needReInitialization = needReInitialization - ) - // Register this instance so pushes delivered to the (process-global) messaging services are // routed back to every initialized SDK instance, not just one, and so the documented // `SDK().initialize()` usage and hosts reading SDK.instance get a working instance. register @@ -424,7 +517,8 @@ open class SDK { SdkRegistry.register(shopId = shopId, sdk = this) isSdkInitialized = true - if (sendProfileOnInit) { + // The new core sends the advertising id with /init, and only with sendAdvertisingId. + if (sendProfileOnInit && !coreV2) { CoroutineScope(Dispatchers.IO).launch { val advertisingId = initializeAdvertisingIdUseCase.invoke() @@ -459,6 +553,92 @@ open class SDK { } + /** Stops what an earlier initialization of this object started. */ + private fun stopCurrent() { + if (::registerManager.isInitialized) registerManager.stop() + if (::networkRepository.isInitialized) networkRepository.release() + } + + /** `/init` and the session of the legacy path, as before. */ + private fun startLegacy(context: Context, config: Rees46Config, autoSendPushToken: Boolean, needReInitialization: Boolean) { + // A did the app brings is taken while the shop has none, as on the new core. + val configured = config.deviceId?.trim().orEmpty() + if (!needReInitialization && configured.isNotEmpty() && userSettingsRepository.getDid().isEmpty()) { + userSettingsRepository.updateDid(configured) + } + + // Popups v3 start from the /init answer; in their test mode /init counts test popups too. + registerManager.onInitFinished = { flag -> + popupsV3InitFlag = flag + popupsV3?.onInit(flag) + } + registerManager.extraInitParams = ::popupsV3InitParams + registerManager.onStateChange = { state -> instanceState.set(state) } + + registerManager.initialize( + context = context, + contentResolver = context.contentResolver, + autoSendPushToken = autoSendPushToken, + needReInitialization = needReInitialization + ) + } + + /** The new core's `/init` and state ([CoreInit]); requests wait for it in [CoreV2]. */ + private fun startCoreV2( + core: CoreV2, + connectivity: ConnectivityMonitor, + context: Context, + config: Rees46Config, + storage: ShopStorage, + autoSendPushToken: Boolean, + needReInitialization: Boolean, + ) { + val contentResolver = context.contentResolver + val init = CoreInit( + setup = InitSetup( + shopId = config.shopId, + needReInitialization = needReInitialization, + deviceId = config.deviceId, + advertisingId = if (config.sendAdvertisingId) ::readAdvertisingId else null, + extraParams = ::popupsV3InitParams, + skipInit = { RegisterManager.isPreLaunchDevice(contentResolver) }, + ), + send = { params -> core.client.send(NetworkMethod.GET(CoreHttpClient.INIT_PATH), params) }, + settings = userSettingsRepository, + storage = storage, + state = instanceState, + connectivity = connectivity, + lifecycle = ProcessAppLifecycle(), + hooks = object : InitHooks { + override fun onAnswer(answer: JSONObject, fresh: Boolean) { + val response = answer.mapToSdkInitResponse() + // A popup is shown from a fresh answer only, never again from the saved one. + if (fresh) response?.popupDto?.let { inAppNotificationManager.shopPopUp(popupDto = it) } + val flag = response?.popupsV3 == true + popupsV3InitFlag = flag + popupsV3?.onInit(flag) + } + + override fun onUsable() { + // What waited for a did on the legacy path (the stories v3 requests) goes now. + networkRepository.executeQueueTasks() + pushTokenManager.initialize(context = context, autoSendPushToken = autoSendPushToken) + } + }, + dispatchers = dispatchers, + ) + core.init = init + init.start() + } + + /** The advertising id for `/init`, or null when it cannot be read. */ + private suspend fun readAdvertisingId(): String? = + initializeAdvertisingIdUseCase.invoke().takeUnless { it.isBlank() || it == AdvertisingUtils.DEFAULT_ADVERTISING_ID } + + /** The test mode of popups v3 makes `/init` count test popups too. */ + private fun popupsV3InitParams(): Map = + if (popupsV3?.testMode == true) mapOf(POPUPS_V3_TEST_MODE to "true") else emptyMap() + private fun initNetworkUseCase(url: String?) { if (url != null) { initNetworkUseCase.invoke( @@ -544,7 +724,7 @@ open class SDK { /** * Return the session ID */ - fun getSid(): String = getUserSettingsValueUseCase.getSid() + fun getSid(): String = sessionId.orEmpty() /** * Return the Advertising ID @@ -694,18 +874,24 @@ open class SDK { setPushToken(token = token, provider = PushProvider.FCM, listener = listener) /** - * Releases this SDK instance so it no longer receives push tokens delivered to the - * process-global messaging services. Call this for short-lived instances to avoid leaking - * them through the active-instances registry. Apps that keep a single long-lived instance - * for the whole process usually do not need it. + * Stops this instance and takes it out of [Rees46]'s registry and the push fan-out. Call it for + * short-lived instances; an app that keeps one instance for the whole process does not need it. * - * On the new core ([Rees46Config.coreV2]) the instance's requests in flight are cancelled and - * end in `onError(-1)`, and so does any request made through it afterwards. + * Afterwards every call through the instance fails with `notInitialized`, on both cores: a + * request's old callback gets `onError(-1)` (a request still waiting for a `did` too), and + * [deleteUserCredentials] throws [SdkException] without deleting anything or asking `/init`. + * [state] is [SdkState.NOT_INITIALIZED]: the state listeners hear it and are dropped. On the new + * core ([Rees46Config.coreV2]) the requests in flight are cancelled (`onError(-1)`) and `/init` + * stops; the legacy one cannot cancel a request in flight. The shop's data stays on the device, and + * a new [Rees46.initialize] for the shop makes a fresh instance. */ fun release() { + released = true SdkRegistry.unregister(this) - // The new core cancels the instance's requests in flight; the legacy one cannot. - core?.release() + // Nothing more goes out: no /init, and requests fail with notInitialized. + stopCurrent() + instanceState.set(SdkState.NOT_INITIALIZED) + instanceState.dropListeners() // Only this instance's level goes: another live instance of the shop keeps logging at its own. logShopId?.let { SdkLogs.unregister(it, owner = this) } } @@ -743,15 +929,18 @@ open class SDK { * * The saved `/init` answer and the queue are kept in `noBackupFilesDir` and never leave the device. * - * @throws SdkException with [SdkError.NotInitialized] when the instance was never initialized. + * @throws SdkException with [SdkError.NotInitialized] when the instance was never initialized or + * was released; nothing is deleted then. */ suspend fun deleteUserCredentials() { val core = core val storage = storage - if (core == null || storage == null) throw SdkException(SdkError.NotInitialized) + if (core == null || storage == null || released) throw SdkException(SdkError.NotInitialized) // From here on an answer to an /init sent before is dropped: one landing during the wipe would - // write the forgotten did back. - val restart = registerManager.beginRestart() + // write the forgotten did back. The new core also holds requests until the new did. + val init = (core as? CoreV2)?.init + init?.holdForRestart() + val restart = if (init == null) registerManager.beginRestart() else 0 val complete = withContext(dispatchers.io) { val stored = core.deleteUserCredentials(storage) val searches = RecentSearches.forget(context, storage.layout.shopId) @@ -774,7 +963,7 @@ open class SDK { ) } try { - registerManager.restartAsNewDevice(restart) + if (init != null) init.restart() else registerManager.restartAsNewDevice(restart) } catch (e: Exception) { // The customer is forgotten either way; the next launch asks /init. log(LogLevel.ERROR, LogCodes.SDK_LOG, "/init could not be restarted after the deletion", throwable = e) @@ -1079,9 +1268,7 @@ open class SDK { * * @return String */ - fun getDid(): String? { - return getUserSettingsValueUseCase.getDid() - } + fun getDid(): String? = deviceId.orEmpty() /** * Signs up for price reduction diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SdkInstanceException.kt b/personalization-sdk/src/main/kotlin/com/personalization/SdkInstanceException.kt index e0e53718..15a8965a 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SdkInstanceException.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SdkInstanceException.kt @@ -1,16 +1,22 @@ package com.personalization /** - * Thrown by [Rees46.getInstance] when the requested shop is unknown to the SDK — neither a live + * An error in the app's code when it looks up an instance through [Rees46]: thrown at once, never + * passed through `SdkError`. Still an [IllegalStateException], as both kinds always were. + */ +sealed class Rees46Error(message: String) : IllegalStateException(message) + +/** + * Thrown by [Rees46.instance] when the requested shop is unknown to the SDK — neither a live * instance nor a pending registration exists for it, so there is no config to build one from. A - * shop that is registered-but-not-yet-initialized does not raise this: `getInstance` materializes it. + * shop that is registered-but-not-yet-initialized does not raise this: `instance` materializes it. * The name is deliberate: the failure is a missing registration, not an instance that was registered * but failed to start. */ -class UnknownShopIdException(message: String) : IllegalStateException(message) +class UnknownShopIdException(message: String) : Rees46Error(message) /** - * Thrown by [Rees46.getInstance] called without a `shopId` while more than one shop is registered. - * The default instance is ambiguous — call `getInstance(shopId)` with an explicit id. + * Thrown by [Rees46.instance] called without a `shopId` while more than one shop is registered. + * The default instance is ambiguous — call `instance(shopId)` with an explicit id. */ -class AmbiguousShopException(message: String) : IllegalStateException(message) +class AmbiguousShopException(message: String) : Rees46Error(message) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SdkRegistry.kt b/personalization-sdk/src/main/kotlin/com/personalization/SdkRegistry.kt index dfa0c06c..174bdec7 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SdkRegistry.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SdkRegistry.kt @@ -11,7 +11,7 @@ import java.util.concurrent.CopyOnWriteArrayList * public surface: it simply owns the static routing state that used to live in [SDK]'s companion * (`currentInstance` + `activeInstances`), so the single-instance behaviour is unchanged while the * plumbing for resolving an instance by `shop_id` is in place and tested. The public - * `Rees46.initialize/getInstance/registerShops` API will be layered on top later. + * `Rees46.initialize/instance/register` API will be layered on top later. * * Two pieces of state, matching what [SDK] needs: * - an ordered list of initialized instances, for fanning a push token out to each ([all]); diff --git a/personalization-sdk/src/main/kotlin/com/personalization/api/managers/TrackingApi.kt b/personalization-sdk/src/main/kotlin/com/personalization/api/managers/TrackingApi.kt index e3ab6dbb..baf9ae91 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/api/managers/TrackingApi.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/api/managers/TrackingApi.kt @@ -8,7 +8,7 @@ import com.personalization.api.models.tracking.TrackingSource /** * Standard tracking events — the `tracking` namespace of the SDK. * - * Reached through an SDK instance: `Rees46.getInstance().tracking.productView("sku-1")`. + * Reached through an SDK instance: `Rees46.instance().tracking.productView("sku-1")`. */ interface TrackingApi { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/SdkState.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/SdkState.kt new file mode 100644 index 00000000..016b2683 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/SdkState.kt @@ -0,0 +1,28 @@ +package com.personalization.core + +/** + * Where an instance's initialization (`/init`) stands. Read it from `SDK.state`, follow it with + * `SDK.onStateChange`. + * + * On the legacy core (`Rees46Config.coreV2 = false`) an instance only goes from [INITIALIZING] to + * [READY]; it is never [RETRYING] or [REFUSED]. + */ +enum class SdkState { + /** Not initialized yet, or released with `release()`: every call fails with `notInitialized`. */ + NOT_INITIALIZED, + + /** `/init` is on its way; requests with a result wait for it. */ + INITIALIZING, + + /** The instance has its `did` and works. */ + READY, + + /** + * `/init` did not go through (no answer, or an answer to repeat) and is being repeated. With a + * `did` from an earlier launch the instance works as [READY]; without one, as [INITIALIZING]. + */ + RETRYING, + + /** The shop turned the SDK away (`402`, `403`, `404` or another `4xx` on `/init`): calls fail with `shopBlocked`. */ + REFUSED, +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Backoff.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Backoff.kt new file mode 100644 index 00000000..f453d240 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Backoff.kt @@ -0,0 +1,31 @@ +package com.personalization.core.internal + +import kotlin.random.Random + +/** + * The pauses between repeats (decision 0002): 1 s, 2 s, 4 s… up to 5 minutes, each spread by ±20 % so + * that devices that failed together do not come back together. A `Retry-After` of the answer is taken + * as it is. + */ +internal class Backoff( + private val baseMs: Long = 1_000, + private val maxMs: Long = 300_000, + private val spread: Double = 0.2, + /** A value in `[0, 1)`; tests pass a constant. */ + private val random: () -> Double = { Random.nextDouble() }, +) { + + /** The pause before repeat number [attempt] (from 0), or [retryAfterMs] when the server named one. */ + fun delayMs(attempt: Int, retryAfterMs: Long? = null): Long { + if (retryAfterMs != null && retryAfterMs >= 0) return retryAfterMs + val doublings = attempt.coerceIn(0, MAX_DOUBLINGS) + val pause = (baseMs shl doublings).coerceAtMost(maxMs) + val factor = 1 + spread * (2 * random() - 1) + return (pause * factor).toLong().coerceAtLeast(0) + } + + private companion object { + /** 2^20 seconds is far past the cap; more doublings would only overflow. */ + const val MAX_DOUBLINGS = 20 + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt index c658f521..f3d99ac8 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt @@ -6,6 +6,7 @@ import com.personalization.core.SdkError import com.personalization.logs.LogCodes import com.personalization.logs.LogReasons import com.personalization.logs.SdkLogs +import com.personalization.sdk.data.models.params.UserBasicParams import com.personalization.sdk.domain.models.NetworkMethod import com.personalization.sdk.domain.repositories.UserSettingsRepository import kotlinx.coroutines.suspendCancellableCoroutine @@ -67,7 +68,10 @@ internal object CoreHttp { * - a request cancelled with the instance is `cancelled`. * * Without a network a request fails at once with `network`. The client never retries; the log gets the - * method, the path and the code, never the query, a body or the answer. + * method, the path and the code, never the query, a body or the answer. An answer's `Retry-After` comes + * back with the failure, for the caller that repeats ([CoreInit]). + * + * `/init` carries `did`, `sid` and `seance` only when they have a value: an empty `did` is not a device. */ internal class CoreHttpClient( private val baseUrl: () -> String, @@ -75,7 +79,7 @@ internal class CoreHttpClient( private val connectivity: ConnectivityMonitor, private val shopId: String?, shared: OkHttpClient = CoreHttp.shared, - timeouts: CoreTimeouts = CoreTimeouts(), + internal val timeouts: CoreTimeouts = CoreTimeouts(), private val clock: Clock = Clock.SYSTEM, ) { @@ -120,6 +124,9 @@ internal class CoreHttpClient( private fun build(method: NetworkMethod, params: JSONObject, phase: Phase): Built { val fields = CommonFields.addTo(params, settings) + if (method.method == INIT_PATH) { + INIT_OPTIONAL_FIELDS.forEach { key -> if (fields.optString(key).isEmpty()) fields.remove(key) } + } val url = (baseUrl() + method.method).toHttpUrl() val builder = Request.Builder() .header(USER_AGENT, SDK.userAgent()) @@ -175,7 +182,8 @@ internal class CoreHttpClient( } catch (e: IOException) { null } - return Exchange(HttpResult.Failure(SdkError.Http(status), errorBody), status, errorBody) + val failure = HttpResult.Failure(SdkError.Http(status), errorBody, retryAfterMs(response)) + return Exchange(failure, status, errorBody) } // OkHttp gives every answer it hands over a body; without one the answer is empty. val text = response.body?.string().orEmpty() @@ -189,6 +197,14 @@ internal class CoreHttpClient( return Exchange(result, status, text) } + /** `Retry-After` in seconds or as an HTTP date, as milliseconds from now; null without one or when it does not parse. */ + private fun retryAfterMs(response: Response): Long? { + val value = response.header(RETRY_AFTER)?.trim()?.takeIf { it.isNotEmpty() } ?: return null + value.toLongOrNull()?.let { seconds -> return if (seconds < 0) null else seconds * 1_000 } + val date = response.headers.getDate(RETRY_AFTER) ?: return null + return (date.time - clock.nowMillis()).coerceAtLeast(0) + } + /** No answer: cancelled with the instance, `timeout` once the request reached the server, `network` before. */ private fun noAnswer(call: Call, phase: Phase): SdkError = when { call.isCanceled() -> SdkError.Cancelled @@ -262,6 +278,10 @@ internal class CoreHttpClient( internal companion object { const val INIT_PATH = "init" private const val USER_AGENT = "User-Agent" + private const val RETRY_AFTER = "Retry-After" + + /** Fields `/init` leaves out while they are empty. */ + private val INIT_OPTIONAL_FIELDS = listOf(UserBasicParams.DID, UserBasicParams.SID, UserBasicParams.SEANCE) private const val NO_STATUS = -1 private const val ERROR_NETWORK = "network" private const val ERROR_TIMEOUT = "timeout" diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt new file mode 100644 index 00000000..d2c04bdb --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt @@ -0,0 +1,475 @@ +package com.personalization.core.internal + +import com.personalization.Cancellable +import com.personalization.LogLevel +import com.personalization.core.SdkError +import com.personalization.core.SdkState +import com.personalization.logs.LogCodes +import com.personalization.logs.LogReasons +import com.personalization.logs.SdkLogs +import com.personalization.sdk.data.models.params.UserBasicParams +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CoroutineExceptionHandler +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancel +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.launch +import kotlinx.coroutines.withTimeoutOrNull +import org.json.JSONObject +import java.util.TimeZone + +/** What `/init` takes from the instance's config. */ +internal class InitSetup( + val shopId: String, + /** Forget the `did` before `/init`, so the server hands out a new one. */ + val needReInitialization: Boolean = false, + /** A `did` the app brings from another SDK; taken only while the shop has none. */ + val deviceId: String? = null, + /** Reads the advertising id; null while `sendAdvertisingId` is off, and then it is never read. */ + val advertisingId: (suspend () -> String?)? = null, + /** Extra query fields of `/init` (the test mode of popups v3). */ + val extraParams: () -> Map = { emptyMap() }, + /** True on a Google Play pre-launch report device, where `/init` is not sent. */ + val skipInit: () -> Boolean = { false }, +) + +/** What the instance does with `/init`. Called on a background thread; an exception is logged. */ +internal interface InitHooks { + /** + * An answer to act on: [fresh] from the server now (its popup is shown), or the saved one while + * `/init` is repeated (its flags only: a popup is never shown twice). + */ + fun onAnswer(answer: JSONObject, fresh: Boolean) + + /** The instance can send from now on: once per start, and again after a restart. */ + fun onUsable() +} + +/** Holds a request until the instance may send it. */ +internal fun interface ReadyGate { + /** Null once the request may go; otherwise why it may not: `shopBlocked`, `notInitialized` or `timeout`. */ + suspend fun awaitReady(timeoutMs: Long): SdkError? +} + +/** + * The new core's `/init` (`core/init.md`, decision 0002) and the [SdkState] it gives the instance. + * + * - **Start.** A shop refused at an earlier launch starts [SdkState.REFUSED]. A shop with a `did` — + * stored, brought in [InitSetup.deviceId], or from the saved answer — is [SdkState.READY] at once and + * `/init` goes in the background (decision 1). Otherwise it is [SdkState.INITIALIZING]. + * - **Answers.** A `2xx` with a `did` is [SdkState.READY]: the `did` is kept if the shop had none, the + * `seance` becomes the `sid`, the answer is saved and the refused state cleared. `402`, `403`, `404` + * and other `4xx` are [SdkState.REFUSED], kept on disk. No answer, a `2xx` without a `did`, `5xx`, + * `408`, `410`, `417`, `426` and `429` are [SdkState.RETRYING]: `/init` is repeated after [Backoff]'s + * pause, or the `Retry-After` of a `429` or `503`. A shop refused at an earlier launch stays refused + * until the server answers. + * - **Without an answer** an instance with a `did` works as [SdkState.READY] (`init_saved_answer`); one + * without waits for the network instead of failing again. + * - **Later `/init`.** On coming back to the foreground more than two hours after the last activity, and + * on [checkShop]. A refused shop is not asked again before the next launch. + * + * Requests with a result wait in [awaitReady]. A stale answer (an `/init` of a start [restart] or + * [release] ended) is dropped. + */ +internal class CoreInit( + private val setup: InitSetup, + private val send: suspend (params: JSONObject) -> HttpResult, + private val settings: UserSettingsRepository, + private val storage: ShopStorage, + private val state: InstanceState, + private val connectivity: ConnectivityMonitor, + private val lifecycle: AppLifecycle, + private val hooks: InitHooks, + dispatchers: CoreDispatchers = CoreDispatchers.DEFAULT, + private val clock: Clock = Clock.SYSTEM, + private val backoff: Backoff = Backoff(), + private val timeZone: () -> TimeZone = { TimeZone.getDefault() }, +) : ReadyGate { + + /** Whether a request may go: computed from the state on every change. */ + private enum class Gate { WAIT, OPEN, BLOCKED, CLOSED } + + private val shopId = setup.shopId + + private val scope = CoroutineScope(SupervisorJob() + dispatchers.io + CoroutineExceptionHandler { _, throwable -> + log(LogLevel.ERROR, LogCodes.SDK_LOG, "/init failed unexpectedly", throwable = throwable) + }) + + private val gate = MutableStateFlow(Gate.WAIT) + + /** Guards the fields below and every write an answer makes, so a restart never lets a stale one in. */ + private val lock = Any() + private var job: Job? = null + private var generation = 0 + private var released = false + private var usable = false + private var fallbackLogged = false + private var lastCheckAt: Long? = null + private var foreground: Cancellable? = null + + /** Read once and reused: the advertising id goes with every `/init` of the launch. */ + @Volatile + private var advertisingIdValue: String? = null + + /** Prepares the shop's storage, sets the first state and sends `/init`. */ + fun start() { + val ready = synchronized(lock) { + if (released) return + prepareStorage() + val first = when { + storage.isRefused -> SdkState.REFUSED + settings.getDid().isNotEmpty() -> SdkState.READY + else -> SdkState.INITIALIZING + } + publish(first) + first == SdkState.READY + } + if (ready) markUsable(generation()) + foreground = lifecycle.addListener(::onForegroundChanged) + synchronized(lock) { launchLocked() } + } + + override suspend fun awaitReady(timeoutMs: Long): SdkError? { + val decided = withTimeoutOrNull(timeoutMs) { gate.first { it != Gate.WAIT } } ?: return SdkError.Timeout + return when (decided) { + Gate.OPEN -> null + Gate.BLOCKED -> SdkError.ShopBlocked + Gate.CLOSED -> SdkError.NotInitialized + Gate.WAIT -> SdkError.Timeout + } + } + + /** + * Asks the server about the shop once, as the queue does after a `402`, `403` or `404` to an event: + * not more often than every 10 minutes, not while `/init` is on its way and not for a refused shop. + * True when `/init` went out. + */ + internal fun checkShop(): Boolean = synchronized(lock) { + if (released || state.current == SdkState.REFUSED || job?.isActive == true) return false + val now = clock.nowMillis() + val last = lastCheckAt + if (last != null && now - last < CHECK_SHOP_INTERVAL_MS) return false + lastCheckAt = now + launchLocked() + } + + /** + * Stops the current start before `deleteUserCredentials()` wipes the customer: the `/init` on its + * way is cancelled and its answer will not be taken, and requests wait for the next `did`. + */ + fun holdForRestart(): Unit = synchronized(lock) { + if (released) return + generation++ + job?.cancel() + job = null + usable = false + fallbackLogged = false + publish(SdkState.INITIALIZING) + } + + /** Starts over as a new device after [holdForRestart]: `/init` goes without a `did`. */ + fun restart(): Unit = synchronized(lock) { + if (released) return + launchLocked() + } + + /** Stops for good: `/init` is cancelled, waiting requests and later ones get `notInitialized`. */ + fun release() { + synchronized(lock) { + if (released) return + released = true + generation++ + job?.cancel() + job = null + gate.value = Gate.CLOSED + } + foreground?.cancel() + scope.cancel() + } + + private fun generation(): Int = synchronized(lock) { generation } + + /** Must hold [lock]. */ + private fun prepareStorage() { + if (setup.needReInitialization) { + storage.forgetDevice() + log( + LogLevel.INFO, + LogCodes.DEVICE_ID_RESET, + "needReInitialization: the did is forgotten and /init asks for a new one", + mapOf("reason" to LogReasons.NEED_REINITIALIZATION), + ) + } else { + val configured = setup.deviceId?.trim().orEmpty() + if (configured.isNotEmpty() && settings.getDid().isEmpty()) settings.updateDid(configured) + } + // The saved answer's did fills a shop that lost its own; it never replaces one. + if (settings.getDid().isEmpty()) { + storage.initAnswer()?.optString(UserBasicParams.DID)?.takeIf { it.isNotEmpty() }?.let(settings::updateDid) + } + forgetStaleSession() + } + + /** Must hold [lock]. Sends `/init` unless it is on its way already; true when it went. */ + private fun launchLocked(): Boolean { + if (released || job?.isActive == true) return false + val start = generation + job = scope.launch { run(start) } + return true + } + + private sealed interface Step { + object Done : Step + class Answered(val answer: JSONObject) : Step + class Retry(val delayMs: Long, val waitForNetwork: Boolean, val fallback: Boolean) : Step + } + + private suspend fun run(start: Int) { + if (setup.skipInit()) { + log(LogLevel.INFO, LogCodes.INIT_SKIPPED, "/init is not sent on a Google Play pre-launch report device", + mapOf("reason" to LogReasons.PRE_LAUNCH_REPORT)) + return + } + var attempt = 0 + while (true) { + synchronized(lock) { + if (start != generation) return + forgetStaleSession() + } + val result = send(params()) + val step = synchronized(lock) { + // An answer to a start that restart() or release() ended belongs to nobody. + if (start != generation || released) return + take(result, attempt) + } + when (step) { + Step.Done -> return + is Step.Answered -> { + hook("onAnswer") { hooks.onAnswer(step.answer, fresh = true) } + markUsable(start) + return + } + is Step.Retry -> { + if (step.fallback) { + log(LogLevel.WARNING, LogCodes.INIT_SAVED_ANSWER, "The SDK works on the did it has while /init is repeated") + val saved = storage.initAnswer() ?: JSONObject() + hook("onAnswer") { hooks.onAnswer(saved, fresh = false) } + markUsable(start) + } + if (step.waitForNetwork) { + waitForNetwork() + } else { + pause(step.delayMs) + attempt++ + } + } + } + } + } + + /** Must hold [lock]. Acts on one answer to `/init`. */ + private fun take(result: HttpResult, attempt: Int): Step { + when (result) { + is HttpResult.Success -> { + val answer = result.body as? JSONObject + val did = answer?.optString(UserBasicParams.DID).orEmpty() + if (answer != null && did.isNotEmpty()) { + succeed(answer, did) + return Step.Answered(answer) + } + log(LogLevel.WARNING, LogCodes.DECODING_FAILED, "The answer to GET init has no did", INIT_CONTEXT) + return retry(attempt, mapOf("error" to ERROR_DECODING), retryAfterMs = null) + } + is HttpResult.Failure -> { + val error = result.error + if (error is SdkError.Http) { + val code = error.code + return when { + code in SHOP_REFUSALS || (code in 400..499 && code !in REPEATED_4XX) -> refuse(code) + else -> retry(attempt, mapOf("status" to code), result.retryAfterMs.takeIf { code in RETRY_AFTER_CODES }) + } + } + if (error == SdkError.Cancelled) return Step.Done + return retry(attempt, mapOf("error" to errorName(error)), retryAfterMs = null) + } + } + } + + /** Must hold [lock]. */ + private fun succeed(answer: JSONObject, did: String) { + if (settings.getDid().isEmpty()) settings.updateDid(did) + answer.optString(UserBasicParams.SEANCE).takeIf { it.isNotEmpty() }?.let(settings::updateSid) + storage.saveInitAnswer(answer) + if (storage.isRefused) storage.isRefused = false + publish(SdkState.READY) + log(LogLevel.INFO, LogCodes.INIT_SUCCEEDED, "/init succeeded") + if (advertisingIdValue != null) log(LogLevel.DEBUG, LogCodes.ADVERTISING_ID_SENT, "Advertising id sent with /init") + } + + /** Must hold [lock]. */ + private fun refuse(code: Int): Step { + storage.isRefused = true + publish(SdkState.REFUSED) + log(LogLevel.ERROR, LogCodes.INIT_REFUSED, "The shop refused the SDK: calls fail with shopBlocked until /init succeeds on a later launch", + mapOf("status" to code)) + if (code !in SHOP_REFUSALS) { + log(LogLevel.ERROR, LogCodes.INIT_FAILED, "/init was rejected: the SDK's request is invalid", mapOf("status" to code)) + } + return Step.Done + } + + /** Must hold [lock]. */ + private fun retry(attempt: Int, cause: Map, retryAfterMs: Long?): Step.Retry { + // A shop refused at an earlier launch stays refused until the server says otherwise. + if (state.current != SdkState.REFUSED) publish(SdkState.RETRYING) + val hasDid = settings.getDid().isNotEmpty() + val waitForNetwork = !hasDid && !connectivity.isConnected + val delayMs = backoff.delayMs(attempt, retryAfterMs) + log( + LogLevel.WARNING, + LogCodes.INIT_RETRY, + if (waitForNetwork) "/init failed and waits for the network" else "/init failed and is repeated", + cause + mapOf("attempt" to attempt + 1) + + (if (waitForNetwork) mapOf("reason" to LogReasons.OFFLINE) else mapOf("delay_ms" to delayMs)), + ) + val fallback = state.current == SdkState.RETRYING && hasDid && !fallbackLogged + if (fallback) fallbackLogged = true + return Step.Retry(delayMs, waitForNetwork, fallback) + } + + /** Must hold [lock]. */ + private fun publish(next: SdkState) { + state.set(next) + gate.value = when (next) { + SdkState.READY -> Gate.OPEN + SdkState.RETRYING -> if (settings.getDid().isNotEmpty()) Gate.OPEN else Gate.WAIT + SdkState.REFUSED -> Gate.BLOCKED + SdkState.INITIALIZING -> Gate.WAIT + SdkState.NOT_INITIALIZED -> if (released) Gate.CLOSED else Gate.WAIT + } + } + + /** Tells the instance it can send, once per start. */ + private fun markUsable(start: Int) { + val first = synchronized(lock) { + if (start != generation || released || usable) return + usable = true + true + } + if (first) hook("onUsable") { hooks.onUsable() } + } + + /** A `sid` whose window ended (two hours without activity) is not sent: the server starts a new session. */ + private fun forgetStaleSession() { + if (settings.getSid().isEmpty()) return + if (clock.nowMillis() - settings.getSidLastActTime() > SESSION_WINDOW_MS) storage.forgetSession() + } + + private fun onForegroundChanged(foreground: Boolean) { + if (!foreground) return + synchronized(lock) { + if (released || state.current == SdkState.REFUSED || job?.isActive == true) return + if (clock.nowMillis() - settings.getSidLastActTime() <= SESSION_WINDOW_MS) return + launchLocked() + } + } + + /** The fields of `/init` beside the common ones: `tz`, the advertising id, the extras. */ + private suspend fun params(): JSONObject { + val params = JSONObject() + // The offset now, summer time included. + params.put(PARAM_TZ, (timeZone().getOffset(clock.nowMillis()) / HOUR_MS).toString()) + advertisingId()?.let { params.put(PARAM_ADVERTISING_ID, it) } + setup.extraParams().forEach { (key, value) -> params.put(key, value) } + return params + } + + private suspend fun advertisingId(): String? { + val read = setup.advertisingId ?: return null + advertisingIdValue?.let { return it } + return try { + read()?.takeIf { it.isNotBlank() }?.also { advertisingIdValue = it } + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + log(LogLevel.WARNING, LogCodes.ADVERTISING_ID_FAILED, "The advertising id could not be read", throwable = e) + null + } + } + + /** A pause between repeats; the network coming back ends a pause it set off. */ + private suspend fun pause(delayMs: Long) { + if (connectivity.isConnected) { + delay(delayMs) + } else { + withTimeoutOrNull(delayMs) { awaitNetwork() } + } + } + + /** Waits for the network; looks again every few minutes in case a change went unheard. */ + private suspend fun waitForNetwork() { + while (!connectivity.isConnected) { + withTimeoutOrNull(NETWORK_RECHECK_MS) { awaitNetwork() } + } + } + + private suspend fun awaitNetwork() { + val back = CompletableDeferred() + val handle = connectivity.addListener { connected -> if (connected) back.complete(Unit) } + try { + if (!connectivity.isConnected) back.await() + } finally { + handle.cancel() + } + } + + private inline fun hook(name: String, block: () -> Unit) { + try { + block() + } catch (e: Exception) { + log(LogLevel.ERROR, LogCodes.SDK_LOG, "Acting on /init failed: $name", throwable = e) + } + } + + private fun errorName(error: SdkError): String = when (error) { + SdkError.Network -> "network" + SdkError.Timeout -> "timeout" + SdkError.Decoding -> ERROR_DECODING + else -> "failed" + } + + private fun log(level: LogLevel, code: String, message: String, context: Map = emptyMap(), throwable: Throwable? = null) = + SdkLogs.log(level, code, message, shopId, context, throwable) + + internal companion object { + /** Two hours without activity end a session. */ + const val SESSION_WINDOW_MS = 2 * 60 * 60 * 1000L + + /** [checkShop] asks the server at most this often. */ + const val CHECK_SHOP_INTERVAL_MS = 10 * 60 * 1000L + + /** How often a wait for the network looks again by itself. */ + const val NETWORK_RECHECK_MS = 5 * 60 * 1000L + + private const val HOUR_MS = 3_600_000 + private const val PARAM_TZ = "tz" + private const val PARAM_ADVERTISING_ID = "android_advertising_id" + private const val ERROR_DECODING = "decoding" + private val INIT_CONTEXT = mapOf("method" to "GET", "path" to CoreHttpClient.INIT_PATH) + + /** The shop is not there for the SDK: unpaid, off or unknown. */ + private val SHOP_REFUSALS = setOf(402, 403, 404) + + /** `4xx` answers that are repeated rather than taken as a refusal. */ + private val REPEATED_4XX = setOf(408, 410, 417, 426, 429) + + /** Answers whose `Retry-After` sets the pause. */ + private val RETRY_AFTER_CODES = setOf(429, 503) + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt index 6f8c0482..409da7bb 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt @@ -4,6 +4,7 @@ import com.personalization.LogLevel import com.personalization.api.OnApiCallbackListener import com.personalization.core.SdkError import com.personalization.logs.LogCodes +import com.personalization.logs.LogReasons import com.personalization.logs.SdkLogs import com.personalization.sdk.domain.models.NetworkMethod import kotlinx.coroutines.CancellationException @@ -16,21 +17,30 @@ import org.json.JSONObject import java.util.concurrent.atomic.AtomicBoolean /** - * The new core. For now it sends the instance's requests through [CoreHttpClient]; `/init`, the session - * and the requests waiting for a `did` stay the legacy ones until the core takes them over. + * The new core: the instance's requests go through [CoreHttpClient], and its `/init` and state are + * [CoreInit]'s. A request waits for the instance to be ready within its own read timeout: a refused + * shop fails it with `shopBlocked` (the old callbacks get `402`), a wait that runs out with `timeout`. + * The session and the queue stay the legacy ones until the core takes them over. * * Every request ends in exactly one callback, on a background thread as today: the answer, or the * error mapped to the old `onError(code, msg)` by [LegacyErrors]. [release] cancels the requests in - * flight (`cancelled`), and a request made after it fails with `notInitialized`. + * flight (`cancelled`) and stops `/init`, and a request made after it fails with `notInitialized`. */ internal class CoreV2( - private val client: CoreHttpClient, + internal val client: CoreHttpClient, private val shopId: String?, dispatchers: CoreDispatchers = CoreDispatchers.DEFAULT, ) : Core { override val isV2: Boolean = true + /** The instance's `/init`, once it started; a request waits on it. */ + @Volatile + internal var init: CoreInit? = null + + /** How long a request waits for the instance to be ready: its own read timeout. */ + private val readyWaitMs: Long = client.timeouts.readMs + /** Nothing a request does may reach the app's process as an exception. */ private val uncaught = CoroutineExceptionHandler { _, throwable -> SdkLogs.log(LogLevel.ERROR, LogCodes.SDK_LOG, "A request of the core failed unexpectedly", shopId, throwable = throwable) @@ -62,6 +72,15 @@ internal class CoreV2( return } val request = requests.launch { + val waited = init?.awaitReady(readyWaitMs) + if (waited != null) { + if (waited == SdkError.Timeout) { + SdkLogs.log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "${method.type} ${method.method}: the SDK is not ready", shopId, + mapOf("method" to method.type, "path" to method.method, "reason" to LogReasons.NOT_READY)) + } + answer(HttpResult.Failure(waited)) + return@launch + } val result = try { client.send(method, params) } catch (e: CancellationException) { @@ -88,6 +107,7 @@ internal class CoreV2( override fun release() { released = true requests.cancel() + init?.release() } /** The events in the queue were the customer's: its directory goes with the credentials. */ diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/HttpResult.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/HttpResult.kt index dd3b6267..7a170999 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/HttpResult.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/HttpResult.kt @@ -12,7 +12,8 @@ internal sealed interface HttpResult { /** * [errorBody] is the body of an answer outside `2xx`, kept only for the old callbacks, which got it - * as their message. It is never written to the log. + * as their message. It is never written to the log. [retryAfterMs] is the answer's `Retry-After`, in + * milliseconds from now, when it had one. */ - class Failure(val error: SdkError, val errorBody: String? = null) : HttpResult + class Failure(val error: SdkError, val errorBody: String? = null, val retryAfterMs: Long? = null) : HttpResult } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/InstanceState.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/InstanceState.kt new file mode 100644 index 00000000..8f3a8346 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/InstanceState.kt @@ -0,0 +1,83 @@ +package com.personalization.core.internal + +import com.personalization.Cancellable +import com.personalization.LogLevel +import com.personalization.core.SdkState +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import kotlinx.coroutines.CoroutineExceptionHandler +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.launch +import java.util.concurrent.CopyOnWriteArrayList + +/** + * The [SdkState] of one instance and the app's listeners of it, for both cores: the legacy path moves + * it from `RegisterManager`, the new core from [CoreInit]. + * + * Each change reaches every listener once, on the main thread, in the order the changes happened. A + * listener that throws is logged as `callback_failed` and keeps its later changes; a cancelled one + * gets nothing more. + */ +internal class InstanceState( + private val shopId: () -> String?, + private val dispatchers: CoreDispatchers = CoreDispatchers.DEFAULT, +) { + + private val lock = Any() + private val flow = MutableStateFlow(SdkState.NOT_INITIALIZED) + private val listeners = CopyOnWriteArrayList() + + /** Created on the first change with a listener: a process without one never loads the main dispatcher. */ + private val delivery: CoroutineScope by lazy { + CoroutineScope(SupervisorJob() + dispatchers.main + CoroutineExceptionHandler { _, throwable -> + SdkLogs.log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "A state listener could not be called", shopId(), throwable = throwable) + }) + } + + val current: SdkState get() = flow.value + + /** The state as a flow, for the core's own waits. */ + val changes: StateFlow get() = flow.asStateFlow() + + /** Moves to [state]; false when the instance is already there, and then nobody hears of it. */ + fun set(state: SdkState): Boolean = synchronized(lock) { + if (flow.value == state) return false + flow.value = state + SdkLogs.log(LogLevel.DEBUG, LogCodes.SDK_LOG, "State: $state", shopId(), mapOf("state" to state.name.lowercase())) + // Scheduled under the lock: two changes from two threads reach the main thread in their order. + val targets = listeners.toList() + if (targets.isNotEmpty()) delivery.launch { targets.forEach { it.deliver(state) } } + true + } + + /** Calls [listener] on the main thread with every later change until the handle is cancelled. */ + fun addListener(listener: (SdkState) -> Unit): Cancellable { + val subscription = Subscription(listener) + listeners += subscription + return Cancellable { + subscription.active = false + listeners -= subscription + } + } + + /** Forgets every listener; a change already on its way to them still arrives. */ + fun dropListeners() = listeners.clear() + + private inner class Subscription(private val listener: (SdkState) -> Unit) { + @Volatile + var active = true + + fun deliver(state: SdkState) { + if (!active) return + try { + listener(state) + } catch (e: Exception) { + SdkLogs.log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "The app's state listener threw", shopId(), throwable = e) + } + } + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt index 1847a09d..7851b4bd 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt @@ -4,6 +4,7 @@ import android.net.Uri import com.personalization.LogLevel import com.personalization.SDK import com.personalization.api.OnApiCallbackListener +import com.personalization.core.SdkError import com.personalization.logs.LogCodes import com.personalization.logs.SdkLogs import com.personalization.sdk.domain.models.NetworkMethod @@ -26,7 +27,8 @@ import java.nio.charset.StandardCharsets /** * Today's request path, unchanged: `HttpURLConnection` on a thread per request. Any `2xx` is a * success, an empty answer or one that is not JSON succeeds with an empty object, no connection is - * `504` and a failure without an answer `-1`. It cannot cancel a request, so [release] does nothing. + * `504` and a failure without an answer `-1`. It cannot cancel a request in flight; after [release] a + * request fails at once with `notInitialized` (`onError(-1)`). */ internal class LegacyCore( private val settings: UserSettingsRepository, @@ -41,7 +43,18 @@ internal class LegacyCore( private val delivery = LegacyDelivery { level, code, message, throwable -> log(level, code, message, throwable = throwable) } + @Volatile + private var released = false + override fun send(method: NetworkMethod, params: JSONObject, listener: OnApiCallbackListener?) { + if (released) { + // Off the caller's thread, as every answer of this path. + if (listener != null) { + val error = LegacyErrors.of(SdkError.NotInitialized) + Thread { delivery.failure(listener, error.code, error.message) }.start() + } + return + } try { executeMethod(method, CommonFields.addTo(params, settings), listener) } catch (e: JSONException) { @@ -49,7 +62,9 @@ internal class LegacyCore( } } - override fun release() = Unit + override fun release() { + released = true + } /** The legacy path has no queue: the partition's credentials and the saved answer go. */ override fun deleteUserCredentials(storage: ShopStorage): Boolean = storage.deleteUserCredentials(includeQueue = false) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ProcessAppLifecycle.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ProcessAppLifecycle.kt new file mode 100644 index 00000000..8ecd8d2a --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ProcessAppLifecycle.kt @@ -0,0 +1,69 @@ +package com.personalization.core.internal + +import android.os.Handler +import android.os.Looper +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.ProcessLifecycleOwner +import com.personalization.Cancellable + +/** + * The app's foreground as `ProcessLifecycleOwner` sees it: started while an activity of the app is, + * stopped a moment after the last one stops (a rotation is not a trip to the background). + * + * A listener hears changes only. The lifecycle replays its current state to a new observer; that replay + * is no change and is not passed on, so an instance made in the foreground does not hear "foreground". + * Observers are added and removed on the main thread, as the lifecycle requires. + */ +internal class ProcessAppLifecycle( + private val owner: () -> LifecycleOwner = { ProcessLifecycleOwner.get() }, +) : AppLifecycle { + + private val main = Handler(Looper.getMainLooper()) + + override val isForeground: Boolean + get() = owner().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED) + + override fun addListener(listener: (foreground: Boolean) -> Unit): Cancellable { + val subscription = Subscription(listener) + onMain { subscription.attach() } + return Cancellable { onMain { subscription.detach() } } + } + + private fun onMain(block: () -> Unit) { + if (Looper.myLooper() == Looper.getMainLooper()) block() else main.post(block) + } + + /** Lives on the main thread. */ + private inner class Subscription(private val listener: (Boolean) -> Unit) : LifecycleEventObserver { + private var lifecycle: Lifecycle? = null + private var detached = false + private var last = false + + fun attach() { + if (detached || lifecycle != null) return + val target = owner().lifecycle + last = target.currentState.isAtLeast(Lifecycle.State.STARTED) + lifecycle = target + target.addObserver(this) + } + + fun detach() { + detached = true + lifecycle?.removeObserver(this) + lifecycle = null + } + + override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { + val foreground = when (event) { + Lifecycle.Event.ON_START -> true + Lifecycle.Event.ON_STOP -> false + else -> return + } + if (detached || foreground == last) return + last = foreground + listener(foreground) + } + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt index 480ea759..950b5879 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt @@ -134,6 +134,27 @@ internal class ShopStorage(context: Context, val layout: StorageLayout) { return editor.commit() } + /** + * Forgets the device for `needReInitialization`: the `did`, the session and the saved `/init` + * answer, so the next `/init` gets a new `did`. False when part of it did not reach the disk. + */ + fun forgetDevice(): Boolean { + val complete = attempt(OP_FORGET_DEVICE) { + preferences.edit().remove(StorageKeys.DID).remove(StorageKeys.SID).remove(StorageKeys.SEANCE).remove(StorageKeys.SID_LAST_ACT).commit() + } + return attempt(OP_DELETE_INIT_ANSWER) { + synchronized(fileLock) { + initAnswerFile.delete() + !initAnswerFile.baseFile.exists() + } + } && complete + } + + /** Ends the session: its `sid` and the time of its last activity go, so `/init` asks for a new one. */ + fun forgetSession() { + preferences.edit().remove(StorageKeys.SID).remove(StorageKeys.SID_LAST_ACT).apply() + } + /** Runs [block]; a false result or an exception is logged under [op]. */ private inline fun attempt(op: String, block: () -> Boolean): Boolean { val done = try { @@ -169,5 +190,6 @@ internal class ShopStorage(context: Context, val layout: StorageLayout) { const val OP_DELETE_INIT_ANSWER = "delete_init_answer" const val OP_DELETE_QUEUE = "delete_queue" const val OP_DELETE_SOURCE_CREDENTIALS = "delete_old_storage_credentials" + const val OP_FORGET_DEVICE = "forget_device" } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt index 6d6f093f..540adaed 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt @@ -222,4 +222,7 @@ internal object LogReasons { const val AUTO_PRESENTATION_OFF = "auto_presentation_off" const val DELETE_USER_CREDENTIALS = "delete_user_credentials" const val PRE_LAUNCH_REPORT = "pre_launch_report" + const val NEED_REINITIALIZATION = "need_reinitialization" + const val ALREADY_INITIALIZED = "already_initialized" + const val NOT_READY = "not_ready" } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt index 1128a291..ecf7eb7e 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt @@ -15,8 +15,9 @@ import javax.inject.Inject import javax.inject.Singleton /** - * The funnel every request of an instance goes through: the namespaces call it, it holds the requests - * that wait for a `did` and hands each request to the instance's [Core], legacy or new. + * The funnel every request of an instance goes through: the namespaces call it and it hands each + * request to the instance's [Core], legacy or new. On the legacy core it holds the requests that wait + * for a `did`; the new core holds a request until the instance is ready itself. */ @Singleton class NetworkRepositoryImpl @Inject constructor( @@ -53,10 +54,7 @@ class NetworkRepositoryImpl @Inject constructor( this.baseUrl = baseUrl } - /** - * Sends this instance's requests through the new client from now on. `/init`, the session and the - * requests waiting for a `did` keep the legacy behaviour until the core takes them over. - */ + /** Sends this instance's requests through the new core from now on. */ internal fun useCoreV2( shopId: String, connectivity: ConnectivityMonitor, @@ -67,7 +65,7 @@ class NetworkRepositoryImpl @Inject constructor( connectivity = connectivity, shopId = shopId, ), - ): Core = CoreV2(client, shopId, dispatchers).also { core = it } + ): CoreV2 = CoreV2(client, shopId, dispatchers).also { core = it } override fun post( method: String, @@ -138,6 +136,11 @@ class NetworkRepositoryImpl @Inject constructor( } private fun sendAsync(sendFunction: () -> Unit) { + // The new core waits for the instance to be ready itself, within the request's timeout. + if (core.isV2) { + sendFunction() + return + } val thread = Thread(sendFunction) val startNow = synchronized(queueLock) { if (userSettingsRepository.getDid().isNotEmpty()) { @@ -161,6 +164,15 @@ class NetworkRepositoryImpl @Inject constructor( synchronized(queueLock) { queue.add(thread) } } + /** + * Stops the instance's core. The requests still waiting for a `did` are let go and fail with + * `notInitialized`, as every request made after this does. + */ + internal fun release() { + core.release() + executeQueueTasks() + } + private fun sendMethod( networkMethod: NetworkMethod, params: JSONObject, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/compose/StoriesWidget.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/compose/StoriesWidget.kt index 7b3eccfd..d23d22b0 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/compose/StoriesWidget.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/compose/StoriesWidget.kt @@ -31,7 +31,7 @@ import com.personalization.stories.views.StoriesView * * @param code the stories block code configured for the shop. * @param shopId the shop whose instance loads the block. `null` uses the single default instance - * (see [Rees46.getInstance]); pass an explicit id in a multi-shop app. + * (see [Rees46.instance]); pass an explicit id in a multi-shop app. * @param needOpeningWebView whether the SDK opens tapped links itself. Mirrors the * `app:need_opening_web_view` attribute; pass `false` to route every link in the host. * @param productBannerTapDefaultMessage toast shown when a promocode is copied. diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoriesView.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoriesView.kt index fed46e26..0fe14ca6 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoriesView.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/views/StoriesView.kt @@ -144,7 +144,7 @@ class StoriesView : ConstraintLayout, ClickListener { // resolve the instance for this view's shop as soon as it is available and load. The // subscription fires immediately when the instance already exists, or later otherwise. // - // A direct Rees46.getInstance/awaitInstance keeps its fail-fast contract, but here the resolve + // A direct Rees46.instance/awaitInstance keeps its fail-fast contract, but here the resolve // happens inside a lifecycle callback the host cannot wrap: a misconfigured view (e.g. no // app:shop_id while several shops are registered → AmbiguousShopException) must log and // render nothing, not crash the screen. diff --git a/personalization-sdk/src/test/java/com/personalization/Rees46ConfigJavaTest.java b/personalization-sdk/src/test/java/com/personalization/Rees46ConfigJavaTest.java index 04495fcd..152ba298 100644 --- a/personalization-sdk/src/test/java/com/personalization/Rees46ConfigJavaTest.java +++ b/personalization-sdk/src/test/java/com/personalization/Rees46ConfigJavaTest.java @@ -41,10 +41,22 @@ public void theNewCoreKeepsItsPlace() { } @Test - public void theStorageKeyIsTheLastParameter() { + public void theStorageKeyKeepsItsPlace() { Rees46Config config = new Rees46Config( "shop", "api.rees46.ru", "android", true, false, true, "SDK", true, LogLevel.WARNING, false, "host"); assertEquals("host", config.getStorageKey()); + assertNull(config.getDeviceId()); + assertFalse(config.getSendAdvertisingId()); + } + + @Test + public void theDeviceIdAndTheAdvertisingIdComeLast() { + Rees46Config config = new Rees46Config( + "shop", "api.rees46.ru", "android", true, false, true, "SDK", true, LogLevel.WARNING, false, null, + "did-from-rn", true); + + assertEquals("did-from-rn", config.getDeviceId()); + assertTrue(config.getSendAdvertisingId()); } } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt index 69515caa..dbe0b6b3 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt @@ -84,22 +84,19 @@ class CoreFunnelTest { } @Test - fun `requests waiting for a did go through the new core once it comes`() { + fun `on the new core a request without a did waits for the instance, not for the did queue`() { repository.useCoreV2("shop", FakeConnectivity(connected = true)) did.set("") server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) val answered = CountDownLatch(1) + + // Without an init to wait on the request goes at once: the did queue no longer holds it. repository.getAsync("products/get", JSONObject(), object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) = answered.countDown() }) - Thread.sleep(200) - assertEquals(0, server.requestCount) - - did.set("did-2") - repository.executeQueueTasks() assertTrue(answered.await(5, TimeUnit.SECONDS)) - assertEquals("did-2", checkNotNull(server.takeRequest().requestUrl).queryParameter("did")) + assertEquals(1, server.requestCount) } @Test From dd0f80e633146eb77690af1147941c48ef97ccf0 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 00:12:37 +0400 Subject: [PATCH 031/109] test(core): every row of the init answer table, the pauses and Retry-After, the refused state kept across launches and cleared by a later success, decision 1 and the saved answer as a fallback, the wait for ready with its timeout, the network, the foreground and checkShop triggers, stale answers after a restart or release, init's tz, stream and advertising id, and the contract fixtures of init on the new core over a local server; the state's listeners on the main thread once per change; the process lifecycle's foreground; and through the SDK on both cores: one instance per shop whoever asks, release leaving notInitialized behind without deleting or sending anything, a fresh instance after it, the legacy state, and the new core's requests waiting for init, refused with 402 and getting a new did with needReInitialization --- .../com/personalization/SdkInstanceTest.kt | 477 +++++++++++++ .../core/internal/BackoffTest.kt | 41 ++ .../core/internal/CoreHttpClientTest.kt | 28 + .../core/internal/CoreInitFixturesTest.kt | 124 ++++ .../core/internal/CoreInitTest.kt | 626 ++++++++++++++++++ .../core/internal/InstanceStateTest.kt | 134 ++++ .../core/internal/ProcessAppLifecycleTest.kt | 74 +++ 7 files changed, 1504 insertions(+) create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/BackoffTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitFixturesTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/InstanceStateTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/ProcessAppLifecycleTest.kt diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt new file mode 100644 index 00000000..42568ed5 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt @@ -0,0 +1,477 @@ +package com.personalization + +import android.content.Context +import android.net.ConnectivityManager +import android.net.NetworkCapabilities +import android.os.Looper +import androidx.test.core.app.ApplicationProvider +import com.personalization.api.OnApiCallbackListener +import com.personalization.core.SdkError +import com.personalization.core.SdkException +import com.personalization.core.SdkState +import com.personalization.core.internal.CoreSelection +import com.personalization.core.internal.CoreV2 +import com.personalization.core.internal.DeviceStorage +import com.personalization.core.internal.ShopStorage +import com.personalization.core.internal.StorageLayout +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeout +import okhttp3.HttpUrl +import okhttp3.mockwebserver.Dispatcher +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import okhttp3.mockwebserver.RecordedRequest +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotSame +import org.junit.Assert.assertNull +import org.junit.Assert.assertSame +import org.junit.Assert.assertThrows +import org.junit.Assert.assertTrue +import org.junit.Assert.fail +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.ShadowNetworkCapabilities +import java.io.File +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.CyclicBarrier +import java.util.concurrent.Executor +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit + +/** + * The instance and the facade on both cores: one instance per shop whoever asks and however often, + * what `release()` leaves behind, the state an app sees, and the new core's `/init` as an app starts it. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class SdkInstanceTest { + + private lateinit var context: Context + private var server = MockWebServer() + private val logged = CopyOnWriteArrayList() + private val defaultExecutor = SdkLogs.handlerExecutor + + /** Every request the server got, `/init` among them. */ + private val requests = LinkedBlockingQueue() + + /** The answer to `/init`, given the did it carried. */ + @Volatile + private var initAnswer: (did: String) -> MockResponse = { did -> + MockResponse().setBody(JSONObject().put("did", did.ifEmpty { "new-did" }).put("seance", "S").toString()) + } + + @Before + fun setUp() { + context = ApplicationProvider.getApplicationContext() + CoreSelection.reset() + SdkRegistry.reset() + Rees46.reset() + SdkLogs.reset() + SdkLogs.handlerExecutor = Executor { it.run() } + SdkLogs.handler = SdkLogHandler { logged += it } + listOf(SHOP, OTHER).forEach { context.getSharedPreferences(StorageLayout(it, null).partitionName, Context.MODE_PRIVATE).edit().clear().commit() } + listOf(PreferencesPartition.LEGACY_KEY, DeviceStorage.NAME).forEach { context.getSharedPreferences(it, Context.MODE_PRIVATE).edit().clear().commit() } + File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() + // Robolectric's default network has no internet capability: give it one, as a device's has. + val manager = context.getSystemService(ConnectivityManager::class.java) + val capabilities = ShadowNetworkCapabilities.newInstance() + shadowOf(capabilities).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + shadowOf(manager).setNetworkCapabilities(manager.activeNetwork, capabilities) + // A test that runs both cores starts over in the middle: a server starts once. + server = MockWebServer() + requests.clear() + server.dispatcher = object : Dispatcher() { + override fun dispatch(request: RecordedRequest): MockResponse { + val url = request.requestUrl ?: return MockResponse().setResponseCode(400) + requests += url + if (url.encodedPath != "/init") return MockResponse().setBody("{}") + return initAnswer(url.queryParameter("did").orEmpty()) + } + } + server.start() + } + + @After + fun tearDown() { + SdkRegistry.all().forEach { it.release() } + server.shutdown() + CoreSelection.reset() + SdkRegistry.reset() + Rees46.reset() + SdkLogs.reset() + SdkLogs.handlerExecutor = defaultExecutor + } + + private fun config(shopId: String = SHOP, coreV2: Boolean = false, stream: String = "android", needReInitialization: Boolean = false, deviceId: String? = null) = + Rees46Config( + shopId = shopId, + apiDomain = server.url("/").toString().trimEnd('/'), + stream = stream, + autoSendPushToken = false, + needReInitialization = needReInitialization, + logLevel = LogLevel.DEBUG, + coreV2 = coreV2, + deviceId = deviceId, + ) + + private fun nextRequest(path: String): HttpUrl { + while (true) { + val url = requests.poll(5, TimeUnit.SECONDS) ?: throw AssertionError("no $path request") + if (url.encodedPath == "/$path") return url + } + } + + /** No more requests to [path] within a moment; requests to other paths (the legacy profile/set) are let by. */ + private fun assertNoMore(path: String) { + val deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(300) + while (true) { + val left = deadline - System.nanoTime() + if (left <= 0) return + val url = requests.poll(left, TimeUnit.NANOSECONDS) ?: return + assertFalse("unexpected $url", url.encodedPath == "/$path") + } + } + + private fun awaitState(sdk: SDK, state: SdkState) = runBlocking { + withTimeout(5_000) { sdk.instanceState.changes.first { it == state } } + } + + private fun partition(shopId: String = SHOP) = + context.getSharedPreferences(StorageLayout(shopId, null).partitionName, Context.MODE_PRIVATE) + + /** A request through the old callbacks, as an app makes it; its one answer. */ + @Suppress("DEPRECATION") + private fun request(sdk: SDK): String { + val answers = LinkedBlockingQueue() + sdk.getAsync("products/get", JSONObject(), object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) { + answers += "success" + } + + override fun onError(code: Int, msg: String?) { + answers += "error:$code" + } + }) + return answers.poll(15, TimeUnit.SECONDS) ?: "none" + } + + // The facade. + + @Test + fun `initialize for a shop again returns its instance and sends no second init`() { + listOf(false, true).forEach { coreV2 -> + if (coreV2) tearDown().also { setUp() } + val first = Rees46.initialize(context, config(coreV2 = coreV2)) + nextRequest("init") + + val second = Rees46.initialize(context, config(coreV2 = coreV2)) + + assertSame(first, second) + assertEquals(1, SdkRegistry.count()) + assertNoMore("init") + assertTrue(logged.none { it.code == LogCodes.INVALID_CONFIG }) + } + } + + @Test + fun `initialize with another config keeps the first one and says so`() { + val first = Rees46.initialize(context, config(stream = "android")) + + val second = Rees46.initialize(context, config(stream = "flutter")) + + assertSame(first, second) + assertEquals("android", first.config?.stream) + val warning = logged.single { it.code == LogCodes.INVALID_CONFIG } + assertEquals(LogLevel.WARNING, warning.level) + assertEquals(SHOP, warning.shopId) + assertEquals("already_initialized", warning.context["reason"]) + } + + @Test + fun `initialize from several threads at once makes one instance`() { + val threads = 8 + val barrier = CyclicBarrier(threads) + val made = CopyOnWriteArrayList() + val done = CountDownLatch(threads) + repeat(threads) { + Thread { + barrier.await() + made += Rees46.initialize(context, config()) + done.countDown() + }.start() + } + + assertTrue(done.await(10, TimeUnit.SECONDS)) + assertEquals(1, made.toSet().size) + assertEquals(1, SdkRegistry.count()) + nextRequest("init") + assertNoMore("init") + } + + @Test + fun `a lazy registration made from several threads at once makes one instance`() { + Rees46.register(context, listOf(config(), config(OTHER))) + val barrier = CyclicBarrier(4) + val made = CopyOnWriteArrayList() + val done = CountDownLatch(4) + repeat(4) { + Thread { + barrier.await() + made += Rees46.instance(SHOP) + done.countDown() + }.start() + } + + assertTrue(done.await(10, TimeUnit.SECONDS)) + assertEquals(1, made.toSet().size) + assertEquals(setOf(OTHER), Rees46.pendingShopIds()) + } + + @Test + @Suppress("DEPRECATION") + fun `the former names still work`() { + Rees46.registerShops(context, listOf(config())) + assertSame(Rees46.instance(SHOP), Rees46.getInstance(SHOP)) + assertSame(Rees46.instance(), Rees46.getInstance()) + } + + @Test + fun `the facade's errors share Rees46Error and stay IllegalStateException`() { + val unknown = assertThrows(Rees46Error::class.java) { Rees46.instance("nope") } + assertTrue(unknown is UnknownShopIdException) + Rees46.register(context, listOf(config(), config(OTHER))) + val ambiguous: IllegalStateException = assertThrows(IllegalStateException::class.java) { Rees46.instance() } + assertTrue(ambiguous is AmbiguousShopException && ambiguous is Rees46Error) + } + + // release(). + + @Test + fun `after release every call fails with notInitialized and nothing is sent or deleted, on both cores`() { + listOf(false, true).forEach { coreV2 -> + if (coreV2) tearDown().also { setUp() } + val sdk = Rees46.initialize(context, config(coreV2 = coreV2)) + nextRequest("init") + awaitState(sdk, SdkState.READY) + val did = sdk.deviceId + + sdk.release() + + assertEquals(SdkState.NOT_INITIALIZED, sdk.state) + assertFalse(Rees46.isInitialized(SHOP)) + assertEquals("core $coreV2", "error:-1", request(sdk)) + try { + runBlocking { sdk.deleteUserCredentials() } + fail("expected SdkException") + } catch (e: SdkException) { + assertEquals(SdkError.NotInitialized, e.error) + } + // Nothing was deleted and no /init went. + assertEquals(did, partition().getString("did", null)) + assertNoMore("init") + } + } + + @Test + fun `a request waiting for a did when the instance is released fails with notInitialized`() { + initAnswer = { MockResponse().setResponseCode(503) } + val sdk = Rees46.initialize(context, config()) + nextRequest("init") + val answers = LinkedBlockingQueue() + @Suppress("DEPRECATION") + sdk.getAsync("products/get", JSONObject(), object : OnApiCallbackListener() { + override fun onError(code: Int, msg: String?) { + answers += code + } + }) + + sdk.release() + + assertEquals(-1, answers.poll(5, TimeUnit.SECONDS)) + } + + @Test + fun `after release initialize makes a fresh instance`() { + val first = Rees46.initialize(context, config(coreV2 = true)) + first.release() + + val second = Rees46.initialize(context, config(coreV2 = true)) + + assertNotSame(first, second) + assertSame(second, Rees46.instance(SHOP)) + awaitState(second, SdkState.READY) + assertEquals(SdkState.NOT_INITIALIZED, first.state) + } + + // The state an app sees. + + @Test + @Suppress("DEPRECATION") + fun `on the legacy core a new device goes from initializing to ready, heard once each on the main thread`() { + val sdk = SDK() + val heard = CopyOnWriteArrayList>() + sdk.onStateChange { heard += it to (Looper.myLooper() == Looper.getMainLooper()) } + assertEquals(SdkState.NOT_INITIALIZED, sdk.state) + + sdk.initialize(context, SHOP, apiDomain = server.url("/").toString().trimEnd('/'), autoSendPushToken = false) + assertEquals(SdkState.INITIALIZING, sdk.state) + assertNull(sdk.deviceId) + assertEquals("", sdk.getDid()) + awaitState(sdk, SdkState.READY) + shadowOf(Looper.getMainLooper()).idle() + + assertEquals(listOf(SdkState.INITIALIZING to true, SdkState.READY to true), heard) + assertEquals("new-did", sdk.deviceId) + assertEquals("S", sdk.sessionId) + assertEquals(SHOP, sdk.shopId) + } + + @Test + fun `on the legacy core a stored did is ready at once and a refusing init changes nothing`() { + partition().edit().putString("did", "stored-did").commit() + initAnswer = { MockResponse().setResponseCode(403) } + + val sdk = Rees46.initialize(context, config()) + + assertEquals(SdkState.READY, sdk.state) + nextRequest("init") + assertEquals(SdkState.READY, sdk.state) + } + + @Test + fun `on the legacy core a failed init leaves a new device initializing, never refused`() { + initAnswer = { MockResponse().setResponseCode(403) } + val handled = CountDownLatch(1) + SdkLogs.handler = SdkLogHandler { if (it.code == LogCodes.INIT_FAILED) handled.countDown() } + + val sdk = Rees46.initialize(context, config()) + + // The legacy path logs the failure once it has handled it. + assertTrue(handled.await(5, TimeUnit.SECONDS)) + assertEquals(SdkState.INITIALIZING, sdk.state) + } + + @Test + fun `on the legacy core a did of the config is taken while the shop has none`() { + Rees46.initialize(context, config(deviceId = "did-from-rn")) + + assertEquals("did-from-rn", nextRequest("init").queryParameter("did")) + } + + // The new core as an app starts it. + + @Test + fun `the new core's init carries the stream of the config and tz, and no advertising id`() { + val sdk = Rees46.initialize(context, config(coreV2 = true, stream = "flutter")) + + val init = nextRequest("init") + assertTrue(sdk.core is CoreV2) + assertEquals("flutter", init.queryParameter("stream")) + assertTrue(init.queryParameter("tz")?.toIntOrNull() != null) + assertNull(init.queryParameter("android_advertising_id")) + assertNull(init.queryParameter("did")) + awaitState(sdk, SdkState.READY) + assertEquals("new-did", sdk.deviceId) + // No profile/set with the advertising id either. + assertNull(requests.poll(300, TimeUnit.MILLISECONDS)) + } + + @Test + fun `on the new core a request waits for init and goes with the new did`() { + val answer = CountDownLatch(1) + initAnswer = { did -> + answer.await(5, TimeUnit.SECONDS) + MockResponse().setBody(JSONObject().put("did", did.ifEmpty { "new-did" }).put("seance", "S").toString()) + } + val sdk = Rees46.initialize(context, config(coreV2 = true)) + nextRequest("init") + assertEquals(SdkState.INITIALIZING, sdk.state) + + val result = LinkedBlockingQueue() + Thread { result += request(sdk) }.start() + assertNull(requests.poll(300, TimeUnit.MILLISECONDS)) + answer.countDown() + + assertEquals("new-did", nextRequest("products/get").queryParameter("did")) + assertEquals("success", result.poll(5, TimeUnit.SECONDS)) + } + + @Test + fun `on the new core a refused shop fails every request with 402 and stays refused at the next launch`() { + initAnswer = { MockResponse().setResponseCode(403) } + val sdk = Rees46.initialize(context, config(coreV2 = true)) + awaitState(sdk, SdkState.REFUSED) + + assertEquals("error:402", request(sdk)) + assertTrue(logged.any { it.code == LogCodes.INIT_REFUSED && it.level == LogLevel.ERROR }) + + sdk.release() + initAnswer = { did -> MockResponse().setBody(JSONObject().put("did", did.ifEmpty { "paid-did" }).toString()) } + val next = Rees46.initialize(context, config(coreV2 = true)) + assertEquals(SdkState.REFUSED, next.state) + awaitState(next, SdkState.READY) + assertEquals("success", request(next)) + } + + @Test + fun `on the new core needReInitialization gives a new did`() { + partition().edit().putString("did", "old-did").putString("sid", "old-sid").commit() + initAnswer = { did -> MockResponse().setBody(JSONObject().put("did", did.ifEmpty { "fresh-did" }).put("seance", "S").toString()) } + + val sdk = Rees46.initialize(context, config(coreV2 = true, needReInitialization = true)) + + assertNull(nextRequest("init").queryParameter("did")) + awaitState(sdk, SdkState.READY) + assertEquals("fresh-did", sdk.deviceId) + assertTrue(logged.any { it.code == LogCodes.DEVICE_ID_RESET && it.context["reason"] == "need_reinitialization" }) + } + + @Test + fun `on the new core a stored did is ready at once and its init still goes`() { + partition().edit().putString("did", "stored-did").commit() + + val sdk = Rees46.initialize(context, config(coreV2 = true)) + + assertEquals(SdkState.READY, sdk.state) + assertEquals("stored-did", nextRequest("init").queryParameter("did")) + } + + @Test + fun `on the new core deleting the customer holds requests until the new did`() { + val sdk = Rees46.initialize(context, config(coreV2 = true)) + awaitState(sdk, SdkState.READY) + requests.clear() + val second = CountDownLatch(1) + initAnswer = { did -> + second.await(5, TimeUnit.SECONDS) + MockResponse().setBody(JSONObject().put("did", did.ifEmpty { "next-did" }).put("seance", "S2").toString()) + } + + runBlocking { sdk.deleteUserCredentials() } + assertEquals(SdkState.INITIALIZING, sdk.state) + assertNull(nextRequest("init").queryParameter("did")) + val result = LinkedBlockingQueue() + Thread { result += request(sdk) }.start() + assertNull(requests.poll(300, TimeUnit.MILLISECONDS)) + second.countDown() + + assertEquals("next-did", nextRequest("products/get").queryParameter("did")) + assertEquals("success", result.poll(5, TimeUnit.SECONDS)) + } + + private companion object { + const val SHOP = "instance-shop" + const val OTHER = "instance-other" + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/BackoffTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/BackoffTest.kt new file mode 100644 index 00000000..787854ae --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/BackoffTest.kt @@ -0,0 +1,41 @@ +package com.personalization.core.internal + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test + +/** The pauses between repeats: 1 s doubling to 5 minutes, ±20 %, a server's Retry-After as it is. */ +class BackoffTest { + + @Test + fun `the pause doubles from a second and stops at five minutes`() { + val backoff = Backoff(random = { 0.5 }) + + assertEquals( + listOf(1_000L, 2_000L, 4_000L, 8_000L, 16_000L, 32_000L, 64_000L, 128_000L, 256_000L, 300_000L, 300_000L), + (0..10).map { backoff.delayMs(it) }, + ) + assertEquals(300_000L, backoff.delayMs(Int.MAX_VALUE)) + } + + @Test + fun `each pause is spread by 20 percent either way`() { + val low = Backoff(random = { 0.0 }) + val high = Backoff(random = { 0.999_999 }) + + assertEquals(800L, low.delayMs(0)) + assertEquals(240_000L, low.delayMs(20)) + assertTrue(high.delayMs(0) in 1_199L..1_200L) + assertTrue(high.delayMs(20) in 359_999L..360_000L) + } + + @Test + fun `a Retry-After is waited as it is`() { + val backoff = Backoff(random = { 0.0 }) + + assertEquals(30_000L, backoff.delayMs(0, retryAfterMs = 30_000)) + assertEquals(3_600_000L, backoff.delayMs(5, retryAfterMs = 3_600_000)) + assertEquals(0L, backoff.delayMs(3, retryAfterMs = 0)) + assertEquals(800L, backoff.delayMs(0, retryAfterMs = -1)) + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt index 8786dc08..d80b711e 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt @@ -208,6 +208,34 @@ class CoreHttpClientTest { assertEquals("42", url.queryParameter("item_id")) } + @Test + fun `init leaves out a did and a session it does not have, and carries the stream of the config`() { + every { settings.getDid() } returns "" + every { settings.getSid() } returns "" + every { settings.getStream() } returns "flutter" + reply(200, "{}") + + send(NetworkMethod.GET("init"), JSONObject().put("tz", "3")) + + val url = checkNotNull(server.takeRequest().requestUrl) + assertEquals(setOf("tz", "shop_id", "segment", "stream"), url.queryParameterNames) + assertEquals("flutter", url.queryParameter("stream")) + } + + @Test + fun `the Retry-After of an answer comes back with it, in seconds or as a date`() { + server.enqueue(MockResponse().setResponseCode(503).setHeader("Retry-After", "30")) + server.enqueue(MockResponse().setResponseCode(429).setHeader("Retry-After", "Wed, 21 Oct 2015 07:28:00 GMT")) + server.enqueue(MockResponse().setResponseCode(503).setHeader("Retry-After", "soon")) + server.enqueue(MockResponse().setResponseCode(500)) + val clock = FakeClock(now = 1_445_412_420_000L) // a minute before the date above + val client = CoreHttpClient({ server.url("/").toString() }, settings, connectivity, "shop", clock = clock) + + val results = List(4) { send(NetworkMethod.GET("init"), client = client) as HttpResult.Failure } + + assertEquals(listOf(30_000L, 60_000L, null, null), results.map { it.retryAfterMs }) + } + @Test fun `a POST carries the common fields in its JSON body`() { reply(200, "{}") diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitFixturesTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitFixturesTest.kt new file mode 100644 index 00000000..96d48ab1 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitFixturesTest.kt @@ -0,0 +1,124 @@ +package com.personalization.core.internal + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import com.personalization.api.OnApiCallbackListener +import com.personalization.core.SdkState +import com.personalization.logs.SdkLogs +import com.personalization.sdk.data.repositories.preferences.PreferencesDataSourceImpl +import com.personalization.sdk.data.repositories.userSettings.UserSettingsRepositoryImpl +import com.personalization.sdk.domain.models.NetworkMethod +import okhttp3.mockwebserver.Dispatcher +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import okhttp3.mockwebserver.RecordedRequest +import okhttp3.mockwebserver.SocketPolicy +import org.json.JSONObject +import org.junit.After +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeout +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.io.File +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit + +/** + * The shared contract fixtures of `/init` (`contract/0/init.json`) against the new core: the real + * client over a local server, the real storage, and the state each answer leaves. A repeat would come + * a minute later, so the state checked is the first answer's. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class CoreInitFixturesTest { + + private val context: Context = ApplicationProvider.getApplicationContext() + private val servers = mutableListOf() + private val releases = mutableListOf<() -> Unit>() + + @After + fun tearDown() { + releases.forEach { it() } + servers.forEach { it.shutdown() } + SdkLogs.reset() + } + + @Test + fun `every case of the init fixtures`() { + val cases = ContractFixtures.cases("init") + assertTrue(cases.isNotEmpty()) + cases.forEachIndexed { index, case -> check(index, case) } + } + + private fun check(index: Int, case: JSONObject) { + val name = case.getString("name") + val layout = StorageLayout("fixture-shop-$index", null) + context.getSharedPreferences(layout.partitionName, Context.MODE_PRIVATE).edit().clear().commit() + File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() + val storage = ShopStorage(context, layout) + case.optJSONObject("saved")?.let { storage.saveInitAnswer(it) } + val settings = UserSettingsRepositoryImpl(PreferencesDataSourceImpl().apply { initialize(context, layout.partitionName, layout.shopId) }) + settings.updateShopId(layout.shopId) + + val response = case.getJSONObject("response") + val server = MockWebServer().also { servers += it } + server.dispatcher = object : Dispatcher() { + override fun dispatch(request: RecordedRequest): MockResponse { + if (request.requestUrl?.encodedPath != "/init") return MockResponse().setBody("{}") + if (response.isNull("status")) return MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE) + val body = response.opt("body")?.takeUnless { it == JSONObject.NULL }?.toString().orEmpty() + return MockResponse().setResponseCode(response.getInt("status")).setBody(body) + } + } + server.start() + + val connectivity = FakeConnectivity(connected = true) + val client = CoreHttpClient( + { server.url("/").toString() }, settings, connectivity, layout.shopId, + timeouts = CoreTimeouts(initReadMs = 300), + ) + val core = CoreV2(client, layout.shopId) + val state = InstanceState(shopId = { layout.shopId }) + val init = CoreInit( + setup = InitSetup(layout.shopId), + send = { params -> client.send(NetworkMethod.GET(CoreHttpClient.INIT_PATH), params) }, + settings = settings, + storage = storage, + state = state, + connectivity = connectivity, + lifecycle = FakeAppLifecycle(), + hooks = object : InitHooks { + override fun onAnswer(answer: JSONObject, fresh: Boolean) = Unit + override fun onUsable() = Unit + }, + backoff = Backoff(baseMs = 60_000), + ) + core.init = init + releases += { core.release() } + init.start() + + val expect = case.getJSONObject("expect") + val expected = SdkState.valueOf(expect.getString("state").uppercase()) + // Every case moves the state its start set: the first answer decides. + assertNotEquals(name, expected, state.current) + runBlocking { withTimeout(5_000) { state.changes.first { it == expected } } } + assertEquals(name, expected, state.current) + if (expect.has("deviceId")) assertEquals(name, expect.getString("deviceId"), settings.getDid()) + if (expect.has("sessionId")) assertEquals(name, expect.getString("sessionId"), settings.getSid()) + if (expect.optBoolean("methodsWorkAsReady")) { + // A request goes at once instead of waiting for /init. + val answered = CountDownLatch(1) + core.send(NetworkMethod.GET("products/get"), JSONObject(), object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) = answered.countDown() + }) + assertTrue(name, answered.await(2, TimeUnit.SECONDS)) + } + if (expected == SdkState.REFUSED) assertTrue(name, storage.isRefused) + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt new file mode 100644 index 00000000..ee1cd72c --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt @@ -0,0 +1,626 @@ +package com.personalization.core.internal + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import com.personalization.LogLevel +import com.personalization.SdkLogHandler +import com.personalization.SdkLogMessage +import com.personalization.core.SdkError +import com.personalization.core.SdkState +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import com.personalization.sdk.data.repositories.preferences.PreferencesDataSourceImpl +import com.personalization.sdk.data.repositories.userSettings.UserSettingsRepositoryImpl +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.async +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.TestCoroutineScheduler +import org.json.JSONArray +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.io.File +import java.util.TimeZone +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.Executor + +/** + * The new core's `/init` (`core/init.md`): every row of the answer table, the pauses, the refused + * state across launches, decision 1, the saved answer, the wait for `ready`, the triggers and the + * stale answers. Time is the test scheduler's; the server is a fake that answers each `/init` when told. + */ +@OptIn(ExperimentalCoroutinesApi::class) +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class CoreInitTest { + + private lateinit var context: Context + private lateinit var storage: ShopStorage + private lateinit var settings: UserSettingsRepository + private val layout = StorageLayout(SHOP, null) + private val scheduler = TestCoroutineScheduler() + private val dispatchers = testCoreDispatchers(scheduler) + private val clock = FakeClock(System.currentTimeMillis()) + private val connectivity = FakeConnectivity(connected = true) + private val lifecycle = FakeAppLifecycle(foreground = true) + private val state = InstanceState(shopId = { SHOP }, dispatchers = dispatchers) + private val logged = CopyOnWriteArrayList() + private val defaultExecutor = SdkLogs.handlerExecutor + + /** What the fake server answers: queued answers first, otherwise a call that waits to be answered. */ + private val queued = ArrayDeque() + private val calls = mutableListOf>() + private val sent = mutableListOf() + private val sidAtSend = mutableListOf() + + private val answers = mutableListOf>() + private var usable = 0 + private val inits = mutableListOf() + + @Before + fun setUp() { + context = ApplicationProvider.getApplicationContext() + context.getSharedPreferences(layout.partitionName, Context.MODE_PRIVATE).edit().clear().commit() + File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() + storage = ShopStorage(context, layout) + settings = UserSettingsRepositoryImpl(PreferencesDataSourceImpl().apply { initialize(context, layout.partitionName, SHOP) }) + SdkLogs.reset() + SdkLogs.handlerExecutor = Executor { it.run() } + SdkLogs.register(SHOP, LogLevel.DEBUG, "SDK") + SdkLogs.handler = SdkLogHandler { logged += it } + } + + @After + fun tearDown() { + inits.forEach { it.release() } + SdkLogs.reset() + SdkLogs.handlerExecutor = defaultExecutor + } + + private fun init(setup: InitSetup = InitSetup(SHOP), timeZone: TimeZone = TimeZone.getTimeZone("UTC")): CoreInit = CoreInit( + setup = setup, + send = { params -> + sent += params + sidAtSend += settings.getSid() + queued.removeFirstOrNull() ?: CompletableDeferred().also { calls += it }.await() + }, + settings = settings, + storage = storage, + state = state, + connectivity = connectivity, + lifecycle = lifecycle, + hooks = object : InitHooks { + override fun onAnswer(answer: JSONObject, fresh: Boolean) { + answers += answer to fresh + } + + override fun onUsable() { + usable++ + } + }, + dispatchers = dispatchers, + clock = clock, + backoff = Backoff(random = { 0.5 }), + timeZone = { timeZone }, + ).also { inits += it } + + /** Answers the oldest `/init` still waiting. */ + private fun answer(result: HttpResult) { + val waiting = calls.firstOrNull { it.isActive } + if (waiting != null) waiting.complete(result) else queued += result + scheduler.runCurrent() + } + + private fun started(setup: InitSetup = InitSetup(SHOP)): CoreInit = init(setup).also { + it.start() + scheduler.runCurrent() + } + + private fun after(millis: Long) { + scheduler.advanceTimeBy(millis) + scheduler.runCurrent() + } + + private fun ok(did: String = "D1", seance: String = "S1") = HttpResult.Success(JSONObject().put("did", did).put("seance", seance)) + private fun http(code: Int, retryAfterMs: Long? = null) = HttpResult.Failure(SdkError.Http(code), null, retryAfterMs) + + private fun codes(code: String) = logged.filter { it.code == code } + + // The answer table. + + @Test + fun `a 2xx with a did is ready, keeps did and sid, saves the answer and clears a refusal of an earlier launch`() { + storage.isRefused = true + started() + + answer(ok("D1", "S1")) + + assertEquals(SdkState.READY, state.current) + assertEquals("D1", settings.getDid()) + assertEquals("S1", settings.getSid()) + assertEquals("D1", storage.initAnswer()?.getString("did")) + assertFalse(storage.isRefused) + assertEquals(listOf(true), answers.map { it.second }) + assertEquals(1, usable) + assertEquals(LogLevel.INFO, codes(LogCodes.INIT_SUCCEEDED).single().level) + } + + @Test + fun `no answer, a 2xx without a did, 5xx, 408, 410, 417, 426 and 429 are repeated`() { + val repeated = listOf( + HttpResult.Failure(SdkError.Network), + HttpResult.Failure(SdkError.Timeout), + HttpResult.Failure(SdkError.Decoding), + HttpResult.Success(JSONObject()), + HttpResult.Success(JSONObject().put("seance", "S")), + HttpResult.Success(JSONArray()), + http(500), http(502), http(503), http(504), + http(408), http(410), http(417), http(426), http(429), + ) + repeated.forEach { result -> + setUp() + val init = started() + answer(result) + + assertEquals("$result", SdkState.RETRYING, state.current) + assertFalse(storage.isRefused) + assertEquals(1, sent.size) + after(1_000) + assertEquals("$result is repeated after a second", 2, sent.size) + init.release() + sent.clear() + calls.clear() + queued.clear() + } + } + + @Test + fun `a 2xx without a did is a decoding failure in the log`() { + started() + + answer(HttpResult.Success(JSONObject().put("seance", "S"))) + + assertEquals(LogLevel.WARNING, codes(LogCodes.DECODING_FAILED).single().level) + assertEquals("", settings.getDid()) + } + + @Test + fun `402, 403 and 404 refuse the shop, kept on disk`() { + listOf(402, 403, 404).forEach { code -> + setUp() + logged.clear() + val init = started() + answer(http(code)) + + assertEquals(SdkState.REFUSED, state.current) + assertTrue(storage.isRefused) + val refused = codes(LogCodes.INIT_REFUSED).single() + assertEquals(LogLevel.ERROR, refused.level) + assertEquals(code, refused.context["status"]) + assertTrue(codes(LogCodes.INIT_FAILED).isEmpty()) + // Not asked again during the run. + after(60 * 60 * 1000L) + assertEquals(1, sent.size) + init.release() + sent.clear() + calls.clear() + } + } + + @Test + fun `another 4xx refuses the shop too, and is an error of the SDK's request`() { + listOf(400, 401, 405, 422, 451).forEach { code -> + setUp() + logged.clear() + val init = started() + answer(http(code)) + + assertEquals(SdkState.REFUSED, state.current) + assertTrue(storage.isRefused) + assertEquals(1, codes(LogCodes.INIT_REFUSED).size) + assertEquals(LogLevel.ERROR, codes(LogCodes.INIT_FAILED).single().level) + init.release() + sent.clear() + calls.clear() + } + } + + // Pauses. + + @Test + fun `repeats wait 1, 2, 4 and 8 seconds, logged as init_retry`() { + started() + answer(http(503)) + listOf(1_000L, 2_000L, 4_000L, 8_000L).forEachIndexed { index, pause -> + after(pause - 1) + assertEquals(index + 1, sent.size) + after(1) + assertEquals(index + 2, sent.size) + answer(if (index < 3) http(503) else ok()) + } + + assertEquals(SdkState.READY, state.current) + val retries = codes(LogCodes.INIT_RETRY) + assertEquals(4, retries.size) + assertTrue(retries.all { it.level == LogLevel.WARNING }) + assertEquals(listOf(1_000L, 2_000L, 4_000L, 8_000L), retries.map { it.context["delay_ms"] }) + } + + @Test + fun `the Retry-After of a 429 or a 503 sets the pause, of other answers it does not`() { + started() + answer(http(503, retryAfterMs = 30_000)) + after(29_999) + assertEquals(1, sent.size) + after(1) + assertEquals(2, sent.size) + + answer(http(429, retryAfterMs = 120_000)) + after(119_999) + assertEquals(2, sent.size) + after(1) + assertEquals(3, sent.size) + + // The third repeat: 4 s, whatever a 500 says. + answer(http(500, retryAfterMs = 600_000)) + after(4_000) + assertEquals(4, sent.size) + } + + // The refused state across launches. + + @Test + fun `a shop refused at a launch starts refused at the next and is cleared by a successful init`() { + started() + answer(http(403)) + inits.single().release() + + // The next launch. + val next = init() + next.start() + assertEquals(SdkState.REFUSED, state.current) + assertEquals(SdkError.ShopBlocked, awaitReady(next, 10_000)) + scheduler.runCurrent() + assertEquals(2, sent.size) + + // No answer keeps it refused while /init is repeated. + answer(HttpResult.Failure(SdkError.Timeout)) + assertEquals(SdkState.REFUSED, state.current) + after(1_000) + answer(ok()) + + assertEquals(SdkState.READY, state.current) + assertFalse(storage.isRefused) + assertEquals(null, awaitReady(next, 10_000)) + } + + @Test + fun `a saved answer does not outweigh a refusal`() { + storage.saveInitAnswer(JSONObject().put("did", "SAVED")) + started() + + answer(http(402)) + + assertEquals(SdkState.REFUSED, state.current) + assertEquals(SdkError.ShopBlocked, awaitReady(inits.single(), 10_000)) + } + + // Decision 1 and the saved answer. + + @Test + fun `a stored did without a saved answer is ready at once, and init still goes`() { + settings.updateDid("OLD") + val init = init() + + init.start() + + assertEquals(SdkState.READY, state.current) + assertEquals(1, usable) + scheduler.runCurrent() + assertEquals(1, sent.size) + answer(ok("OLD", "S2")) + assertEquals(SdkState.READY, state.current) + assertEquals("S2", settings.getSid()) + assertEquals(1, usable) + } + + @Test + fun `without an answer a device with a did retries and works as ready, told once`() { + settings.updateDid("OLD") + storage.saveInitAnswer(JSONObject().put("did", "OLD").put("popups_v3", true)) + val init = started() + + answer(HttpResult.Failure(SdkError.Network)) + after(1_000) + answer(HttpResult.Failure(SdkError.Timeout)) + + assertEquals(SdkState.RETRYING, state.current) + assertEquals(null, awaitReady(init, 1)) + assertEquals(1, codes(LogCodes.INIT_SAVED_ANSWER).size) + // The saved answer's flags, never its popup again. + assertEquals(listOf(false), answers.map { it.second }) + assertTrue(answers.single().first.optBoolean("popups_v3")) + assertEquals(1, usable) + } + + @Test + fun `the saved answer's did fills a shop without one and never replaces one`() { + storage.saveInitAnswer(JSONObject().put("did", "SAVED")) + started() + assertEquals("SAVED", settings.getDid()) + assertEquals(SdkState.READY, state.current) + inits.single().release() + + settings.updateDid("MINE") + started() + assertEquals("MINE", settings.getDid()) + } + + @Test + fun `a new device without an answer retries and waits`() { + val init = started() + + answer(HttpResult.Failure(SdkError.Timeout)) + + assertEquals(SdkState.RETRYING, state.current) + assertTrue(codes(LogCodes.INIT_SAVED_ANSWER).isEmpty()) + assertEquals(SdkError.Timeout, awaitReady(init, 10_000)) + assertEquals(0, usable) + } + + // The config. + + @Test + fun `the did of the config is taken only while the shop has none`() { + started(InitSetup(SHOP, deviceId = "FROM_RN")) + assertEquals("FROM_RN", settings.getDid()) + assertEquals(SdkState.READY, state.current) + inits.single().release() + + started(InitSetup(SHOP, deviceId = "ANOTHER")) + assertEquals("FROM_RN", settings.getDid()) + } + + @Test + fun `needReInitialization forgets the did, the session and the saved answer before init`() { + settings.updateDid("OLD") + settings.updateSid("OLD_SID") + storage.saveInitAnswer(JSONObject().put("did", "OLD")) + + started(InitSetup(SHOP, needReInitialization = true, deviceId = "IGNORED")) + + assertEquals(SdkState.INITIALIZING, state.current) + assertEquals("", settings.getDid()) + assertEquals(listOf(""), sidAtSend) + assertNull(storage.initAnswer()) + assertEquals(LogLevel.INFO, codes(LogCodes.DEVICE_ID_RESET).single().level) + answer(ok("NEW", "S")) + assertEquals("NEW", settings.getDid()) + } + + @Test + fun `init carries the offset of now, summer time included, and the extras`() { + val berlin = TimeZone.getTimeZone("Europe/Berlin") + clock.now = 1_782_000_000_000L // July 2026 + init(InitSetup(SHOP, extraParams = { mapOf("testmode" to "true") }), berlin).start() + scheduler.runCurrent() + clock.now = 1_768_000_000_000L // January 2026 + answer(HttpResult.Failure(SdkError.Timeout)) + after(1_000) + + assertEquals(listOf("2", "1"), sent.map { it.getString("tz") }) + assertTrue(sent.all { it.getString("testmode") == "true" }) + assertTrue(sent.none { it.has("stream") || it.has("android_advertising_id") }) + } + + @Test + fun `the advertising id goes only with sendAdvertisingId, read once`() { + var reads = 0 + started(InitSetup(SHOP, advertisingId = { reads++; "gaid-1" })) + answer(HttpResult.Failure(SdkError.Timeout)) + after(1_000) + answer(ok()) + + assertEquals(listOf("gaid-1", "gaid-1"), sent.map { it.getString("android_advertising_id") }) + assertEquals(1, reads) + assertEquals(1, codes(LogCodes.ADVERTISING_ID_SENT).size) + } + + @Test + fun `on a pre-launch report device init is skipped`() { + settings.updateDid("OLD") + started(InitSetup(SHOP, skipInit = { true })) + + assertTrue(sent.isEmpty()) + assertEquals(SdkState.READY, state.current) + assertEquals(LogLevel.INFO, codes(LogCodes.INIT_SKIPPED).single().level) + } + + // Waiting for ready. + + @Test + fun `a request waits for ready within its timeout`() { + val init = started() + val waiting = waitReady(init, 10_000) + + after(5_000) + assertFalse(waiting.isCompleted) + answer(ok()) + + assertEquals(null, waiting.getCompleted()) + } + + @Test + fun `a wait that runs out is a timeout, a refusal is shopBlocked, a release notInitialized`() { + val init = started() + val timedOut = waitReady(init, 10_000) + after(10_000) + assertEquals(SdkError.Timeout, timedOut.getCompleted()) + + val blocked = waitReady(init, 10_000) + answer(http(402)) + assertEquals(SdkError.ShopBlocked, blocked.getCompleted()) + + setUp() + val other = started() + val released = waitReady(other, 10_000) + other.release() + scheduler.runCurrent() + assertEquals(SdkError.NotInitialized, released.getCompleted()) + } + + // The network. + + @Test + fun `without a network and a did init waits for the network, not for the pause`() { + connectivity.set(false) + started() + answer(HttpResult.Failure(SdkError.Network)) + + after(30 * 60 * 1000L) + assertEquals(1, sent.size) + assertEquals(SdkState.RETRYING, state.current) + + connectivity.set(true) + scheduler.runCurrent() + assertEquals(2, sent.size) + } + + @Test + fun `with a did and no network init repeats on the pause, or as soon as the network is back`() { + settings.updateDid("OLD") + connectivity.set(false) + started() + answer(HttpResult.Failure(SdkError.Network)) + after(1_000) + assertEquals(2, sent.size) + + answer(HttpResult.Failure(SdkError.Network)) + after(500) + connectivity.set(true) + scheduler.runCurrent() + assertEquals(3, sent.size) + } + + // Later /init. + + @Test + fun `coming back after more than two hours sends init, without the ended session`() { + started() + answer(ok("D1", "S1")) + lifecycle.set(false) + // The window runs from the last activity, which the session's start recorded. + clock.now = settings.getSidLastActTime() + + clock.advance(CoreInit.SESSION_WINDOW_MS - 1) + lifecycle.set(true) + scheduler.runCurrent() + assertEquals(1, sent.size) + + lifecycle.set(false) + clock.advance(2) + lifecycle.set(true) + scheduler.runCurrent() + assertEquals(2, sent.size) + assertEquals("", sidAtSend.last()) + answer(ok("D1", "S2")) + assertEquals("S2", settings.getSid()) + } + + @Test + fun `a refused shop is not asked again on coming back`() { + started() + answer(http(403)) + lifecycle.set(false) + clock.advance(CoreInit.SESSION_WINDOW_MS + 1) + lifecycle.set(true) + scheduler.runCurrent() + + assertEquals(1, sent.size) + } + + @Test + fun `checkShop sends init at most once in ten minutes`() { + val init = started() + assertFalse("init is on its way", init.checkShop()) + answer(ok()) + + assertTrue(init.checkShop()) + scheduler.runCurrent() + assertEquals(2, sent.size) + answer(ok()) + clock.advance(CoreInit.CHECK_SHOP_INTERVAL_MS - 1) + assertFalse(init.checkShop()) + clock.advance(1) + assertTrue(init.checkShop()) + scheduler.runCurrent() + answer(http(403)) + clock.advance(CoreInit.CHECK_SHOP_INTERVAL_MS) + assertFalse("refused", init.checkShop()) + } + + // Restart and release. + + @Test + fun `an answer to an init sent before a restart is not taken`() { + val init = started() + val stale = calls.single() + + init.holdForRestart() + assertEquals(SdkState.INITIALIZING, state.current) + stale.complete(ok("STALE")) + scheduler.runCurrent() + assertEquals("", settings.getDid()) + + init.restart() + scheduler.runCurrent() + answer(ok("FRESH")) + assertEquals("FRESH", settings.getDid()) + assertEquals(SdkState.READY, state.current) + assertEquals(1, usable) + } + + @Test + fun `after release nothing is sent and nothing is taken`() { + val init = started() + init.release() + scheduler.runCurrent() + + calls.single().complete(ok()) + scheduler.runCurrent() + lifecycle.set(false) + clock.advance(CoreInit.SESSION_WINDOW_MS + 1) + lifecycle.set(true) + after(60 * 60 * 1000L) + + assertEquals(1, sent.size) + assertEquals("", settings.getDid()) + assertFalse(init.checkShop()) + assertEquals(SdkError.NotInitialized, awaitReady(init, 10_000)) + } + + private fun waitReady(init: CoreInit, timeoutMs: Long) = + CoroutineScope(StandardTestDispatcher(scheduler)).async { init.awaitReady(timeoutMs) }.also { scheduler.runCurrent() } + + private fun awaitReady(init: CoreInit, timeoutMs: Long): SdkError? { + val waiting = waitReady(init, timeoutMs) + scheduler.advanceTimeBy(timeoutMs) + scheduler.runCurrent() + return waiting.getCompleted() + } + + private companion object { + const val SHOP = "init-shop" + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/InstanceStateTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/InstanceStateTest.kt new file mode 100644 index 00000000..3d5556df --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/InstanceStateTest.kt @@ -0,0 +1,134 @@ +package com.personalization.core.internal + +import android.os.Looper +import com.personalization.LogLevel +import com.personalization.SdkLogHandler +import com.personalization.SdkLogMessage +import com.personalization.core.SdkState +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.Executor + +/** The state's listeners: on the main thread, once per change, in order, a throwing one logged. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class InstanceStateTest { + + private val logged = CopyOnWriteArrayList() + private val defaultExecutor = SdkLogs.handlerExecutor + + /** On the real main dispatcher: the main looper delivers. */ + private val state = InstanceState(shopId = { "shop" }) + + @Before + fun setUp() { + SdkLogs.reset() + SdkLogs.handlerExecutor = Executor { it.run() } + SdkLogs.register("shop", LogLevel.DEBUG, "SDK") + SdkLogs.handler = SdkLogHandler { logged += it } + } + + @After + fun tearDown() { + SdkLogs.reset() + SdkLogs.handlerExecutor = defaultExecutor + } + + private fun idle() = shadowOf(Looper.getMainLooper()).idle() + + @Test + fun `each change reaches a listener once, on the main thread, in order`() { + val heard = mutableListOf>() + state.addListener { heard += it to (Looper.myLooper() == Looper.getMainLooper()) } + + // From a background thread, as the cores move it. + val done = CountDownLatch(1) + Thread { + state.set(SdkState.INITIALIZING) + state.set(SdkState.INITIALIZING) + state.set(SdkState.RETRYING) + state.set(SdkState.READY) + done.countDown() + }.start() + done.await() + idle() + + assertEquals( + listOf(SdkState.INITIALIZING to true, SdkState.RETRYING to true, SdkState.READY to true), + heard, + ) + assertEquals(SdkState.READY, state.current) + } + + @Test + fun `the same state again is no change`() { + val heard = mutableListOf() + state.addListener { heard += it } + + assertTrue(state.set(SdkState.READY)) + assertFalse(state.set(SdkState.READY)) + idle() + + assertEquals(listOf(SdkState.READY), heard) + } + + @Test + fun `a listener that throws is logged and keeps hearing, the others are not disturbed`() { + val others = mutableListOf() + var calls = 0 + state.addListener { + calls++ + throw IllegalStateException("app") + } + state.addListener { others += it } + + state.set(SdkState.INITIALIZING) + state.set(SdkState.READY) + idle() + + assertEquals(2, calls) + assertEquals(listOf(SdkState.INITIALIZING, SdkState.READY), others) + val failures = logged.filter { it.code == LogCodes.CALLBACK_FAILED } + assertEquals(2, failures.size) + assertTrue(failures.all { it.level == LogLevel.ERROR && it.shopId == "shop" }) + } + + @Test + fun `a cancelled listener hears nothing more, even a change already on its way`() { + val heard = mutableListOf() + val handle = state.addListener { heard += it } + + state.set(SdkState.INITIALIZING) + handle.cancel() + state.set(SdkState.READY) + idle() + + assertEquals(emptyList(), heard) + } + + @Test + fun `dropped listeners still hear the change made before`() { + val heard = mutableListOf() + state.addListener { heard += it } + + state.set(SdkState.READY) + state.set(SdkState.NOT_INITIALIZED) + state.dropListeners() + state.set(SdkState.INITIALIZING) + idle() + + assertEquals(listOf(SdkState.READY, SdkState.NOT_INITIALIZED), heard) + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ProcessAppLifecycleTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ProcessAppLifecycleTest.kt new file mode 100644 index 00000000..8398a490 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ProcessAppLifecycleTest.kt @@ -0,0 +1,74 @@ +package com.personalization.core.internal + +import android.os.Looper +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.LifecycleRegistry +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import java.util.concurrent.CountDownLatch + +/** The app's foreground from the process lifecycle: changes only, never the replay of the current state. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class ProcessAppLifecycleTest { + + private class Owner : LifecycleOwner { + val registry = LifecycleRegistry(this) + override val lifecycle: Lifecycle get() = registry + } + + private val owner = Owner() + private val appLifecycle = ProcessAppLifecycle { owner } + + @Test + fun `a listener added in the foreground hears the trip to the background and back, not the replay`() { + owner.registry.currentState = Lifecycle.State.RESUMED + val heard = mutableListOf() + + appLifecycle.addListener { heard += it } + assertTrue(appLifecycle.isForeground) + owner.registry.currentState = Lifecycle.State.CREATED + owner.registry.currentState = Lifecycle.State.RESUMED + + assertEquals(listOf(false, true), heard) + } + + @Test + fun `an instance made in the background hears the first trip to the foreground`() { + owner.registry.currentState = Lifecycle.State.CREATED + val heard = mutableListOf() + + appLifecycle.addListener { heard += it } + assertFalse(appLifecycle.isForeground) + owner.registry.currentState = Lifecycle.State.STARTED + + assertEquals(listOf(true), heard) + } + + @Test + fun `a listener added off the main thread is attached on it, and cancelled stops hearing`() { + owner.registry.currentState = Lifecycle.State.STARTED + val heard = mutableListOf() + val added = CountDownLatch(1) + var handle: com.personalization.Cancellable? = null + Thread { + handle = appLifecycle.addListener { heard += it } + added.countDown() + }.start() + added.await() + shadowOf(Looper.getMainLooper()).idle() + + owner.registry.currentState = Lifecycle.State.CREATED + handle?.cancel() + owner.registry.currentState = Lifecycle.State.STARTED + + assertEquals(listOf(false), heard) + } +} From 138ade4b160c49294d01674db41de624bc19a88d Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 00:12:41 +0400 Subject: [PATCH 032/109] test(sdk): the storage tests wait for the legacy path's requests to be answered instead of sleeping and polling: its threads come from a seam a test can wait on, and a slow /init is held by the test and let go when it should land --- .../core/internal/LegacyCore.kt | 21 +++++++---- .../com/personalization/SdkStorageTest.kt | 37 +++++++++++++------ .../core/internal/CoreFakes.kt | 34 +++++++++++++++++ 3 files changed, 73 insertions(+), 19 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt index 7851b4bd..fecbe365 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt @@ -23,6 +23,7 @@ import java.net.ConnectException import java.net.HttpURLConnection import java.net.URL import java.nio.charset.StandardCharsets +import java.util.concurrent.Executor /** * Today's request path, unchanged: `HttpURLConnection` on a thread per request. Any `2xx` is a @@ -51,7 +52,7 @@ internal class LegacyCore( // Off the caller's thread, as every answer of this path. if (listener != null) { val error = LegacyErrors.of(SdkError.NotInitialized) - Thread { delivery.failure(listener, error.code, error.message) }.start() + threads.execute { delivery.failure(listener, error.code, error.message) } } return } @@ -74,7 +75,7 @@ internal class LegacyCore( params: JSONObject, listener: OnApiCallbackListener? ) { - val thread = Thread { + threads.execute { val startTime = System.currentTimeMillis() val requestBodyForLog = if (networkMethod is NetworkMethod.POST) params.toString() else null var logUrl = "" @@ -148,8 +149,6 @@ internal class LegacyCore( } } } - - thread.start() } private sealed interface Outcome { @@ -248,9 +247,15 @@ internal class LegacyCore( SdkLogs.log(level, code, message, shopId, context, throwable) } - private companion object { - const val CONNECT_TIMEOUT_MS = 5_000 - const val READ_TIMEOUT_MS = 10_000 - val HTTP_SUCCESS = 200..299 + internal companion object { + private const val CONNECT_TIMEOUT_MS = 5_000 + private const val READ_TIMEOUT_MS = 10_000 + private val HTTP_SUCCESS = 200..299 + + internal val DEFAULT_THREADS = Executor { Thread(it).start() } + + /** Runs each request and each answer of the path: a thread apiece. Tests pass one they can wait on. */ + @Volatile + internal var threads: Executor = DEFAULT_THREADS } } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt index 3aa4a72d..49ee998b 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt @@ -9,6 +9,8 @@ import com.personalization.core.SdkException import com.personalization.core.SdkResult import com.personalization.core.internal.CoreSelection import com.personalization.core.internal.DeviceStorage +import com.personalization.core.internal.LegacyCore +import com.personalization.core.internal.TrackingThreads import com.personalization.core.internal.ShopStorage import com.personalization.core.internal.StorageLayout import com.personalization.core.internal.StorageMigrations @@ -31,7 +33,6 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner -import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config import java.io.File import java.util.concurrent.CopyOnWriteArrayList @@ -53,6 +54,9 @@ class SdkStorageTest { private lateinit var context: Context private val server = MockWebServer() private val inits = CopyOnWriteArrayList() + + /** The legacy path's requests run here, so a test waits until they are answered instead of sleeping. */ + private val threads = TrackingThreads() private val initCount = AtomicInteger() private val tokens = LinkedBlockingQueue() private val logged = CopyOnWriteArrayList() @@ -80,6 +84,7 @@ class SdkStorageTest { context.getSharedPreferences(it, Context.MODE_PRIVATE).edit().clear().commit() } File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() + LegacyCore.threads = threads server.dispatcher = object : Dispatcher() { override fun dispatch(request: RecordedRequest): MockResponse { val url = request.requestUrl ?: return MockResponse().setResponseCode(400) @@ -97,6 +102,7 @@ class SdkStorageTest { fun tearDown() { SdkRegistry.all().forEach { it.release() } server.shutdown() + LegacyCore.threads = LegacyCore.DEFAULT_THREADS CoreSelection.reset() SdkRegistry.reset() Rees46.reset() @@ -113,15 +119,14 @@ class SdkStorageTest { storageKey = storageKey, ) + /** Every `/init` sent so far has been answered and its answer handled. */ private fun awaitInits(count: Int) { - val deadline = System.currentTimeMillis() + 5_000 - while (inits.size < count && System.currentTimeMillis() < deadline) Thread.sleep(10) - assertTrue("expected $count /init, got $inits", inits.size >= count) + threads.awaitIdle() + assertEquals("/init sent: $inits", count, inits.size) } private fun awaitDid(sdk: SDK, expected: String) { - val deadline = System.currentTimeMillis() + 5_000 - while (sdk.getDid() != expected && System.currentTimeMillis() < deadline) Thread.sleep(10) + threads.awaitIdle() assertEquals(expected, sdk.getDid()) } @@ -177,19 +182,26 @@ class SdkStorageTest { @Test fun `an init answer for the customer before the deletion is not taken`() { - // A new device: its first /init is slow, and the deletion starts over before it answers. + // A new device: its first /init is held, and the deletion starts over before it answers. + val firstArrived = CountDownLatch(1) + val answerFirst = CountDownLatch(1) initAnswer = { n, _ -> + if (n == 1) { + firstArrived.countDown() + answerFirst.await(5, TimeUnit.SECONDS) + } val did = if (n == 1) "stale-did" else "fresh-did" - MockResponse().setBody("""{"did":"$did","seance":"S$n"}""").apply { if (n == 1) setHeadersDelay(1_000, TimeUnit.MILLISECONDS) } + MockResponse().setBody("""{"did":"$did","seance":"S$n"}""") } val sdk = Rees46.initialize(context, config()) - awaitInits(1) + assertTrue(firstArrived.await(5, TimeUnit.SECONDS)) runBlocking { sdk.deleteUserCredentials() } + answerFirst.countDown() + // Both answers handled, the late one last. awaitDid(sdk, "fresh-did") - Thread.sleep(1_500) - assertEquals("fresh-did", sdk.getDid()) + assertEquals(listOf("", ""), inits) } @Test @@ -246,6 +258,9 @@ class SdkStorageTest { assertEquals("legacy-did", sdk.getDid()) runBlocking { sdk.deleteUserCredentials() } + // The new customer's /init is done, and nothing writes to the partition any more. + awaitDid(sdk, "new-did") + sdk.release() val legacy = context.getSharedPreferences(PreferencesPartition.LEGACY_KEY, Context.MODE_PRIVATE) assertEquals(setOf("shop_id", "segment_ab_testing"), legacy.all.keys) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt index 9f3056ce..20dab6a4 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt @@ -59,3 +59,37 @@ internal fun testCoreDispatchers(scheduler: TestCoroutineScheduler = TestCorouti val dispatcher = StandardTestDispatcher(scheduler) return CoreDispatchers(io = dispatcher, main = { dispatcher }, default = dispatcher) } + +/** + * Threads for the legacy path ([LegacyCore.threads]) that a test can wait on: [awaitIdle] returns once + * every request started so far has had its answer delivered, the requests those answers started too. + */ +internal class TrackingThreads : java.util.concurrent.Executor { + private val lock = Object() + private var running = 0 + + override fun execute(command: Runnable) { + synchronized(lock) { running++ } + Thread { + try { + command.run() + } finally { + synchronized(lock) { + running-- + lock.notifyAll() + } + } + }.start() + } + + fun awaitIdle(timeoutMs: Long = 10_000) { + val deadline = System.currentTimeMillis() + timeoutMs + synchronized(lock) { + while (running > 0) { + val left = deadline - System.currentTimeMillis() + check(left > 0) { "$running requests of the legacy path still running" } + lock.wait(left) + } + } + } +} From a98821d93df9be1f8399a920d3f25d96ba46435a Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 00:52:43 +0400 Subject: [PATCH 033/109] fix(core): onStateChange calls a new listener at once with the current state, on the main thread, then with every change; the current state is scheduled under the lock a change takes, so a change made meanwhile arrives after it, never before --- .../main/kotlin/com/personalization/SDK.kt | 8 ++-- .../core/internal/InstanceState.kt | 20 +++++--- .../com/personalization/SdkInstanceTest.kt | 2 +- .../core/internal/InstanceStateTest.kt | 47 ++++++++++++++++--- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 70a8e816..c92b385d 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -227,10 +227,10 @@ open class SDK { get() = instanceState.current /** - * Calls [listener] on the main thread with every later change of [state], once per change, until - * the returned handle is cancelled. May be set before the instance is initialized. An exception - * the listener throws is logged as `callback_failed`. [release] tells it [SdkState.NOT_INITIALIZED] - * and drops it. + * Calls [listener] on the main thread with the current [state] at once, then with every change of + * it, once per change, until the returned handle is cancelled. May be set before the instance is + * initialized (it hears [SdkState.NOT_INITIALIZED] first). An exception the listener throws is logged + * as `callback_failed`. [release] tells it [SdkState.NOT_INITIALIZED] and drops it. */ fun onStateChange(listener: (SdkState) -> Unit): Cancellable = instanceState.addListener(listener) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/InstanceState.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/InstanceState.kt index 8f3a8346..b1f43ab6 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/InstanceState.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/InstanceState.kt @@ -18,9 +18,9 @@ import java.util.concurrent.CopyOnWriteArrayList * The [SdkState] of one instance and the app's listeners of it, for both cores: the legacy path moves * it from `RegisterManager`, the new core from [CoreInit]. * - * Each change reaches every listener once, on the main thread, in the order the changes happened. A - * listener that throws is logged as `callback_failed` and keeps its later changes; a cancelled one - * gets nothing more. + * A new listener hears the current state at once, then each change once, on the main thread, in the + * order the changes happened. A listener that throws is logged as `callback_failed` and keeps its + * later changes; a cancelled one gets nothing more. */ internal class InstanceState( private val shopId: () -> String?, @@ -31,7 +31,7 @@ internal class InstanceState( private val flow = MutableStateFlow(SdkState.NOT_INITIALIZED) private val listeners = CopyOnWriteArrayList() - /** Created on the first change with a listener: a process without one never loads the main dispatcher. */ + /** Created with the first listener: a process without one never loads the main dispatcher. */ private val delivery: CoroutineScope by lazy { CoroutineScope(SupervisorJob() + dispatchers.main + CoroutineExceptionHandler { _, throwable -> SdkLogs.log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "A state listener could not be called", shopId(), throwable = throwable) @@ -54,10 +54,18 @@ internal class InstanceState( true } - /** Calls [listener] on the main thread with every later change until the handle is cancelled. */ + /** + * Calls [listener] on the main thread with the current state at once, then with every later change + * until the handle is cancelled. The current state is scheduled under the lock a change takes, so + * a change made meanwhile arrives after it, never before. + */ fun addListener(listener: (SdkState) -> Unit): Cancellable { val subscription = Subscription(listener) - listeners += subscription + synchronized(lock) { + listeners += subscription + val current = flow.value + delivery.launch { subscription.deliver(current) } + } return Cancellable { subscription.active = false listeners -= subscription diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt index 42568ed5..153e4744 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt @@ -331,7 +331,7 @@ class SdkInstanceTest { awaitState(sdk, SdkState.READY) shadowOf(Looper.getMainLooper()).idle() - assertEquals(listOf(SdkState.INITIALIZING to true, SdkState.READY to true), heard) + assertEquals(listOf(SdkState.NOT_INITIALIZED to true, SdkState.INITIALIZING to true, SdkState.READY to true), heard) assertEquals("new-did", sdk.deviceId) assertEquals("S", sdk.sessionId) assertEquals(SHOP, sdk.shopId) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/InstanceStateTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/InstanceStateTest.kt index 3d5556df..cb7267c9 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/InstanceStateTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/InstanceStateTest.kt @@ -21,7 +21,7 @@ import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.CountDownLatch import java.util.concurrent.Executor -/** The state's listeners: on the main thread, once per change, in order, a throwing one logged. */ +/** The state's listeners: the current state at once, then on the main thread once per change, in order, a throwing one logged. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [28]) class InstanceStateTest { @@ -66,12 +66,44 @@ class InstanceStateTest { idle() assertEquals( - listOf(SdkState.INITIALIZING to true, SdkState.RETRYING to true, SdkState.READY to true), + listOf(SdkState.NOT_INITIALIZED to true, SdkState.INITIALIZING to true, SdkState.RETRYING to true, SdkState.READY to true), heard, ) assertEquals(SdkState.READY, state.current) } + @Test + fun `a new listener hears the current state at once on the main thread, then the changes`() { + state.set(SdkState.RETRYING) + idle() + val heard = mutableListOf>() + + state.addListener { heard += it to (Looper.myLooper() == Looper.getMainLooper()) } + assertEquals("delivered on the main thread, not inside addListener", emptyList>(), heard) + idle() + assertEquals(listOf(SdkState.RETRYING to true), heard) + + state.set(SdkState.READY) + idle() + assertEquals(listOf(SdkState.RETRYING to true, SdkState.READY to true), heard) + } + + @Test + fun `a change made right after adding a listener comes after the current state, never before`() { + val heard = CopyOnWriteArrayList() + val done = CountDownLatch(1) + Thread { + state.addListener { heard += it } + state.set(SdkState.INITIALIZING) + state.set(SdkState.READY) + done.countDown() + }.start() + done.await() + idle() + + assertEquals(listOf(SdkState.NOT_INITIALIZED, SdkState.INITIALIZING, SdkState.READY), heard) + } + @Test fun `the same state again is no change`() { val heard = mutableListOf() @@ -81,7 +113,7 @@ class InstanceStateTest { assertFalse(state.set(SdkState.READY)) idle() - assertEquals(listOf(SdkState.READY), heard) + assertEquals(listOf(SdkState.NOT_INITIALIZED, SdkState.READY), heard) } @Test @@ -98,10 +130,10 @@ class InstanceStateTest { state.set(SdkState.READY) idle() - assertEquals(2, calls) - assertEquals(listOf(SdkState.INITIALIZING, SdkState.READY), others) + assertEquals(3, calls) + assertEquals(listOf(SdkState.NOT_INITIALIZED, SdkState.INITIALIZING, SdkState.READY), others) val failures = logged.filter { it.code == LogCodes.CALLBACK_FAILED } - assertEquals(2, failures.size) + assertEquals(3, failures.size) assertTrue(failures.all { it.level == LogLevel.ERROR && it.shopId == "shop" }) } @@ -111,6 +143,7 @@ class InstanceStateTest { val handle = state.addListener { heard += it } state.set(SdkState.INITIALIZING) + // Cancelled before the main thread ran: not even the current state arrives. handle.cancel() state.set(SdkState.READY) idle() @@ -129,6 +162,6 @@ class InstanceStateTest { state.set(SdkState.INITIALIZING) idle() - assertEquals(listOf(SdkState.READY, SdkState.NOT_INITIALIZED), heard) + assertEquals(listOf(SdkState.NOT_INITIALIZED, SdkState.READY, SdkState.NOT_INITIALIZED), heard) } } From 844d5f33bc59f796048dab7d0e8e59d092217247 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 00:52:52 +0400 Subject: [PATCH 034/109] fix(sdk): the forms of PreferencesDataSource.initialize, PreferencesRepository.initialize and InitPreferencesUseCase.invoke with legacyPreferencesKey are back, deprecated: code built against earlier releases keeps compiling and linking, and they open the shop's partition as the current forms do; the key itself is not read, the SDK migrates the store of older releases on its own --- .../preferences/PreferencesDataSource.kt | 15 +++++ .../repositories/PreferencesRepository.kt | 15 +++++ .../preferences/InitPreferencesUseCase.kt | 15 +++++ .../preferences/PreferencesBackCompatTest.kt | 58 +++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesBackCompatTest.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt index 24008e31..2b6f9f91 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt @@ -14,6 +14,21 @@ interface PreferencesDataSource { shopId: String? = null ) + /** + * The form of earlier releases. [legacyPreferencesKey] is not read any more: the store of older + * releases is read into the partition before, by the SDK's own migration. + */ + @Deprecated( + message = "legacyPreferencesKey is ignored: the SDK migrates the store of older releases itself.", + replaceWith = ReplaceWith("initialize(context, preferencesKey, shopId)") + ) + fun initialize( + context: Context, + preferencesKey: String, + legacyPreferencesKey: String?, + shopId: String? = null + ) = initialize(context = context, preferencesKey = preferencesKey, shopId = shopId) + fun getPushToken(provider: String): String fun savePushToken(provider: String, value: String) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/repositories/PreferencesRepository.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/repositories/PreferencesRepository.kt index 15e1ff21..127ad7b8 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/repositories/PreferencesRepository.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/repositories/PreferencesRepository.kt @@ -10,6 +10,21 @@ interface PreferencesRepository { shopId: String? = null ) + /** + * The form of earlier releases. [legacyPreferencesKey] is not read any more: the SDK reads the store + * of older releases into the shop's partition itself, once, before this is called. + */ + @Deprecated( + message = "legacyPreferencesKey is ignored: the SDK migrates the store of older releases itself.", + replaceWith = ReplaceWith("initialize(context, preferencesKey, shopId)") + ) + fun initialize( + context: Context, + preferencesKey: String, + legacyPreferencesKey: String?, + shopId: String? = null + ) = initialize(context = context, preferencesKey = preferencesKey, shopId = shopId) + fun getPushToken(provider: String): String fun savePushToken(provider: String, value: String) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/preferences/InitPreferencesUseCase.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/preferences/InitPreferencesUseCase.kt index 9f1e0de8..a9a77ac6 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/preferences/InitPreferencesUseCase.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/preferences/InitPreferencesUseCase.kt @@ -19,4 +19,19 @@ class InitPreferencesUseCase @Inject constructor( shopId = shopId ) } + + /** + * The form of earlier releases. [legacyPreferencesKey] is not read any more: the SDK reads the store + * of older releases into the shop's partition itself, once, before this is called. + */ + @Deprecated( + message = "legacyPreferencesKey is ignored: the SDK migrates the store of older releases itself.", + replaceWith = ReplaceWith("invoke(context, preferencesKey, shopId)") + ) + fun invoke( + context: Context, + preferencesKey: String, + legacyPreferencesKey: String?, + shopId: String? = null + ) = invoke(context = context, preferencesKey = preferencesKey, shopId = shopId) } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesBackCompatTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesBackCompatTest.kt new file mode 100644 index 00000000..b853d19a --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesBackCompatTest.kt @@ -0,0 +1,58 @@ +package com.personalization.sdk.data.repositories.preferences + +import android.content.Context +import com.personalization.sdk.domain.repositories.PreferencesRepository +import com.personalization.sdk.domain.usecases.preferences.InitPreferencesUseCase +import io.mockk.mockk +import io.mockk.verify +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** + * The forms with `legacyPreferencesKey` that earlier releases had stay, deprecated: code built against + * them keeps compiling and running, and they open the partition as the current forms do. + */ +@Suppress("DEPRECATION") +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class PreferencesBackCompatTest { + + private val context = mockk(relaxed = true) + + @Test + fun `the data source and the repository take the old form and open the partition`() { + val source = mockk(relaxed = true) + val repository: PreferencesRepository = PreferencesRepositoryImpl(source) + + repository.initialize(context, "partition", "old_key", "shop") + repository.initialize(context = context, preferencesKey = "partition", legacyPreferencesKey = null) + + verify(exactly = 1) { source.initialize(context, "partition", "shop") } + verify(exactly = 1) { source.initialize(context, "partition", null) } + } + + @Test + fun `the use case takes the old form and opens the partition`() { + val repository = mockk(relaxed = true) + + InitPreferencesUseCase(repository).invoke(context, "partition", "old_key", "shop") + + verify(exactly = 1) { repository.initialize(context, "partition", "shop") } + } + + @Test + fun `the data source's old form opens the partition`() { + val source = PreferencesDataSourceImpl() + val real = androidx.test.core.app.ApplicationProvider.getApplicationContext() + + source.initialize(real, "back_compat_partition", "old_key", "shop") + source.saveValue("probe", "value") + + org.junit.Assert.assertEquals( + "value", + real.getSharedPreferences("back_compat_partition", Context.MODE_PRIVATE).getString("probe", null), + ) + } +} From 79b3ec1c92be2aa947d2d3d0029f937a90087b58 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 00:53:03 +0400 Subject: [PATCH 035/109] fix(core): the new core's /init carries no segment, as core/init.md lists its fields; the A/B group stays on every other request --- .../core/internal/CoreHttpClient.kt | 5 ++++- .../core/internal/CoreHttpClientTest.kt | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt index f3d99ac8..a15025c2 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt @@ -71,7 +71,8 @@ internal object CoreHttp { * method, the path and the code, never the query, a body or the answer. An answer's `Retry-After` comes * back with the failure, for the caller that repeats ([CoreInit]). * - * `/init` carries `did`, `sid` and `seance` only when they have a value: an empty `did` is not a device. + * `/init` carries `did`, `sid` and `seance` only when they have a value (an empty `did` is not a + * device), and no `segment`. */ internal class CoreHttpClient( private val baseUrl: () -> String, @@ -126,6 +127,8 @@ internal class CoreHttpClient( val fields = CommonFields.addTo(params, settings) if (method.method == INIT_PATH) { INIT_OPTIONAL_FIELDS.forEach { key -> if (fields.optString(key).isEmpty()) fields.remove(key) } + // `/init` does not read the A/B group (core/init.md): it is the events' field. + fields.remove(UserBasicParams.SEGMENT) } val url = (baseUrl() + method.method).toHttpUrl() val builder = Request.Builder() diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt index d80b711e..3311c2bd 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt @@ -209,7 +209,7 @@ class CoreHttpClientTest { } @Test - fun `init leaves out a did and a session it does not have, and carries the stream of the config`() { + fun `init leaves out a did and a session it does not have and the segment, and carries the stream of the config`() { every { settings.getDid() } returns "" every { settings.getSid() } returns "" every { settings.getStream() } returns "flutter" @@ -218,10 +218,20 @@ class CoreHttpClientTest { send(NetworkMethod.GET("init"), JSONObject().put("tz", "3")) val url = checkNotNull(server.takeRequest().requestUrl) - assertEquals(setOf("tz", "shop_id", "segment", "stream"), url.queryParameterNames) + assertEquals(setOf("tz", "shop_id", "stream"), url.queryParameterNames) assertEquals("flutter", url.queryParameter("stream")) } + @Test + fun `init carries a did and a session it has, still without the segment`() { + reply(200, "{}") + + send(NetworkMethod.GET("init"), JSONObject()) + + val url = checkNotNull(server.takeRequest().requestUrl) + assertEquals(setOf("shop_id", "did", "sid", "seance", "stream"), url.queryParameterNames) + } + @Test fun `the Retry-After of an answer comes back with it, in seconds or as a date`() { server.enqueue(MockResponse().setResponseCode(503).setHeader("Retry-After", "30")) From c30a97dbd999ea8b654abe988e2a83f6217a354d Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 00:53:10 +0400 Subject: [PATCH 036/109] =?UTF-8?q?fix(core):=20a=20Retry-After=20pause=20?= =?UTF-8?q?is=20kept=20between=201=20s=20and=205=20min,=20for=20/init=20as?= =?UTF-8?q?=20for=20any=20repeat:=200=20and=20a=20date=20already=20past=20?= =?UTF-8?q?wait=20a=20second,=20an=20hour=20or=20a=20number=20too=20large?= =?UTF-8?q?=20for=20milliseconds=20five=20minutes;=20the=20header=20may=20?= =?UTF-8?q?hold=20seconds=20(whole=20or=20fractional)=20or=20an=20HTTP=20d?= =?UTF-8?q?ate,=20and=20a=20negative=20value,=20NaN=20or=20Infinity=20neve?= =?UTF-8?q?r=20throws;=20the=20=C2=B120=20%=20spread=20no=20longer=20takes?= =?UTF-8?q?=20a=20pause=20past=205=20min;=20OkHttp=20no=20longer=20repeats?= =?UTF-8?q?=20a=20408=20or=20a=20503=20with=20Retry-After:=200=20by=20itse?= =?UTF-8?q?lf,=20nor=20throws=20on=20its=20own=20thread=20for=20a=20Retry-?= =?UTF-8?q?After=20too=20large=20for=20an=20Int=20on=20those=20two=20codes?= =?UTF-8?q?,=20which=20would=20have=20ended=20the=20app's=20process:=20the?= =?UTF-8?q?=20client=20keeps=20the=20header=20for=20itself=20and=20the=20c?= =?UTF-8?q?ore=20decides=20about=20repeats?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../personalization/core/internal/Backoff.kt | 15 +++-- .../core/internal/CoreHttpClient.kt | 55 +++++++++++++++++-- .../core/internal/BackoffTest.kt | 19 +++++-- .../core/internal/CoreHttpClientTest.kt | 52 +++++++++++++++--- .../core/internal/CoreInitTest.kt | 23 ++++++++ 5 files changed, 142 insertions(+), 22 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Backoff.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Backoff.kt index f453d240..1e2e7521 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Backoff.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Backoff.kt @@ -4,8 +4,10 @@ import kotlin.random.Random /** * The pauses between repeats (decision 0002): 1 s, 2 s, 4 s… up to 5 minutes, each spread by ±20 % so - * that devices that failed together do not come back together. A `Retry-After` of the answer is taken - * as it is. + * that devices that failed together do not come back together. The spread never takes a pause past + * the 5 minutes. A `Retry-After` of the answer is waited too, but never less than a second and never + * more than the longest pause: a server's `0`, a date already past or a value of days are all kept in + * those bounds. */ internal class Backoff( private val baseMs: Long = 1_000, @@ -15,17 +17,20 @@ internal class Backoff( private val random: () -> Double = { Random.nextDouble() }, ) { - /** The pause before repeat number [attempt] (from 0), or [retryAfterMs] when the server named one. */ + /** The pause before repeat number [attempt] (from 0), or the one [retryAfterMs] names when the server named one. */ fun delayMs(attempt: Int, retryAfterMs: Long? = null): Long { - if (retryAfterMs != null && retryAfterMs >= 0) return retryAfterMs + if (retryAfterMs != null && retryAfterMs >= 0) return retryAfterMs.coerceIn(MIN_RETRY_AFTER_MS, maxMs) val doublings = attempt.coerceIn(0, MAX_DOUBLINGS) val pause = (baseMs shl doublings).coerceAtMost(maxMs) val factor = 1 + spread * (2 * random() - 1) - return (pause * factor).toLong().coerceAtLeast(0) + return (pause * factor).toLong().coerceIn(0, maxMs) } private companion object { /** 2^20 seconds is far past the cap; more doublings would only overflow. */ const val MAX_DOUBLINGS = 20 + + /** A `Retry-After` of `0` or of a moment already past still waits this long. */ + const val MIN_RETRY_AFTER_MS = 1_000L } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt index a15025c2..6d200879 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt @@ -14,6 +14,7 @@ import okhttp3.Call import okhttp3.Callback import okhttp3.Dispatcher import okhttp3.EventListener +import okhttp3.Interceptor import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient @@ -90,6 +91,7 @@ internal class CoreHttpClient( .readTimeout(timeouts.readMs, TimeUnit.MILLISECONDS) .writeTimeout(timeouts.writeMs, TimeUnit.MILLISECONDS) .eventListenerFactory(PhaseListener) + .addNetworkInterceptor(RetryAfterGuard) .build() internal val initClient: OkHttpClient = client.newBuilder() @@ -200,11 +202,31 @@ internal class CoreHttpClient( return Exchange(result, status, text) } - /** `Retry-After` in seconds or as an HTTP date, as milliseconds from now; null without one or when it does not parse. */ + /** + * `Retry-After` in seconds or as an HTTP date, as milliseconds from now; null without one or when it + * does not parse. Read from the copy [RetryAfterGuard] keeps. Whatever the server writes, nothing + * throws: a number of seconds may be fractional, negative or `NaN` (`0`: [Backoff] waits a second) + * or too large for milliseconds or `inf` ([Long.MAX_VALUE]: [Backoff] waits five minutes); a date + * already past is `0`. + */ private fun retryAfterMs(response: Response): Long? { - val value = response.header(RETRY_AFTER)?.trim()?.takeIf { it.isNotEmpty() } ?: return null - value.toLongOrNull()?.let { seconds -> return if (seconds < 0) null else seconds * 1_000 } - val date = response.headers.getDate(RETRY_AFTER) ?: return null + val value = (response.header(RetryAfterGuard.COPY) ?: response.header(RETRY_AFTER))?.trim()?.takeIf { it.isNotEmpty() } ?: return null + if (value.all { it in '0'..'9' }) { + val seconds = value.toLongOrNull() ?: return Long.MAX_VALUE + return if (seconds > Long.MAX_VALUE / 1_000) Long.MAX_VALUE else seconds * 1_000 + } + value.toDoubleOrNull()?.let { seconds -> + return when { + seconds.isNaN() || seconds <= 0 -> 0 + seconds >= Long.MAX_VALUE / 1_000.0 -> Long.MAX_VALUE + else -> (seconds * 1_000).toLong() + } + } + val date = try { + okhttp3.Headers.headersOf(RETRY_AFTER, value).getDate(RETRY_AFTER) + } catch (e: IllegalArgumentException) { + null + } ?: return null return (date.time - clock.nowMillis()).coerceAtLeast(0) } @@ -293,6 +315,31 @@ internal class CoreHttpClient( } } +/** + * Keeps OkHttp from repeating an answered request by itself. OkHttp sends a `408` again at once (unless + * its `Retry-After` is positive) and a `503` whose `Retry-After` is `0`; it also parses that header as an + * `Int` and throws on a larger number, on its own thread, which would end the app's process. For those + * two codes the header is moved to [COPY] (empty without one), where the client reads it, and + * `Retry-After: 1` takes its place: OkHttp hands the answer over as it is, and the core decides about + * repeats (decision 0002). + */ +internal object RetryAfterGuard : Interceptor { + const val COPY = "X-Sdk-Retry-After" + private const val RETRY_AFTER = "Retry-After" + private val CODES = setOf(408, 503) + + override fun intercept(chain: Interceptor.Chain): Response { + val response = chain.proceed(chain.request()) + if (response.code !in CODES) return response + // Empty when the server sent none: the client then reads no Retry-After, not this "1". + val original = response.header(RETRY_AFTER).orEmpty() + return response.newBuilder() + .header(COPY, original) + .header(RETRY_AFTER, "1") + .build() + } +} + /** Whether the request reached the server: a failure before it is `network`, after it `timeout`. */ internal class Phase { @Volatile diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/BackoffTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/BackoffTest.kt index 787854ae..698c1132 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/BackoffTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/BackoffTest.kt @@ -4,7 +4,7 @@ import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test -/** The pauses between repeats: 1 s doubling to 5 minutes, ±20 %, a server's Retry-After as it is. */ +/** The pauses between repeats: 1 s doubling to 5 minutes, ±20 % within the 5 minutes, a server's Retry-After from 1 s to 5 minutes. */ class BackoffTest { @Test @@ -19,23 +19,30 @@ class BackoffTest { } @Test - fun `each pause is spread by 20 percent either way`() { + fun `each pause is spread by 20 percent either way, never past five minutes`() { val low = Backoff(random = { 0.0 }) val high = Backoff(random = { 0.999_999 }) assertEquals(800L, low.delayMs(0)) assertEquals(240_000L, low.delayMs(20)) assertTrue(high.delayMs(0) in 1_199L..1_200L) - assertTrue(high.delayMs(20) in 359_999L..360_000L) + assertTrue(high.delayMs(7) in 153_599L..153_600L) + // 256 s spread upwards and the cap itself: the spread stops at five minutes. + assertEquals(300_000L, high.delayMs(8)) + assertEquals(300_000L, high.delayMs(20)) + assertEquals(300_000L, high.delayMs(Int.MAX_VALUE)) } @Test - fun `a Retry-After is waited as it is`() { + fun `a Retry-After is waited, from a second to five minutes`() { val backoff = Backoff(random = { 0.0 }) assertEquals(30_000L, backoff.delayMs(0, retryAfterMs = 30_000)) - assertEquals(3_600_000L, backoff.delayMs(5, retryAfterMs = 3_600_000)) - assertEquals(0L, backoff.delayMs(3, retryAfterMs = 0)) + assertEquals(300_000L, backoff.delayMs(5, retryAfterMs = 300_000)) + assertEquals(300_000L, backoff.delayMs(5, retryAfterMs = 3_600_000)) + assertEquals(300_000L, backoff.delayMs(5, retryAfterMs = Long.MAX_VALUE)) + assertEquals(1_000L, backoff.delayMs(3, retryAfterMs = 0)) + assertEquals(1_000L, backoff.delayMs(3, retryAfterMs = 400)) assertEquals(800L, backoff.delayMs(0, retryAfterMs = -1)) } } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt index 3311c2bd..3bceae32 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt @@ -234,16 +234,54 @@ class CoreHttpClientTest { @Test fun `the Retry-After of an answer comes back with it, in seconds or as a date`() { - server.enqueue(MockResponse().setResponseCode(503).setHeader("Retry-After", "30")) - server.enqueue(MockResponse().setResponseCode(429).setHeader("Retry-After", "Wed, 21 Oct 2015 07:28:00 GMT")) - server.enqueue(MockResponse().setResponseCode(503).setHeader("Retry-After", "soon")) - server.enqueue(MockResponse().setResponseCode(500)) - val clock = FakeClock(now = 1_445_412_420_000L) // a minute before the date above + val values = listOf( + 503 to "30", + 429 to "Wed, 21 Oct 2015 07:28:00 GMT", + 503 to "soon", + 500 to null, + 503 to "0", + 503 to "Wed, 21 Oct 2015 07:26:00 GMT", + 503 to "9223372036854776", + 503 to "99999999999999999999999", + 503 to "-5", + 503 to "NaN", + 503 to "Infinity", + 503 to "1.5", + ) + values.forEach { (code, header) -> + server.enqueue(MockResponse().setResponseCode(code).apply { if (header != null) setHeader("Retry-After", header) }) + } + val clock = FakeClock(now = 1_445_412_420_000L) // a minute before the first date above val client = CoreHttpClient({ server.url("/").toString() }, settings, connectivity, "shop", clock = clock) - val results = List(4) { send(NetworkMethod.GET("init"), client = client) as HttpResult.Failure } + val results = values.map { send(NetworkMethod.GET("init"), client = client) as HttpResult.Failure } + + // A date already past, a negative number and NaN are now; a number too large for milliseconds + // does not wrap around. Nothing throws. + assertEquals( + listOf(30_000L, 60_000L, null, null, 0L, 0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L, 0L, Long.MAX_VALUE, 1_500L), + results.map { it.retryAfterMs }, + ) + assertEquals(values.size, server.requestCount) + val backoff = Backoff(random = { 0.5 }) + assertEquals( + listOf(30_000L, 60_000L, 1_000L, 1_000L, 300_000L, 300_000L, 1_000L, 1_000L, 300_000L, 1_500L), + listOf(0, 1, 4, 5, 6, 7, 8, 9, 10, 11).map { backoff.delayMs(0, results[it].retryAfterMs) }, + ) + } + + @Test + fun `OkHttp does not repeat a 408 or a 503 that says to come back at once`() { + server.enqueue(MockResponse().setResponseCode(408)) + server.enqueue(MockResponse().setResponseCode(503).setHeader("Retry-After", "0")) + server.enqueue(MockResponse().setResponseCode(503).setHeader("Retry-After", "99999999999")) + server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) + + assertEquals(SdkError.Http(408), send(NetworkMethod.POST("push")).error()) + assertEquals(SdkError.Http(503), send(NetworkMethod.POST("push")).error()) + assertEquals(SdkError.Http(503), send(NetworkMethod.POST("push")).error()) - assertEquals(listOf(30_000L, 60_000L, null, null), results.map { it.retryAfterMs }) + assertEquals(3, server.requestCount) } @Test diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt index ee1cd72c..220a3994 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt @@ -278,6 +278,29 @@ class CoreInitTest { assertEquals(4, sent.size) } + @Test + fun `a Retry-After waits at least a second and at most five minutes`() { + started() + // 0, or a date already past: a second. + answer(http(503, retryAfterMs = 0)) + after(999) + assertEquals(1, sent.size) + after(1) + assertEquals(2, sent.size) + + // An hour, or a number too large for milliseconds: five minutes. + answer(http(429, retryAfterMs = 3_600_000)) + after(299_999) + assertEquals(2, sent.size) + after(1) + assertEquals(3, sent.size) + + answer(http(503, retryAfterMs = Long.MAX_VALUE)) + after(300_000) + assertEquals(4, sent.size) + assertEquals(listOf(1_000L, 300_000L, 300_000L), codes(LogCodes.INIT_RETRY).map { it.context["delay_ms"] }) + } + // The refused state across launches. @Test From f97677c44fb0acd7d342ebcd0fc04b124c2ef895 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 00:53:20 +0400 Subject: [PATCH 037/109] =?UTF-8?q?fix(core):=20stage=203=20after=20review?= =?UTF-8?q?.=20deleteUserCredentials()=20runs=20to=20its=20end=20once=20th?= =?UTF-8?q?e=20hold=20has=20begun:=20a=20caller=20cancelled=20midway=20no?= =?UTF-8?q?=20longer=20leaves=20the=20instance=20initializing=20for=20good?= =?UTF-8?q?=20with=20its=20requests=20waiting,=20on=20both=20cores.=20Whil?= =?UTF-8?q?e=20a=20deletion=20holds=20the=20new=20core=20no=20/init=20goes?= =?UTF-8?q?,=20not=20on=20coming=20back=20to=20the=20foreground=20and=20no?= =?UTF-8?q?t=20through=20checkShop(),=20until=20the=20restart;=20an=20/ini?= =?UTF-8?q?t=20with=20the=20old=20did=20on=20its=20way=20is=20cancelled=20?= =?UTF-8?q?and=20its=20answer,=20its=20popup=20and=20its=20hooks=20are=20d?= =?UTF-8?q?ropped=20(the=20hooks=20check=20the=20start=20under=20the=20loc?= =?UTF-8?q?k).=20The=20new=20core's=20start=20decides=20its=20first=20stat?= =?UTF-8?q?e=20from=20the=20preferences=20already=20loaded=20=E2=80=94=20a?= =?UTF-8?q?=20refused=20shop=20starts=20refused,=20any=20stored=20did=20is?= =?UTF-8?q?=20ready=20at=20once=20(decision=201)=20=E2=80=94=20and=20prepa?= =?UTF-8?q?res=20the=20rest=20on=20the=20io=20dispatcher=20before=20the=20?= =?UTF-8?q?first=20/init:=20needReInitialization,=20the=20config's=20did,?= =?UTF-8?q?=20the=20saved=20answer's=20did,=20the=20ended=20session;=20com?= =?UTF-8?q?ing=20back=20to=20the=20foreground=20is=20handled=20there=20too?= =?UTF-8?q?,=20so=20the=20main=20thread=20never=20waits=20for=20an=20answe?= =?UTF-8?q?r=20being=20written.=20A=20wipe=20(deleteUserCredentials,=20nee?= =?UTF-8?q?dReInitialization)=20marks=20the=20partition=20in=20the=20same?= =?UTF-8?q?=20commit,=20and=20while=20the=20mark=20is=20set=20a=20saved=20?= =?UTF-8?q?answer=20that=20outlived=20the=20wipe=20gives=20no=20did;=20the?= =?UTF-8?q?=20next=20successful=20/init=20clears=20it.=20Rees46Config.devi?= =?UTF-8?q?ceId=20is=20taken=20once=20per=20shop,=20on=20both=20cores,=20b?= =?UTF-8?q?ehind=20a=20migration=20flag:=20it=20does=20not=20come=20back?= =?UTF-8?q?=20after=20deleteUserCredentials()=20even=20when=20the=20app=20?= =?UTF-8?q?passes=20it=20on=20every=20launch,=20and=20a=20shop=20that=20ke?= =?UTF-8?q?eps=20a=20did=20of=20its=20own=20logs=20device=5Fid=5Fkept=20on?= =?UTF-8?q?ce.=20An=20/init=20cancelled=20by=20something=20else=20than=20t?= =?UTF-8?q?he=20SDK=20(an=20interceptor,=20the=20network=20layer)=20is=20r?= =?UTF-8?q?epeated=20like=20any=20failure.=20A=20request=20of=20the=20new?= =?UTF-8?q?=20core=20waits=20for=20the=20core's=20/init=20to=20be=20set=20?= =?UTF-8?q?as=20for=20the=20instance=20to=20be=20ready,=20within=20its=20r?= =?UTF-8?q?ead=20timeout,=20and=20the=20core=20gets=20its=20/init=20before?= =?UTF-8?q?=20it=20takes=20a=20request.=20A=20push-only=20instance=20givin?= =?UTF-8?q?g=20way=20to=20the=20app's=20instance=20of=20its=20shop=20lets?= =?UTF-8?q?=20its=20requests=20in=20flight=20finish;=20only=20release()=20?= =?UTF-8?q?by=20the=20app=20cancels=20them.=20release()=20waits=20for=20an?= =?UTF-8?q?=20initialization=20in=20progress=20instead=20of=20being=20undo?= =?UTF-8?q?ne=20by=20it,=20and=20a=20release=20racing=20start()=20leaves?= =?UTF-8?q?=20no=20lifecycle=20observer=20behind.=20The=20KDoc=20of=20SDK?= =?UTF-8?q?=20says=20that=20the=20members=20this=20release=20adds=20may=20?= =?UTF-8?q?clash=20with=20an=20app's=20subclass.=20The=20tests=20wait=20on?= =?UTF-8?q?=20seams=20instead=20of=20the=20wall=20clock:=20the=20push=20to?= =?UTF-8?q?ken's=20test=20reads=20the=20requests=20held=20for=20a=20did,?= =?UTF-8?q?=20the=20instance's=20test=20the=20legacy=20threads=20and=20the?= =?UTF-8?q?=20new=20core's=20/init.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/kotlin/com/personalization/Rees46.kt | 5 +- .../main/kotlin/com/personalization/SDK.kt | 67 +++++--- .../personalization/core/internal/CoreInit.kt | 136 +++++++++++---- .../personalization/core/internal/CoreV2.kt | 46 ++++- .../core/internal/ShopStorage.kt | 46 ++++- .../core/internal/StorageKeys.kt | 13 +- .../com/personalization/logs/SdkLogs.kt | 3 + .../network/NetworkRepositoryImpl.kt | 26 ++- .../com/personalization/PushTokenDidTest.kt | 8 +- .../com/personalization/SdkInstanceTest.kt | 39 +++-- .../com/personalization/SdkStorageTest.kt | 67 +++++++- .../core/internal/CoreFakes.kt | 28 +++ .../core/internal/CoreFunnelTest.kt | 10 +- .../core/internal/CoreInitTest.kt | 159 ++++++++++++++++++ .../core/internal/CoreV2Test.kt | 35 +++- .../core/internal/ShopStorageTest.kt | 3 + .../com/personalization/logs/LogCodesTest.kt | 2 +- 17 files changed, 599 insertions(+), 94 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt index 0ecd9dc2..efb4d3a0 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt @@ -54,8 +54,9 @@ object Rees46 { } val sdk = SDK() sdk.initializeWith(context, config) - // A shop brought up only to track a push gives way to the full instance. - if (existing != null && existing !== sdk) existing.release() + // A shop brought up only to track a push gives way to the full instance; the push's tracking + // in flight still gets through. + existing?.giveWay() // Removed once the instance is registered: a lookup meanwhile still finds the shop. pending.remove(config.shopId) sdk diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index c92b385d..fe2ace17 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -79,6 +79,7 @@ import com.personalization.utils.DomainFormattingUtils.formatApiDomain import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -89,6 +90,14 @@ import org.json.JSONObject private const val GAID_KEY = "google_advertising_id" +/** + * One shop's instance of the SDK: made by [Rees46.initialize], found with [Rees46.instance]. + * + * The class stays `open` for apps that subclassed it in earlier releases. The members this release + * adds — [shopId], [deviceId], [sessionId], [state], [onStateChange] and + * [deleteUserCredentials] — may clash with members of the same name in such a subclass: a subclass + * that declares one of them needs to rename its own. + */ @OptIn(InternalPersonalizationV3Api::class) open class SDK { @@ -502,12 +511,17 @@ open class SDK { val coreV2 = CoreSelection.resolve(coreV2Requested, shopId) if (coreV2) { val connectivity = AndroidConnectivityMonitor(context) - val core = networkRepository.useCoreV2(shopId, connectivity, dispatchers) + lateinit var init: CoreInit + // The core gets its /init before it takes a request: none goes without waiting for it. + val core = networkRepository.useCoreV2(shopId, connectivity, dispatchers) { core -> + init = createCoreInit(core, connectivity, context, config, storage, autoSendPushToken, needReInitialization) + core.init = init + } this.core = core - startCoreV2(core, connectivity, context, config, storage, autoSendPushToken, needReInitialization) + init.start() } else { core = networkRepository.core - startLegacy(context, config, autoSendPushToken, needReInitialization) + startLegacy(context, config, storage, autoSendPushToken, needReInitialization) } // Register this instance so pushes delivered to the (process-global) messaging services are @@ -553,19 +567,19 @@ open class SDK { } - /** Stops what an earlier initialization of this object started. */ - private fun stopCurrent() { + /** + * Stops what an earlier initialization of this object started. Without [cancelRequests] the new + * core's requests in flight finish (see [giveWay]). + */ + private fun stopCurrent(cancelRequests: Boolean = true) { if (::registerManager.isInitialized) registerManager.stop() - if (::networkRepository.isInitialized) networkRepository.release() + if (::networkRepository.isInitialized) networkRepository.release(cancelRequests) } /** `/init` and the session of the legacy path, as before. */ - private fun startLegacy(context: Context, config: Rees46Config, autoSendPushToken: Boolean, needReInitialization: Boolean) { - // A did the app brings is taken while the shop has none, as on the new core. - val configured = config.deviceId?.trim().orEmpty() - if (!needReInitialization && configured.isNotEmpty() && userSettingsRepository.getDid().isEmpty()) { - userSettingsRepository.updateDid(configured) - } + private fun startLegacy(context: Context, config: Rees46Config, storage: ShopStorage, autoSendPushToken: Boolean, needReInitialization: Boolean) { + // A did the app brings is taken once, while the shop has none, as on the new core. + if (!needReInitialization) storage.adoptConfiguredDid(config.deviceId, userSettingsRepository) // Popups v3 start from the /init answer; in their test mode /init counts test popups too. registerManager.onInitFinished = { flag -> @@ -583,8 +597,8 @@ open class SDK { ) } - /** The new core's `/init` and state ([CoreInit]); requests wait for it in [CoreV2]. */ - private fun startCoreV2( + /** The new core's `/init` and state ([CoreInit]); requests wait for it in [CoreV2]. Started by the caller. */ + private fun createCoreInit( core: CoreV2, connectivity: ConnectivityMonitor, context: Context, @@ -592,9 +606,9 @@ open class SDK { storage: ShopStorage, autoSendPushToken: Boolean, needReInitialization: Boolean, - ) { + ): CoreInit { val contentResolver = context.contentResolver - val init = CoreInit( + return CoreInit( setup = InitSetup( shopId = config.shopId, needReInitialization = needReInitialization, @@ -627,8 +641,6 @@ open class SDK { }, dispatchers = dispatchers, ) - core.init = init - init.start() } /** The advertising id for `/init`, or null when it cannot be read. */ @@ -885,11 +897,20 @@ open class SDK { * stops; the legacy one cannot cancel a request in flight. The shop's data stays on the device, and * a new [Rees46.initialize] for the shop makes a fresh instance. */ - fun release() { + fun release() = stop(cancelRequests = true) + + /** + * [release] for a push-only instance ([initializeForPush]) that gives way to the app's instance of + * its shop: the requests it has in flight — the push's tracking — finish and get their answers. + */ + internal fun giveWay() = stop(cancelRequests = false) + + /** Waits for an initialization in progress: a release during it is not undone by its end. */ + private fun stop(cancelRequests: Boolean): Unit = synchronized(initLock) { released = true SdkRegistry.unregister(this) // Nothing more goes out: no /init, and requests fail with notInitialized. - stopCurrent() + stopCurrent(cancelRequests) instanceState.set(SdkState.NOT_INITIALIZED) instanceState.dropListeners() // Only this instance's level goes: another live instance of the shop keeps logging at its own. @@ -936,6 +957,12 @@ open class SDK { val core = core val storage = storage if (core == null || storage == null || released) throw SdkException(SdkError.NotInitialized) + // Once the hold begins, the wipe and the restart run to the end even if the caller is cancelled: + // stopping between them would leave the instance initializing for good. + withContext(NonCancellable) { forgetCustomer(core, storage) } + } + + private suspend fun forgetCustomer(core: Core, storage: ShopStorage) { // From here on an answer to an /init sent before is dropped: one landing during the wipe would // write the forgotten did back. The new core also holds requests until the new did. val init = (core as? CoreV2)?.init diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt index d2c04bdb..c5be996c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt @@ -75,7 +75,12 @@ internal fun interface ReadyGate { * on [checkShop]. A refused shop is not asked again before the next launch. * * Requests with a result wait in [awaitReady]. A stale answer (an `/init` of a start [restart] or - * [release] ended) is dropped. + * [release] ended) is dropped, and while [holdForRestart] holds the instance no `/init` goes at all. + * + * Disk work stays off the caller's thread: [start] decides the first state from what the preferences + * already hold (a stored `did` is ready at once) and prepares the rest of the storage on [CoreDispatchers.io] + * before the first `/init`. The app's foreground is handled there too, so the main thread never waits + * for an answer being written under the lock. */ internal class CoreInit( private val setup: InitSetup, @@ -108,41 +113,87 @@ internal class CoreInit( private var job: Job? = null private var generation = 0 private var released = false + + /** Storage prepared for the first `/init`; none goes before. */ + private var prepared = false + + /** Held by [holdForRestart] until [restart]: the customer is being deleted, no `/init` goes. */ + private var held = false private var usable = false private var fallbackLogged = false private var lastCheckAt: Long? = null private var foreground: Cancellable? = null + /** How many `/init` went out; for tests. */ + @Volatile + internal var sent = 0 + private set + + /** Whether an `/init` is on its way or waits for its next attempt; for tests. */ + internal val isRunning: Boolean + get() = synchronized(lock) { job?.isActive == true } + /** Read once and reused: the advertising id goes with every `/init` of the launch. */ @Volatile private var advertisingIdValue: String? = null - /** Prepares the shop's storage, sets the first state and sends `/init`. */ + /** + * Sets the first state and sends `/init` once the shop's storage is prepared. The first state comes + * from what the preferences hold: a refused shop starts refused, a stored `did` ready at once + * (decision 1). The rest — `needReInitialization`, the `did` of the config or of the saved answer, + * the ended session — is disk work and runs on [CoreDispatchers.io]. + */ fun start() { val ready = synchronized(lock) { if (released) return - prepareStorage() val first = when { storage.isRefused -> SdkState.REFUSED - settings.getDid().isNotEmpty() -> SdkState.READY + !setup.needReInitialization && settings.getDid().isNotEmpty() -> SdkState.READY else -> SdkState.INITIALIZING } publish(first) first == SdkState.READY } if (ready) markUsable(generation()) - foreground = lifecycle.addListener(::onForegroundChanged) - synchronized(lock) { launchLocked() } + val handle = lifecycle.addListener(::onForegroundChanged) + // A release that came meanwhile does not leave the observer behind. + val kept = synchronized(lock) { if (released) false else true.also { foreground = handle } } + if (!kept) { + handle.cancel() + return + } + val start = generation() + scope.launch { prepare(start) } + } + + /** On [CoreDispatchers.io]: the disk work of the start, then the first `/init`. */ + private fun prepare(start: Int) { + val becameReady = synchronized(lock) { + if (released) return + // A deletion that began meanwhile has the customer forgotten: nothing of theirs comes back. + if (start == generation) prepareStorage() + prepared = true + val ready = state.current == SdkState.INITIALIZING && !held && settings.getDid().isNotEmpty() + if (ready) publish(SdkState.READY) + launchLocked() + ready + } + if (becameReady) markUsable(start) } override suspend fun awaitReady(timeoutMs: Long): SdkError? { val decided = withTimeoutOrNull(timeoutMs) { gate.first { it != Gate.WAIT } } ?: return SdkError.Timeout - return when (decided) { - Gate.OPEN -> null - Gate.BLOCKED -> SdkError.ShopBlocked - Gate.CLOSED -> SdkError.NotInitialized - Gate.WAIT -> SdkError.Timeout - } + return errorOf(decided) + } + + /** Waits without a limit until a request may go or may not; the caller sets the deadline. */ + internal suspend fun ready(): SdkError? = errorOf(gate.first { it != Gate.WAIT }) + + /** Null for an open gate, otherwise why a request may not go; [Gate.WAIT] is never decided. */ + private fun errorOf(decided: Gate): SdkError? = when (decided) { + Gate.BLOCKED -> SdkError.ShopBlocked + Gate.CLOSED -> SdkError.NotInitialized + else -> null } /** @@ -151,7 +202,7 @@ internal class CoreInit( * True when `/init` went out. */ internal fun checkShop(): Boolean = synchronized(lock) { - if (released || state.current == SdkState.REFUSED || job?.isActive == true) return false + if (released || held || !prepared || state.current == SdkState.REFUSED || job?.isActive == true) return false val now = clock.nowMillis() val last = lastCheckAt if (last != null && now - last < CHECK_SHOP_INTERVAL_MS) return false @@ -161,11 +212,13 @@ internal class CoreInit( /** * Stops the current start before `deleteUserCredentials()` wipes the customer: the `/init` on its - * way is cancelled and its answer will not be taken, and requests wait for the next `did`. + * way is cancelled and its answer will not be taken, requests wait for the next `did`, and no + * `/init` goes — not on coming back to the foreground, not on [checkShop] — until [restart]. */ fun holdForRestart(): Unit = synchronized(lock) { if (released) return generation++ + held = true job?.cancel() job = null usable = false @@ -176,20 +229,22 @@ internal class CoreInit( /** Starts over as a new device after [holdForRestart]: `/init` goes without a `did`. */ fun restart(): Unit = synchronized(lock) { if (released) return + held = false launchLocked() } /** Stops for good: `/init` is cancelled, waiting requests and later ones get `notInitialized`. */ fun release() { - synchronized(lock) { + val listening = synchronized(lock) { if (released) return released = true generation++ job?.cancel() job = null gate.value = Gate.CLOSED + foreground.also { foreground = null } } - foreground?.cancel() + listening?.cancel() scope.cancel() } @@ -206,19 +261,22 @@ internal class CoreInit( mapOf("reason" to LogReasons.NEED_REINITIALIZATION), ) } else { - val configured = setup.deviceId?.trim().orEmpty() - if (configured.isNotEmpty() && settings.getDid().isEmpty()) settings.updateDid(configured) + storage.adoptConfiguredDid(setup.deviceId, settings) } - // The saved answer's did fills a shop that lost its own; it never replaces one. - if (settings.getDid().isEmpty()) { + // The saved answer's did fills a shop that lost its own; it never replaces one. After a wipe + // (deleteUserCredentials, needReInitialization) whose file did not go, it is the forgotten one. + if (settings.getDid().isEmpty() && !storage.isWiped) { storage.initAnswer()?.optString(UserBasicParams.DID)?.takeIf { it.isNotEmpty() }?.let(settings::updateDid) } forgetStaleSession() } - /** Must hold [lock]. Sends `/init` unless it is on its way already; true when it went. */ + /** + * Must hold [lock]. Sends `/init` unless it is on its way already, the storage is not prepared yet or + * a deletion holds the instance; true when it went. + */ private fun launchLocked(): Boolean { - if (released || job?.isActive == true) return false + if (released || held || !prepared || job?.isActive == true) return false val start = generation job = scope.launch { run(start) } return true @@ -242,6 +300,7 @@ internal class CoreInit( if (start != generation) return forgetStaleSession() } + sent++ val result = send(params()) val step = synchronized(lock) { // An answer to a start that restart() or release() ended belongs to nobody. @@ -251,7 +310,7 @@ internal class CoreInit( when (step) { Step.Done -> return is Step.Answered -> { - hook("onAnswer") { hooks.onAnswer(step.answer, fresh = true) } + answered(start, step.answer, fresh = true) markUsable(start) return } @@ -259,7 +318,7 @@ internal class CoreInit( if (step.fallback) { log(LogLevel.WARNING, LogCodes.INIT_SAVED_ANSWER, "The SDK works on the did it has while /init is repeated") val saved = storage.initAnswer() ?: JSONObject() - hook("onAnswer") { hooks.onAnswer(saved, fresh = false) } + answered(start, saved, fresh = false) markUsable(start) } if (step.waitForNetwork) { @@ -295,7 +354,8 @@ internal class CoreInit( else -> retry(attempt, mapOf("status" to code), result.retryAfterMs.takeIf { code in RETRY_AFTER_CODES }) } } - if (error == SdkError.Cancelled) return Step.Done + // A cancel the SDK did not make (an interceptor, the network layer): this start is still + // current, so /init is repeated as after any other failure. Its own cancels never get here. return retry(attempt, mapOf("error" to errorName(error)), retryAfterMs = null) } } @@ -306,6 +366,8 @@ internal class CoreInit( if (settings.getDid().isEmpty()) settings.updateDid(did) answer.optString(UserBasicParams.SEANCE).takeIf { it.isNotEmpty() }?.let(settings::updateSid) storage.saveInitAnswer(answer) + // The answer on disk is the new customer's now. + if (storage.isWiped) storage.isWiped = false if (storage.isRefused) storage.isRefused = false publish(SdkState.READY) log(LogLevel.INFO, LogCodes.INIT_SUCCEEDED, "/init succeeded") @@ -355,14 +417,24 @@ internal class CoreInit( } } - /** Tells the instance it can send, once per start. */ + /** + * Hands an answer of [start] to the instance (its popup, its flags), unless a restart or a release + * ended that start meanwhile: under [lock], so the forgotten customer's popup is never shown. + */ + private fun answered(start: Int, answer: JSONObject, fresh: Boolean) { + synchronized(lock) { + if (start != generation || released) return + hook("onAnswer") { hooks.onAnswer(answer, fresh) } + } + } + + /** Tells the instance it can send, once per start, and only while that start is the current one. */ private fun markUsable(start: Int) { - val first = synchronized(lock) { + synchronized(lock) { if (start != generation || released || usable) return usable = true - true + hook("onUsable") { hooks.onUsable() } } - if (first) hook("onUsable") { hooks.onUsable() } } /** A `sid` whose window ended (two hours without activity) is not sent: the server starts a new session. */ @@ -371,8 +443,13 @@ internal class CoreInit( if (clock.nowMillis() - settings.getSidLastActTime() > SESSION_WINDOW_MS) storage.forgetSession() } + /** Heard on the main thread; handled on [CoreDispatchers.io], where waiting for [lock] blocks nothing. */ private fun onForegroundChanged(foreground: Boolean) { if (!foreground) return + scope.launch { onForeground() } + } + + private fun onForeground() { synchronized(lock) { if (released || state.current == SdkState.REFUSED || job?.isActive == true) return if (clock.nowMillis() - settings.getSidLastActTime() <= SESSION_WINDOW_MS) return @@ -441,6 +518,7 @@ internal class CoreInit( SdkError.Network -> "network" SdkError.Timeout -> "timeout" SdkError.Decoding -> ERROR_DECODING + SdkError.Cancelled -> "cancelled" else -> "failed" } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt index 409da7bb..7a68233f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt @@ -12,7 +12,11 @@ import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.filterNotNull +import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch +import kotlinx.coroutines.withTimeoutOrNull import org.json.JSONObject import java.util.concurrent.atomic.AtomicBoolean @@ -34,9 +38,17 @@ internal class CoreV2( override val isV2: Boolean = true - /** The instance's `/init`, once it started; a request waits on it. */ - @Volatile - internal var init: CoreInit? = null + private val initFlow = MutableStateFlow(null) + + /** + * The instance's `/init`; a request waits on it. Set before the core takes requests: one that comes + * while it is not set yet waits for it like for the instance to be ready. + */ + internal var init: CoreInit? + get() = initFlow.value + set(value) { + initFlow.value = value + } /** How long a request waits for the instance to be ready: its own read timeout. */ private val readyWaitMs: Long = client.timeouts.readMs @@ -72,7 +84,7 @@ internal class CoreV2( return } val request = requests.launch { - val waited = init?.awaitReady(readyWaitMs) + val waited = awaitInstance() if (waited != null) { if (waited == SdkError.Timeout) { SdkLogs.log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "${method.type} ${method.method}: the SDK is not ready", shopId, @@ -104,9 +116,31 @@ internal class CoreV2( } } - override fun release() { + /** + * Waits, within the read timeout, for the instance's `/init` to be set and for the instance to be + * ready; then the request gets its own full timeout. Null when it may go. + */ + private suspend fun awaitInstance(): SdkError? { + val waited = withTimeoutOrNull(readyWaitMs) { + val current = initFlow.filterNotNull().first() + Verdict(current.ready()) + } + return if (waited == null) SdkError.Timeout else waited.error + } + + /** What a wait decided; null [error] when the request may go. */ + private class Verdict(val error: SdkError?) + + override fun release() = release(cancelRequests = true) + + /** + * Stops the core: `/init` stops and later requests fail with `notInitialized`. With [cancelRequests] + * the requests in flight are cancelled too; without, as when a push-only instance gives way to the + * app's, they finish and get their answers. + */ + fun release(cancelRequests: Boolean) { released = true - requests.cancel() + if (cancelRequests) requests.cancel() init?.release() } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt index 950b5879..5c296b99 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt @@ -6,6 +6,7 @@ import android.util.AtomicFile import com.personalization.LogLevel import com.personalization.logs.LogCodes import com.personalization.logs.SdkLogs +import com.personalization.sdk.domain.repositories.UserSettingsRepository import org.json.JSONException import org.json.JSONObject import java.io.File @@ -76,6 +77,39 @@ internal class ShopStorage(context: Context, val layout: StorageLayout) { preferences.edit().putBoolean(StorageKeys.REFUSED, value).apply() } + /** + * Takes the `did` an app brings in `Rees46Config.deviceId`, once for the shop: on the first launch + * that brings one, while the shop has no `did` of its own. A shop that has one keeps it and logs + * `device_id_kept`. Either way the flag [StorageKeys.MIGRATED_CONFIG_DEVICE_ID] is set and later + * launches leave the config's `did` alone, so it does not come back after `deleteUserCredentials()` + * even when the app passes it on every launch. True when it was taken. + */ + fun adoptConfiguredDid(deviceId: String?, settings: UserSettingsRepository): Boolean { + val configured = deviceId?.trim().orEmpty() + if (configured.isEmpty() || preferences.getBoolean(StorageKeys.MIGRATED_CONFIG_DEVICE_ID, false)) return false + val own = settings.getDid() + val taken = own.isEmpty() + if (taken) { + settings.updateDid(configured) + } else if (own != configured) { + SdkLogs.log( + LogLevel.WARNING, + LogCodes.DEVICE_ID_KEPT, + "Rees46Config.deviceId is not taken: the shop keeps the did it has", + layout.shopId, + ) + } + preferences.edit().putBoolean(StorageKeys.MIGRATED_CONFIG_DEVICE_ID, true).commit() + return taken + } + + /** Whether the customer was wiped and no `/init` has succeeded since ([StorageKeys.CREDENTIALS_WIPED]). */ + var isWiped: Boolean + get() = preferences.getBoolean(StorageKeys.CREDENTIALS_WIPED, false) + set(value) { + preferences.edit().putBoolean(StorageKeys.CREDENTIALS_WIPED, value).apply() + } + /** The directory of the shop's event queue, created if needed; null when the disk refused it. */ fun queueDirectory(): File? { val queue = File(directory, QUEUE_DIRECTORY) @@ -92,7 +126,8 @@ internal class ShopStorage(context: Context, val layout: StorageLayout) { * Forgets the customer: [StorageKeys.USER_CREDENTIALS] and the saved `/init` answer, and with * [includeQueue] the queue's directory. The A/B group, the push tokens, the advertising id, the * stories' state and the migration flags stay. Written with `commit()`: a crash right after must - * not bring the customer back. False when part of it did not reach the disk. + * not bring the customer back. The same commit sets [isWiped], so a saved answer whose file outlives + * the wipe gives its `did` to nobody. False when part of it did not reach the disk. * * The stores a migration reads lose the customer too ([StorageKeys.SOURCE_CREDENTIALS], nothing * else of them): a partition restored without its migration flags, or lost, would otherwise read @@ -102,6 +137,7 @@ internal class ShopStorage(context: Context, val layout: StorageLayout) { var complete = attempt(OP_DELETE_CREDENTIALS) { val editor = preferences.edit() StorageKeys.USER_CREDENTIALS.forEach { editor.remove(it) } + editor.putBoolean(StorageKeys.CREDENTIALS_WIPED, true) editor.commit() } listOfNotNull(layout.namedFile, layout.legacyFile).forEach { name -> @@ -136,11 +172,15 @@ internal class ShopStorage(context: Context, val layout: StorageLayout) { /** * Forgets the device for `needReInitialization`: the `did`, the session and the saved `/init` - * answer, so the next `/init` gets a new `did`. False when part of it did not reach the disk. + * answer, so the next `/init` gets a new `did`; [isWiped] as [deleteUserCredentials] sets it. False + * when part of it did not reach the disk. */ fun forgetDevice(): Boolean { val complete = attempt(OP_FORGET_DEVICE) { - preferences.edit().remove(StorageKeys.DID).remove(StorageKeys.SID).remove(StorageKeys.SEANCE).remove(StorageKeys.SID_LAST_ACT).commit() + preferences.edit() + .remove(StorageKeys.DID).remove(StorageKeys.SID).remove(StorageKeys.SEANCE).remove(StorageKeys.SID_LAST_ACT) + .putBoolean(StorageKeys.CREDENTIALS_WIPED, true) + .commit() } return attempt(OP_DELETE_INIT_ANSWER) { synchronized(fileLock) { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt index 125cb70f..2fa37cc0 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt @@ -42,6 +42,14 @@ internal object StorageKeys { /** The shop refused the SDK (`402`, `403`, `404` or another `4xx` on `/init`). */ const val REFUSED = "refused" + /** + * The customer was wiped (`deleteUserCredentials()`, `needReInitialization`) and no `/init` has + * succeeded since: the saved `/init` answer, should its file have outlived the wipe, is the forgotten + * customer's and gives no `did`. Written in the same commit as the wipe; the next successful `/init` + * clears it. + */ + const val CREDENTIALS_WIPED = "credentials_wiped" + // One-shot migrations: each marks itself here once it has run, so it never runs twice for a shop. /** The old full-name file (`preferencesKey` of `SDK.initialize`) was read into the partition. */ const val MIGRATED_NAMED_FILE = "migrated_named_file" @@ -52,7 +60,10 @@ internal object StorageKeys { /** The shop's A/B group, push tokens and advertising id were offered to the device-level store. */ const val MIGRATED_DEVICE_LEVEL = "migrated_device_level" - val MIGRATION_FLAGS: Set = setOf(MIGRATED_NAMED_FILE, MIGRATED_LEGACY_FILE, MIGRATED_DEVICE_LEVEL) + /** `Rees46Config.deviceId` was considered once: taken by a shop without a `did`, or kept out. */ + const val MIGRATED_CONFIG_DEVICE_ID = "migrated_config_device_id" + + val MIGRATION_FLAGS: Set = setOf(MIGRATED_NAMED_FILE, MIGRATED_LEGACY_FILE, MIGRATED_DEVICE_LEVEL, MIGRATED_CONFIG_DEVICE_ID) /** * Every key the SDK has kept in a shop's store: what a migration may take from a file that may be diff --git a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt index 540adaed..9e41a6e7 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt @@ -168,6 +168,9 @@ internal object LogCodes { /** The shop refused and the core is in the `refused` state; the legacy path has no such state. */ const val INIT_REFUSED = "init_refused" const val DEVICE_ID_RESET = "device_id_reset" + + /** `Rees46Config.deviceId` not taken because the shop has a `did` of its own (`warning`, once per shop). */ + const val DEVICE_ID_KEPT = "device_id_kept" const val STORAGE_FAILED = "storage_failed" const val QUEUE_DROPPED = "queue_dropped" diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt index ecf7eb7e..62dbb307 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt @@ -48,13 +48,20 @@ class NetworkRepositoryImpl @Inject constructor( private val queueLock = Any() private val queue = ArrayList() + /** How many requests wait for a `did` on the legacy core; for tests. */ + internal val waitingForDid: Int + get() = synchronized(queueLock) { queue.size } + override fun initialize( baseUrl: String ) { this.baseUrl = baseUrl } - /** Sends this instance's requests through the new core from now on. */ + /** + * Sends this instance's requests through the new core from now on. [attach] runs before the core + * takes any request: it gives the core its `/init`, so no request goes without waiting for it. + */ internal fun useCoreV2( shopId: String, connectivity: ConnectivityMonitor, @@ -65,7 +72,11 @@ class NetworkRepositoryImpl @Inject constructor( connectivity = connectivity, shopId = shopId, ), - ): CoreV2 = CoreV2(client, shopId, dispatchers).also { core = it } + attach: (CoreV2) -> Unit = {}, + ): CoreV2 = CoreV2(client, shopId, dispatchers).also { + attach(it) + core = it + } override fun post( method: String, @@ -166,10 +177,15 @@ class NetworkRepositoryImpl @Inject constructor( /** * Stops the instance's core. The requests still waiting for a `did` are let go and fail with - * `notInitialized`, as every request made after this does. + * `notInitialized`, as every request made after this does. Without [cancelRequests] the new core lets + * the requests in flight finish (a push-only instance giving way to the app's); the legacy one never + * cancels them. */ - internal fun release() { - core.release() + internal fun release(cancelRequests: Boolean = true) { + when (val current = core) { + is CoreV2 -> current.release(cancelRequests) + else -> current.release() + } executeQueueTasks() } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/PushTokenDidTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/PushTokenDidTest.kt index 3435976b..114a6062 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/PushTokenDidTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/PushTokenDidTest.kt @@ -15,7 +15,6 @@ import okhttp3.mockwebserver.MockWebServer import org.json.JSONObject import org.junit.After import org.junit.Assert.assertEquals -import org.junit.Assert.assertNull import org.junit.Before import org.junit.Test import org.junit.runner.RunWith @@ -63,7 +62,9 @@ class PushTokenDidTest { fun `the app's token waits for the did and goes with it`() { manager.sendToken("app-token", PushProvider.FCM, null) - assertNull(server.takeRequest(300, TimeUnit.MILLISECONDS)) + // Held with the requests that wait for a did: never handed to a thread. + assertEquals(1, repository.waitingForDid) + assertEquals(0, server.requestCount) did.set("new-did") repository.executeQueueTasks() @@ -79,7 +80,8 @@ class PushTokenDidTest { manager.onTokenReceived("provider-token", PushProvider.HMS) - assertNull(server.takeRequest(300, TimeUnit.MILLISECONDS)) + assertEquals(1, repository.waitingForDid) + assertEquals(0, server.requestCount) did.set("new-did") repository.executeQueueTasks() val body = JSONObject(checkNotNull(server.takeRequest(5, TimeUnit.SECONDS)).body.readUtf8()) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt index 153e4744..59151d66 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt @@ -12,8 +12,10 @@ import com.personalization.core.SdkState import com.personalization.core.internal.CoreSelection import com.personalization.core.internal.CoreV2 import com.personalization.core.internal.DeviceStorage +import com.personalization.core.internal.LegacyCore import com.personalization.core.internal.ShopStorage import com.personalization.core.internal.StorageLayout +import com.personalization.core.internal.TrackingThreads import com.personalization.logs.LogCodes import com.personalization.logs.SdkLogs import kotlinx.coroutines.flow.first @@ -48,6 +50,7 @@ import java.util.concurrent.CyclicBarrier import java.util.concurrent.Executor import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicInteger /** * The instance and the facade on both cores: one instance per shop whoever asks and however often, @@ -64,6 +67,10 @@ class SdkInstanceTest { /** Every request the server got, `/init` among them. */ private val requests = LinkedBlockingQueue() + private val initRequests = AtomicInteger() + + /** The legacy path's requests run here, so a test waits until they are answered instead of sleeping. */ + private val threads = TrackingThreads() /** The answer to `/init`, given the did it carried. */ @Volatile @@ -91,11 +98,14 @@ class SdkInstanceTest { // A test that runs both cores starts over in the middle: a server starts once. server = MockWebServer() requests.clear() + initRequests.set(0) + LegacyCore.threads = threads server.dispatcher = object : Dispatcher() { override fun dispatch(request: RecordedRequest): MockResponse { val url = request.requestUrl ?: return MockResponse().setResponseCode(400) requests += url if (url.encodedPath != "/init") return MockResponse().setBody("{}") + initRequests.incrementAndGet() return initAnswer(url.queryParameter("did").orEmpty()) } } @@ -106,6 +116,7 @@ class SdkInstanceTest { fun tearDown() { SdkRegistry.all().forEach { it.release() } server.shutdown() + LegacyCore.threads = LegacyCore.DEFAULT_THREADS CoreSelection.reset() SdkRegistry.reset() Rees46.reset() @@ -132,15 +143,17 @@ class SdkInstanceTest { } } - /** No more requests to [path] within a moment; requests to other paths (the legacy profile/set) are let by. */ - private fun assertNoMore(path: String) { - val deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(300) - while (true) { - val left = deadline - System.nanoTime() - if (left <= 0) return - val url = requests.poll(left, TimeUnit.NANOSECONDS) ?: return - assertFalse("unexpected $url", url.encodedPath == "/$path") + /** + * [sdk] sent one `/init` and has none on its way: the legacy core's requests have all been answered + * (its threads are idle), the new core's `/init` is not running and went once. + */ + private fun assertOneInit(sdk: SDK) { + threads.awaitIdle() + (sdk.core as? CoreV2)?.init?.let { init -> + assertFalse("an /init is on its way", init.isRunning) + assertEquals(1, init.sent) } + assertEquals(1, initRequests.get()) } private fun awaitState(sdk: SDK, state: SdkState) = runBlocking { @@ -179,7 +192,8 @@ class SdkInstanceTest { assertSame(first, second) assertEquals(1, SdkRegistry.count()) - assertNoMore("init") + awaitState(first, SdkState.READY) + assertOneInit(first) assertTrue(logged.none { it.code == LogCodes.INVALID_CONFIG }) } } @@ -216,7 +230,8 @@ class SdkInstanceTest { assertEquals(1, made.toSet().size) assertEquals(1, SdkRegistry.count()) nextRequest("init") - assertNoMore("init") + awaitState(made.first(), SdkState.READY) + assertOneInit(made.first()) } @Test @@ -279,7 +294,7 @@ class SdkInstanceTest { } // Nothing was deleted and no /init went. assertEquals(did, partition().getString("did", null)) - assertNoMore("init") + assertOneInit(sdk) } } @@ -318,7 +333,7 @@ class SdkInstanceTest { @Test @Suppress("DEPRECATION") - fun `on the legacy core a new device goes from initializing to ready, heard once each on the main thread`() { + fun `on the legacy core a new device goes from initializing to ready, heard once each on the main thread after the current state`() { val sdk = SDK() val heard = CopyOnWriteArrayList>() sdk.onStateChange { heard += it to (Looper.myLooper() == Looper.getMainLooper()) } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt index 49ee998b..f53ee0b3 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt @@ -1,25 +1,41 @@ package com.personalization import android.content.Context +import android.net.ConnectivityManager +import android.net.NetworkCapabilities import android.os.Looper import androidx.test.core.app.ApplicationProvider import com.personalization.core.CompletionListener import com.personalization.core.SdkError import com.personalization.core.SdkException import com.personalization.core.SdkResult +import com.personalization.core.SdkState import com.personalization.core.internal.CoreSelection import com.personalization.core.internal.DeviceStorage import com.personalization.core.internal.LegacyCore -import com.personalization.core.internal.TrackingThreads import com.personalization.core.internal.ShopStorage import com.personalization.core.internal.StorageLayout import com.personalization.core.internal.StorageMigrations +import com.personalization.core.internal.TrackingThreads import com.personalization.core.internal.testCoreDispatchers import com.personalization.logs.LogCodes import com.personalization.logs.SdkLogs import com.personalization.ui.widgets.RecentSearches +import java.io.File +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.Executor +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicInteger +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.TestCoroutineScheduler +import kotlinx.coroutines.withTimeout +import kotlinx.coroutines.withTimeoutOrNull import okhttp3.mockwebserver.Dispatcher import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockWebServer @@ -33,14 +49,9 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config -import java.io.File -import java.util.concurrent.CopyOnWriteArrayList -import java.util.concurrent.CountDownLatch -import java.util.concurrent.LinkedBlockingQueue -import java.util.concurrent.Executor -import java.util.concurrent.TimeUnit -import java.util.concurrent.atomic.AtomicInteger +import org.robolectric.shadows.ShadowNetworkCapabilities /** * Storage through the SDK as an app starts it: the partition [Rees46Config.storageKey] picks, the old @@ -84,6 +95,11 @@ class SdkStorageTest { context.getSharedPreferences(it, Context.MODE_PRIVATE).edit().clear().commit() } File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() + // Robolectric's default network has no internet capability: give it one, as a device's has. + val manager = context.getSystemService(ConnectivityManager::class.java) + val capabilities = ShadowNetworkCapabilities.newInstance() + shadowOf(capabilities).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + shadowOf(manager).setNetworkCapabilities(manager.activeNetwork, capabilities) LegacyCore.threads = threads server.dispatcher = object : Dispatcher() { override fun dispatch(request: RecordedRequest): MockResponse { @@ -231,6 +247,41 @@ class SdkStorageTest { assertEquals(LogLevel.ERROR, logged.single { it.code == LogCodes.CALLBACK_FAILED }.level) } + @Test + fun `a deletion whose caller is cancelled midway still wipes and restarts, on both cores`() { + initAnswer = { n, _ -> MockResponse().setBody("""{"did":"did-$n","seance":"S$n"}""") } + listOf(false, true).forEach { coreV2 -> + SdkRegistry.all().forEach { it.release() } + CoreSelection.reset() + initCount.set(0) + inits.clear() + context.getSharedPreferences(StorageLayout(SHOP, null).partitionName, Context.MODE_PRIVATE).edit().clear().commit() + val sdk = Rees46.initialize(context, config(coreV2 = coreV2)) + awaitReady(sdk, "did-1") + val scheduler = TestCoroutineScheduler() + sdk.dispatchers = testCoreDispatchers(scheduler) + + // The caller goes as soon as the wipe is handed to the io dispatcher. + val caller = CoroutineScope(Dispatchers.Unconfined).launch { sdk.deleteUserCredentials() } + assertEquals(SdkState.INITIALIZING, sdk.state) + caller.cancel() + scheduler.advanceUntilIdle() + + assertTrue(caller.isCancelled) + awaitReady(sdk, "did-2") + assertEquals("core v2: $coreV2", listOf("", ""), inits) + } + } + + /** Waits for the instance to be ready with [did]. */ + private fun awaitReady(sdk: SDK, did: String) { + threads.awaitIdle() + val reached = runBlocking { + withTimeoutOrNull(5_000) { sdk.instanceState.changes.first { it == SdkState.READY && sdk.getDid() == did } } + } + assertTrue("state ${sdk.state}, did ${sdk.getDid()}, inits $inits, expected $did", reached != null) + } + @Test fun `an instance never initialized has nothing to delete`() { try { diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt index 20dab6a4..a32848cc 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt @@ -93,3 +93,31 @@ internal class TrackingThreads : java.util.concurrent.Executor { } } } + +/** + * A started [CoreInit] for [shopId], whose `/init` [send] answers without a server: with the `did` + * [settings] already hold, the instance is ready at once. For tests of what goes after `/init`. + */ +internal fun startedCoreInit( + context: android.content.Context, + settings: com.personalization.sdk.domain.repositories.UserSettingsRepository, + connectivity: ConnectivityMonitor = FakeConnectivity(connected = true), + shopId: String = "shop", + dispatchers: CoreDispatchers = CoreDispatchers.DEFAULT, + send: suspend (org.json.JSONObject) -> HttpResult = { + HttpResult.Success(org.json.JSONObject().put("did", settings.getDid().ifEmpty { "did" }).put("seance", "S")) + }, +): CoreInit = CoreInit( + setup = InitSetup(shopId), + send = send, + settings = settings, + storage = ShopStorage(context, StorageLayout(shopId, null)), + state = InstanceState(shopId = { shopId }, dispatchers = dispatchers), + connectivity = connectivity, + lifecycle = FakeAppLifecycle(foreground = true), + hooks = object : InitHooks { + override fun onAnswer(answer: org.json.JSONObject, fresh: Boolean) = Unit + override fun onUsable() = Unit + }, + dispatchers = dispatchers, +).also { it.start() } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt index dbe0b6b3..f87fbfce 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt @@ -76,7 +76,9 @@ class CoreFunnelTest { // HttpURLConnection on the JVM asks for no compression; OkHttp asks for gzip. assertEquals(null, server.takeRequest().headers["Accept-Encoding"]) - repository.useCoreV2("shop", FakeConnectivity(connected = true)) + repository.useCoreV2("shop", FakeConnectivity(connected = true)) { + it.init = startedCoreInit(ApplicationProvider.getApplicationContext(), settings) + } assertTrue(repository.core.isV2) server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) await { repository.get("products/get", JSONObject(), it) } @@ -85,12 +87,14 @@ class CoreFunnelTest { @Test fun `on the new core a request without a did waits for the instance, not for the did queue`() { - repository.useCoreV2("shop", FakeConnectivity(connected = true)) did.set("") + // The instance's /init gives it a did once it answers: the request goes then. + repository.useCoreV2("shop", FakeConnectivity(connected = true)) { + it.init = startedCoreInit(ApplicationProvider.getApplicationContext(), settings) + } server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) val answered = CountDownLatch(1) - // Without an init to wait on the request goes at once: the did queue no longer holds it. repository.getAsync("products/get", JSONObject(), object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) = answered.countDown() }) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt index 220a3994..a8bcc398 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt @@ -415,6 +415,59 @@ class CoreInitTest { assertEquals("FROM_RN", settings.getDid()) } + @Test + fun `the did of the config is taken once and after a deletion does not come back, even passed again`() { + started(InitSetup(SHOP, deviceId = "FROM_RN")) + assertEquals("FROM_RN", settings.getDid()) + inits.single().release() + + assertTrue(storage.deleteUserCredentials(includeQueue = true)) + started(InitSetup(SHOP, deviceId = "FROM_RN")) + + assertEquals("", settings.getDid()) + assertEquals(SdkState.INITIALIZING, state.current) + assertTrue(codes(LogCodes.DEVICE_ID_KEPT).isEmpty()) + } + + @Test + fun `a did of the config that the shop does not take is logged once`() { + settings.updateDid("MINE") + started(InitSetup(SHOP, deviceId = "FROM_RN")) + inits.single().release() + started(InitSetup(SHOP, deviceId = "FROM_RN")) + + assertEquals("MINE", settings.getDid()) + val kept = codes(LogCodes.DEVICE_ID_KEPT).single() + assertEquals(LogLevel.WARNING, kept.level) + assertTrue("no did in the log", "MINE" !in kept.message && "FROM_RN" !in kept.message && kept.context.isEmpty()) + } + + @Test + fun `an init cancelled by something else than the SDK is repeated`() { + started() + + answer(HttpResult.Failure(SdkError.Cancelled)) + + assertEquals(SdkState.RETRYING, state.current) + after(1_000) + assertEquals(2, sent.size) + answer(ok()) + assertEquals(SdkState.READY, state.current) + } + + @Test + fun `an answer whose start a deletion ended shows nothing and makes nothing usable`() { + // Held right as the answer is taken: the hooks see the new start and do nothing. + val init = started() + val first = calls.single() + init.holdForRestart() + first.complete(ok("STALE")) + scheduler.runCurrent() + + assertTrue(answers.isEmpty()) + assertEquals(0, usable) + } + @Test fun `needReInitialization forgets the did, the session and the saved answer before init`() { settings.updateDid("OLD") @@ -595,6 +648,112 @@ class CoreInitTest { // Restart and release. + @Test + fun `while a deletion holds the instance no init goes, not on coming back nor on checkShop`() { + val init = started() + answer(ok("D1", "S1")) + lifecycle.set(false) + + init.holdForRestart() + clock.advance(CoreInit.SESSION_WINDOW_MS + 1) + lifecycle.set(true) + scheduler.runCurrent() + assertFalse(init.checkShop()) + after(CoreInit.CHECK_SHOP_INTERVAL_MS) + assertEquals(1, sent.size) + + init.restart() + scheduler.runCurrent() + assertEquals(2, sent.size) + } + + @Test + fun `the storage is prepared off the caller's thread, a stored did is ready at once`() { + storage.saveInitAnswer(JSONObject().put("did", "SAVED")) + val init = init() + + init.start() + // Nothing read from the file and nothing sent yet: that is the io dispatcher's work. + assertEquals("", settings.getDid()) + assertEquals(SdkState.INITIALIZING, state.current) + assertTrue(sent.isEmpty()) + + scheduler.runCurrent() + assertEquals("SAVED", settings.getDid()) + assertEquals(SdkState.READY, state.current) + assertEquals(1, sent.size) + assertEquals(1, usable) + } + + @Test + fun `coming back to the foreground is handled off the main thread`() { + started() + answer(ok("D1", "S1")) + lifecycle.set(false) + clock.now = settings.getSidLastActTime() + CoreInit.SESSION_WINDOW_MS + 1 + + lifecycle.set(true) + assertEquals(1, sent.size) + scheduler.runCurrent() + assertEquals(2, sent.size) + } + + @Test + fun `after a wipe the saved answer gives no did until an init succeeds`() { + // The wipe committed its preferences, but the saved answer's file outlived it. + storage.saveInitAnswer(JSONObject().put("did", "FORGOTTEN")) + storage.isWiped = true + started() + + assertEquals("", settings.getDid()) + assertEquals(SdkState.INITIALIZING, state.current) + assertEquals("init goes without a did", 1, sent.size) + answer(ok("NEW", "S")) + assertEquals("NEW", settings.getDid()) + assertFalse(storage.isWiped) + inits.single().release() + + // Later launches read the new answer again. + settings.removeDid() + started() + assertEquals("NEW", settings.getDid()) + } + + @Test + fun `a release that comes while start adds its lifecycle listener does not leave it behind`() { + lateinit var racing: CoreInit + var listening = 0 + val raceLifecycle = object : AppLifecycle { + override val isForeground = true + override fun addListener(listener: (foreground: Boolean) -> Unit): com.personalization.Cancellable { + listening++ + racing.release() + return com.personalization.Cancellable { listening-- } + } + } + racing = CoreInit( + setup = InitSetup(SHOP), + send = { sent += it; ok() }, + settings = settings, + storage = storage, + state = state, + connectivity = connectivity, + lifecycle = raceLifecycle, + hooks = object : InitHooks { + override fun onAnswer(answer: JSONObject, fresh: Boolean) = Unit + override fun onUsable() = Unit + }, + dispatchers = dispatchers, + clock = clock, + ) + + racing.start() + scheduler.runCurrent() + + assertEquals(0, listening) + assertTrue(sent.isEmpty()) + } + @Test fun `an answer to an init sent before a restart is not taken`() { val init = started() diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt index ffce0536..db123b43 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreV2Test.kt @@ -1,5 +1,6 @@ package com.personalization.core.internal +import androidx.test.core.app.ApplicationProvider import com.personalization.LogLevel import com.personalization.SdkLogHandler import com.personalization.SdkLogMessage @@ -47,7 +48,7 @@ class CoreV2Test { fun setUp() { server.start() val client = CoreHttpClient({ server.url("/").toString() }, settings, connectivity, "shop") - core = CoreV2(client, "shop") + core = CoreV2(client, "shop").also { it.init = startedCoreInit(ApplicationProvider.getApplicationContext(), settings, connectivity) } } @After @@ -131,6 +132,38 @@ class CoreV2Test { assertTrue(logged.none { "a@example.com" in it.message || "a@example.com" in it.context.toString() }) } + @Test + fun `a request waits for the core's init to be set, within its read timeout`() { + val client = CoreHttpClient({ server.url("/").toString() }, settings, connectivity, "shop", timeouts = CoreTimeouts(readMs = 300)) + val bare = CoreV2(client, "shop") + val timedOut = Recorder() + bare.send(NetworkMethod.GET("products/get"), JSONObject(), timedOut) + assertEquals(listOf("error:-1"), timedOut.await().map { it.substringBefore(":The") }) + assertEquals(0, server.requestCount) + + server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) + val waiting = Recorder() + bare.send(NetworkMethod.GET("products/get"), JSONObject(), waiting) + bare.init = startedCoreInit(ApplicationProvider.getApplicationContext(), settings, connectivity) + assertEquals(listOf("success:{}"), waiting.await()) + bare.release() + } + + @Test + fun `a release that lets requests finish gives the one in flight its answer`() { + server.enqueue(MockResponse().setResponseCode(200).setBody("{}").setHeadersDelay(300, TimeUnit.MILLISECONDS)) + val inFlight = Recorder() + core.send(NetworkMethod.GET("products/get"), JSONObject(), inFlight) + server.takeRequest(5, TimeUnit.SECONDS) + + core.release(cancelRequests = false) + + assertEquals(listOf("success:{}"), inFlight.await()) + val after = Recorder() + core.send(NetworkMethod.GET("products/get"), JSONObject(), after) + assertEquals(listOf("error:-1"), after.await().map { it.substringBefore(":The") }) + } + @Test fun `a request without a listener still goes`() { server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt index 0db1826a..76ce6f16 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt @@ -142,9 +142,12 @@ class ShopStorageTest { "token" to "fcm-old", "push_token_hms" to "hms-1", StorageKeys.MIGRATED_LEGACY_FILE to true, + // Written in the wipe's own commit: a saved answer that outlived it gives no did. + StorageKeys.CREDENTIALS_WIPED to true, ), left ) + assertTrue(storage.isWiped) assertNull(storage.initAnswer()) // The legacy core has no queue: its directory is not its to delete. assertTrue(File(queue, "event-1").isFile) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/logs/LogCodesTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/logs/LogCodesTest.kt index 83b1cf33..7135907a 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/logs/LogCodesTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/logs/LogCodesTest.kt @@ -274,7 +274,7 @@ class LogCodesTest { "push_received", "push_dropped", "push_failed", "push_permission", "push_permission_failed", "push_token_sent", "push_token_skipped", "push_token_failed", "advertising_id_sent", "advertising_id_failed", "popup_not_shown", "core_flag_mismatch", "not_implemented", - "request_cancelled", "init_skipped", "push_token_received", + "request_cancelled", "init_skipped", "push_token_received", "device_id_kept", "sdk_log", "ui", "stories", "stories_v3", "popups_v3", "push", ) } From a22f835d5d34480333c3e57fed4853874c579b2c Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 01:00:51 +0400 Subject: [PATCH 038/109] feat(core): the session of the new core (stage 4, core/session.md, decision 0001). The sid is the server's, the seance of /init; the core never makes one, and a sid an earlier release made goes out once, with the /init of a window still alive, before the server's replaces it. /init carries sid and seance only while the window is alive. The window is two hours from the last activity and every request of the shop checks it: when it has ended, /init goes first and the request waits for its answer within its read timeout, then goes with the new sid (or, when that /init fails and the instance works on what it has, without one); requests that find it ended together send one /init, a request at the start of a launch waits for the start's /init, and the state stays ready meanwhile. Only /init and the events of the queue extend the window; a request with a result no longer moves the last activity on the new core (the legacy one is unchanged). Coming back to the foreground after the window ended starts a new session. sessionId is the current sid, and the bridges get an internal listener of every new sid on the main thread (SDK.onSessionChange). --- .../main/kotlin/com/personalization/SDK.kt | 41 +++++- .../com/personalization/core/internal/Core.kt | 5 +- .../personalization/core/internal/CoreInit.kt | 119 +++++++++++++++--- .../personalization/core/internal/CoreV2.kt | 15 ++- .../core/internal/SessionWindow.kt | 92 ++++++++++++++ .../network/NetworkRepositoryImpl.kt | 5 +- 6 files changed, 248 insertions(+), 29 deletions(-) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/SessionWindow.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index fe2ace17..82720505 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -43,6 +43,7 @@ import com.personalization.core.internal.InitHooks import com.personalization.core.internal.InitSetup import com.personalization.core.internal.InstanceState import com.personalization.core.internal.ProcessAppLifecycle +import com.personalization.core.internal.SessionWindow import com.personalization.core.internal.ShopStorage import com.personalization.core.internal.StorageLayout import com.personalization.core.internal.StorageMigrations @@ -84,6 +85,7 @@ import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.util.Locale +import java.util.concurrent.CopyOnWriteArrayList import javax.inject.Inject import org.json.JSONException import org.json.JSONObject @@ -251,10 +253,44 @@ open class SDK { val deviceId: String? get() = if (::userSettingsRepository.isInitialized) userSettingsRepository.getDid().ifEmpty { null } else null - /** The `sid` of the current session; null while there is none, or before initialization. */ + /** + * The `sid` of the current session; null while there is none, or before initialization. On the new + * core it is the server's (the `seance` of `/init`), and a session two hours without events ends: + * the next request or event starts a new one with `/init` first. + */ val sessionId: String? get() = if (::userSettingsRepository.isInitialized) userSettingsRepository.getSid().ifEmpty { null } else null + /** The bridges' listeners of a new `sid` ([onSessionChange]). */ + private val sessionListeners = CopyOnWriteArrayList<(String) -> Unit>() + + /** + * For the bridges (stage 7): [listener] hears every new `sid` of the new core, on the main thread, + * until the handle is cancelled — instead of asking [sessionId] on a timer. The legacy core does not + * call it. May be set before the instance is initialized. + */ + internal fun onSessionChange(listener: (sessionId: String) -> Unit): Cancellable { + sessionListeners += listener + return Cancellable { sessionListeners -= listener } + } + + /** Tells the bridges' listeners about [sid] on the main thread; one that throws is logged. */ + private fun sessionChanged(sid: String) { + val targets = sessionListeners.toList() + if (targets.isEmpty()) return + calls.launch { + withContext(dispatchers.main) { + targets.forEach { listener -> + try { + listener(sid) + } catch (e: Exception) { + log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "A session listener threw", throwable = e) + } + } + } + } + } + /** The level of this instance's log; [Rees46Config.logLevel] sets it for instances made through [Rees46]. */ internal var logLevel: LogLevel = LogLevel.WARNING @@ -623,6 +659,9 @@ open class SDK { state = instanceState, connectivity = connectivity, lifecycle = ProcessAppLifecycle(), + session = SessionWindow(userSettingsRepository, storage).also { session -> + session.addListener { sid, changed -> if (changed) sessionChanged(sid) } + }, hooks = object : InitHooks { override fun onAnswer(answer: JSONObject, fresh: Boolean) { val response = answer.mapToSdkInitResponse() diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt index 27d17ce2..70b53d49 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt @@ -10,9 +10,8 @@ import org.json.JSONObject * `SendNetworkMethodUseCase`) hands every request to the instance's core, so the namespaces are the * same code for both cores. * - * [LegacyCore] keeps today's paths. [CoreV2] is the new core, chosen by `Rees46Config.coreV2`; for now - * it has only the new client, while `/init`, the session and the requests waiting for a `did` stay the - * legacy ones for both cores. + * [LegacyCore] keeps today's paths. [CoreV2] is the new core, chosen by `Rees46Config.coreV2`: its own + * client, `/init` ([CoreInit]) and session ([SessionWindow]). */ internal interface Core { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt index c5be996c..2ea4cf42 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt @@ -18,6 +18,7 @@ import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import kotlinx.coroutines.withTimeoutOrNull @@ -71,11 +72,15 @@ internal fun interface ReadyGate { * until the server answers. * - **Without an answer** an instance with a `did` works as [SdkState.READY] (`init_saved_answer`); one * without waits for the network instead of failing again. - * - **Later `/init`.** On coming back to the foreground more than two hours after the last activity, and - * on [checkShop]. A refused shop is not asked again before the next launch. + * - **Later `/init`.** When the session's window ended ([SessionWindow], `core/session.md`): found by + * a request ([awaitUsable]), by the queue before an event ([renewSession]) or on coming back to the + * foreground; and on [checkShop]. A refused shop is not asked again before the next launch. + * - **Session.** `/init` carries the `sid` only while its window is alive; the `seance` of the answer is + * the session from then on. The core never makes a `sid` of its own. * - * Requests with a result wait in [awaitReady]. A stale answer (an `/init` of a start [restart] or - * [release] ended) is dropped, and while [holdForRestart] holds the instance no `/init` goes at all. + * Requests with a result wait in [awaitUsable]: for the instance to be ready and, when the window + * ended, for the `/init` of a new session. A stale answer (an `/init` of a start [restart] or [release] + * ended) is dropped, and while [holdForRestart] holds the instance no `/init` goes at all. * * Disk work stays off the caller's thread: [start] decides the first state from what the preferences * already hold (a stored `did` is ready at once) and prepares the rest of the storage on [CoreDispatchers.io] @@ -95,6 +100,8 @@ internal class CoreInit( private val clock: Clock = Clock.SYSTEM, private val backoff: Backoff = Backoff(), private val timeZone: () -> TimeZone = { TimeZone.getDefault() }, + /** The shop's session; the instance's other parts read it too. */ + internal val session: SessionWindow = SessionWindow(settings, storage, clock), ) : ReadyGate { /** Whether a request may go: computed from the state on every change. */ @@ -108,6 +115,9 @@ internal class CoreInit( private val gate = MutableStateFlow(Gate.WAIT) + /** How many answers to `/init` were taken: a wait for the next one watches it. */ + private val answers = MutableStateFlow(0L) + /** Guards the fields below and every write an answer makes, so a restart never lets a stale one in. */ private val lock = Any() private var job: Job? = null @@ -117,6 +127,10 @@ internal class CoreInit( /** Storage prepared for the first `/init`; none goes before. */ private var prepared = false + /** `/init` is not sent on this device (a Google Play pre-launch report device). */ + @Volatile + private var skipped = false + /** Held by [holdForRestart] until [restart]: the customer is being deleted, no `/init` goes. */ private var held = false private var usable = false @@ -133,6 +147,11 @@ internal class CoreInit( internal val isRunning: Boolean get() = synchronized(lock) { job?.isActive == true } + /** Returns once the `/init` running now, if any, has ended; for tests. */ + internal suspend fun awaitIdle() { + synchronized(lock) { job }?.join() + } + /** Read once and reused: the advertising id goes with every `/init` of the launch. */ @Volatile private var advertisingIdValue: String? = null @@ -189,6 +208,69 @@ internal class CoreInit( /** Waits without a limit until a request may go or may not; the caller sets the deadline. */ internal suspend fun ready(): SdkError? = errorOf(gate.first { it != Gate.WAIT }) + /** For a request with a result: [usable] within [timeoutMs]. */ + suspend fun awaitUsable(timeoutMs: Long): SdkError? { + val verdict = withTimeoutOrNull(timeoutMs) { Verdict(usable()) } ?: return SdkError.Timeout + return verdict.error + } + + private class Verdict(val error: SdkError?) + + /** + * Waits, without a limit (the caller sets the deadline), until a request may go: the instance is + * ready, and when the session's window has ended, the `/init` of a new session ([renewSession]) has + * answered. The request then goes with the new `sid`, or, when that `/init` failed and the instance + * works on what it has, without one (decision 1). Null when it may go; otherwise `shopBlocked` or + * `notInitialized`. + */ + internal suspend fun usable(): SdkError? { + var renewed = false + while (true) { + errorOf(gate.first { it != Gate.WAIT })?.let { return it } + if (renewed || session.isAlive()) return null + val seen = answers.value + if (!renewSession()) return null + // The answer, or the gate closing (a release, a deletion's hold) ends the wait. + combine(answers, gate) { count, current -> count > seen || current != Gate.OPEN }.first { it } + renewed = true + } + } + + /** + * Starts a new session when the window of the current one has ended: its `sid` is forgotten and + * `/init` goes, unless one is on its way. True when a caller should wait for the answer: the instance + * is [SdkState.READY] and `/init` is out. While `/init` is being repeated ([SdkState.RETRYING]) the + * instance works on what it has and nobody waits; a refused or released instance renews nothing. + */ + internal fun renewSession(): Boolean = synchronized(lock) { + if (released || skipped || state.current == SdkState.REFUSED || session.isAlive()) return false + session.forgetIfEnded() + launchLocked() + // Before the storage is prepared the start's own /init is about to go: that is the one to wait for. + state.current == SdkState.READY && (job?.isActive == true || !prepared) + } + + /** + * Asks the server about the shop after a `402`, `403` or `404` to an event ([checkShop]), and waits + * up to [timeoutMs] for the answer of the `/init` that went, or of the one already on its way. The + * state after it: [SdkState.READY] when the shop is fine, [SdkState.REFUSED] when it is not, anything + * else when the answer did not tell. Without an `/init` to wait for (one went less than 10 minutes + * ago) the state is as that one left it. + */ + internal suspend fun checkShopAndAwait(timeoutMs: Long): SdkState { + val seen: Long + synchronized(lock) { + if (released) return SdkState.NOT_INITIALIZED + seen = answers.value + checkShop() + if (job?.isActive != true) return state.current + } + withTimeoutOrNull(timeoutMs) { + combine(answers, gate) { count, current -> count > seen || current == Gate.CLOSED }.first { it } + } + return state.current + } + /** Null for an open gate, otherwise why a request may not go; [Gate.WAIT] is never decided. */ private fun errorOf(decided: Gate): SdkError? = when (decided) { Gate.BLOCKED -> SdkError.ShopBlocked @@ -289,9 +371,12 @@ internal class CoreInit( } private suspend fun run(start: Int) { - if (setup.skipInit()) { - log(LogLevel.INFO, LogCodes.INIT_SKIPPED, "/init is not sent on a Google Play pre-launch report device", - mapOf("reason" to LogReasons.PRE_LAUNCH_REPORT)) + if (skipped || setup.skipInit()) { + if (!skipped) { + skipped = true + log(LogLevel.INFO, LogCodes.INIT_SKIPPED, "/init is not sent on a Google Play pre-launch report device", + mapOf("reason" to LogReasons.PRE_LAUNCH_REPORT)) + } return } var attempt = 0 @@ -305,7 +390,7 @@ internal class CoreInit( val step = synchronized(lock) { // An answer to a start that restart() or release() ended belongs to nobody. if (start != generation || released) return - take(result, attempt) + take(result, attempt).also { answers.value = answers.value + 1 } } when (step) { Step.Done -> return @@ -364,7 +449,9 @@ internal class CoreInit( /** Must hold [lock]. */ private fun succeed(answer: JSONObject, did: String) { if (settings.getDid().isEmpty()) settings.updateDid(did) - answer.optString(UserBasicParams.SEANCE).takeIf { it.isNotEmpty() }?.let(settings::updateSid) + // The server's sid replaces whatever went: a sid an earlier release made goes out only once. + val seance = answer.optString(UserBasicParams.SEANCE) + if (seance.isNotEmpty()) session.start(seance) else session.extend() storage.saveInitAnswer(answer) // The answer on disk is the new customer's now. if (storage.isWiped) storage.isWiped = false @@ -439,8 +526,7 @@ internal class CoreInit( /** A `sid` whose window ended (two hours without activity) is not sent: the server starts a new session. */ private fun forgetStaleSession() { - if (settings.getSid().isEmpty()) return - if (clock.nowMillis() - settings.getSidLastActTime() > SESSION_WINDOW_MS) storage.forgetSession() + session.forgetIfEnded() } /** Heard on the main thread; handled on [CoreDispatchers.io], where waiting for [lock] blocks nothing. */ @@ -449,12 +535,9 @@ internal class CoreInit( scope.launch { onForeground() } } + /** Coming back after the window ended starts a new session. */ private fun onForeground() { - synchronized(lock) { - if (released || state.current == SdkState.REFUSED || job?.isActive == true) return - if (clock.nowMillis() - settings.getSidLastActTime() <= SESSION_WINDOW_MS) return - launchLocked() - } + renewSession() } /** The fields of `/init` beside the common ones: `tz`, the advertising id, the extras. */ @@ -526,8 +609,8 @@ internal class CoreInit( SdkLogs.log(level, code, message, shopId, context, throwable) internal companion object { - /** Two hours without activity end a session. */ - const val SESSION_WINDOW_MS = 2 * 60 * 60 * 1000L + /** Two hours without activity end a session ([SessionWindow.WINDOW_MS]). */ + const val SESSION_WINDOW_MS = SessionWindow.WINDOW_MS /** [checkShop] asks the server at most this often. */ const val CHECK_SHOP_INTERVAL_MS = 10 * 60 * 1000L diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt index 7a68233f..fbf8f2f6 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt @@ -22,9 +22,11 @@ import java.util.concurrent.atomic.AtomicBoolean /** * The new core: the instance's requests go through [CoreHttpClient], and its `/init` and state are - * [CoreInit]'s. A request waits for the instance to be ready within its own read timeout: a refused - * shop fails it with `shopBlocked` (the old callbacks get `402`), a wait that runs out with `timeout`. - * The session and the queue stay the legacy ones until the core takes them over. + * [CoreInit]'s, its session [SessionWindow]'s. A request waits for the instance to be ready within its + * own read timeout, and for the `/init` of a new session when the window of the last one ended: a + * refused shop fails it with `shopBlocked` (the old callbacks get `402`), a wait that runs out with + * `timeout`. A request with a result does not extend the session. The queue stays the legacy one until + * the core takes it over. * * Every request ends in exactly one callback, on a background thread as today: the answer, or the * error mapped to the old `onError(code, msg)` by [LegacyErrors]. [release] cancels the requests in @@ -117,13 +119,14 @@ internal class CoreV2( } /** - * Waits, within the read timeout, for the instance's `/init` to be set and for the instance to be - * ready; then the request gets its own full timeout. Null when it may go. + * Waits, within the read timeout, for the instance's `/init` to be set, for the instance to be ready + * and, when the session's window ended, for the `/init` of a new session ([CoreInit.usable]); then + * the request gets its own full timeout. Null when it may go. */ private suspend fun awaitInstance(): SdkError? { val waited = withTimeoutOrNull(readyWaitMs) { val current = initFlow.filterNotNull().first() - Verdict(current.ready()) + Verdict(current.usable()) } return if (waited == null) SdkError.Timeout else waited.error } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/SessionWindow.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/SessionWindow.kt new file mode 100644 index 00000000..ac0e2d6a --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/SessionWindow.kt @@ -0,0 +1,92 @@ +package com.personalization.core.internal + +import com.personalization.Cancellable +import com.personalization.LogLevel +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import java.util.concurrent.CopyOnWriteArrayList + +/** + * A shop's session on the new core (`core/session.md`, decision 0001): the `sid` the server hands out in + * the `seance` of `/init`, and the time of the last activity, both kept in the shop's partition, so a + * restart inside the window goes on with the same session. + * + * - The core never makes a `sid` of its own. A `sid` an earlier release made goes out once, with the + * `/init` of a window still alive, and the server's answer replaces it. + * - The window is alive while there is a `sid` and the last activity is at most [windowMs] ago. + * - Only `/init` ([start]) and the events of the queue ([extend]) move the last activity; requests with + * a result do not, as the server does not extend its own session on them either. + * + * Listeners hear every `sid` `/init` brings, and whether it differs from the one before ("the session + * changed", for the bridges). They are called on the thread that stored it and must not block. + */ +internal class SessionWindow( + private val settings: UserSettingsRepository, + private val storage: ShopStorage, + private val clock: Clock = Clock.SYSTEM, + val windowMs: Long = WINDOW_MS, +) { + + private val lock = Any() + private val listeners = CopyOnWriteArrayList<(sid: String, changed: Boolean) -> Unit>() + + /** The last `sid` listeners heard of; a `sid` forgotten when its window ended still counts. */ + private var lastKnown: String = settings.getSid() + + /** Whether the session is alive at [now]: a `sid`, and its last activity at most [windowMs] before. */ + fun isAlive(now: Long = clock.nowMillis()): Boolean = synchronized(lock) { alive(now) } + + private fun alive(now: Long): Boolean = + settings.getSid().isNotEmpty() && now - settings.getSidLastActTime() <= windowMs + + /** + * Ends a session whose window has passed: its `sid` and last activity go, so `/init` goes without + * them and the server starts a new session. True when one ended. + */ + fun forgetIfEnded(now: Long = clock.nowMillis()): Boolean = synchronized(lock) { + if (settings.getSid().isEmpty() || alive(now)) return false + storage.forgetSession() + true + } + + /** + * An event of the queue accepted at [at]: the last activity moves there while the session is alive. + * An ended session is not brought back: the event goes after the `/init` of a new one. + */ + fun extend(at: Long = clock.nowMillis()) { + synchronized(lock) { + if (!alive(at) || at <= settings.getSidLastActTime()) return + storage.preferences.edit().putLong(StorageKeys.SID_LAST_ACT, at).apply() + } + } + + /** `/init` answered with [sid]: the session is [sid] and its last activity now. */ + fun start(sid: String) { + val changed = synchronized(lock) { + storage.preferences.edit() + .putString(StorageKeys.SID, sid) + .putLong(StorageKeys.SID_LAST_ACT, clock.nowMillis()) + .apply() + (sid != lastKnown).also { lastKnown = sid } + } + listeners.forEach { listener -> + try { + listener(sid, changed) + } catch (e: Exception) { + SdkLogs.log(LogLevel.ERROR, LogCodes.SDK_LOG, "A session listener failed", storage.layout.shopId, throwable = e) + } + } + } + + /** Calls [listener] with each `sid` `/init` brings until the handle is cancelled. */ + fun addListener(listener: (sid: String, changed: Boolean) -> Unit): Cancellable { + listeners += listener + return Cancellable { listeners -= listener } + } + + internal companion object { + /** Two hours without activity end a session. */ + const val WINDOW_MS = 2 * 60 * 60 * 1000L + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt index 62dbb307..7eb91775 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt @@ -194,7 +194,10 @@ class NetworkRepositoryImpl @Inject constructor( params: JSONObject, listener: OnApiCallbackListener?, ) { - userSettingsRepository.updateSidLastActTime() + val core = core + // The legacy session moves with every request. On the new core only /init and the queue's + // events move it (core/session.md): a request with a result does not. + if (!core.isV2) userSettingsRepository.updateSidLastActTime() core.send(networkMethod, params, listener) } } From 04032b19d377d09997dfbd3a92eac0135fdc942f Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 01:00:51 +0400 Subject: [PATCH 039/109] test(core): the session: a new device has no sid until /init brings the server's, an install's own sid goes once with a live window and the server's replaces it, an ended sid stays out of /init, a restart inside the window goes on with it; a request inside the window goes at once without extending it, one after it waits for the new session's /init and goes with its sid while the state stays ready, several share one /init, a failed /init lets them go on what the instance has, the wait ends with the request's timeout, and a request at the start of a launch waits for the start's /init; an event extends a live session and not an ended one; the foreground within and after the window; the listeners hear each sid and whether it changed, the bridges on the main thread through the SDK; a refused or released instance renews nothing; the funnel moves the legacy session only --- .../com/personalization/SdkInstanceTest.kt | 21 ++ .../core/internal/CoreFunnelTest.kt | 15 + .../core/internal/CoreSessionTest.kt | 353 ++++++++++++++++++ 3 files changed, 389 insertions(+) create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt index 59151d66..2d9039f1 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt @@ -150,6 +150,8 @@ class SdkInstanceTest { private fun assertOneInit(sdk: SDK) { threads.awaitIdle() (sdk.core as? CoreV2)?.init?.let { init -> + // The answer may still be handled after the state turned ready. + runBlocking { withTimeout(5_000) { init.awaitIdle() } } assertFalse("an /init is on its way", init.isRunning) assertEquals(1, init.sent) } @@ -452,6 +454,25 @@ class SdkInstanceTest { assertTrue(logged.any { it.code == LogCodes.DEVICE_ID_RESET && it.context["reason"] == "need_reinitialization" }) } + @Test + fun `on the new core the session is the server's and the bridges hear it on the main thread`() { + val heard = CopyOnWriteArrayList>() + val sdk = SDK() + sdk.onSessionChange { heard += it to (Looper.myLooper() == Looper.getMainLooper()) } + sdk.initializeWith(context, config(coreV2 = true)) + nextRequest("init") + awaitState(sdk, SdkState.READY) + // Handed to the main thread from the io dispatcher: run the main looper until it arrives. + val deadline = System.currentTimeMillis() + 5_000 + while (heard.isEmpty() && System.currentTimeMillis() < deadline) { + shadowOf(Looper.getMainLooper()).idle() + Thread.sleep(5) + } + + assertEquals("S", sdk.sessionId) + assertEquals(listOf("S" to true), heard) + } + @Test fun `on the new core a stored did is ready at once and its init still goes`() { partition().edit().putString("did", "stored-did").commit() diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt index f87fbfce..410d9e47 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFunnelTest.kt @@ -12,6 +12,7 @@ import com.personalization.sdk.data.repositories.network.NetworkRepositoryImpl import com.personalization.sdk.domain.repositories.UserSettingsRepository import io.mockk.every import io.mockk.mockk +import io.mockk.verify import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockWebServer import org.json.JSONObject @@ -103,6 +104,20 @@ class CoreFunnelTest { assertEquals(1, server.requestCount) } + @Test + fun `a request moves the legacy session's last activity, not the new core's`() { + server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) + await { repository.get("products/get", JSONObject(), it) } + verify(exactly = 1) { settings.updateSidLastActTime() } + + repository.useCoreV2("shop", FakeConnectivity(connected = true)) { + it.init = startedCoreInit(ApplicationProvider.getApplicationContext(), settings) + } + server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) + await { repository.get("products/get", JSONObject(), it) } + verify(exactly = 1) { settings.updateSidLastActTime() } + } + @Test fun `an instance made with coreV2 runs on the new core, its requests on the new client`() { server.enqueue(MockResponse().setResponseCode(200).setBody("""{"did":"server-did","seance":"S"}""")) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt new file mode 100644 index 00000000..26b58e40 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt @@ -0,0 +1,353 @@ +package com.personalization.core.internal + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import com.personalization.core.SdkError +import com.personalization.core.SdkState +import com.personalization.logs.SdkLogs +import com.personalization.sdk.data.repositories.preferences.PreferencesDataSourceImpl +import com.personalization.sdk.data.repositories.userSettings.UserSettingsRepositoryImpl +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.async +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.TestCoroutineScheduler +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.io.File + +/** + * The new core's session (`core/session.md`, decision 0001): the server's `sid`, the two-hour window + * every request checks, what extends it and what does not, and "the session changed" for the bridges. + * Time is the fake clock's for the window and the test scheduler's for the waits. + */ +@OptIn(ExperimentalCoroutinesApi::class) +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class CoreSessionTest { + + private lateinit var context: Context + private lateinit var storage: ShopStorage + private lateinit var settings: UserSettingsRepository + private val layout = StorageLayout(SHOP, null) + private val scheduler = TestCoroutineScheduler() + private val dispatchers = testCoreDispatchers(scheduler) + private val clock = FakeClock() + private val connectivity = FakeConnectivity(connected = true) + private val lifecycle = FakeAppLifecycle(foreground = true) + private val state = InstanceState(shopId = { SHOP }, dispatchers = dispatchers) + + private val calls = mutableListOf>() + private val sidAtSend = mutableListOf() + private val inits = mutableListOf() + private val heard = mutableListOf>() + + @Before + fun setUp() { + context = ApplicationProvider.getApplicationContext() + context.getSharedPreferences(layout.partitionName, Context.MODE_PRIVATE).edit().clear().commit() + File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() + storage = ShopStorage(context, layout) + settings = UserSettingsRepositoryImpl(PreferencesDataSourceImpl().apply { initialize(context, layout.partitionName, SHOP) }) + SdkLogs.reset() + } + + @After + fun tearDown() { + inits.forEach { it.release() } + SdkLogs.reset() + } + + private fun started(): CoreInit { + val session = SessionWindow(settings, storage, clock).also { it.addListener { sid, changed -> heard += sid to changed } } + return CoreInit( + setup = InitSetup(SHOP), + send = { + sidAtSend += settings.getSid() + CompletableDeferred().also { calls += it }.await() + }, + settings = settings, + storage = storage, + state = state, + connectivity = connectivity, + lifecycle = lifecycle, + hooks = object : InitHooks { + override fun onAnswer(answer: JSONObject, fresh: Boolean) = Unit + override fun onUsable() = Unit + }, + dispatchers = dispatchers, + clock = clock, + backoff = Backoff(random = { 0.5 }), + session = session, + ).also { + inits += it + it.start() + scheduler.runCurrent() + } + } + + private fun answer(result: HttpResult) { + calls.first { it.isActive }.complete(result) + scheduler.runCurrent() + } + + private fun ok(sid: String, did: String = "D1") = HttpResult.Success(JSONObject().put("did", did).put("seance", sid)) + + /** A request with a result asking to go, as [CoreV2] asks. */ + private fun request(init: CoreInit, timeoutMs: Long = 10_000): Deferred = + CoroutineScope(StandardTestDispatcher(scheduler)).async { init.awaitUsable(timeoutMs) }.also { scheduler.runCurrent() } + + private fun knownDevice(sid: String, lastActivity: Long) { + settings.updateDid("D1") + storage.preferences.edit().putString(StorageKeys.SID, sid).putLong(StorageKeys.SID_LAST_ACT, lastActivity).commit() + } + + private val lastActivity: Long get() = settings.getSidLastActTime() + + // The sid comes from the server. + + @Test + fun `a new device has no sid until init brings the server's, and the core makes none`() { + val init = started() + assertEquals("", settings.getSid()) + assertEquals(listOf(""), sidAtSend) + + answer(ok("SERVER1")) + + assertEquals("SERVER1", settings.getSid()) + assertEquals(clock.now, lastActivity) + assertTrue(init.session.isAlive()) + } + + @Test + fun `an install's own sid goes out once with init while its window is alive, then the server's is used`() { + knownDevice("CLIENT_MADE", lastActivity = clock.now - 60_000) + started() + + assertEquals(listOf("CLIENT_MADE"), sidAtSend) + answer(ok("SERVER1")) + assertEquals("SERVER1", settings.getSid()) + } + + @Test + fun `init leaves out a sid whose window ended`() { + knownDevice("OLD", lastActivity = clock.now - SessionWindow.WINDOW_MS - 1) + started() + + assertEquals(listOf(""), sidAtSend) + answer(ok("NEW")) + assertEquals("NEW", settings.getSid()) + } + + @Test + fun `a restart inside the window goes on with the same session`() { + val first = started() + answer(ok("S1")) + first.release() + clock.advance(SessionWindow.WINDOW_MS - 1) + + val next = started() + + assertTrue(next.session.isAlive()) + assertEquals("S1", sidAtSend.last()) + } + + // The window every request checks. + + @Test + fun `a request inside the window goes at once and does not extend it`() { + val init = started() + answer(ok("S1")) + val startedAt = lastActivity + clock.advance(SessionWindow.WINDOW_MS - 1) + + val waiting = request(init) + + assertTrue(waiting.isCompleted) + assertNull(waiting.getCompleted()) + assertEquals(1, sidAtSend.size) + assertEquals("a request with a result does not extend the session", startedAt, lastActivity) + } + + @Test + fun `a request after the window ended sends init first and goes with the new sid, the state staying ready`() { + val init = started() + answer(ok("S1")) + val states = mutableListOf() + state.addListener { states += it } + scheduler.runCurrent() + states.clear() + clock.advance(SessionWindow.WINDOW_MS + 1) + + val waiting = request(init) + + assertFalse("waits for the new session", waiting.isCompleted) + assertEquals("the ended sid does not go", listOf("", ""), sidAtSend) + answer(ok("S2")) + assertNull(waiting.getCompleted()) + assertEquals("S2", settings.getSid()) + assertEquals(clock.now, lastActivity) + assertEquals(SdkState.READY, state.current) + assertTrue("no state change for a new session", states.isEmpty()) + } + + @Test + fun `requests that find the window ended together send one init`() { + val init = started() + answer(ok("S1")) + clock.advance(SessionWindow.WINDOW_MS + 1) + + val first = request(init) + val second = request(init) + + assertEquals(2, sidAtSend.size) + answer(ok("S2")) + assertNull(first.getCompleted()) + assertNull(second.getCompleted()) + } + + @Test + fun `when the new session's init fails the request goes on what the instance has`() { + val init = started() + answer(ok("S1")) + clock.advance(SessionWindow.WINDOW_MS + 1) + + val waiting = request(init) + answer(HttpResult.Failure(SdkError.Http(503))) + + assertNull(waiting.getCompleted()) + assertEquals("", settings.getSid()) + assertEquals(SdkState.RETRYING, state.current) + // While init is repeated, a request does not wait for it. + assertNull(request(init).getCompleted()) + } + + @Test + fun `a wait for the new session runs out with the request's timeout`() { + val init = started() + answer(ok("S1")) + clock.advance(SessionWindow.WINDOW_MS + 1) + + val waiting = request(init, timeoutMs = 10_000) + scheduler.advanceTimeBy(10_001) + scheduler.runCurrent() + + assertEquals(SdkError.Timeout, waiting.getCompleted()) + } + + @Test + fun `a request at the start of a launch after the window ended waits for the start's init`() { + knownDevice("OLD", lastActivity = clock.now - SessionWindow.WINDOW_MS - 1) + val init = CoreInit( + setup = InitSetup(SHOP), + send = { sidAtSend += settings.getSid(); CompletableDeferred().also { calls += it }.await() }, + settings = settings, + storage = storage, + state = state, + connectivity = connectivity, + lifecycle = lifecycle, + hooks = object : InitHooks { + override fun onAnswer(answer: JSONObject, fresh: Boolean) = Unit + override fun onUsable() = Unit + }, + dispatchers = dispatchers, + clock = clock, + ).also { inits += it } + init.start() + // Ready at once on its stored did (decision 1), before the storage is even prepared. + assertEquals(SdkState.READY, state.current) + + val waiting = request(init) + assertFalse(waiting.isCompleted) + assertEquals("one init, the start's", 1, sidAtSend.size) + answer(ok("S2")) + assertNull(waiting.getCompleted()) + assertEquals("S2", settings.getSid()) + } + + // What extends the window. + + @Test + fun `an event extends a live session, not an ended one`() { + val init = started() + answer(ok("S1")) + + clock.advance(SessionWindow.WINDOW_MS - 1) + init.session.extend(clock.now) + assertEquals(clock.now, lastActivity) + clock.advance(SessionWindow.WINDOW_MS - 1) + assertTrue(init.session.isAlive()) + + clock.advance(SessionWindow.WINDOW_MS + 1) + val ended = lastActivity + init.session.extend(clock.now) + assertEquals(ended, lastActivity) + assertFalse(init.session.isAlive()) + } + + @Test + fun `coming back to the foreground inside the window sends nothing, after it a new session`() { + val init = started() + answer(ok("S1")) + lifecycle.set(false) + clock.advance(SessionWindow.WINDOW_MS) + lifecycle.set(true) + scheduler.runCurrent() + assertEquals(1, sidAtSend.size) + + lifecycle.set(false) + clock.advance(1) + lifecycle.set(true) + scheduler.runCurrent() + assertEquals(2, sidAtSend.size) + assertEquals("the ended sid does not go", "", sidAtSend.last()) + answer(ok("S2")) + assertTrue(init.session.isAlive()) + } + + // "The session changed". + + @Test + fun `the listeners hear every sid init brings, and whether it changed`() { + val init = started() + answer(ok("S1")) + init.checkShop() + scheduler.runCurrent() + answer(ok("S1")) + clock.advance(SessionWindow.WINDOW_MS + 1) + init.renewSession() + scheduler.runCurrent() + answer(ok("S2")) + + assertEquals(listOf("S1" to true, "S1" to false, "S2" to true), heard) + } + + @Test + fun `a refused or released instance renews nothing`() { + val init = started() + answer(HttpResult.Failure(SdkError.Http(403))) + clock.advance(SessionWindow.WINDOW_MS + 1) + + assertFalse(init.renewSession()) + assertEquals(SdkError.ShopBlocked, request(init).getCompleted()) + init.release() + assertFalse(init.renewSession()) + assertEquals(1, sidAtSend.size) + } + + private companion object { + const val SHOP = "session-shop" + } +} From 0a75f2006a1bcd84cde173151c14daebdf2f10a6 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 01:23:12 +0400 Subject: [PATCH 040/109] fix(core): a request that finds the session's window ended while the last /init is still handling its answer sends a new /init instead of waiting for an answer that has come already and timing out: once an /init has its answer, a new one may go --- .../kotlin/com/personalization/core/internal/CoreInit.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt index 2ea4cf42..8983b8a9 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt @@ -390,7 +390,11 @@ internal class CoreInit( val step = synchronized(lock) { // An answer to a start that restart() or release() ended belongs to nobody. if (start != generation || released) return - take(result, attempt).also { answers.value = answers.value + 1 } + take(result, attempt).also { step -> + answers.value = answers.value + 1 + // What follows an answer is the instance's work, not /init: a new one may go meanwhile. + if (step !is Step.Retry) job = null + } } when (step) { Step.Done -> return From 1c621687182fb46af704fc36774d4508502ae5e5 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 01:23:28 +0400 Subject: [PATCH 041/109] =?UTF-8?q?feat(core):=20the=20event=20queue=20of?= =?UTF-8?q?=20the=20new=20core=20(stage=205,=20core/queue.md,=20decision?= =?UTF-8?q?=200002).=20/push,=20/push/custom,=20track/stories,=20track/sli?= =?UTF-8?q?der=20and=20popup/showed=20go=20through=20a=20queue=20per=20sho?= =?UTF-8?q?p=20on=20disk=20(noBackupFilesDir,=20one=20file=20per=20event,?= =?UTF-8?q?=20written=20to=20a=20temporary=20file=20and=20renamed,=20in=20?= =?UTF-8?q?the=20calls'=20order);=20an=20event=20is=20accepted=20once=20it?= =?UTF-8?q?s=20file=20is=20written,=20a=20released=20instance=20gets=20not?= =?UTF-8?q?Initialized=20and=20a=20refused=20shop=20shopBlocked=20at=20onc?= =?UTF-8?q?e,=20and=20on=20the=20new=20core=20an=20empty=20id=20or=20query?= =?UTF-8?q?=20is=20refused=20before=20it=20is=20queued.=20The=20common=20f?= =?UTF-8?q?ields=20go=20on=20when=20an=20event=20is=20sent,=20with=20the?= =?UTF-8?q?=20sid=20of=20that=20moment,=20the=20stored=20tracking=20source?= =?UTF-8?q?=20as=20a=20source=20object=20on=20/push=20only=20and=20the=20t?= =?UTF-8?q?ime=20the=20event=20was=20made=20as=20time=20on=20/push/custom?= =?UTF-8?q?=20only,=20unless=20the=20call=20set=20its=20own.=20Events=20go?= =?UTF-8?q?=20one=20at=20a=20time,=20oldest=20first,=20once=20the=20instan?= =?UTF-8?q?ce=20is=20ready=20and=20the=20session=20alive;=20sending=20star?= =?UTF-8?q?ts=20on=20ready,=20on=20the=20app's=20foreground=20and=20backgr?= =?UTF-8?q?ound,=20on=20the=20network's=20return,=20on=20flush()=20and=20o?= =?UTF-8?q?n=20timers.=20A=202xx=20removes=20the=20event;=20no=20network?= =?UTF-8?q?=20waits=20for=20it=20and=20a=20timeout=20repeats=20it=20after?= =?UTF-8?q?=20a=20pause,=20neither=20counting;=205xx,=20408,=20410,=20417,?= =?UTF-8?q?=20426=20and=20429=20repeat=20it=20after=201=20s=20doubling=20t?= =?UTF-8?q?o=205=20min=20(=C2=B120=20%)=20or=20the=20Retry-After=20of=20a?= =?UTF-8?q?=20429=20or=20503,=20and=20the=20tenth=20drops=20it;=20402,=204?= =?UTF-8?q?03=20and=20404=20pause=20the=20queue=20for=20checkShop()=20thro?= =?UTF-8?q?ugh=20/init,=20at=20most=20every=2010=20minutes:=20a=20refusal?= =?UTF-8?q?=20clears=20the=20queue,=20a=20shop=20found=20fine=20sends=20th?= =?UTF-8?q?e=20event=20once=20more=20and=20a=20second=20such=20answer=20dr?= =?UTF-8?q?ops=20it;=20other=20answers=20drop=20it=20at=20once,=20and=20a?= =?UTF-8?q?=20dropped=20event=20never=20holds=20the=20others.=20Events=20o?= =?UTF-8?q?lder=20than=207=20days=20are=20dropped,=20at=201000=20the=20old?= =?UTF-8?q?est=20goes,=20and=20each=20drop=20is=20queue=5Fdropped=20with?= =?UTF-8?q?=20its=20reason.=20The=20old=20listeners=20hear=20an=20event's?= =?UTF-8?q?=20fate=20once:=20the=20server's=20answer=20when=20it=20went,?= =?UTF-8?q?=20or=20the=20error=20that=20dropped=20it;=20a=20popup=20in=20a?= =?UTF-8?q?n=20event's=20answer=20is=20shown=20only=20when=20the=20event?= =?UTF-8?q?=20went=20within=2010=20s=20of=20being=20made=20and=20the=20app?= =?UTF-8?q?=20is=20in=20the=20foreground,=20otherwise=20popup=5Fnot=5Fshow?= =?UTF-8?q?n.=20SDK.flush(),=20suspending,=20and=20its=20Java=20form=20wit?= =?UTF-8?q?h=20a=20CompletionListener=20send=20the=20queue=20now=20a?= =?UTF-8?q?nd=20return=20once=20it=20was=20tried;=20the=20legacy=20core=20?= =?UTF-8?q?returns=20at=20once.=20release()=20stops=20sending=20and=20keep?= =?UTF-8?q?s=20the=20files;=20deleteUserCredentials()=20and=20a=20refused?= =?UTF-8?q?=20shop=20clear=20the=20queue,=20an=20event=20accepted=20after?= =?UTF-8?q?=20the=20deletion's=20call=20staying.=20Two=20instances=20of=20?= =?UTF-8?q?one=20shop=20share=20the=20queue's=20files=20and=20only=20the?= =?UTF-8?q?=20one=20made=20last=20sends,=20so=20nothing=20goes=20twice.=20?= =?UTF-8?q?The=20legacy=20core=20is=20unchanged.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/personalization/Rees46Config.kt | 10 +- .../main/kotlin/com/personalization/SDK.kt | 65 ++- .../com/personalization/core/internal/Core.kt | 4 +- .../personalization/core/internal/CoreV2.kt | 48 +- .../core/internal/EventQueue.kt | 511 ++++++++++++++++++ .../core/internal/LegacyCore.kt | 2 +- .../core/internal/QueueStore.kt | 266 +++++++++ .../core/internal/ShopStorage.kt | 3 + .../com/personalization/di/SdkModule.kt | 6 +- .../trackEvent/impl/TrackEventManagerImpl.kt | 77 ++- .../features/tracking/impl/TrackingApiImpl.kt | 28 + .../com/personalization/logs/SdkLogs.kt | 26 + .../network/SendNetworkMethodUseCase.kt | 8 + 13 files changed, 999 insertions(+), 55 deletions(-) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt index 3b5770f4..000ab9f7 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46Config.kt @@ -32,7 +32,15 @@ package com.personalization * without a `did`: a new device's first requests fail with `onError(-1)` if `/init` does not answer * in time; * - [needReInitialization] really gives the device a new `did`; [stream] goes with `/init` too; the - * advertising id is read and sent only with [sendAdvertisingId]. + * advertising id is read and sent only with [sendAdvertisingId]; + * - its session: the `sid` is the server's, and a session two hours without events ends — the next + * request or event sends `/init` first; requests with a result no longer extend it; + * - its event queue: tracking events (`/push`, `/push/custom`, `track/stories`, `popup/showed`) are + * kept on disk and sent in order, repeated after failures and after a restart, for up to 7 days; + * their listeners hear the server's answer once the event went, or the error that dropped it, and an + * empty id is refused at once; [SDK.flush] sends the queue now; the stored tracking source goes on + * `/push` only, and a popup in an event's answer is shown only within 10 s of the event and in the + * foreground. * @property deviceId a `did` the app already keeps, from another SDK or an earlier version of it: taken * only while the shop has no `did` of its own, and sent with the next `/init`. Ignored with * [needReInitialization]. diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 82720505..3ade09f1 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -42,7 +42,9 @@ import com.personalization.core.internal.CoreV2 import com.personalization.core.internal.InitHooks import com.personalization.core.internal.InitSetup import com.personalization.core.internal.InstanceState +import com.personalization.core.internal.EventQueue import com.personalization.core.internal.ProcessAppLifecycle +import com.personalization.core.internal.QueueStore import com.personalization.core.internal.SessionWindow import com.personalization.core.internal.ShopStorage import com.personalization.core.internal.StorageLayout @@ -69,6 +71,7 @@ import com.personalization.sdk.domain.usecases.notification.GetAllNotificationsU import com.personalization.sdk.domain.usecases.preferences.GetPreferencesValueUseCase import com.personalization.sdk.domain.usecases.preferences.InitPreferencesUseCase import com.personalization.sdk.domain.usecases.recommendation.SetRecommendedByUseCase +import com.personalization.sdk.domain.usecases.trackingSource.GetTrackingSourceUseCase import com.personalization.sdk.domain.usecases.userSettings.GetUserSettingsValueUseCase import com.personalization.sdk.domain.usecases.userSettings.InitUserSettingsUseCase import com.personalization.sdk.domain.usecases.userSettings.InitializeAdvertisingIdUseCase @@ -96,7 +99,7 @@ private const val GAID_KEY = "google_advertising_id" * One shop's instance of the SDK: made by [Rees46.initialize], found with [Rees46.instance]. * * The class stays `open` for apps that subclassed it in earlier releases. The members this release - * adds — [shopId], [deviceId], [sessionId], [state], [onStateChange] and + * adds — [shopId], [deviceId], [sessionId], [state], [onStateChange], [flush] and * [deleteUserCredentials] — may clash with members of the same name in such a subclass: a subclass * that declares one of them needs to rename its own. */ @@ -144,6 +147,9 @@ open class SDK { @Inject internal lateinit var userSettingsRepository: UserSettingsRepository + @Inject + internal lateinit var getTrackingSourceUseCase: GetTrackingSourceUseCase + @Inject lateinit var notificationHandler: NotificationHandler @@ -547,11 +553,24 @@ open class SDK { val coreV2 = CoreSelection.resolve(coreV2Requested, shopId) if (coreV2) { val connectivity = AndroidConnectivityMonitor(context) + val lifecycle = ProcessAppLifecycle() lateinit var init: CoreInit - // The core gets its /init before it takes a request: none goes without waiting for it. + // The core gets its /init and its queue before it takes a request: none goes without them. val core = networkRepository.useCoreV2(shopId, connectivity, dispatchers) { core -> - init = createCoreInit(core, connectivity, context, config, storage, autoSendPushToken, needReInitialization) + init = createCoreInit(core, connectivity, lifecycle, context, config, storage, autoSendPushToken, needReInitialization) core.init = init + core.queue = EventQueue( + shopId = shopId, + store = QueueStore.of(storage.queuePath, shopId), + init = init, + state = instanceState, + send = core.client::send, + connectivity = connectivity, + lifecycle = lifecycle, + source = ::trackingSource, + sendDirectly = core::sendDirectly, + dispatchers = dispatchers, + ).also { it.start() } } this.core = core init.start() @@ -637,6 +656,7 @@ open class SDK { private fun createCoreInit( core: CoreV2, connectivity: ConnectivityMonitor, + lifecycle: ProcessAppLifecycle, context: Context, config: Rees46Config, storage: ShopStorage, @@ -658,7 +678,7 @@ open class SDK { storage = storage, state = instanceState, connectivity = connectivity, - lifecycle = ProcessAppLifecycle(), + lifecycle = lifecycle, session = SessionWindow(userSettingsRepository, storage).also { session -> session.addListener { sid, changed -> if (changed) sessionChanged(sid) } }, @@ -682,6 +702,12 @@ open class SDK { ) } + /** The source `tracking.setSource` stored, as the queue puts it on `/push`; null when none or expired. */ + private fun trackingSource(): JSONObject? { + val source = getTrackingSourceUseCase() ?: return null + return JSONObject().put("from", source.type).put("code", source.code) + } + /** The advertising id for `/init`, or null when it cannot be read. */ private suspend fun readAdvertisingId(): String? = initializeAdvertisingIdUseCase.invoke().takeUnless { it.isBlank() || it == AdvertisingUtils.DEFAULT_ADVERTISING_ID } @@ -956,6 +982,37 @@ open class SDK { logShopId?.let { SdkLogs.unregister(it, owner = this) } } + /** + * Sends the events waiting in the queue now, without waiting for the queue's own triggers, and + * returns once each of them was tried: sent, dropped, or held — by no network, by the instance not + * being ready yet, or by a server that asked to wait (`Retry-After`). It does not repeat a failed + * event at once and promises no delivery. On the legacy core, which has no queue, it returns at once. + * + * @throws SdkException with [SdkError.NotInitialized] when the instance was never initialized or + * was released, with [SdkError.ShopBlocked] when the shop refused the SDK. + */ + suspend fun flush() { + val core = core + if (core == null || released) throw SdkException(SdkError.NotInitialized) + (core as? CoreV2)?.flush() + } + + /** + * [flush] for Java and for code outside coroutines: [listener] gets [SdkResult.Success] once the + * queue was tried, or [SdkResult.Failure] with the error, once, on the main thread. + */ + fun flush(listener: CompletionListener) { + calls.launch { + val result: SdkResult = try { + flush() + SdkResult.Success(Unit) + } catch (e: SdkException) { + SdkResult.Failure(e.error) + } + complete(listener, result) + } + } + /** * Forgets this shop's customer on this device: the `did`, the session (`sid` and the time of the last * activity), the saved `/init` answer, the source [tracking] `setSource` set, the dates the push diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt index 70b53d49..25e3cffb 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/Core.kt @@ -11,7 +11,7 @@ import org.json.JSONObject * same code for both cores. * * [LegacyCore] keeps today's paths. [CoreV2] is the new core, chosen by `Rees46Config.coreV2`: its own - * client, `/init` ([CoreInit]) and session ([SessionWindow]). + * client, `/init` ([CoreInit]), session ([SessionWindow]) and event queue ([EventQueue]). */ internal interface Core { @@ -32,5 +32,5 @@ internal interface Core { * Forgets the customer in [storage]: what [ShopStorage.deleteUserCredentials] wipes, and whatever * the core keeps of its own. Runs on a background thread; false when part of it did not reach the disk. */ - fun deleteUserCredentials(storage: ShopStorage): Boolean + suspend fun deleteUserCredentials(storage: ShopStorage): Boolean } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt index fbf8f2f6..223a78c5 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt @@ -3,6 +3,7 @@ package com.personalization.core.internal import com.personalization.LogLevel import com.personalization.api.OnApiCallbackListener import com.personalization.core.SdkError +import com.personalization.core.SdkException import com.personalization.logs.LogCodes import com.personalization.logs.LogReasons import com.personalization.logs.SdkLogs @@ -25,8 +26,8 @@ import java.util.concurrent.atomic.AtomicBoolean * [CoreInit]'s, its session [SessionWindow]'s. A request waits for the instance to be ready within its * own read timeout, and for the `/init` of a new session when the window of the last one ended: a * refused shop fails it with `shopBlocked` (the old callbacks get `402`), a wait that runs out with - * `timeout`. A request with a result does not extend the session. The queue stays the legacy one until - * the core takes it over. + * `timeout`. A request with a result does not extend the session. Events (`/push`, `/push/custom`, + * `track/stories`, `track/slider`, `popup/showed`) go to the [EventQueue] instead, once it is set. * * Every request ends in exactly one callback, on a background thread as today: the answer, or the * error mapped to the old `onError(code, msg)` by [LegacyErrors]. [release] cancels the requests in @@ -52,6 +53,10 @@ internal class CoreV2( initFlow.value = value } + /** The instance's event queue; set with [init], before the core takes requests. */ + @Volatile + internal var queue: EventQueue? = null + /** How long a request waits for the instance to be ready: its own read timeout. */ private val readyWaitMs: Long = client.timeouts.readMs @@ -85,6 +90,23 @@ internal class CoreV2( endings.launch { answer(HttpResult.Failure(SdkError.NotInitialized)) } return } + val queue = queue + if (queue != null && method is NetworkMethod.POST && method.method in EventQueue.PATHS) { + queue.accept(method.method, params, listener) + return + } + sendNow(method, params, answer) + } + + /** Sends an event the queue's disk would not take, once, as a request. */ + internal fun sendDirectly(path: String, params: JSONObject, listener: OnApiCallbackListener?) { + val answered = AtomicBoolean(false) + sendNow(NetworkMethod.POST(path), params) { result: HttpResult -> + answered.compareAndSet(false, true).also { first -> if (first && listener != null) deliver(listener, result) } + } + } + + private fun sendNow(method: NetworkMethod, params: JSONObject, answer: (HttpResult) -> Boolean) { val request = requests.launch { val waited = awaitInstance() if (waited != null) { @@ -145,10 +167,28 @@ internal class CoreV2( released = true if (cancelRequests) requests.cancel() init?.release() + queue?.release(cancel = cancelRequests) + } + + /** Sends the queue now ([EventQueue.flush]). */ + suspend fun flush() { + if (released) throw SdkException(SdkError.NotInitialized) + queue?.flush() + } + + /** + * The events in the queue were the customer's: the queue is emptied — an event accepted before the + * call goes, its listener hears `-1` — and its directory goes with the credentials while no event + * can be accepted. + */ + override suspend fun deleteUserCredentials(storage: ShopStorage): Boolean { + val queue = queue ?: return storage.deleteUserCredentials(includeQueue = true) + return queue.clear(LogReasons.DELETE_USER_CREDENTIALS, CLEARED) { storage.deleteUserCredentials(includeQueue = true) } } - /** The events in the queue were the customer's: its directory goes with the credentials. */ - override fun deleteUserCredentials(storage: ShopStorage): Boolean = storage.deleteUserCredentials(includeQueue = true) + private companion object { + val CLEARED = LegacyError(LegacyErrors.NO_ANSWER, "The event was deleted with the customer's data") + } private fun deliver(listener: OnApiCallbackListener, result: HttpResult) = when (result) { is HttpResult.Success -> delivery.success(listener, result.body) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt new file mode 100644 index 00000000..6f810394 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt @@ -0,0 +1,511 @@ +package com.personalization.core.internal + +import com.personalization.Cancellable +import com.personalization.LogLevel +import com.personalization.api.OnApiCallbackListener +import com.personalization.core.SdkError +import com.personalization.core.SdkException +import com.personalization.core.SdkState +import com.personalization.logs.LogCodes +import com.personalization.logs.LogReasons +import com.personalization.logs.SdkLogs +import com.personalization.sdk.domain.models.NetworkMethod +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CoroutineExceptionHandler +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancel +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import kotlinx.coroutines.selects.select +import kotlinx.coroutines.sync.withLock +import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeoutOrNull +import org.json.JSONObject + +/** + * An answer listener the SDK wraps around an event (the tracking methods): before the answer, the queue + * says whether a popup in it may be shown (decision 2). The legacy core never sets it. + */ +internal abstract class EventAnswerListener : OnApiCallbackListener() { + /** Null while a popup of the answer may be shown; otherwise why not, for `popup_not_shown`. */ + @Volatile + var popupHeldBack: String? = null +} + +/** + * The new core's event queue (`core/queue.md`, decision 0002): `/push`, `/push/custom`, `track/stories`, + * `track/slider` and `popup/showed` go through it, in order, and survive no network and a restart. + * + * - **Accepting.** [accept] answers a released instance with `notInitialized` and a refused shop with + * `shopBlocked` at once; otherwise it moves the session's last activity and hands the event to a + * writer that keeps the calls' order: an event is accepted once its file is on disk ([QueueStore]). + * A disk that refuses it sends it once, as a request. + * - **Stamping.** The common fields — `did`, the `sid` of the moment — go on when the event is sent, + * with the stored tracking source as a `source` object on `/push` only and the time it was made as + * `time` on `/push/custom` only (the only path that reads it), unless the call set its own. + * - **Sending.** One event at a time, oldest first, once the instance is ready and the session alive + * (a session whose window ended gets its `/init` first). Sending starts on [SdkState.READY], on the + * app's foreground and background, on the network's return, on [flush] and on a timer. + * - **Answers.** `2xx` removes the event. No network waits for it and a timeout repeats it after a pause, + * neither counting as an attempt. `5xx`, `408`, `410`, `417`, `426`, `429` repeat it after a pause + * (the `Retry-After` of a `429` or `503`), and the 10th drops it. `402`, `403`, `404` pause the queue + * while `/init` checks the shop ([CoreInit.checkShopAndAwait]): a refusal clears the queue, a shop + * found fine sends the event once more, and a second such answer drops it. Other answers drop it at + * once. A dropped event does not hold the others. + * - **Limits.** An event older than 7 days is dropped when its turn comes; at 1000 events the oldest go. + * Each drop is `queue_dropped` with its reason. + * - **Listeners.** An old-style listener hears the event's fate once: the server's answer when it went, + * or the error that dropped it. A popup in the answer is shown only when the event went within 10 s + * of being made and the app is in the foreground (decision 2). + * + * [release] stops sending and keeps the files for the next launch; [clear] empties the queue (the + * customer's deletion, the shop's refusal, which the queue hears from the state by itself). + */ +internal class EventQueue( + private val shopId: String, + private val store: QueueStore, + private val init: CoreInit, + private val state: InstanceState, + private val send: suspend (NetworkMethod, JSONObject) -> HttpResult, + private val connectivity: ConnectivityMonitor, + private val lifecycle: AppLifecycle, + /** The stored tracking source as `source` (`from`, `code`); null when there is none or it expired. */ + private val source: () -> JSONObject? = { null }, + /** Sends an event the disk would not take, once, as a request. */ + private val sendDirectly: (path: String, body: JSONObject, listener: OnApiCallbackListener?) -> Unit = { _, _, _ -> }, + dispatchers: CoreDispatchers = CoreDispatchers.DEFAULT, + private val clock: Clock = Clock.SYSTEM, + private val backoff: Backoff = Backoff(), + /** How long a pass waits for the instance and its session before it waits for a trigger instead. */ + private val waitMs: Long = 10_000, + /** At most this many events wait; tests make it small. */ + private val cap: Int = CAP, +) { + + private val handler = CoroutineExceptionHandler { _, throwable -> + log(LogLevel.ERROR, LogCodes.SDK_LOG, "The event queue failed unexpectedly", throwable = throwable) + } + + /** The writer and the answers that do not wait for a send: it outlives [release] to write what came before. */ + private val writing = CoroutineScope(SupervisorJob() + dispatchers.io + handler) + + /** The sending; [release] cancels it. */ + private val working = CoroutineScope(SupervisorJob() + dispatchers.io + handler) + + private val commands = Channel(Channel.UNLIMITED) + private val wake = Channel(Channel.CONFLATED) + + /** A trigger that may end a pause: not a new event, which waits for the pause like the others. */ + @Volatile + private var urgent = false + + @Volatile + private var released = false + + /** Released with its sending cancelled: nobody here will send what is still written. */ + @Volatile + private var cancelled = false + + private val flushLock = Any() + private val flushes = ArrayList>() + private val subscriptions = ArrayList() + + /** Failures in a row since the last event went: the index of the next pause. The worker's own. */ + private var failures = 0 + + private val delivery = LegacyDelivery { level, code, message, throwable -> log(level, code, message, throwable = throwable) } + + private sealed interface Command { + class Accept(val path: String, val body: JSONObject, val createdAt: Long, val listener: OnApiCallbackListener?) : Command + class Clear(val reason: String, val error: LegacyError, val then: () -> Boolean, val done: CompletableDeferred?) : Command + class Flush(val done: CompletableDeferred) : Command + } + + private sealed interface Outcome { + /** Nothing to send, or not this queue's to send: wait for a trigger. */ + object Idle : Outcome + + /** The instance or its session is not there yet: wait for a trigger or the timer. */ + object Waiting : Outcome + + /** No network: wait for it, looking again now and then. */ + object Offline : Outcome + + class Pause(val delayMs: Long, val serverNamed: Boolean) : Outcome + } + + /** Takes over the shop's queue and starts listening for the triggers. */ + fun start() { + store.claim(this) + val heard = listOf( + connectivity.addListener { connected -> if (connected) kick(urgent = true) }, + lifecycle.addListener { kick(urgent = true) }, + init.session.addListener { _, _ -> kick(urgent = true) }, + ) + val kept = synchronized(subscriptions) { if (released) false else subscriptions.addAll(heard) } + if (!kept) { + heard.forEach { it.cancel() } + return + } + writing.launch { for (command in commands) handle(command) } + val following = working.launch { + state.changes.collect { current -> + when (current) { + SdkState.READY, SdkState.RETRYING -> kick(urgent = true) + SdkState.REFUSED -> clearLater(LogReasons.REFUSED, LegacyErrors.of(SdkError.ShopBlocked)) + else -> Unit + } + } + } + // A release that lets the event in flight finish still stops following the state. + synchronized(subscriptions) { if (released) following.cancel() else subscriptions += Cancellable { following.cancel() } } + working.launch { work() } + } + + /** + * Takes an event of [path] with its own fields [params]. [listener] hears its fate once: at once + * when the instance was released or the shop refused, otherwise after it went or was dropped. + */ + fun accept(path: String, params: JSONObject, listener: OnApiCallbackListener?) { + val error = when { + released -> SdkError.NotInitialized + state.current == SdkState.REFUSED -> SdkError.ShopBlocked + else -> null + } + if (error != null) { + if (listener != null) writing.launch { LegacyErrors.of(error).let { delivery.failure(listener, it.code, it.message) } } + return + } + val createdAt = clock.nowMillis() + init.session.extend(createdAt) + if (commands.trySend(Command.Accept(path, params, createdAt, listener)).isFailure && listener != null) { + writing.launch { LegacyErrors.of(SdkError.NotInitialized).let { delivery.failure(listener, it.code, it.message) } } + } + } + + /** + * Sends what is in the queue now, without waiting for a trigger, and returns once each of those + * events was tried: sent, dropped, or held by no network, the instance not being ready, the shop's + * check or a server's `Retry-After`. + * + * @throws SdkException `notInitialized` after [release], `shopBlocked` for a refused shop. + */ + suspend fun flush() { + if (released) throw SdkException(SdkError.NotInitialized) + if (state.current == SdkState.REFUSED) throw SdkException(SdkError.ShopBlocked) + val done = CompletableDeferred() + if (commands.trySend(Command.Flush(done)).isFailure) throw SdkException(SdkError.NotInitialized) + done.await() + } + + /** + * Empties the queue for [reason] and runs [then] while no event can be accepted: an event accepted + * before this call goes, one accepted after it stays. The listeners waiting hear [error]. Returns + * [then]'s result. + */ + suspend fun clear(reason: String, error: LegacyError, then: () -> Boolean): Boolean { + val done = CompletableDeferred() + if (released || commands.trySend(Command.Clear(reason, error, then, done)).isFailure) return clearNow(reason, error, then) + return withContext(NonCancellable) { done.await() } + } + + /** [clear] without waiting, for the shop's refusal heard from the state. */ + private fun clearLater(reason: String, error: LegacyError) { + commands.trySend(Command.Clear(reason, error, { true }, null)) + } + + /** + * Stops sending; the events stay on disk for the next launch. With [cancel] the event in flight is + * cancelled and the listeners of this queue's events hear `-1`; without, as when a push-only + * instance gives way to the app's, the event in flight finishes and the listeners stay for the + * queue that sends next. Events accepted before are still written. + */ + fun release(cancel: Boolean = true) { + val heard = synchronized(subscriptions) { + if (released) return + released = true + subscriptions.toList().also { subscriptions.clear() } + } + heard.forEach { it.cancel() } + commands.close() + val next = store.leave(this) + if (cancel) { + cancelled = true + working.cancel() + val error = LegacyErrors.of(SdkError.Cancelled) + store.takeListeners(this).forEach { delivery.failure(it.listener, error.code, error.message) } + } else { + kick(urgent = true) + } + completeFlushes() + next?.kick(urgent = true) + } + + /** Wakes the worker; an [urgent] trigger also ends a pause the server did not name. */ + internal fun kick(urgent: Boolean) { + if (urgent) this.urgent = true + wake.trySend(Unit) + } + + private suspend fun handle(command: Command) { + when (command) { + is Command.Accept -> write(command) + is Command.Clear -> command.done.let { done -> + val result = clearNow(command.reason, command.error, command.then) + done?.complete(result) + } + is Command.Flush -> { + val owner = store.owner + if (owner != null && owner !== this) { + owner.flushFromOther(command.done) + } else { + synchronized(flushLock) { flushes += command.done } + kick(urgent = true) + } + } + } + } + + /** A flush asked of another queue of the shop: this one sends. */ + private fun flushFromOther(done: CompletableDeferred) { + if (released) { + done.complete(Unit) + return + } + synchronized(flushLock) { flushes += done } + kick(urgent = true) + } + + private fun write(accept: Command.Accept) { + val dropped = ArrayList>() + // After a cancelling release the event is still written, for the next launch, but nobody here + // will send it: its listener hears -1 now. + val keepListener = accept.listener.takeUnless { cancelled } + val event = store.add(this, accept.path, accept.body, accept.createdAt, keepListener, cap, dropped) + dropped.forEach { (old, waiting) -> dropped(old?.path, old?.attempts, LogReasons.OVERFLOW, waiting, OVERFLOWED) } + if (keepListener == null && accept.listener != null) { + LegacyErrors.of(SdkError.Cancelled).let { delivery.failure(accept.listener, it.code, it.message) } + } + if (event == null) { + // Not accepted: the disk refused it (storage_failed). It goes once, as a request. + sendDirectly(accept.path, accept.body, accept.listener) + return + } + kick(urgent = false) + } + + private fun clearNow(reason: String, error: LegacyError, then: () -> Boolean): Boolean { + val (count, waiting, result) = store.clear(then) + if (count > 0) { + log(LogLevel.WARNING, LogCodes.QUEUE_DROPPED, "The queue was cleared", mapOf("reason" to reason, "count" to count)) + } + waiting.forEach { delivery.failure(it.listener, error.code, error.message) } + return result + } + + private suspend fun work() { + while (!released) { + val tried = synchronized(flushLock) { flushes.toList().also { flushes.clear() } } + val outcome = if (store.isOwner(this)) store.sending.withLock { pass() } else Outcome.Idle + tried.forEach { it.complete(Unit) } + when (outcome) { + Outcome.Idle -> wake.receive() + Outcome.Waiting -> withTimeoutOrNull(TIMER_MS) { wake.receive() } + Outcome.Offline -> withTimeoutOrNull(NETWORK_RECHECK_MS) { wake.receive() } + is Outcome.Pause -> pause(outcome) + } + } + } + + /** + * Waits out a pause. An urgent trigger (the network back, the foreground, a flush, ready) ends one + * the backoff set; a server's `Retry-After` is waited in full, and a flush meanwhile is done at once: + * nothing can go before. + */ + private suspend fun pause(pause: Outcome.Pause) = coroutineScope { + val over = launch { delay(pause.delayMs) } + try { + while (over.isActive && !released) { + if (urgent && !pause.serverNamed) return@coroutineScope + if (pause.serverNamed) completeFlushes() + select { + over.onJoin {} + wake.onReceive {} + } + } + } finally { + over.cancel() + } + } + + private fun completeFlushes() { + val done = synchronized(flushLock) { flushes.toList().also { flushes.clear() } } + done.forEach { it.complete(Unit) } + } + + /** Sends events oldest first until the queue is empty or cannot send now. */ + private suspend fun pass(): Outcome { + urgent = false + while (true) { + if (released || !store.isOwner(this) || state.current == SdkState.REFUSED) return Outcome.Idle + val event = store.head() ?: return Outcome.Idle + if (clock.nowMillis() - event.createdAt > TTL_MS) { + drop(event, LogReasons.EXPIRED, EXPIRED) + continue + } + if (!connectivity.isConnected) return Outcome.Offline + // Ready, and when the session's window ended, the /init of a new one first. + if (init.awaitUsable(waitMs) != null || !init.session.isAlive()) return Outcome.Waiting + val sentAt = clock.nowMillis() + val result = sendOne(event) + // The answer is in: what it decides is done even if the queue is released meanwhile. + val next = withContext(NonCancellable) { take(event, result, sentAt) } + if (next != null) return next + } + } + + private suspend fun sendOne(event: QueuedEvent): HttpResult { + val body = event.body + if (event.path == PATH_PUSH) source()?.let { body.put(FIELD_SOURCE, it) } + if (event.path == PATH_CUSTOM && !body.has(FIELD_TIME)) body.put(FIELD_TIME, event.createdAt / 1_000) + return try { + send(NetworkMethod.POST(event.path), body) + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "POST ${event.path}: no answer", mapOf("method" to "POST", "path" to event.path), e) + HttpResult.Failure(SdkError.Network) + } + } + + /** What the answer to [event] does; null to go on with the next event. */ + private suspend fun take(event: QueuedEvent, result: HttpResult, sentAt: Long): Outcome? { + when (result) { + is HttpResult.Success -> return delivered(event, result.body, sentAt) + is HttpResult.Failure -> return when (val error = result.error) { + // A 2xx whose body is not JSON: the event went all the same. + SdkError.Decoding -> delivered(event, JSONObject(), sentAt) + SdkError.Network -> if (connectivity.isConnected) pause(null) else Outcome.Offline + SdkError.Timeout -> pause(null) + // A cancel the queue did not make (an interceptor): repeated like a timeout. + SdkError.Cancelled -> if (released) Outcome.Idle else pause(null) + is SdkError.Http -> answered(event, error.code, result) + else -> { + drop(event, LogReasons.REJECTED, LegacyErrors.of(error)) + null + } + } + } + } + + private suspend fun answered(event: QueuedEvent, code: Int, result: HttpResult.Failure): Outcome? { + val error = LegacyErrors.of(SdkError.Http(code), result.errorBody) + when { + code in 500..599 || code in REPEATED_4XX -> { + val attempts = event.attempts + 1 + if (attempts >= MAX_ATTEMPTS) { + drop(event, LogReasons.ATTEMPTS, error, attempts) + return null + } + store.update(event.copy(attempts = attempts)) + return pause(result.retryAfterMs.takeIf { code in RETRY_AFTER_CODES }) + } + code in SHOP_REFUSALS -> { + if (event.shopChecked) { + // The shop is fine, this service is not (an unpaid CRM for push/custom). + drop(event, LogReasons.SERVICE_REFUSED, error) + return null + } + return when (init.checkShopAndAwait(waitMs)) { + SdkState.READY -> { + store.update(event.copy(shopChecked = true)) + null + } + // Cleared by the state's own news; a released queue sends nothing more. + SdkState.REFUSED, SdkState.NOT_INITIALIZED -> Outcome.Idle + // The check told nothing yet: the event waits, the check goes again later. + else -> pause(null) + } + } + else -> { + drop(event, LogReasons.REJECTED, error) + return null + } + } + } + + private fun pause(retryAfterMs: Long?): Outcome.Pause = + Outcome.Pause(backoff.delayMs(failures++, retryAfterMs), serverNamed = retryAfterMs != null) + + /** The event went: it leaves the queue and its listener hears the answer. */ + private fun delivered(event: QueuedEvent, body: Any, sentAt: Long): Outcome? { + failures = 0 + // Gone meanwhile (cleared, or dropped for room): its listener heard that already. + val removed = store.remove(event.id) ?: return null + val listener = removed.waiting?.listener ?: return null + if (listener is EventAnswerListener) { + listener.popupHeldBack = when { + sentAt - event.createdAt > POPUP_WINDOW_MS -> LogReasons.LATE_EVENT + !lifecycle.isForeground -> LogReasons.APP_IN_BACKGROUND + else -> null + } + } + delivery.success(listener, body) + return null + } + + private fun drop(event: QueuedEvent, reason: String, error: LegacyError, attempts: Int = event.attempts) { + val removed = store.remove(event.id) ?: return + dropped(event.path, attempts, reason, removed.waiting, error) + } + + private fun dropped(path: String?, attempts: Int?, reason: String, waiting: QueueStore.Waiting?, error: LegacyError) { + log( + LogLevel.WARNING, + LogCodes.QUEUE_DROPPED, + "An event was dropped from the queue", + buildMap { + put("reason", reason) + path?.let { put("path", it) } + attempts?.let { put("attempts", it) } + }, + ) + waiting?.let { delivery.failure(it.listener, error.code, error.message) } + } + + private fun log(level: LogLevel, code: String, message: String, context: Map = emptyMap(), throwable: Throwable? = null) = + SdkLogs.log(level, code, message, shopId, context, throwable) + + internal companion object { + const val PATH_PUSH = "push" + const val PATH_CUSTOM = "push/custom" + + /** What the queue takes; the push events (`track/delivered` and the others) join with stage 6. */ + val PATHS: Set = setOf(PATH_PUSH, PATH_CUSTOM, "track/stories", "track/slider", "popup/showed") + + const val TTL_MS = 7 * 24 * 60 * 60 * 1000L + const val CAP = 1_000 + const val MAX_ATTEMPTS = 10 + const val POPUP_WINDOW_MS = 10_000L + + /** How often a queue that waits for the instance looks again by itself. */ + const val TIMER_MS = 60_000L + + /** How often a queue that waits for the network looks again by itself. */ + const val NETWORK_RECHECK_MS = 5 * 60 * 1000L + + private const val FIELD_SOURCE = "source" + private const val FIELD_TIME = "time" + private val SHOP_REFUSALS = setOf(402, 403, 404) + private val REPEATED_4XX = setOf(408, 410, 417, 426, 429) + private val RETRY_AFTER_CODES = setOf(429, 503) + + private val EXPIRED = LegacyError(LegacyErrors.NO_ANSWER, "The event expired in the queue") + private val OVERFLOWED = LegacyError(LegacyErrors.NO_ANSWER, "The event was dropped: the queue was full") + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt index fecbe365..4bb4562a 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt @@ -68,7 +68,7 @@ internal class LegacyCore( } /** The legacy path has no queue: the partition's credentials and the saved answer go. */ - override fun deleteUserCredentials(storage: ShopStorage): Boolean = storage.deleteUserCredentials(includeQueue = false) + override suspend fun deleteUserCredentials(storage: ShopStorage): Boolean = storage.deleteUserCredentials(includeQueue = false) private fun executeMethod( networkMethod: NetworkMethod, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt new file mode 100644 index 00000000..df50d11e --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt @@ -0,0 +1,266 @@ +package com.personalization.core.internal + +import com.personalization.LogLevel +import com.personalization.api.OnApiCallbackListener +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import kotlinx.coroutines.sync.Mutex +import org.json.JSONException +import org.json.JSONObject +import java.io.File +import java.io.FileOutputStream +import java.io.IOException +import java.util.Locale + +/** + * One event in a shop's queue: the request's own fields ([body], without the common ones, which go on + * at send time), when it was made, and what its answers so far did to it. + */ +internal class QueuedEvent( + /** Its file's name without the extension: zero-padded, so the names sort in the queue's order. */ + val id: String, + val path: String, + val body: JSONObject, + val createdAt: Long, + /** Answers to repeat (`5xx`, `408`, `410`, `417`, `426`, `429`) it has had; at 10 it is dropped. */ + val attempts: Int = 0, + /** It had a `402`, `403` or `404`, and `/init` found the shop fine since: the next one drops it. */ + val shopChecked: Boolean = false, +) { + fun copy(attempts: Int = this.attempts, shopChecked: Boolean = this.shopChecked) = + QueuedEvent(id, path, body, createdAt, attempts, shopChecked) + + fun toJson(): JSONObject = JSONObject() + .put(FIELD_VERSION, VERSION) + .put(FIELD_PATH, path) + .put(FIELD_BODY, body) + .put(FIELD_CREATED_AT, createdAt) + .put(FIELD_ATTEMPTS, attempts) + .put(FIELD_SHOP_CHECKED, shopChecked) + + companion object { + private const val VERSION = 1 + private const val FIELD_VERSION = "v" + private const val FIELD_PATH = "path" + private const val FIELD_BODY = "body" + private const val FIELD_CREATED_AT = "created_at" + private const val FIELD_ATTEMPTS = "attempts" + private const val FIELD_SHOP_CHECKED = "shop_checked" + + /** Null for a file that is not an event of this format. */ + fun parse(id: String, text: String): QueuedEvent? = try { + val json = JSONObject(text) + val path = json.optString(FIELD_PATH) + val body = json.optJSONObject(FIELD_BODY) + if (json.optInt(FIELD_VERSION) != VERSION || path.isEmpty() || body == null || !json.has(FIELD_CREATED_AT)) { + null + } else { + QueuedEvent(id, path, body, json.getLong(FIELD_CREATED_AT), json.optInt(FIELD_ATTEMPTS), json.optBoolean(FIELD_SHOP_CHECKED)) + } + } catch (e: JSONException) { + null + } + } +} + +/** + * The files of one shop's queue, in its `queue/` directory under `noBackupFilesDir`: one JSON file per + * event, named by a sequence so the names sort in order. One store per directory in the process, shared + * by every [EventQueue] of the shop, so two instances of one shop never send an event twice: + * - the queue made last is the [owner] and the only one that sends; the others write what they accept + * and wake it; + * - [sending] is held for an event from its send to its answer: a new owner waits for the old one's + * event in flight; + * - an event's old-style listener waits here, so whoever sends it calls it. + * + * A write goes to a temporary file first and is renamed over the event's file: a crash midway leaves + * the old file or the new one, never a part. Disk failures are `storage_failed`; nothing here throws. + */ +internal class QueueStore private constructor(private val directory: File, private val shopId: String?) { + + private val lock = Any() + private val owners = ArrayList() + private val listeners = HashMap() + private var sequence = 0L + + /** Held from an event's send to its answer. */ + val sending = Mutex() + + /** An event's listener and the queue that accepted it. */ + class Waiting(val queue: EventQueue, val listener: OnApiCallbackListener) + + /** An event [remove] took off the queue, with its listener if it had one. */ + class Removed(val waiting: Waiting?) + + /** [queue] becomes the owner: it sends from now on. */ + fun claim(queue: EventQueue) = synchronized(lock) { + owners -= queue + owners += queue + } + + /** [queue] stops: the queue made before it, if one is live, sends again. Returns the new owner. */ + fun leave(queue: EventQueue): EventQueue? = synchronized(lock) { + owners -= queue + owners.lastOrNull() + } + + val owner: EventQueue? get() = synchronized(lock) { owners.lastOrNull() } + + fun isOwner(queue: EventQueue): Boolean = synchronized(lock) { owners.lastOrNull() === queue } + + /** The ids on disk in order; a temporary file left by a crash is removed. */ + private fun ids(): List { + val names = directory.list() ?: return emptyList() + val ids = ArrayList(names.size) + for (name in names) { + when { + name.endsWith(EXTENSION) -> ids += name.removeSuffix(EXTENSION) + name.endsWith(TEMPORARY) -> File(directory, name).delete() + } + } + ids.sort() + return ids + } + + /** How many events wait. */ + fun size(): Int = synchronized(lock) { ids().size } + + /** The oldest event that can be read; a file that cannot is dropped (`queue_dropped`, `unreadable`). */ + fun head(): QueuedEvent? = synchronized(lock) { + for (id in ids()) { + read(id)?.let { return it } + File(directory, id + EXTENSION).delete() + listeners.remove(id) + SdkLogs.log(LogLevel.WARNING, LogCodes.QUEUE_DROPPED, "An event that could not be read was dropped", shopId, + mapOf("reason" to REASON_UNREADABLE)) + } + null + } + + fun contains(id: String): Boolean = synchronized(lock) { File(directory, id + EXTENSION).isFile } + + /** + * Writes a new event from [path], [body] and [createdAt] and keeps [listener] for it. Before, the + * oldest events go while the queue would hold more than [cap]; they come back in [dropped]. Returns + * the event, or null when the disk refused it (it is then not accepted: the caller decides). + */ + fun add( + queue: EventQueue, + path: String, + body: JSONObject, + createdAt: Long, + listener: OnApiCallbackListener?, + cap: Int, + dropped: MutableList>, + ): QueuedEvent? = synchronized(lock) { + val ids = ids() + val over = ids.size - cap + 1 + if (over > 0) { + ids.take(over).forEach { id -> dropped += read(id) to listeners.remove(id); File(directory, id + EXTENSION).delete() } + } + val last = ids.lastOrNull()?.toLongOrNull() ?: 0L + sequence = maxOf(sequence, last) + 1 + val event = QueuedEvent(String.format(Locale.ROOT, ID_FORMAT, sequence), path, body, createdAt) + if (!write(event)) return null + if (listener != null) listeners[event.id] = Waiting(queue, listener) + event + } + + /** Rewrites [event] (its attempts, its check) if it is still there; false when it is gone or the disk refused. */ + fun update(event: QueuedEvent): Boolean = synchronized(lock) { + if (!File(directory, event.id + EXTENSION).isFile) return false + write(event) + } + + /** Removes [id] with its listener; null when it was gone already (cleared, or dropped meanwhile). */ + fun remove(id: String): Removed? = synchronized(lock) { + val file = File(directory, id + EXTENSION) + if (!file.isFile) return null + if (!file.delete() && file.exists()) failed(OP_DELETE, null) + Removed(listeners.remove(id)) + } + + /** The listener [id] has, without taking it. */ + fun listenerOf(id: String): Waiting? = synchronized(lock) { listeners[id] } + + /** + * Removes every event and hands back their listeners, then runs [then] while no event can be + * accepted (the customer's data goes with it, `deleteUserCredentials()`). Returns how many went and + * [then]'s result. + */ + fun clear(then: () -> T): Triple, T> = synchronized(lock) { + val ids = ids() + ids.forEach { id -> + val file = File(directory, id + EXTENSION) + if (!file.delete() && file.exists()) failed(OP_DELETE, null) + } + val waiting = listeners.values.toList() + listeners.clear() + Triple(ids.size, waiting, then()) + } + + /** Takes the listeners [queue] accepted: it stops, their events stay on disk for a later launch. */ + fun takeListeners(queue: EventQueue): List = synchronized(lock) { + val mine = listeners.filterValues { it.queue === queue } + mine.keys.forEach(listeners::remove) + mine.values.toList() + } + + private fun read(id: String): QueuedEvent? = try { + QueuedEvent.parse(id, File(directory, id + EXTENSION).readText(Charsets.UTF_8)) + } catch (e: IOException) { + failed(OP_READ, e) + null + } + + /** A temporary file synced to the disk, then renamed over the event's file. */ + private fun write(event: QueuedEvent): Boolean { + if (!directory.isDirectory && !directory.mkdirs() && !directory.isDirectory) { + failed(OP_WRITE, null) + return false + } + val temporary = File(directory, event.id + TEMPORARY) + return try { + FileOutputStream(temporary).use { stream -> + stream.write(event.toJson().toString().toByteArray(Charsets.UTF_8)) + stream.fd.sync() + } + if (temporary.renameTo(File(directory, event.id + EXTENSION))) { + true + } else { + temporary.delete() + failed(OP_WRITE, null) + false + } + } catch (e: IOException) { + temporary.delete() + failed(OP_WRITE, e) + false + } + } + + private fun failed(op: String, throwable: Throwable?) { + SdkLogs.log(LogLevel.WARNING, LogCodes.STORAGE_FAILED, "Storage operation failed: $op", shopId, mapOf("op" to op), throwable) + } + + internal companion object { + private const val EXTENSION = ".json" + private const val TEMPORARY = ".tmp" + private const val ID_FORMAT = "%019d" + const val OP_READ = "read_queued_event" + const val OP_WRITE = "write_queued_event" + const val OP_DELETE = "delete_queued_event" + + private const val REASON_UNREADABLE = "unreadable" + + private val stores = HashMap() + + /** The process's store of [directory]. */ + fun of(directory: File, shopId: String?): QueueStore = synchronized(stores) { + stores.getOrPut(directory.absolutePath) { QueueStore(directory, shopId) } + } + + /** Forgets every store, as a new process would; for tests. */ + internal fun reset() = synchronized(stores) { stores.clear() } + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt index 5c296b99..ee55cb2f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt @@ -110,6 +110,9 @@ internal class ShopStorage(context: Context, val layout: StorageLayout) { preferences.edit().putBoolean(StorageKeys.CREDENTIALS_WIPED, value).apply() } + /** Where the shop's event queue lives ([queueDirectory] creates it). */ + val queuePath: File get() = File(directory, QUEUE_DIRECTORY) + /** The directory of the shop's event queue, created if needed; null when the disk refused it. */ fun queueDirectory(): File? { val queue = File(directory, QUEUE_DIRECTORY) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/di/SdkModule.kt b/personalization-sdk/src/main/kotlin/com/personalization/di/SdkModule.kt index a30f36d8..34db6e4b 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/di/SdkModule.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/di/SdkModule.kt @@ -112,11 +112,13 @@ class SdkModule { fun provideTrackingApi( trackEventManager: TrackEventManager, storiesManager: StoriesManager, - setTrackingSourceUseCase: SetTrackingSourceUseCase + setTrackingSourceUseCase: SetTrackingSourceUseCase, + sendNetworkMethodUseCase: SendNetworkMethodUseCase, ): TrackingApi = TrackingApiImpl( trackEventManager = trackEventManager, storiesManager = storiesManager, - setTrackingSourceUseCase = setTrackingSourceUseCase + setTrackingSourceUseCase = setTrackingSourceUseCase, + checksArguments = { sendNetworkMethodUseCase.queuesEvents }, ) @Singleton diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt index 33ea2894..44b0d38d 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt @@ -12,6 +12,7 @@ import com.personalization.api.managers.InAppNotificationManager import com.personalization.api.managers.TrackEventManager import com.personalization.api.models.purchase.PurchaseTrackingRequest import com.personalization.api.params.ProductItemParams +import com.personalization.core.internal.EventAnswerListener import com.personalization.sdk.data.mappers.popup.PopupDtoMapper import com.personalization.sdk.data.models.params.SdkInitializationParams.PARAM_POPUP import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase @@ -50,18 +51,7 @@ internal class TrackEventManagerImpl @Inject constructor( setRecommendedByUseCase(null) } - val internalListener = object : OnApiCallbackListener() { - override fun onSuccess(response: JSONObject?) { - response?.let { - handlePopup(response) - } - listener?.onSuccess(response) - } - - override fun onError(code: Int, msg: String?) { - listener?.onError(code, msg) - } - } + val internalListener = answerListener(listener) val body = params.build() attachStoredSource(body) @@ -87,18 +77,7 @@ internal class TrackEventManagerImpl @Inject constructor( } } - val internalListener = object : OnApiCallbackListener() { - override fun onSuccess(response: JSONObject?) { - response?.let { - handlePopup(response) - } - listener?.onSuccess(response) - } - - override fun onError(code: Int, msg: String?) { - listener?.onError(code, msg) - } - } + val internalListener = answerListener(listener) attachStoredSource(body) @@ -220,9 +199,12 @@ internal class TrackEventManagerImpl @Inject constructor( * * Port of iOS `TrackEventServiceImpl`: the source rides in its own `source` object and stays on * every request until it expires — it is not spent by the first one. Attached to `push` and - * `push/custom`; `popup/showed` carries no attribution, on either platform. + * `push/custom` on the legacy core; `popup/showed` carries no attribution, on either platform. On + * the new core the queue stamps it when the event is sent, on `push` only (contract `tracking`). */ private fun attachStoredSource(body: JSONObject) { + // The new core's queue puts it on /push when it sends the event, and nowhere else. + if (sendNetworkMethodUseCase.queuesEvents) return val source = getTrackingSourceUseCase() ?: return try { body.put( @@ -238,18 +220,7 @@ internal class TrackEventManagerImpl @Inject constructor( private fun postCustomEvent(json: JSONObject, listener: OnApiCallbackListener?) { attachStoredSource(json) - val internalListener = object : OnApiCallbackListener() { - override fun onSuccess(response: JSONObject?) { - response?.let { - handlePopup(response) - } - listener?.onSuccess(response) - } - - override fun onError(code: Int, msg: String?) { - listener?.onError(code, msg) - } - } + val internalListener = answerListener(listener) sendNetworkMethodUseCase.postAsync( CUSTOM_PUSH_REQUEST, @@ -265,11 +236,35 @@ internal class TrackEventManagerImpl @Inject constructor( null } - private fun handlePopup(response: JSONObject) { - val popUpData = response.optJSONObject(PARAM_POPUP)?.let { PopupDtoMapper.map(it) } - if (popUpData != null) { - inAppNotificationManager.shopPopUp(popupDto = popUpData) + /** + * Wraps the app's [listener]: a popup in the answer is shown, then the app hears the answer. On the + * new core the queue may hold the popup back (decision 2: the event went late, or the app is in the + * background); the app still gets the whole answer. + */ + private fun answerListener(listener: OnApiCallbackListener?) = object : EventAnswerListener() { + override fun onSuccess(response: JSONObject?) { + response?.let { handlePopup(it, popupHeldBack) } + listener?.onSuccess(response) + } + + override fun onError(code: Int, msg: String?) { + listener?.onError(code, msg) + } + } + + private fun handlePopup(response: JSONObject, heldBack: String? = null) { + val popUpData = response.optJSONObject(PARAM_POPUP)?.let { PopupDtoMapper.map(it) } ?: return + if (heldBack != null) { + SdkLogs.log( + LogLevel.WARNING, + LogCodes.POPUP_NOT_SHOWN, + "The popup of an event's answer is not shown", + getUserSettingsValueUseCase.getShopId().takeIf { it.isNotEmpty() }, + mapOf("reason" to heldBack), + ) + return } + inAppNotificationManager.shopPopUp(popupDto = popUpData) } companion object { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/tracking/impl/TrackingApiImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/tracking/impl/TrackingApiImpl.kt index c943d50e..37a56dea 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/tracking/impl/TrackingApiImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/tracking/impl/TrackingApiImpl.kt @@ -1,5 +1,6 @@ package com.personalization.features.tracking.impl +import com.personalization.LogLevel import com.personalization.Params import com.personalization.Params.TrackEvent import com.personalization.api.OnApiCallbackListener @@ -10,6 +11,10 @@ import com.personalization.api.models.tracking.TrackingItem import com.personalization.api.models.tracking.TrackingSource import com.personalization.api.models.tracking.putSource import com.personalization.api.params.ProductItemParams +import com.personalization.core.internal.LegacyDelivery +import com.personalization.core.internal.LegacyErrors +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs import com.personalization.sdk.domain.usecases.trackingSource.SetTrackingSourceUseCase import com.personalization.stories.StoriesManager import org.json.JSONArray @@ -26,13 +31,30 @@ internal class TrackingApiImpl @Inject constructor( private val trackEventManager: TrackEventManager, private val storiesManager: StoriesManager, private val setTrackingSourceUseCase: SetTrackingSourceUseCase, + /** + * Whether an empty id or query is refused before it is queued (contract `tracking`: `invalidArgument`). + * True on the new core, whose queue keeps events; the legacy core sends them as before. + */ + private val checksArguments: () -> Boolean = { false }, ) : TrackingApi { + /** Refuses a blank [value] of [name] on the new core: the listener hears `-1` at once. True when refused. */ + private fun refused(name: String, value: String, listener: OnApiCallbackListener?): Boolean { + if (value.isNotBlank() || !checksArguments()) return false + val message = "$name is empty" + SdkLogs.log(LogLevel.WARNING, LogCodes.INVALID_ARGUMENT, "A tracking call was refused: $message", null, mapOf("reason" to "empty_$name")) + listener?.let { delivery.failure(it, LegacyErrors.NO_ANSWER, message) } + return true + } + + private val delivery = LegacyDelivery { level, code, message, throwable -> SdkLogs.log(level, code, message, throwable = throwable) } + override fun productView( itemId: String, source: TrackingSource?, listener: OnApiCallbackListener?, ) { + if (refused("itemId", itemId, listener)) return trackEventManager.track( event = TrackEvent.VIEW, params = Params().put(ProductItemParams(itemId)).withSource(source), @@ -41,6 +63,7 @@ internal class TrackingApiImpl @Inject constructor( } override fun categoryView(categoryId: String, listener: OnApiCallbackListener?) { + if (refused("categoryId", categoryId, listener)) return trackEventManager.track( event = TrackEvent.CATEGORY, params = Params().put(Params.Parameter.CATEGORY_ID, categoryId), @@ -53,6 +76,7 @@ internal class TrackingApiImpl @Inject constructor( results: List?, listener: OnApiCallbackListener?, ) { + if (refused("query", query, listener)) return val params = Params().put(Params.Parameter.SEARCH_QUERY, query) if (!results.isNullOrEmpty()) { params.put(Params.RESULTS_PARAM, results.joinToString(separator = ",")) @@ -65,6 +89,7 @@ internal class TrackingApiImpl @Inject constructor( source: TrackingSource?, listener: OnApiCallbackListener?, ) { + if (refused("itemId", item.id, listener)) return trackEventManager.track( event = TrackEvent.CART, params = Params().put(item.toProductParams()).withSource(source), @@ -81,6 +106,7 @@ internal class TrackingApiImpl @Inject constructor( } override fun removeFromCart(itemId: String, listener: OnApiCallbackListener?) { + if (refused("itemId", itemId, listener)) return trackEventManager.track( event = TrackEvent.REMOVE_FROM_CART, params = Params().put(ProductItemParams(itemId)), @@ -93,6 +119,7 @@ internal class TrackingApiImpl @Inject constructor( source: TrackingSource?, listener: OnApiCallbackListener?, ) { + if (refused("itemId", itemId, listener)) return trackEventManager.track( event = TrackEvent.WISH, params = Params().put(ProductItemParams(itemId)).withSource(source), @@ -109,6 +136,7 @@ internal class TrackingApiImpl @Inject constructor( } override fun removeFromFavorites(itemId: String, listener: OnApiCallbackListener?) { + if (refused("itemId", itemId, listener)) return trackEventManager.track( event = TrackEvent.REMOVE_FROM_WISH, params = Params().put(ProductItemParams(itemId)), diff --git a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt index 9e41a6e7..6ee1b8a1 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt @@ -228,4 +228,30 @@ internal object LogReasons { const val NEED_REINITIALIZATION = "need_reinitialization" const val ALREADY_INITIALIZED = "already_initialized" const val NOT_READY = "not_ready" + + // Why the queue dropped events (`queue_dropped`). + /** Older than the queue keeps an event (7 days). */ + const val EXPIRED = "expired" + + /** The oldest event made room: the queue holds at most 1000. */ + const val OVERFLOW = "overflow" + + /** Ten answers to repeat in a row. */ + const val ATTEMPTS = "attempts" + + /** An answer that says the event itself is wrong (`400`, `401`, `405`, `422`, another `4xx`). */ + const val REJECTED = "rejected" + + /** A `402`, `403` or `404` once more after `/init` found the shop fine: the service is refused, not the shop. */ + const val SERVICE_REFUSED = "service_refused" + + /** The shop refused the SDK: its queue is cleared. */ + const val REFUSED = "refused" + + // Why a popup of an event's answer was not shown (`popup_not_shown`, decision 2). + /** The event went more than 10 s after it was made. */ + const val LATE_EVENT = "late_event" + + /** The app was in the background when the answer came. */ + const val APP_IN_BACKGROUND = "app_in_background" } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/network/SendNetworkMethodUseCase.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/network/SendNetworkMethodUseCase.kt index 3d5b4bb2..c719b0f0 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/network/SendNetworkMethodUseCase.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/network/SendNetworkMethodUseCase.kt @@ -1,6 +1,7 @@ package com.personalization.sdk.domain.usecases.network import com.personalization.api.OnApiCallbackListener +import com.personalization.sdk.data.repositories.network.NetworkRepositoryImpl import com.personalization.sdk.domain.repositories.NetworkRepository import javax.inject.Inject import org.json.JSONObject @@ -9,6 +10,13 @@ class SendNetworkMethodUseCase @Inject constructor( private val networkRepository: NetworkRepository ) { + /** + * Whether events go through the new core's queue, which stamps the tracking source when it sends + * them: the callers then leave it out. + */ + internal val queuesEvents: Boolean + get() = (networkRepository as? NetworkRepositoryImpl)?.core?.isV2 == true + /** * Direct query execution */ From 1f09eb8044140b73e2f874bb92fb96b67bd59dfb Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 01:23:28 +0400 Subject: [PATCH 042/109] test(core): the event queue over its files and a real /init: every row of decision 0002's table (2xx and a 2xx that is not JSON, no network, a timeout, each repeated code to its tenth answer, Retry-After on 429 and 503 only within 1 s and 5 min and not ended by a trigger, 402/403/404 with the shop's check that passes, refuses or comes less than ten minutes after the last, other 4xx), the order and readiness, a restart over the same files with a crash's leftovers, the attempts kept on disk, the 7 days and the cap, flush() in a pause, offline, under a Retry-After, right after an event and after release or a refusal, the triggers and the timers, clearing with an event accepted after it, release, the popup rule, two instances of one shop, a disk that refuses an event, the source and time on the wire and the session an event extends or waits for; through the SDK the shared tracking fixtures on the new core, an old listener hearing the answer once the event went, the stored source on /push only and flush() on both cores and from Java; a cancelled deletion waits on a held io thread instead of a test scheduler --- .../com/personalization/SdkStorageTest.kt | 16 +- .../com/personalization/TrackingQueueTest.kt | 318 +++++++ .../core/internal/CoreFakes.kt | 5 + .../core/internal/EventQueueTest.kt | 831 ++++++++++++++++++ .../core/internal/ShopStorageTest.kt | 5 +- 5 files changed, 1168 insertions(+), 7 deletions(-) create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt index f53ee0b3..e6551642 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt @@ -10,6 +10,7 @@ import com.personalization.core.SdkError import com.personalization.core.SdkException import com.personalization.core.SdkResult import com.personalization.core.SdkState +import com.personalization.core.internal.CoreDispatchers import com.personalization.core.internal.CoreSelection import com.personalization.core.internal.DeviceStorage import com.personalization.core.internal.LegacyCore @@ -25,11 +26,13 @@ import java.io.File import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.CountDownLatch import java.util.concurrent.Executor +import java.util.concurrent.Executors import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicInteger import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking @@ -258,17 +261,20 @@ class SdkStorageTest { context.getSharedPreferences(StorageLayout(SHOP, null).partitionName, Context.MODE_PRIVATE).edit().clear().commit() val sdk = Rees46.initialize(context, config(coreV2 = coreV2)) awaitReady(sdk, "did-1") - val scheduler = TestCoroutineScheduler() - sdk.dispatchers = testCoreDispatchers(scheduler) + // The io thread is busy until the test lets it go: the wipe waits there. + val busy = CountDownLatch(1) + val io = Executors.newSingleThreadExecutor() + io.execute { busy.await() } + sdk.dispatchers = CoreDispatchers(io = io.asCoroutineDispatcher(), main = { Dispatchers.Unconfined }) - // The caller goes as soon as the wipe is handed to the io dispatcher. val caller = CoroutineScope(Dispatchers.Unconfined).launch { sdk.deleteUserCredentials() } assertEquals(SdkState.INITIALIZING, sdk.state) caller.cancel() - scheduler.advanceUntilIdle() + busy.countDown() - assertTrue(caller.isCancelled) awaitReady(sdk, "did-2") + assertTrue(caller.isCancelled) + io.shutdown() assertEquals("core v2: $coreV2", listOf("", ""), inits) } } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt new file mode 100644 index 00000000..fd4f0c91 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt @@ -0,0 +1,318 @@ +package com.personalization + +import android.content.Context +import android.net.ConnectivityManager +import android.net.NetworkCapabilities +import android.os.Looper +import androidx.test.core.app.ApplicationProvider +import com.personalization.api.OnApiCallbackListener +import com.personalization.api.models.purchase.PurchaseItemRequest +import com.personalization.api.models.purchase.PurchaseTrackingRequest +import com.personalization.api.models.tracking.TrackingItem +import com.personalization.api.models.tracking.TrackingSource +import com.personalization.api.models.tracking.TrackingSourceType +import com.personalization.core.CompletionListener +import com.personalization.core.SdkError +import com.personalization.core.SdkException +import com.personalization.core.SdkResult +import com.personalization.core.SdkState +import com.personalization.core.internal.ContractFixtures +import com.personalization.core.internal.CoreSelection +import com.personalization.core.internal.DeviceStorage +import com.personalization.core.internal.LegacyCore +import com.personalization.core.internal.QueueStore +import com.personalization.core.internal.ShopStorage +import com.personalization.core.internal.StorageLayout +import com.personalization.core.internal.TrackingThreads +import com.personalization.logs.SdkLogs +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeout +import okhttp3.mockwebserver.Dispatcher +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import okhttp3.mockwebserver.RecordedRequest +import org.json.JSONArray +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Assert.fail +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.ShadowNetworkCapabilities +import java.io.File +import java.util.concurrent.CountDownLatch +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit + +/** + * Tracking through the SDK on the new core, as an app calls it: the shared contract fixtures + * (`contract/0/tracking.json`) on the wire, the old listeners hearing the server's answer once the + * event went, the stored source on `/push` only, and `flush()` on both cores. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class TrackingQueueTest { + + private lateinit var context: Context + private var server = MockWebServer() + + /** Each request the server got: its path and its body (or query, for a GET). */ + private val requests = LinkedBlockingQueue>() + + /** Held answers to events while it is not null. */ + @Volatile + private var holdEvents: CountDownLatch? = null + private val threads = TrackingThreads() + + @Before + fun setUp() { + context = ApplicationProvider.getApplicationContext() + CoreSelection.reset() + SdkRegistry.reset() + Rees46.reset() + SdkLogs.reset() + QueueStore.reset() + context.getSharedPreferences(StorageLayout(SHOP, null).partitionName, Context.MODE_PRIVATE).edit().clear().commit() + listOf(PreferencesPartition.LEGACY_KEY, DeviceStorage.NAME).forEach { + context.getSharedPreferences(it, Context.MODE_PRIVATE).edit().clear().commit() + } + File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() + val manager = context.getSystemService(ConnectivityManager::class.java) + val capabilities = ShadowNetworkCapabilities.newInstance() + shadowOf(capabilities).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + shadowOf(manager).setNetworkCapabilities(manager.activeNetwork, capabilities) + LegacyCore.threads = threads + // A test that runs both cores starts over in the middle: a server starts once. + server = MockWebServer() + requests.clear() + server.dispatcher = object : Dispatcher() { + override fun dispatch(request: RecordedRequest): MockResponse { + val url = request.requestUrl ?: return MockResponse().setResponseCode(400) + val path = url.encodedPath.removePrefix("/") + val body = if (request.method == "POST") JSONObject(request.body.readUtf8()) + else JSONObject().apply { url.queryParameterNames.forEach { put(it, url.queryParameter(it)) } } + requests += path to body + if (path == "init") return MockResponse().setBody("""{"did":"D1","seance":"S1"}""") + holdEvents?.await(5, TimeUnit.SECONDS) + return MockResponse().setBody("""{"answer":"$path"}""") + } + } + server.start() + } + + @After + fun tearDown() { + holdEvents?.countDown() + SdkRegistry.all().forEach { it.release() } + server.shutdown() + LegacyCore.threads = LegacyCore.DEFAULT_THREADS + CoreSelection.reset() + SdkRegistry.reset() + Rees46.reset() + SdkLogs.reset() + QueueStore.reset() + } + + private fun sdk(coreV2: Boolean = true): SDK { + val sdk = Rees46.initialize( + context, + Rees46Config( + shopId = SHOP, + apiDomain = server.url("/").toString().trimEnd('/'), + autoSendPushToken = false, + coreV2 = coreV2, + ) + ) + runBlocking { withTimeout(5_000) { sdk.instanceState.changes.first { it == SdkState.READY } } } + return sdk + } + + /** The next request to [path], skipping others (`/init`, the legacy `profile/set`). */ + private fun next(path: String): JSONObject { + while (true) { + val (got, body) = requests.poll(5, TimeUnit.SECONDS) ?: throw AssertionError("no $path request") + if (got == path) return body + } + } + + private class Heard : OnApiCallbackListener() { + val calls = LinkedBlockingQueue() + override fun onSuccess(response: JSONObject?) { + calls += "success:$response" + } + + override fun onError(code: Int, msg: String?) { + calls += "error:$code" + } + } + + @Test + fun `every case of the tracking fixtures on the new core`() { + val sdk = sdk() + ContractFixtures.cases("tracking").forEach { case -> + val name = case.getString("name") + val call = case.getJSONObject("call") + val heard = Heard() + call(sdk, call.getString("method"), call.getJSONObject("args"), heard) + if (case.has("error")) { + // Refused before the queue, at once: the old callbacks' client-side codes are negative. + val answer = heard.calls.poll(1, TimeUnit.SECONDS) + assertTrue("$name: $answer", answer?.startsWith("error:-") == true) + return@forEach + } + val request = case.getJSONObject("request") + val body = next(request.getString("path")) + assertSubset(name, request.getJSONObject("body"), body) + // The core's own fields, put on when it went. + assertEquals(name, SHOP, body.getString("shop_id")) + assertEquals(name, "D1", body.getString("did")) + assertEquals(name, "S1", body.getString("sid")) + assertEquals(name, "S1", body.getString("seance")) + assertEquals(name, "android", body.getString("stream")) + assertTrue(name, body.getString("segment").isNotEmpty()) + assertFalse(name, body.has("source")) + assertEquals(name, request.getString("path") == "push/custom", body.has("time")) + assertTrue(name, heard.calls.poll(5, TimeUnit.SECONDS)?.startsWith("success") == true) + } + } + + @Test + fun `an old listener hears the server's answer once the event went`() { + val sdk = sdk() + val hold = CountDownLatch(1).also { holdEvents = it } + val heard = Heard() + + sdk.tracking.productView("37", listener = heard) + next("push") + assertNull("not before the answer", heard.calls.poll(200, TimeUnit.MILLISECONDS)) + hold.countDown() + + assertEquals("""success:{"answer":"push"}""", heard.calls.poll(5, TimeUnit.SECONDS)) + assertNull("once", heard.calls.poll(200, TimeUnit.MILLISECONDS)) + } + + @Test + fun `the stored source goes on push when it is sent, and not on push custom`() { + val sdk = sdk() + sdk.tracking.setSource(TrackingSource(TrackingSourceType.CHAIN, "c1")) + + sdk.tracking.productView("37") + sdk.tracking.custom("added_to_compare") + + val push = next("push") + assertEquals("chain", push.getJSONObject("source").getString("from")) + assertEquals("c1", push.getJSONObject("source").getString("code")) + assertFalse(next("push/custom").has("source")) + } + + @Test + fun `flush sends the queue on the new core and returns at once on the legacy one, after release notInitialized`() { + listOf(true, false).forEach { coreV2 -> + if (!coreV2) tearDown().also { setUp() } + val sdk = sdk(coreV2) + sdk.tracking.productView("37") + + runBlocking { withTimeout(5_000) { sdk.flush() } } + + next("push") + threads.awaitIdle() + sdk.release() + try { + runBlocking { sdk.flush() } + fail("expected SdkException") + } catch (e: SdkException) { + assertEquals("core v2: $coreV2", SdkError.NotInitialized, e.error) + } + } + } + + @Test + fun `the Java form of flush ends once on the main thread`() { + val sdk = sdk() + val ends = LinkedBlockingQueue, Boolean>>() + + sdk.flush(CompletionListener { ends += it to (Looper.myLooper() == Looper.getMainLooper()) }) + val deadline = System.currentTimeMillis() + 5_000 + while (ends.isEmpty() && System.currentTimeMillis() < deadline) { + shadowOf(Looper.getMainLooper()).idle() + Thread.sleep(5) + } + + assertEquals(SdkResult.Success(Unit) to true, ends.single()) + } + + /** Calls the `tracking` method of a fixture with its contract arguments. */ + private fun call(sdk: SDK, method: String, args: JSONObject, listener: OnApiCallbackListener) { + val tracking = sdk.tracking + when (method) { + "productView" -> tracking.productView(args.getString("itemId"), args.optJSONObject("source")?.let(::source), listener) + "categoryView" -> tracking.categoryView(args.getString("categoryId"), listener) + "addToCart" -> tracking.addToCart(item(args.getJSONObject("item")), listener = listener) + "syncCart" -> tracking.syncCart(args.getJSONArray("items").objects().map(::item), listener) + "syncFavorites" -> tracking.syncFavorites(args.getJSONArray("itemIds").strings(), listener) + "purchase" -> tracking.purchase(purchase(args.getJSONObject("request")), listener = listener) + "storyView" -> tracking.storyView(args.getString("storyId"), args.getString("slideId"), args.optString("code").ifEmpty { null }, listener) + "custom" -> tracking.custom( + event = args.getString("event"), + category = args.optString("category").ifEmpty { null }, + customFields = args.optJSONObject("customFields")?.let { fields -> fields.keys().asSequence().associateWith { fields.get(it) } }, + listener = listener, + ) + else -> fail("no call for $method") + } + } + + private fun source(json: JSONObject) = TrackingSource( + TrackingSourceType.values().first { it.name.replace("_", "").equals(json.getString("type"), ignoreCase = true) }, + json.getString("code"), + ) + + private fun item(json: JSONObject) = TrackingItem( + id = json.getString("id"), + quantity = json.optInt("quantity", 1), + price = if (json.has("price")) json.getDouble("price") else null, + fashionSize = json.optString("fashionSize").ifEmpty { null }, + ) + + private fun purchase(json: JSONObject) = PurchaseTrackingRequest( + orderId = json.getString("orderId"), + orderPrice = json.getDouble("orderPrice"), + items = json.getJSONArray("items").objects().map { PurchaseItemRequest(it.getString("id"), it.getInt("amount"), it.getDouble("price")) }, + isTaxFree = json.optBoolean("isTaxFree"), + ) + + private fun JSONArray.objects(): List = List(length()) { getJSONObject(it) } + private fun JSONArray.strings(): List = List(length()) { getString(it) } + + /** Each field of [expected] is in [actual] with the same value; objects in arrays compare the same way. */ + private fun assertSubset(name: String, expected: Any?, actual: Any?) { + when (expected) { + is JSONObject -> { + assertTrue("$name: $actual is no object", actual is JSONObject) + expected.keys().forEach { key -> + assertTrue("$name: no $key in $actual", (actual as JSONObject).has(key)) + assertSubset("$name.$key", expected.get(key), actual.get(key)) + } + } + is JSONArray -> { + assertTrue("$name: $actual is no array", actual is JSONArray && actual.length() == expected.length()) + for (index in 0 until expected.length()) assertSubset("$name[$index]", expected.get(index), (actual as JSONArray).get(index)) + } + // The SDK has always sent an item's amount as a string ("2"): the server reads both. + is Number -> assertEquals(name, expected.toDouble(), actual.toString().toDouble(), 0.0) + else -> assertEquals(name, expected, actual) + } + } + + private companion object { + const val SHOP = "tracking-shop" + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt index a32848cc..5832aa5b 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt @@ -32,6 +32,11 @@ internal class FakeConnectivity(connected: Boolean = true) : ConnectivityMonitor isConnected = connected listeners.forEach { it(connected) } } + + /** A change nobody hears of, as when the system's callback does not come. */ + fun setQuietly(connected: Boolean) { + isConnected = connected + } } /** Foreground or background, switched by the test; listeners hear each switch. */ diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt new file mode 100644 index 00000000..bc23bb31 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt @@ -0,0 +1,831 @@ +package com.personalization.core.internal + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import com.personalization.LogLevel +import com.personalization.SdkLogHandler +import com.personalization.SdkLogMessage +import com.personalization.api.OnApiCallbackListener +import com.personalization.core.SdkError +import com.personalization.core.SdkException +import com.personalization.core.SdkState +import com.personalization.logs.LogCodes +import com.personalization.logs.LogReasons +import com.personalization.logs.SdkLogs +import com.personalization.sdk.data.repositories.preferences.PreferencesDataSourceImpl +import com.personalization.sdk.data.repositories.userSettings.UserSettingsRepositoryImpl +import com.personalization.sdk.domain.models.NetworkMethod +import com.personalization.sdk.domain.repositories.UserSettingsRepository +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.async +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.TestCoroutineScheduler +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.io.File +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.Executor + +/** + * The new core's event queue (`core/queue.md`, decision 0002) over its real files and a real `/init`: + * every row of the answers' table, the order, a restart, the limits, `flush()`, the triggers, the + * clearing, the popup rule, `release()`, two instances of one shop and what goes on the wire. Time is the + * fake clock's for the events' age and the session, the test scheduler's for the pauses. + */ +@OptIn(ExperimentalCoroutinesApi::class) +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class EventQueueTest { + + private lateinit var context: Context + private lateinit var storage: ShopStorage + private lateinit var settings: UserSettingsRepository + private val layout = StorageLayout(SHOP, null) + private val scheduler = TestCoroutineScheduler() + private val dispatchers = testCoreDispatchers(scheduler) + private val clock = FakeClock() + private val connectivity = FakeConnectivity(connected = true) + private val lifecycle = FakeAppLifecycle(foreground = true) + private lateinit var state: InstanceState + private val logged = CopyOnWriteArrayList() + private val defaultExecutor = SdkLogs.handlerExecutor + + /** `/init`: answered at once with [initAnswer] unless [holdInit]; then the test answers [initCalls]. */ + private var initAnswer: () -> HttpResult = { ok() } + private var holdInit = false + private val initCalls = mutableListOf>() + private var initsSent = 0 + + /** Events: answered from [eventAnswers], else a `2xx` with `{}`; or held while [holdEvents]. */ + private val eventAnswers = ArrayDeque() + private var holdEvents = false + private val eventCalls = mutableListOf>() + + /** What went: the path, the body with its own fields and the sid of that moment. */ + private val sent = mutableListOf() + + private class Sent(val path: String, val body: JSONObject, val sid: String) + + private var source: JSONObject? = null + private val inits = mutableListOf() + private val queues = mutableListOf() + private val direct = mutableListOf() + + @Before + fun setUp() { + context = ApplicationProvider.getApplicationContext() + context.getSharedPreferences(layout.partitionName, Context.MODE_PRIVATE).edit().clear().commit() + File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() + QueueStore.reset() + storage = ShopStorage(context, layout) + settings = UserSettingsRepositoryImpl(PreferencesDataSourceImpl().apply { initialize(context, layout.partitionName, SHOP) }) + state = InstanceState(shopId = { SHOP }, dispatchers = dispatchers) + logged.clear() + sent.clear() + eventAnswers.clear() + eventCalls.clear() + initCalls.clear() + direct.clear() + initsSent = 0 + holdInit = false + holdEvents = false + initAnswer = { ok() } + source = null + SdkLogs.reset() + SdkLogs.handlerExecutor = Executor { it.run() } + SdkLogs.register(SHOP, LogLevel.DEBUG, "SDK") + SdkLogs.handler = SdkLogHandler { logged += it } + } + + @After + fun tearDown() { + queues.forEach { it.release() } + inits.forEach { it.release() } + queues.clear() + inits.clear() + SdkLogs.reset() + SdkLogs.handlerExecutor = defaultExecutor + QueueStore.reset() + } + + private fun ok(did: String = "D1", sid: String = "S1") = HttpResult.Success(JSONObject().put("did", did).put("seance", sid)) + private fun http(code: Int, retryAfterMs: Long? = null) = HttpResult.Failure(SdkError.Http(code), """{"status":"error"}""", retryAfterMs) + + /** An instance as an app starts it: its `/init`, then its queue over the shop's files. */ + private fun start(cap: Int = EventQueue.CAP): EventQueue { + val init = CoreInit( + setup = InitSetup(SHOP), + send = { + initsSent++ + if (holdInit) CompletableDeferred().also { initCalls += it }.await() else initAnswer() + }, + settings = settings, + storage = storage, + state = state, + connectivity = connectivity, + lifecycle = lifecycle, + hooks = object : InitHooks { + override fun onAnswer(answer: JSONObject, fresh: Boolean) = Unit + override fun onUsable() = Unit + }, + dispatchers = dispatchers, + clock = clock, + backoff = Backoff(random = { 0.5 }), + session = SessionWindow(settings, storage, clock), + ).also { inits += it } + val queue = EventQueue( + shopId = SHOP, + store = QueueStore.of(storage.queuePath, SHOP), + init = init, + state = state, + send = { method, body -> + sent += Sent(method.method, JSONObject(body.toString()), settings.getSid()) + if (holdEvents) CompletableDeferred().also { eventCalls += it }.await() + else eventAnswers.removeFirstOrNull() ?: HttpResult.Success(JSONObject()) + }, + connectivity = connectivity, + lifecycle = lifecycle, + source = { source }, + sendDirectly = { path, _, _ -> direct += path }, + dispatchers = dispatchers, + clock = clock, + backoff = Backoff(random = { 0.5 }), + cap = cap, + ).also { queues += it } + queue.start() + init.start() + run() + return queue + } + + private fun run() = scheduler.runCurrent() + + private fun after(millis: Long) { + scheduler.advanceTimeBy(millis) + scheduler.runCurrent() + } + + private fun event(queue: EventQueue, id: String, path: String = "push", listener: OnApiCallbackListener? = null) { + queue.accept(path, JSONObject().put("event", "view").put("items", org.json.JSONArray().put(JSONObject().put("id", id))), listener) + } + + private fun ids(): List = sent.map { it.body.optJSONArray("items")?.optJSONObject(0)?.optString("id").orEmpty() } + + private fun files(): List = storage.queuePath.list()?.sorted().orEmpty() + + private fun codes(code: String) = logged.filter { it.code == code } + + private fun flush(queue: EventQueue): Deferred = CoroutineScope(StandardTestDispatcher(scheduler)).async { + try { + queue.flush() + null + } catch (e: SdkException) { + e + } + }.also { run() } + + /** Records what an old-style listener hears. */ + private class Heard : EventAnswerListener() { + val calls = CopyOnWriteArrayList() + var heldBack: String? = "unset" + + override fun onSuccess(response: JSONObject?) { + heldBack = popupHeldBack + calls += "success:$response" + } + + override fun onError(code: Int, msg: String?) { + calls += "error:$code" + } + } + + // Order, readiness, the restart. + + @Test + fun `events wait for ready and go in order, each with the sid of its send`() { + holdInit = true + val queue = start() + listOf("1", "2", "3").forEach { event(queue, it) } + run() + assertTrue("nothing goes before ready", sent.isEmpty()) + assertEquals(3, files().size) + + initCalls.single().complete(ok(sid = "S1")) + run() + + assertEquals(listOf("1", "2", "3"), ids()) + assertTrue(sent.all { it.sid == "S1" }) + assertTrue(files().isEmpty()) + } + + @Test + fun `an event is accepted once its file is on disk, written whole`() { + val queue = start() + connectivity.set(false) + event(queue, "1") + assertTrue("written off the caller's thread", files().isEmpty()) + run() + + val names = files() + assertEquals(1, names.size) + assertTrue(names.single().endsWith(".json")) + val stored = JSONObject(File(storage.queuePath, names.single()).readText()) + assertEquals("push", stored.getString("path")) + assertEquals(clock.now, stored.getLong("created_at")) + assertFalse("no common fields on disk", stored.getJSONObject("body").has("did")) + } + + @Test + fun `the queue outlives a restart, a new instance over the same files sends them`() { + val first = start() + connectivity.set(false) + listOf("1", "2").forEach { event(first, it) } + run() + first.release() + inits.forEach { it.release() } + queues.clear() + inits.clear() + // A new process: no store in memory, a temporary file a crash left, an unreadable event. + QueueStore.reset() + File(storage.queuePath, "0000000000000000099.tmp").writeText("{") + File(storage.queuePath, "0000000000000000000.json").writeText("not json") + state = InstanceState(shopId = { SHOP }, dispatchers = dispatchers) + + connectivity.set(true) + start() + + assertEquals(listOf("1", "2"), ids()) + assertTrue(files().isEmpty()) + assertEquals("unreadable", codes(LogCodes.QUEUE_DROPPED).single().context["reason"]) + } + + // The answers (decision 0002). + + @Test + fun `a 2xx removes the event and its listener hears the server's answer`() { + val queue = start() + eventAnswers += HttpResult.Success(JSONObject().put("ok", 1)) + eventAnswers += HttpResult.Failure(SdkError.Decoding) + val first = Heard() + val second = Heard() + event(queue, "1", listener = first) + event(queue, "2", listener = second) + run() + + assertEquals(listOf("""success:{"ok":1}"""), first.calls) + assertEquals("a 2xx that is not JSON went all the same", listOf("success:{}"), second.calls) + assertTrue(files().isEmpty()) + } + + @Test + fun `without a network events wait without an attempt and go when it comes back`() { + val queue = start() + connectivity.set(false) + val heard = Heard() + event(queue, "1", listener = heard) + after(EventQueue.NETWORK_RECHECK_MS * 3) + + assertTrue(sent.isEmpty()) + assertTrue(heard.calls.isEmpty()) + connectivity.set(true) + run() + assertEquals(listOf("1"), ids()) + assertEquals(1, heard.calls.size) + } + + @Test + fun `a timeout or a lost connection repeats after a pause and does not count`() { + val queue = start() + repeat(12) { eventAnswers += HttpResult.Failure(SdkError.Timeout) } + event(queue, "1") + run() + assertEquals(1, sent.size) + after(1_000) + assertEquals(2, sent.size) + after(2_000) + assertEquals(3, sent.size) + // Never dropped for timeouts: the attempts stay at 0. + val stored = JSONObject(File(storage.queuePath, files().single()).readText()) + assertEquals(0, stored.getInt("attempts")) + } + + @Test + fun `5xx, 408, 410, 417, 426 and 429 repeat with growing pauses and the tenth drops the event, not the next`() { + listOf(500, 502, 503, 408, 410, 417, 426, 429).forEach { code -> + tearDown() + setUp() + val queue = start() + repeat(10) { eventAnswers += http(code) } + val heard = Heard() + event(queue, "1", listener = heard) + event(queue, "2") + run() + // 1, 2, 4… seconds, up to 256: nine pauses after the first nine answers. + listOf(1_000L, 2_000L, 4_000L, 8_000L, 16_000L, 32_000L, 64_000L, 128_000L, 256_000L).forEachIndexed { index, pause -> + after(pause - 1) + assertEquals("$code", index + 1, sent.size) + after(1) + } + assertEquals("$code", listOf("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "2"), ids()) + assertEquals("$code", listOf("error:$code"), heard.calls) + val drop = codes(LogCodes.QUEUE_DROPPED).single() + assertEquals(LogLevel.WARNING, drop.level) + assertEquals(LogReasons.ATTEMPTS, drop.context["reason"]) + assertEquals(10, drop.context["attempts"]) + } + } + + @Test + fun `the attempts are kept across a restart`() { + val queue = start() + repeat(3) { eventAnswers += http(500) } + event(queue, "1") + run() + after(1_000) + after(2_000) + assertEquals(3, sent.size) + val stored = JSONObject(File(storage.queuePath, files().single()).readText()) + assertEquals(3, stored.getInt("attempts")) + } + + @Test + fun `Retry-After counts on 429 and 503 only, from a second to five minutes, and no trigger ends it`() { + val queue = start() + eventAnswers += http(503, retryAfterMs = 30_000) + eventAnswers += http(500, retryAfterMs = 600_000) + eventAnswers += http(429, retryAfterMs = 3_600_000) + event(queue, "1") + run() + + // The server's 30 s: a network flap or the foreground does not end them. + connectivity.set(false) + connectivity.set(true) + lifecycle.set(false) + after(29_999) + assertEquals(1, sent.size) + after(1) + assertEquals(2, sent.size) + + // A 500's Retry-After is not read: the backoff's 2 s, which a trigger ends. + after(1_000) + lifecycle.set(true) + run() + assertEquals(3, sent.size) + + // An hour asked: five minutes. + after(299_999) + assertEquals(3, sent.size) + after(1) + assertEquals(4, sent.size) + } + + @Test + fun `402, 403 or 404 pause the queue for a check of the shop, which then sends the event once more`() { + listOf(402, 403, 404).forEach { code -> + tearDown() + setUp() + val queue = start() + eventAnswers += http(code) + val heard = Heard() + event(queue, "1", listener = heard) + run() + + assertEquals("$code: /init checked the shop", 2, initsSent) + assertEquals("$code", listOf("1", "1"), ids()) + assertEquals("$code", 1, heard.calls.size) + assertTrue(heard.calls.single().startsWith("success")) + } + } + + @Test + fun `a second 402, 403 or 404 after the shop was found fine drops the event as a refused service`() { + val queue = start() + eventAnswers += http(402) + eventAnswers += http(402) + val heard = Heard() + event(queue, "1", path = "push/custom", listener = heard) + event(queue, "2") + run() + + assertEquals(listOf("push/custom", "push/custom", "push"), sent.map { it.path }) + assertEquals(listOf("error:402"), heard.calls) + assertEquals(LogReasons.SERVICE_REFUSED, codes(LogCodes.QUEUE_DROPPED).single().context["reason"]) + assertEquals(SdkState.READY, state.current) + } + + @Test + fun `a check of the shop that refuses it clears the queue and takes no more events`() { + val queue = start() + eventAnswers += http(403) + initAnswer = { http(403) } + val first = Heard() + val second = Heard() + connectivity.set(false) + event(queue, "1", listener = first) + event(queue, "2", listener = second) + run() + connectivity.set(true) + run() + + assertEquals(SdkState.REFUSED, state.current) + assertEquals(listOf("error:402"), first.calls) + assertEquals(listOf("error:402"), second.calls) + assertTrue(files().isEmpty()) + val later = Heard() + event(queue, "3", listener = later) + run() + assertEquals("refused at once", listOf("error:402"), later.calls) + assertEquals(1, sent.size) + assertEquals(LogReasons.REFUSED, codes(LogCodes.QUEUE_DROPPED).single().context["reason"]) + } + + @Test + fun `the check of the shop goes at most every ten minutes`() { + val queue = start() + eventAnswers += http(404) + eventAnswers += http(404) + eventAnswers += http(404) + event(queue, "1") + event(queue, "2") + run() + + // The first event: a 404, the check, a 404 again and it is dropped. The second: a 404 finds the + // shop checked less than ten minutes ago and fine, and it goes once more without another /init. + assertEquals(2, initsSent) + assertEquals(listOf("1", "1", "2", "2"), ids()) + } + + @Test + fun `400, 401, 405, 422 and other 4xx drop the event and the next one goes`() { + listOf(400, 401, 405, 422, 451).forEach { code -> + tearDown() + setUp() + val queue = start() + eventAnswers += http(code) + val heard = Heard() + event(queue, "1", listener = heard) + event(queue, "2") + run() + + assertEquals("$code", listOf("1", "2"), ids()) + assertEquals("$code", listOf("error:$code"), heard.calls) + assertEquals(LogReasons.REJECTED, codes(LogCodes.QUEUE_DROPPED).single().context["reason"]) + } + } + + // The limits. + + @Test + fun `an event older than seven days is dropped when its turn comes`() { + val queue = start() + connectivity.set(false) + val heard = Heard() + event(queue, "old", listener = heard) + run() + clock.advance(EventQueue.TTL_MS + 1) + event(queue, "new") + connectivity.set(true) + run() + + assertEquals(listOf("new"), ids()) + assertEquals(listOf("error:-1"), heard.calls) + assertEquals(LogReasons.EXPIRED, codes(LogCodes.QUEUE_DROPPED).single().context["reason"]) + } + + @Test + fun `at the cap the oldest event makes room`() { + assertEquals(1_000, EventQueue.CAP) + val queue = start(cap = 3) + connectivity.set(false) + val oldest = Heard() + event(queue, "1", listener = oldest) + listOf("2", "3", "4").forEach { event(queue, it) } + run() + assertEquals(3, files().size) + + connectivity.set(true) + run() + assertEquals(listOf("2", "3", "4"), ids()) + assertEquals(listOf("error:-1"), oldest.calls) + assertEquals(LogReasons.OVERFLOW, codes(LogCodes.QUEUE_DROPPED).single().context["reason"]) + } + + // flush(). + + @Test + fun `flush sends now and returns once the queue was tried`() { + val queue = start() + repeat(2) { eventAnswers += http(500) } + event(queue, "1") + run() + assertEquals(1, sent.size) + + // In the backoff's pause: flush sends at once. + val flushed = flush(queue) + assertTrue(flushed.isCompleted) + assertNull(flushed.getCompleted()) + assertEquals(2, sent.size) + } + + @Test + fun `flush returns without sending what cannot go now`() { + val queue = start() + connectivity.set(false) + event(queue, "1") + assertNull(flush(queue).getCompleted()) + assertTrue(sent.isEmpty()) + + // A server's Retry-After: flush does not override it. + connectivity.set(true) + run() + eventAnswers += http(503, retryAfterMs = 60_000) + event(queue, "2") + run() + val count = sent.size + val flushed = flush(queue) + assertTrue(flushed.isCompleted) + assertEquals(count, sent.size) + } + + @Test + fun `flush covers the events accepted right before it`() { + val queue = start() + connectivity.set(false) + run() + connectivity.set(true) + event(queue, "1") + val flushed = flush(queue) + assertTrue(flushed.isCompleted) + assertEquals(listOf("1"), ids()) + } + + @Test + fun `flush fails with notInitialized after release and with shopBlocked for a refused shop`() { + initAnswer = { http(402) } + val queue = start() + assertEquals(SdkError.ShopBlocked, (flush(queue).getCompleted() as SdkException).error) + queue.release() + assertEquals(SdkError.NotInitialized, (flush(queue).getCompleted() as SdkException).error) + } + + // The triggers. + + @Test + fun `sending starts on ready, on the foreground and the background, on the network and on the timer`() { + holdInit = true + val queue = start() + event(queue, "ready") + run() + initCalls.single().complete(ok()) + run() + assertEquals(listOf("ready"), ids()) + + // Held by a pause the backoff set: each trigger ends it. + listOf<() -> Unit>( + { lifecycle.set(false) }, + { lifecycle.set(true) }, + { connectivity.set(false); connectivity.set(true) }, + ).forEachIndexed { index, trigger -> + eventAnswers += http(500) + event(queue, "t$index") + run() + val before = sent.size + trigger() + run() + assertEquals("trigger $index", before + 1, sent.size) + } + + // Waiting for a session whose /init does not answer: the pass gives up after its wait, and a + // session that comes without a word is found by the timer. + clock.advance(SessionWindow.WINDOW_MS + 1) + event(queue, "timer") + run() + val waiting = sent.size + storage.preferences.edit().putString(StorageKeys.SID, "S3").putLong(StorageKeys.SID_LAST_ACT, clock.now).commit() + after(10_000) + assertEquals(waiting, sent.size) + after(EventQueue.TIMER_MS) + assertEquals(waiting + 1, sent.size) + assertEquals("S3", sent.last().sid) + } + + @Test + fun `a network that comes back without a word is found by the queue looking again`() { + val queue = start() + connectivity.set(false) + event(queue, "1") + run() + connectivity.setQuietly(true) + after(EventQueue.NETWORK_RECHECK_MS - 1) + assertTrue(sent.isEmpty()) + after(1) + assertEquals(listOf("1"), ids()) + } + + @Test + fun `a new session sends what waited for it`() { + val queue = start() + clock.advance(SessionWindow.WINDOW_MS + 1) + initAnswer = { HttpResult.Failure(SdkError.Http(500)) } + event(queue, "1") + run() + assertTrue("events need a sid", sent.isEmpty()) + + initAnswer = { ok(sid = "S2") } + after(1_000) + assertEquals(listOf("S2"), sent.map { it.sid }) + } + + // Clearing, release, the popup rule. + + @Test + fun `clearing empties the queue, its listeners hear it, and an event accepted after it stays`() { + val queue = start() + connectivity.set(false) + val before = Heard() + event(queue, "before", listener = before) + val cleared = CoroutineScope(StandardTestDispatcher(scheduler)).async { + queue.clear(LogReasons.DELETE_USER_CREDENTIALS, LegacyError(-1, "gone")) { true } + } + run() + event(queue, "after") + run() + + assertTrue(cleared.getCompleted()) + assertEquals(listOf("error:-1"), before.calls) + assertEquals(1, files().size) + connectivity.set(true) + run() + assertEquals(listOf("after"), ids()) + } + + @Test + fun `release stops sending and keeps the events on disk, its listeners hear -1`() { + val queue = start() + connectivity.set(false) + val waiting = Heard() + event(queue, "1", listener = waiting) + run() + + queue.release() + connectivity.set(true) + after(EventQueue.TIMER_MS) + + assertTrue(sent.isEmpty()) + assertEquals(1, files().size) + assertEquals(listOf("error:-1"), waiting.calls) + val later = Heard() + event(queue, "2", listener = later) + run() + assertEquals(listOf("error:-1"), later.calls) + } + + @Test + fun `a popup is shown only for an event sent within ten seconds and in the foreground`() { + val queue = start() + val fresh = Heard() + event(queue, "1", listener = fresh) + run() + assertNull(fresh.heldBack) + + connectivity.set(false) + val late = Heard() + event(queue, "2", listener = late) + run() + clock.advance(EventQueue.POPUP_WINDOW_MS + 1) + connectivity.set(true) + run() + assertEquals(LogReasons.LATE_EVENT, late.heldBack) + + lifecycle.set(false) + val background = Heard() + event(queue, "3", listener = background) + run() + assertEquals(LogReasons.APP_IN_BACKGROUND, background.heldBack) + } + + @Test + fun `two instances of one shop never send an event twice`() { + val first = start() + connectivity.set(false) + event(first, "1") + run() + val second = EventQueue( + shopId = SHOP, + store = QueueStore.of(storage.queuePath, SHOP), + init = inits.single(), + state = state, + send = { method, body -> + sent += Sent(method.method, JSONObject(body.toString()), settings.getSid()) + HttpResult.Success(JSONObject()) + }, + connectivity = connectivity, + lifecycle = lifecycle, + dispatchers = dispatchers, + clock = clock, + ).also { queues += it } + second.start() + event(second, "2") + run() + + connectivity.set(true) + run() + assertEquals(listOf("1", "2"), ids()) + + // The one made last sends; when it goes, the first takes over. + second.release() + event(first, "3") + run() + assertEquals(listOf("1", "2", "3"), ids()) + } + + @Test + fun `an event the disk does not take goes once as a request`() { + val queue = start() + storage.directory.mkdirs() + // The queue's directory is a file: nothing can be written there. + storage.queuePath.writeText("in the way") + event(queue, "1") + run() + + assertEquals(listOf("push"), direct) + assertTrue(codes(LogCodes.STORAGE_FAILED).isNotEmpty()) + storage.queuePath.delete() + } + + // What goes on the wire. + + @Test + fun `the source goes on push only and the time of the event on push custom only, the call's own time kept`() { + val queue = start() + connectivity.set(false) + source = JSONObject().put("from", "chain").put("code", "c1") + val made = clock.now + listOf("push", "push/custom", "track/stories", "track/slider", "popup/showed").forEach { path -> + queue.accept(path, JSONObject().put("event", "e"), null) + } + queue.accept("push/custom", JSONObject().put("event", "own").put("time", 42), null) + run() + clock.advance(60_000) + connectivity.set(true) + run() + + assertEquals(listOf("push", "push/custom", "track/stories", "track/slider", "popup/showed", "push/custom"), sent.map { it.path }) + assertEquals(listOf(true, false, false, false, false, false), sent.map { it.body.has("source") }) + assertEquals("c1", sent[0].body.getJSONObject("source").getString("code")) + assertEquals(made / 1_000, sent[1].body.getLong("time")) + assertEquals(listOf(false, true, false, false, false, true), sent.map { it.body.has("time") }) + assertEquals(42, sent[5].body.getInt("time")) + } + + @Test + fun `the source of the moment of sending, not of the event`() { + val queue = start() + connectivity.set(false) + source = JSONObject().put("from", "chain").put("code", "old") + event(queue, "1") + run() + source = JSONObject().put("from", "bulk").put("code", "new") + connectivity.set(true) + run() + + assertEquals("new", sent.single().body.getJSONObject("source").getString("code")) + } + + @Test + fun `an event extends a live session, and one that waited past the window goes with a new sid after init`() { + val queue = start() + val firstActivity = settings.getSidLastActTime() + clock.advance(60_000) + event(queue, "1") + assertEquals(firstActivity + 60_000, settings.getSidLastActTime()) + run() + + connectivity.set(false) + event(queue, "2") + run() + clock.advance(SessionWindow.WINDOW_MS + 1) + initAnswer = { ok(sid = "S2") } + connectivity.set(true) + run() + + assertEquals(listOf("S1", "S2"), sent.map { it.sid }) + assertEquals(2, initsSent) + } + + private companion object { + const val SHOP = "queue-shop" + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt index 76ce6f16..53d080ea 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt @@ -17,6 +17,7 @@ import com.personalization.sdk.domain.usecases.preferences.GetPreferencesValueUs import com.personalization.sdk.domain.usecases.preferences.SavePreferencesValueUseCase import io.mockk.mockk import io.mockk.verify +import kotlinx.coroutines.runBlocking import org.json.JSONObject import org.junit.After import org.junit.Assert.assertEquals @@ -125,7 +126,7 @@ class ShopStorageTest { val queue = storage.queueDirectory() File(queue, "event-1").writeText("{}") - assertTrue(LegacyCore(mockk(relaxed = true)) { "" }.deleteUserCredentials(storage)) + assertTrue(runBlocking { LegacyCore(mockk(relaxed = true)) { "" }.deleteUserCredentials(storage) }) val left = storage.preferences.all listOf( @@ -165,7 +166,7 @@ class ShopStorageTest { File(queue, "event-1").writeText("{}") val core = CoreV2(CoreHttpClient({ "https://example.invalid/" }, mockk(relaxed = true), FakeConnectivity(), SHOP), SHOP) - assertTrue(core.deleteUserCredentials(storage)) + assertTrue(runBlocking { core.deleteUserCredentials(storage) }) core.release() assertFalse(queue?.exists() == true) From 0e3cd3ae56e40939eecaeb85b9fd46e0f9e5fcec Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:13:57 +0400 Subject: [PATCH 043/109] fix(sdk): initialize of the preferences data source and repository and InitPreferencesUseCase.invoke take the parameters of 2.49.0 again, (context, preferencesKey, legacyPreferencesKey, shopId): a three-argument call written for that release binds its third argument to the legacy key, as it did, instead of taking it for the shop. The legacy key is still not read; the SDK migrates the store of older releases itself. The overloads that split the old form from a new one are gone, and the SDK names the shop it passes. --- .../preferences/PreferencesDataSource.kt | 19 ++-------- .../preferences/PreferencesDataSourceImpl.kt | 1 + .../preferences/PreferencesRepositoryImpl.kt | 1 + .../repositories/PreferencesRepository.kt | 19 +++------- .../preferences/InitPreferencesUseCase.kt | 21 +++-------- .../core/internal/CoreInitFixturesTest.kt | 2 +- .../core/internal/CoreInitTest.kt | 2 +- .../core/internal/CoreSessionTest.kt | 2 +- .../core/internal/EventQueueTest.kt | 2 +- .../preferences/PreferencesBackCompatTest.kt | 37 +++++++++++-------- .../TrackingSourceDataSourceImplTest.kt | 1 + 11 files changed, 43 insertions(+), 64 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt index 2b6f9f91..1b428a97 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSource.kt @@ -7,28 +7,17 @@ interface PreferencesDataSource { /** * Opens the preferences partition named [preferencesKey] of [shopId]'s instance. The data of older * releases is read into it before, by [com.personalization.core.internal.StorageMigrations]. + * + * The parameters are those of earlier releases, so a call written for them binds as it did: + * [legacyPreferencesKey] is not read any more, the SDK migrates the store of older releases itself. */ fun initialize( context: Context, preferencesKey: String, + legacyPreferencesKey: String? = null, shopId: String? = null ) - /** - * The form of earlier releases. [legacyPreferencesKey] is not read any more: the store of older - * releases is read into the partition before, by the SDK's own migration. - */ - @Deprecated( - message = "legacyPreferencesKey is ignored: the SDK migrates the store of older releases itself.", - replaceWith = ReplaceWith("initialize(context, preferencesKey, shopId)") - ) - fun initialize( - context: Context, - preferencesKey: String, - legacyPreferencesKey: String?, - shopId: String? = null - ) = initialize(context = context, preferencesKey = preferencesKey, shopId = shopId) - fun getPushToken(provider: String): String fun savePushToken(provider: String, value: String) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceImpl.kt index 63f2465f..366921d7 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesDataSourceImpl.kt @@ -24,6 +24,7 @@ class PreferencesDataSourceImpl @Inject constructor() : PreferencesDataSource { override fun initialize( context: Context, preferencesKey: String, + legacyPreferencesKey: String?, shopId: String? ) { // The data of older releases is read into the partition before this, by StorageMigrations. diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesRepositoryImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesRepositoryImpl.kt index ce3bf341..204d0454 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesRepositoryImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesRepositoryImpl.kt @@ -11,6 +11,7 @@ class PreferencesRepositoryImpl @Inject constructor( override fun initialize( context: Context, preferencesKey: String, + legacyPreferencesKey: String?, shopId: String? ) = preferencesDataSource.initialize( context = context, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/repositories/PreferencesRepository.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/repositories/PreferencesRepository.kt index 127ad7b8..c41ffad4 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/repositories/PreferencesRepository.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/repositories/PreferencesRepository.kt @@ -4,26 +4,17 @@ import android.content.Context interface PreferencesRepository { - fun initialize( - context: Context, - preferencesKey: String, - shopId: String? = null - ) - /** - * The form of earlier releases. [legacyPreferencesKey] is not read any more: the SDK reads the store - * of older releases into the shop's partition itself, once, before this is called. + * Opens the shop's partition [preferencesKey]. The parameters are those of earlier releases, so a call + * written for them binds as it did: [legacyPreferencesKey] is not read any more, the SDK reads the + * store of older releases into the shop's partition itself, once, before this is called. */ - @Deprecated( - message = "legacyPreferencesKey is ignored: the SDK migrates the store of older releases itself.", - replaceWith = ReplaceWith("initialize(context, preferencesKey, shopId)") - ) fun initialize( context: Context, preferencesKey: String, - legacyPreferencesKey: String?, + legacyPreferencesKey: String? = null, shopId: String? = null - ) = initialize(context = context, preferencesKey = preferencesKey, shopId = shopId) + ) fun getPushToken(provider: String): String fun savePushToken(provider: String, value: String) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/preferences/InitPreferencesUseCase.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/preferences/InitPreferencesUseCase.kt index a9a77ac6..b079f9f7 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/preferences/InitPreferencesUseCase.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/domain/usecases/preferences/InitPreferencesUseCase.kt @@ -8,9 +8,15 @@ class InitPreferencesUseCase @Inject constructor( private val preferencesRepository: PreferencesRepository ) { + /** + * Opens the shop's partition [preferencesKey]. The parameters are those of earlier releases, so a call + * written for them binds as it did: [legacyPreferencesKey] is not read any more, the SDK reads the + * store of older releases into the shop's partition itself, once, before this is called. + */ fun invoke( context: Context, preferencesKey: String, + legacyPreferencesKey: String? = null, shopId: String? = null ) { preferencesRepository.initialize( @@ -19,19 +25,4 @@ class InitPreferencesUseCase @Inject constructor( shopId = shopId ) } - - /** - * The form of earlier releases. [legacyPreferencesKey] is not read any more: the SDK reads the store - * of older releases into the shop's partition itself, once, before this is called. - */ - @Deprecated( - message = "legacyPreferencesKey is ignored: the SDK migrates the store of older releases itself.", - replaceWith = ReplaceWith("invoke(context, preferencesKey, shopId)") - ) - fun invoke( - context: Context, - preferencesKey: String, - legacyPreferencesKey: String?, - shopId: String? = null - ) = invoke(context = context, preferencesKey = preferencesKey, shopId = shopId) } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitFixturesTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitFixturesTest.kt index 96d48ab1..9d15b747 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitFixturesTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitFixturesTest.kt @@ -63,7 +63,7 @@ class CoreInitFixturesTest { File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() val storage = ShopStorage(context, layout) case.optJSONObject("saved")?.let { storage.saveInitAnswer(it) } - val settings = UserSettingsRepositoryImpl(PreferencesDataSourceImpl().apply { initialize(context, layout.partitionName, layout.shopId) }) + val settings = UserSettingsRepositoryImpl(PreferencesDataSourceImpl().apply { initialize(context, layout.partitionName, shopId = layout.shopId) }) settings.updateShopId(layout.shopId) val response = case.getJSONObject("response") diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt index a8bcc398..bce3cd71 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt @@ -74,7 +74,7 @@ class CoreInitTest { context.getSharedPreferences(layout.partitionName, Context.MODE_PRIVATE).edit().clear().commit() File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() storage = ShopStorage(context, layout) - settings = UserSettingsRepositoryImpl(PreferencesDataSourceImpl().apply { initialize(context, layout.partitionName, SHOP) }) + settings = UserSettingsRepositoryImpl(PreferencesDataSourceImpl().apply { initialize(context, layout.partitionName, shopId = SHOP) }) SdkLogs.reset() SdkLogs.handlerExecutor = Executor { it.run() } SdkLogs.register(SHOP, LogLevel.DEBUG, "SDK") diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt index 26b58e40..8f904bd2 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt @@ -60,7 +60,7 @@ class CoreSessionTest { context.getSharedPreferences(layout.partitionName, Context.MODE_PRIVATE).edit().clear().commit() File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() storage = ShopStorage(context, layout) - settings = UserSettingsRepositoryImpl(PreferencesDataSourceImpl().apply { initialize(context, layout.partitionName, SHOP) }) + settings = UserSettingsRepositoryImpl(PreferencesDataSourceImpl().apply { initialize(context, layout.partitionName, shopId = SHOP) }) SdkLogs.reset() } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt index bc23bb31..af1efc8f 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt @@ -90,7 +90,7 @@ class EventQueueTest { File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() QueueStore.reset() storage = ShopStorage(context, layout) - settings = UserSettingsRepositoryImpl(PreferencesDataSourceImpl().apply { initialize(context, layout.partitionName, SHOP) }) + settings = UserSettingsRepositoryImpl(PreferencesDataSourceImpl().apply { initialize(context, layout.partitionName, shopId = SHOP) }) state = InstanceState(shopId = { SHOP }, dispatchers = dispatchers) logged.clear() sent.clear() diff --git a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesBackCompatTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesBackCompatTest.kt index b853d19a..f19f6482 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesBackCompatTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/preferences/PreferencesBackCompatTest.kt @@ -5,16 +5,18 @@ import com.personalization.sdk.domain.repositories.PreferencesRepository import com.personalization.sdk.domain.usecases.preferences.InitPreferencesUseCase import io.mockk.mockk import io.mockk.verify +import org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config /** - * The forms with `legacyPreferencesKey` that earlier releases had stay, deprecated: code built against - * them keeps compiling and running, and they open the partition as the current forms do. + * The parameters of `initialize` and `InitPreferencesUseCase.invoke` stay those of 2.49.0 + * (`context, preferencesKey, legacyPreferencesKey, shopId`): a call written for that release binds as it + * did — its third argument is the legacy key, not the shop — and opens the partition, the legacy key + * being ignored. */ -@Suppress("DEPRECATION") @RunWith(RobolectricTestRunner::class) @Config(sdk = [28]) class PreferencesBackCompatTest { @@ -22,37 +24,40 @@ class PreferencesBackCompatTest { private val context = mockk(relaxed = true) @Test - fun `the data source and the repository take the old form and open the partition`() { + fun `the repository's three-argument call of 2_49_0 binds its third argument to the legacy key`() { val source = mockk(relaxed = true) val repository: PreferencesRepository = PreferencesRepositoryImpl(source) + repository.initialize(context, "partition", "old_key") repository.initialize(context, "partition", "old_key", "shop") - repository.initialize(context = context, preferencesKey = "partition", legacyPreferencesKey = null) + repository.initialize(context = context, preferencesKey = "partition", shopId = "named") - verify(exactly = 1) { source.initialize(context, "partition", "shop") } - verify(exactly = 1) { source.initialize(context, "partition", null) } + verify(exactly = 1) { source.initialize(context, "partition", null, null) } + verify(exactly = 1) { source.initialize(context, "partition", null, "shop") } + verify(exactly = 1) { source.initialize(context, "partition", null, "named") } } @Test - fun `the use case takes the old form and opens the partition`() { + fun `the use case takes the forms of 2_49_0`() { val repository = mockk(relaxed = true) + val useCase = InitPreferencesUseCase(repository) - InitPreferencesUseCase(repository).invoke(context, "partition", "old_key", "shop") + useCase.invoke(context, "partition", "old_key") + useCase.invoke(context, "partition", "old_key", "shop") - verify(exactly = 1) { repository.initialize(context, "partition", "shop") } + verify(exactly = 1) { repository.initialize(context, "partition", null, null) } + verify(exactly = 1) { repository.initialize(context, "partition", null, "shop") } } @Test - fun `the data source's old form opens the partition`() { + fun `the data source's three-argument call opens the partition and not a shop named like the legacy key`() { val source = PreferencesDataSourceImpl() val real = androidx.test.core.app.ApplicationProvider.getApplicationContext() - source.initialize(real, "back_compat_partition", "old_key", "shop") + source.initialize(real, "back_compat_partition", "old_key") source.saveValue("probe", "value") - org.junit.Assert.assertEquals( - "value", - real.getSharedPreferences("back_compat_partition", Context.MODE_PRIVATE).getString("probe", null), - ) + assertEquals("value", real.getSharedPreferences("back_compat_partition", Context.MODE_PRIVATE).getString("probe", null)) + assertEquals(null, real.getSharedPreferences("old_key", Context.MODE_PRIVATE).getString("probe", null)) } } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceDataSourceImplTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceDataSourceImplTest.kt index c44fa21f..e0b82923 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceDataSourceImplTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/sdk/data/repositories/trackingSource/TrackingSourceDataSourceImplTest.kt @@ -115,6 +115,7 @@ class TrackingSourceDataSourceImplTest { override fun initialize( context: android.content.Context, preferencesKey: String, + legacyPreferencesKey: String?, shopId: String? ) = Unit From 2a31c4057b96506bd5eb734e0b3042a11b39857d Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:13:57 +0400 Subject: [PATCH 044/109] fix(core): tracking on the new core refuses an empty custom event and an empty story or slide id with invalidArgument, before the queue, and every such refusal reaches its listener off the caller's thread, as the core's other failures do; a story event of a virtual slide (a story id that is not a positive whole number, a slide id that is not one in digits) is not sent and is no error, its listener hearing an empty success (contract tracking). popup_not_shown is debug for a late event and an app in the background, which hold a popup back on purpose (decision 2). The legacy core is unchanged. --- .../trackEvent/impl/TrackEventManagerImpl.kt | 3 +- .../features/tracking/impl/TrackingApiImpl.kt | 35 ++++++++++++++++-- .../com/personalization/TrackingQueueTest.kt | 36 +++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt index 44b0d38d..9c8ebb3a 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt @@ -255,8 +255,9 @@ internal class TrackEventManagerImpl @Inject constructor( private fun handlePopup(response: JSONObject, heldBack: String? = null) { val popUpData = response.optJSONObject(PARAM_POPUP)?.let { PopupDtoMapper.map(it) } ?: return if (heldBack != null) { + // Held back on purpose (decision 2): a late event, an app in the background. SdkLogs.log( - LogLevel.WARNING, + LogLevel.DEBUG, LogCodes.POPUP_NOT_SHOWN, "The popup of an event's answer is not shown", getUserSettingsValueUseCase.getShopId().takeIf { it.isNotEmpty() }, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/tracking/impl/TrackingApiImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/tracking/impl/TrackingApiImpl.kt index 37a56dea..1c1e66b3 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/tracking/impl/TrackingApiImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/tracking/impl/TrackingApiImpl.kt @@ -11,13 +11,18 @@ import com.personalization.api.models.tracking.TrackingItem import com.personalization.api.models.tracking.TrackingSource import com.personalization.api.models.tracking.putSource import com.personalization.api.params.ProductItemParams +import com.personalization.core.internal.CoreDispatchers import com.personalization.core.internal.LegacyDelivery import com.personalization.core.internal.LegacyErrors import com.personalization.logs.LogCodes import com.personalization.logs.SdkLogs import com.personalization.sdk.domain.usecases.trackingSource.SetTrackingSourceUseCase import com.personalization.stories.StoriesManager +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.launch import org.json.JSONArray +import org.json.JSONObject import javax.inject.Inject /** @@ -38,17 +43,38 @@ internal class TrackingApiImpl @Inject constructor( private val checksArguments: () -> Boolean = { false }, ) : TrackingApi { - /** Refuses a blank [value] of [name] on the new core: the listener hears `-1` at once. True when refused. */ + /** + * Refuses a blank [value] of [name] on the new core, before the queue: the listener hears `-1` + * (`invalidArgument`) on a background thread, as for the core's other refusals. True when refused. + */ private fun refused(name: String, value: String, listener: OnApiCallbackListener?): Boolean { if (value.isNotBlank() || !checksArguments()) return false val message = "$name is empty" SdkLogs.log(LogLevel.WARNING, LogCodes.INVALID_ARGUMENT, "A tracking call was refused: $message", null, mapOf("reason" to "empty_$name")) - listener?.let { delivery.failure(it, LegacyErrors.NO_ANSWER, message) } + listener?.let { refusals.launch { delivery.failure(it, LegacyErrors.NO_ANSWER, message) } } return true } private val delivery = LegacyDelivery { level, code, message, throwable -> SdkLogs.log(level, code, message, throwable = throwable) } + /** + * On the new core, a slide `track/stories` does not take — a story id that is not a positive whole + * number (a product campaign's virtual slide, `{story_id}_{index}`), or a slide id that is not one in + * digits — is not sent and is no error (contract `tracking`): the listener hears an empty success. + */ + private fun virtualSlide(storyId: String, slideId: String, listener: OnApiCallbackListener?): Boolean { + if (!checksArguments()) return false + val story = storyId.toLongOrNull()?.takeIf { it > 0 && it.toString() == storyId } + val slide = slideId.takeIf { id -> id.all { it in '0'..'9' } }?.toLongOrNull()?.takeIf { it > 0 } + if (story != null && slide != null) return false + SdkLogs.log(LogLevel.DEBUG, LogCodes.STORIES, "A story event of a virtual slide is not sent", null, mapOf("reason" to "virtual_slide")) + listener?.let { refusals.launch { delivery.success(it, JSONObject()) } } + return true + } + + /** Where a refusal reaches its listener: off the caller's thread, once. */ + private val refusals = CoroutineScope(SupervisorJob() + CoreDispatchers.DEFAULT.io) + override fun productView( itemId: String, source: TrackingSource?, @@ -150,6 +176,8 @@ internal class TrackingApiImpl @Inject constructor( code: String?, listener: OnApiCallbackListener?, ) { + if (refused("storyId", storyId, listener) || refused("slideId", slideId, listener)) return + if (virtualSlide(storyId, slideId, listener)) return storiesManager.trackStory( event = STORY_VIEW_EVENT, code = code, @@ -165,6 +193,8 @@ internal class TrackingApiImpl @Inject constructor( code: String?, listener: OnApiCallbackListener?, ) { + if (refused("storyId", storyId, listener) || refused("slideId", slideId, listener)) return + if (virtualSlide(storyId, slideId, listener)) return storiesManager.trackStory( event = STORY_CLICK_EVENT, code = code, @@ -199,6 +229,7 @@ internal class TrackingApiImpl @Inject constructor( customFields: Map?, listener: OnApiCallbackListener?, ) { + if (refused("event", event, listener)) return trackEventManager.trackEvent( event = event, time = time, diff --git a/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt index fd4f0c91..e0e6fc4b 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt @@ -249,6 +249,42 @@ class TrackingQueueTest { assertEquals(SdkResult.Success(Unit) to true, ends.single()) } + @Test + fun `on the new core an empty event or story id is refused off the caller's thread, and a virtual slide is not sent`() { + val sdk = sdk() + val caller = Thread.currentThread() + val heard = LinkedBlockingQueue() + val listener = object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) { + heard += "success:$response:${Thread.currentThread() !== caller}" + } + + override fun onError(code: Int, msg: String?) { + heard += "error:$code:${Thread.currentThread() !== caller}" + } + } + + sdk.tracking.custom(event = "", listener = listener) + sdk.tracking.storyView("", "34", "main_stories", listener) + sdk.tracking.storyClick("12", "", "main_stories", listener) + sdk.tracking.categoryView("", listener) + repeat(4) { assertEquals("error:-1:true", heard.poll(5, TimeUnit.SECONDS)) } + + sdk.tracking.storyView("12_3", "34", "main_stories", listener) + sdk.tracking.storyClick("12", "slide-a", "main_stories", listener) + repeat(2) { assertEquals("success:{}:true", heard.poll(5, TimeUnit.SECONDS)) } + + // Only the event after them goes. + sdk.tracking.storyView("12", "34", "main_stories", listener) + val seen = mutableListOf() + while (seen.lastOrNull() != "track/stories") { + val (path, body) = requests.poll(5, TimeUnit.SECONDS) ?: throw AssertionError("no track/stories request") + seen += path + if (path == "track/stories") assertEquals(12, body.getInt("story_id")) + } + assertEquals(listOf("track/stories"), seen.filter { it != "init" }) + } + /** Calls the `tracking` method of a fixture with its contract arguments. */ private fun call(sdk: SDK, method: String, args: JSONObject, listener: OnApiCallbackListener) { val tracking = sdk.tracking From 10042e982cb3c5aec7fa9a9e91b017a98d03af22 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:13:58 +0400 Subject: [PATCH 045/109] fix(core): the guard that keeps OkHttp from repeating a 408 or a 503 no longer throws on a Retry-After outside ASCII, which would have ended the app's process from OkHttp's thread: the header is copied as it came, and dropped if even that fails; such a value is no pause of the server's --- .../core/internal/CoreHttpClient.kt | 15 ++++++++++----- .../core/internal/CoreHttpClientTest.kt | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt index 6d200879..4b26cbc3 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt @@ -321,7 +321,8 @@ internal class CoreHttpClient( * `Int` and throws on a larger number, on its own thread, which would end the app's process. For those * two codes the header is moved to [COPY] (empty without one), where the client reads it, and * `Retry-After: 1` takes its place: OkHttp hands the answer over as it is, and the core decides about - * repeats (decision 0002). + * repeats (decision 0002). A header OkHttp would refuse to build (text outside ASCII) is copied as it is, + * and dropped if even that fails: the guard itself never throws. */ internal object RetryAfterGuard : Interceptor { const val COPY = "X-Sdk-Retry-After" @@ -333,10 +334,14 @@ internal object RetryAfterGuard : Interceptor { if (response.code !in CODES) return response // Empty when the server sent none: the client then reads no Retry-After, not this "1". val original = response.header(RETRY_AFTER).orEmpty() - return response.newBuilder() - .header(COPY, original) - .header(RETRY_AFTER, "1") - .build() + val headers = response.headers.newBuilder().removeAll(COPY).removeAll(RETRY_AFTER) + try { + headers.addUnsafeNonAscii(COPY, original) + } catch (e: IllegalArgumentException) { + // Not even a header OkHttp keeps: the answer goes without its Retry-After. + } + headers.add(RETRY_AFTER, "1") + return response.newBuilder().headers(headers.build()).build() } } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt index 3bceae32..3d3d9269 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreHttpClientTest.kt @@ -284,6 +284,21 @@ class CoreHttpClientTest { assertEquals(3, server.requestCount) } + @Test + fun `a Retry-After outside ASCII on a 408 or a 503 neither throws nor repeats`() { + server.enqueue(MockResponse().setResponseCode(503).addHeaderLenient("Retry-After", "5 сек")) + server.enqueue(MockResponse().setResponseCode(408).addHeaderLenient("Retry-After", "ünf")) + + val first = send(NetworkMethod.POST("push")) + val second = send(NetworkMethod.POST("push")) + + assertEquals(SdkError.Http(503), first.error()) + assertEquals(SdkError.Http(408), second.error()) + // Not a number of seconds nor a date: no pause of the server's. + assertNull((first as HttpResult.Failure).retryAfterMs) + assertEquals(2, server.requestCount) + } + @Test fun `a POST carries the common fields in its JSON body`() { reply(200, "{}") From b13da8c189089a647cbf8bc1118b7c86842f54aa Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:13:58 +0400 Subject: [PATCH 046/109] fix(core): the event queue after review. An event read before the customer was deleted does not go under the next customer's did: a clear moves the store's generation before it wipes, and an event is stamped and sent only while its generation is current and its file still there. The common fields (did, sid, segment, stream), the source on /push and the time on /push/custom go on a copy when an event is sent and never reach its file, so a repeat after the source expired goes without it; an event the disk refuses and that goes once as a request carries its source and time too. A shop's check counts only with a new successful /init answer: a wait that runs out or a failed answer is a pause, and a check that may not go yet (one less than ten minutes ago) holds the event until it may, which no trigger ends. A disk that fails to rewrite the shop's check pauses the queue with the backoff instead of sending the event again at once, the new state kept in memory; an event whose file stays after it went is skipped from then on and removed again later; a file the disk cannot read now stays for a later try, and only one that does not parse is dropped as unreadable, its listener hearing an error. Fields JSON cannot hold (NaN, Infinity) are refused at once with invalidArgument; no command or event that throws stops the writer or the sender, which pauses before it goes on, and flush() and the customer's deletion never wait for a writer that ended. Only the process of the app that holds the queue directory's file lock sends; event files are named by time, a counter, the process and a random part, so two processes never pick one name, and a temporary file is removed only once it is old. A flush asked before release() and handled after it ends with notInitialized, an event accepted by an instance that does not send wakes the one that does, and an acceptance callback hears the event once it is written. The main thread never waits for the queue's disk work: the owners and the listeners have locks of their own. --- .../main/kotlin/com/personalization/SDK.kt | 5 +- .../core/internal/CoreHttpClient.kt | 13 +- .../personalization/core/internal/CoreInit.kt | 53 +++- .../core/internal/EventQueue.kt | 273 ++++++++++++---- .../core/internal/LegacyDelivery.kt | 2 + .../core/internal/QueueStore.kt | 286 +++++++++++++---- .../core/internal/EventQueueTest.kt | 299 +++++++++++++++++- 7 files changed, 791 insertions(+), 140 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 3ade09f1..41b2063f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -32,6 +32,7 @@ import com.personalization.core.SdkException import com.personalization.core.SdkResult import com.personalization.core.SdkState import com.personalization.core.internal.AndroidConnectivityMonitor +import com.personalization.core.internal.CommonFields import com.personalization.core.internal.ConnectivityMonitor import com.personalization.core.internal.Core import com.personalization.core.internal.CoreDispatchers @@ -564,7 +565,9 @@ open class SDK { store = QueueStore.of(storage.queuePath, shopId), init = init, state = instanceState, - send = core.client::send, + // The queue stamps the common fields on a copy of each event: nothing of them reaches its files. + send = { method, body -> core.client.send(method, body, commonFields = false) }, + stamp = { body -> CommonFields.addTo(body, userSettingsRepository) }, connectivity = connectivity, lifecycle = lifecycle, source = ::trackingSource, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt index 4b26cbc3..895047ce 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt @@ -102,8 +102,11 @@ internal class CoreHttpClient( internal fun clientFor(method: NetworkMethod): OkHttpClient = if (method.method == INIT_PATH) initClient else client - /** Sends [params] (the method's own fields) to [method]; cancelling the caller cancels the request. */ - suspend fun send(method: NetworkMethod, params: JSONObject): HttpResult { + /** + * Sends [params] (the method's own fields) to [method]; cancelling the caller cancels the request. With + * [commonFields] false the fields are all there already (the queue stamps its events itself). + */ + suspend fun send(method: NetworkMethod, params: JSONObject, commonFields: Boolean = true): HttpResult { if (!connectivity.isConnected) { log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "${line(method)}: no network") { context(method) + ("reason" to LogReasons.OFFLINE) @@ -112,7 +115,7 @@ internal class CoreHttpClient( } val phase = Phase() val built = try { - build(method, params, phase) + build(method, params, phase, commonFields) } catch (e: Exception) { log(LogLevel.ERROR, LogCodes.REQUEST_BUILD_FAILED, "${line(method)}: the request could not be built", e) { context(method) } return HttpResult.Failure(SdkError.InvalidArgument("The request could not be built")) @@ -125,8 +128,8 @@ internal class CoreHttpClient( private class Built(val request: Request, val body: String?) - private fun build(method: NetworkMethod, params: JSONObject, phase: Phase): Built { - val fields = CommonFields.addTo(params, settings) + private fun build(method: NetworkMethod, params: JSONObject, phase: Phase, commonFields: Boolean): Built { + val fields = if (commonFields) CommonFields.addTo(params, settings) else params if (method.method == INIT_PATH) { INIT_OPTIONAL_FIELDS.forEach { key -> if (fields.optString(key).isEmpty()) fields.remove(key) } // `/init` does not read the A/B group (core/init.md): it is the events' field. diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt index 8983b8a9..ac0ef3da 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt @@ -135,6 +135,9 @@ internal class CoreInit( private var held = false private var usable = false private var fallbackLogged = false + + /** How many `/init` succeeded: [checkShopAndAwait] tells a success from another answer. */ + private var successes = 0L private var lastCheckAt: Long? = null private var foreground: Cancellable? = null @@ -250,25 +253,56 @@ internal class CoreInit( state.current == SdkState.READY && (job?.isActive == true || !prepared) } + /** What [checkShopAndAwait] learnt of the shop. */ + internal sealed interface ShopCheck { + /** A new `/init` succeeded: the shop is fine. */ + object Fine : ShopCheck + + /** The shop refused the SDK. */ + object Refused : ShopCheck + + /** The instance was released. */ + object Released : ShopCheck + + /** + * No new answer told: the check could not go yet — [retryInMs] until it may, after one less than + * 10 minutes ago — or its answer did not come in time or was not a success (null). + */ + class Unknown(val retryInMs: Long?) : ShopCheck + } + /** * Asks the server about the shop after a `402`, `403` or `404` to an event ([checkShop]), and waits - * up to [timeoutMs] for the answer of the `/init` that went, or of the one already on its way. The - * state after it: [SdkState.READY] when the shop is fine, [SdkState.REFUSED] when it is not, anything - * else when the answer did not tell. Without an `/init` to wait for (one went less than 10 minutes - * ago) the state is as that one left it. + * up to [timeoutMs] for the answer of the `/init` that went, or of the one already on its way. Only a + * new answer tells: [ShopCheck.Fine] when it was a success, [ShopCheck.Refused] when the shop refused; + * a wait that ends first, a failure, or a check that may not go yet is [ShopCheck.Unknown]. */ - internal suspend fun checkShopAndAwait(timeoutMs: Long): SdkState { + internal suspend fun checkShopAndAwait(timeoutMs: Long): ShopCheck { val seen: Long + val seenSuccesses: Long synchronized(lock) { - if (released) return SdkState.NOT_INITIALIZED + if (released) return ShopCheck.Released + if (state.current == SdkState.REFUSED) return ShopCheck.Refused seen = answers.value - checkShop() - if (job?.isActive != true) return state.current + seenSuccesses = successes + val went = checkShop() + if (!went && job?.isActive != true) { + val last = lastCheckAt + val wait = if (last == null) null else (CHECK_SHOP_INTERVAL_MS - (clock.nowMillis() - last)).takeIf { it > 0 } + return ShopCheck.Unknown(wait) + } } withTimeoutOrNull(timeoutMs) { combine(answers, gate) { count, current -> count > seen || current == Gate.CLOSED }.first { it } } - return state.current + return synchronized(lock) { + when { + released -> ShopCheck.Released + state.current == SdkState.REFUSED -> ShopCheck.Refused + successes > seenSuccesses -> ShopCheck.Fine + else -> ShopCheck.Unknown(null) + } + } } /** Null for an open gate, otherwise why a request may not go; [Gate.WAIT] is never decided. */ @@ -460,6 +494,7 @@ internal class CoreInit( // The answer on disk is the new customer's now. if (storage.isWiped) storage.isWiped = false if (storage.isRefused) storage.isRefused = false + successes++ publish(SdkState.READY) log(LogLevel.INFO, LogCodes.INIT_SUCCEEDED, "/init succeeded") if (advertisingIdValue != null) log(LogLevel.DEBUG, LogCodes.ADVERTISING_ID_SENT, "Advertising id sent with /init") diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt index 6f810394..20ec1f29 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt @@ -14,6 +14,7 @@ import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel @@ -41,22 +42,28 @@ internal abstract class EventAnswerListener : OnApiCallbackListener() { * The new core's event queue (`core/queue.md`, decision 0002): `/push`, `/push/custom`, `track/stories`, * `track/slider` and `popup/showed` go through it, in order, and survive no network and a restart. * - * - **Accepting.** [accept] answers a released instance with `notInitialized` and a refused shop with - * `shopBlocked` at once; otherwise it moves the session's last activity and hands the event to a - * writer that keeps the calls' order: an event is accepted once its file is on disk ([QueueStore]). - * A disk that refuses it sends it once, as a request. - * - **Stamping.** The common fields — `did`, the `sid` of the moment — go on when the event is sent, - * with the stored tracking source as a `source` object on `/push` only and the time it was made as - * `time` on `/push/custom` only (the only path that reads it), unless the call set its own. + * - **Accepting.** [accept] answers a released instance with `notInitialized`, a refused shop with + * `shopBlocked` and fields JSON cannot hold with `invalidArgument`, at once; otherwise it moves the + * session's last activity and hands the event to a writer that keeps the calls' order: an event is + * accepted once its file is on disk ([QueueStore]), and the caller's `accepted` hears it then. A disk + * that refuses it sends it once, as a request. + * - **Stamping.** The common fields — `did`, the `sid` of the moment, `segment`, `stream` — go on a copy + * when the event is sent ([stamp]), with the stored tracking source as a `source` object on `/push` only + * and the time it was made as `time` on `/push/custom` only (the only path that reads it), unless the + * call set its own. Nothing stamped reaches the disk. * - **Sending.** One event at a time, oldest first, once the instance is ready and the session alive - * (a session whose window ended gets its `/init` first). Sending starts on [SdkState.READY], on the - * app's foreground and background, on the network's return, on [flush] and on a timer. + * (a session whose window ended gets its `/init` first), by the one process of the app that holds the + * queue's file lock. An event cleared while it waited (the customer's deletion) does not go. Sending + * starts on [SdkState.READY], on the app's foreground and background, on the network's return, on + * [flush] and on a timer. * - **Answers.** `2xx` removes the event. No network waits for it and a timeout repeats it after a pause, * neither counting as an attempt. `5xx`, `408`, `410`, `417`, `426`, `429` repeat it after a pause * (the `Retry-After` of a `429` or `503`), and the 10th drops it. `402`, `403`, `404` pause the queue * while `/init` checks the shop ([CoreInit.checkShopAndAwait]): a refusal clears the queue, a shop - * found fine sends the event once more, and a second such answer drops it. Other answers drop it at - * once. A dropped event does not hold the others. + * found fine by a new answer sends the event once more, and a second such answer drops it; a check + * that may not go yet holds the event until it may. Other answers drop it at once. A dropped event + * does not hold the others. A disk that fails to read or rewrite an event pauses the queue instead of + * sending it again at once. * - **Limits.** An event older than 7 days is dropped when its turn comes; at 1000 events the oldest go. * Each drop is `queue_dropped` with its reason. * - **Listeners.** An old-style listener hears the event's fate once: the server's answer when it went, @@ -64,19 +71,23 @@ internal abstract class EventAnswerListener : OnApiCallbackListener() { * of being made and the app is in the foreground (decision 2). * * [release] stops sending and keeps the files for the next launch; [clear] empties the queue (the - * customer's deletion, the shop's refusal, which the queue hears from the state by itself). + * customer's deletion, the shop's refusal, which the queue hears from the state by itself). Neither the + * writer nor the sender stops on an unexpected exception: it is logged and the next command or event goes. */ internal class EventQueue( private val shopId: String, private val store: QueueStore, private val init: CoreInit, private val state: InstanceState, + /** Sends an event whose fields are all on it: it adds none. */ private val send: suspend (NetworkMethod, JSONObject) -> HttpResult, private val connectivity: ConnectivityMonitor, private val lifecycle: AppLifecycle, /** The stored tracking source as `source` (`from`, `code`); null when there is none or it expired. */ private val source: () -> JSONObject? = { null }, - /** Sends an event the disk would not take, once, as a request. */ + /** Puts the common fields on an event's copy before it goes. */ + private val stamp: (JSONObject) -> JSONObject = { it }, + /** Sends an event the disk would not take, once, as a request; the common fields are its to add. */ private val sendDirectly: (path: String, body: JSONObject, listener: OnApiCallbackListener?) -> Unit = { _, _, _ -> }, dispatchers: CoreDispatchers = CoreDispatchers.DEFAULT, private val clock: Clock = Clock.SYSTEM, @@ -100,6 +111,10 @@ internal class EventQueue( private val commands = Channel(Channel.UNLIMITED) private val wake = Channel(Channel.CONFLATED) + /** The writer: while it runs, a command sent to it is handled. */ + @Volatile + private var writer: Job? = null + /** A trigger that may end a pause: not a new event, which waits for the pause like the others. */ @Volatile private var urgent = false @@ -121,7 +136,14 @@ internal class EventQueue( private val delivery = LegacyDelivery { level, code, message, throwable -> log(level, code, message, throwable = throwable) } private sealed interface Command { - class Accept(val path: String, val body: JSONObject, val createdAt: Long, val listener: OnApiCallbackListener?) : Command + class Accept( + val path: String, + val body: JSONObject, + val createdAt: Long, + val listener: OnApiCallbackListener?, + val accepted: ((SdkError?) -> Unit)?, + ) : Command + class Clear(val reason: String, val error: LegacyError, val then: () -> Boolean, val done: CompletableDeferred?) : Command class Flush(val done: CompletableDeferred) : Command } @@ -130,7 +152,7 @@ internal class EventQueue( /** Nothing to send, or not this queue's to send: wait for a trigger. */ object Idle : Outcome - /** The instance or its session is not there yet: wait for a trigger or the timer. */ + /** The instance or its session is not there yet, or another process sends: wait for a trigger or the timer. */ object Waiting : Outcome /** No network: wait for it, looking again now and then. */ @@ -152,7 +174,15 @@ internal class EventQueue( heard.forEach { it.cancel() } return } - writing.launch { for (command in commands) handle(command) } + writer = writing.launch { + try { + for (command in commands) handleSafely(command) + } finally { + // Ended for whatever reason: nothing sent to it later may wait for it. + commands.close() + while (true) settle(commands.tryReceive().getOrNull() ?: break, null) + } + } val following = working.launch { state.changes.collect { current -> when (current) { @@ -169,29 +199,63 @@ internal class EventQueue( /** * Takes an event of [path] with its own fields [params]. [listener] hears its fate once: at once - * when the instance was released or the shop refused, otherwise after it went or was dropped. + * when the instance was released, the shop refused or the fields are not JSON, otherwise after it went + * or was dropped. [accepted] hears, once, null as soon as the event is written (or, refused by the + * disk, sent once as a request), or the error that refused it. The method itself returns at once. */ - fun accept(path: String, params: JSONObject, listener: OnApiCallbackListener?) { + fun accept(path: String, params: JSONObject, listener: OnApiCallbackListener?, accepted: ((SdkError?) -> Unit)? = null) { + // A copy of the fields as they are now, which also tells fields JSON cannot hold (NaN, Infinity). + val body = snapshot(params) val error = when { released -> SdkError.NotInitialized state.current == SdkState.REFUSED -> SdkError.ShopBlocked + body == null -> SdkError.InvalidArgument("The event's fields cannot be written as JSON") else -> null } - if (error != null) { - if (listener != null) writing.launch { LegacyErrors.of(error).let { delivery.failure(listener, it.code, it.message) } } + if (error != null || body == null) { + if (error is SdkError.InvalidArgument) { + log(LogLevel.WARNING, LogCodes.INVALID_ARGUMENT, "An event was refused: its fields are not JSON", mapOf("reason" to REASON_INVALID_JSON, "path" to path)) + } + refuse(error ?: SdkError.NotInitialized, listener, accepted) return } val createdAt = clock.nowMillis() init.session.extend(createdAt) - if (commands.trySend(Command.Accept(path, params, createdAt, listener)).isFailure && listener != null) { - writing.launch { LegacyErrors.of(SdkError.NotInitialized).let { delivery.failure(listener, it.code, it.message) } } + if (writer?.isActive != true || commands.trySend(Command.Accept(path, body, createdAt, listener, accepted)).isFailure) { + refuse(SdkError.NotInitialized, listener, accepted) + } + } + + private fun snapshot(params: JSONObject): JSONObject? = try { + // JSONObject.toString() gives null for a value JSON cannot hold. + val text: String? = params.toString() + text?.let { JSONObject(it) } + } catch (e: Exception) { + null + } + + private fun refuse(error: SdkError, listener: OnApiCallbackListener?, accepted: ((SdkError?) -> Unit)?) { + if (listener == null && accepted == null) return + writing.launch { + listener?.let { delivery.failure(it, LegacyErrors.of(error)) } + accepted?.let { tell(it, error) } + } + } + + /** Hands [accepted] its answer; one that throws is logged. */ + private fun tell(accepted: (SdkError?) -> Unit, error: SdkError?) { + try { + accepted(error) + } catch (e: Exception) { + log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "An event's acceptance listener threw", throwable = e) } } /** * Sends what is in the queue now, without waiting for a trigger, and returns once each of those * events was tried: sent, dropped, or held by no network, the instance not being ready, the shop's - * check or a server's `Retry-After`. + * check or a server's `Retry-After`. A pause of the backoff ends: the event waiting in it goes again at + * once. * * @throws SdkException `notInitialized` after [release], `shopBlocked` for a refused shop. */ @@ -199,18 +263,20 @@ internal class EventQueue( if (released) throw SdkException(SdkError.NotInitialized) if (state.current == SdkState.REFUSED) throw SdkException(SdkError.ShopBlocked) val done = CompletableDeferred() - if (commands.trySend(Command.Flush(done)).isFailure) throw SdkException(SdkError.NotInitialized) + if (writer?.isActive != true || commands.trySend(Command.Flush(done)).isFailure) throw SdkException(SdkError.NotInitialized) done.await() } /** * Empties the queue for [reason] and runs [then] while no event can be accepted: an event accepted * before this call goes, one accepted after it stays. The listeners waiting hear [error]. Returns - * [then]'s result. + * [then]'s result. Without a writer to keep the order (released) it is done here. */ suspend fun clear(reason: String, error: LegacyError, then: () -> Boolean): Boolean { val done = CompletableDeferred() - if (released || commands.trySend(Command.Clear(reason, error, then, done)).isFailure) return clearNow(reason, error, then) + if (released || writer?.isActive != true || commands.trySend(Command.Clear(reason, error, then, done)).isFailure) { + return clearNow(reason, error, then) + } return withContext(NonCancellable) { done.await() } } @@ -223,7 +289,8 @@ internal class EventQueue( * Stops sending; the events stay on disk for the next launch. With [cancel] the event in flight is * cancelled and the listeners of this queue's events hear `-1`; without, as when a push-only * instance gives way to the app's, the event in flight finishes and the listeners stay for the - * queue that sends next. Events accepted before are still written. + * queue that sends next. Events accepted before are still written; a flush asked before it and not + * handled yet ends with `notInitialized`. */ fun release(cancel: Boolean = true) { val heard = synchronized(subscriptions) { @@ -238,7 +305,7 @@ internal class EventQueue( cancelled = true working.cancel() val error = LegacyErrors.of(SdkError.Cancelled) - store.takeListeners(this).forEach { delivery.failure(it.listener, error.code, error.message) } + store.takeListeners(this).forEach { delivery.failure(it.listener, error) } } else { kick(urgent = true) } @@ -252,7 +319,40 @@ internal class EventQueue( wake.trySend(Unit) } - private suspend fun handle(command: Command) { + /** One command; nothing it throws stops the writer, and whoever waits for it still hears an end. */ + private fun handleSafely(command: Command) { + try { + handle(command) + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + log(LogLevel.ERROR, LogCodes.SDK_LOG, "A command of the event queue failed", throwable = e) + settle(command, e) + } + } + + /** Ends [command], not handled or failed with [failure]: whoever waits for it hears. */ + private fun settle(command: Command, failure: Exception?) { + when (command) { + is Command.Accept -> { + val error = if (failure == null) SdkError.NotInitialized else SdkError.InvalidArgument("The event could not be written") + command.listener?.let { delivery.failure(it, LegacyErrors.of(error)) } + command.accepted?.let { tell(it, error) } + } + // Failed midway: reported as not complete. Never handled: its work is done here. + is Command.Clear -> command.done?.complete( + failure == null && try { + command.then() + } catch (e: Exception) { + log(LogLevel.ERROR, LogCodes.STORAGE_FAILED, "The customer's data could not be deleted", throwable = e) + false + } + ) + is Command.Flush -> command.done.completeExceptionally(SdkException(SdkError.NotInitialized)) + } + } + + private fun handle(command: Command) { when (command) { is Command.Accept -> write(command) is Command.Clear -> command.done.let { done -> @@ -260,6 +360,11 @@ internal class EventQueue( done?.complete(result) } is Command.Flush -> { + if (released) { + // Asked before the release, handled after it: nothing here sends any more. + command.done.completeExceptionally(SdkException(SdkError.NotInitialized)) + return + } val owner = store.owner if (owner != null && owner !== this) { owner.flushFromOther(command.done) @@ -289,14 +394,17 @@ internal class EventQueue( val event = store.add(this, accept.path, accept.body, accept.createdAt, keepListener, cap, dropped) dropped.forEach { (old, waiting) -> dropped(old?.path, old?.attempts, LogReasons.OVERFLOW, waiting, OVERFLOWED) } if (keepListener == null && accept.listener != null) { - LegacyErrors.of(SdkError.Cancelled).let { delivery.failure(accept.listener, it.code, it.message) } + delivery.failure(accept.listener, LegacyErrors.of(SdkError.Cancelled)) } + accept.accepted?.let { tell(it, null) } if (event == null) { - // Not accepted: the disk refused it (storage_failed). It goes once, as a request. - sendDirectly(accept.path, accept.body, accept.listener) + // Not accepted: the disk refused it (storage_failed). It goes once, as a request, with what + // the queue would have put on it. + sendDirectly(accept.path, withOwnFields(accept.path, JSONObject(accept.body.toString()), accept.createdAt), accept.listener) return } - kick(urgent = false) + // The queue that sends: this one, or the one made after it. + (store.owner ?: this).kick(urgent = false) } private fun clearNow(reason: String, error: LegacyError, then: () -> Boolean): Boolean { @@ -304,15 +412,24 @@ internal class EventQueue( if (count > 0) { log(LogLevel.WARNING, LogCodes.QUEUE_DROPPED, "The queue was cleared", mapOf("reason" to reason, "count" to count)) } - waiting.forEach { delivery.failure(it.listener, error.code, error.message) } + waiting.forEach { delivery.failure(it.listener, error) } return result } private suspend fun work() { while (!released) { - val tried = synchronized(flushLock) { flushes.toList().also { flushes.clear() } } - val outcome = if (store.isOwner(this)) store.sending.withLock { pass() } else Outcome.Idle - tried.forEach { it.complete(Unit) } + val outcome = try { + val tried = synchronized(flushLock) { flushes.toList().also { flushes.clear() } } + val outcome = if (store.isOwner(this)) store.sending.withLock { passHoldingLock() } else Outcome.Idle + tried.forEach { it.complete(Unit) } + outcome + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + // Never a tight loop: the next pass comes after a pause. + log(LogLevel.ERROR, LogCodes.SDK_LOG, "Sending the event queue failed", throwable = e) + pause(null) + } when (outcome) { Outcome.Idle -> wake.receive() Outcome.Waiting -> withTimeoutOrNull(TIMER_MS) { wake.receive() } @@ -348,12 +465,33 @@ internal class EventQueue( done.forEach { it.complete(Unit) } } + /** One pass while this process holds the queue's file lock; when another process holds it, that one sends. */ + private suspend fun passHoldingLock(): Outcome { + if (!store.lockForSending()) return Outcome.Waiting + try { + return pass() + } finally { + store.unlockForSending() + } + } + /** Sends events oldest first until the queue is empty or cannot send now. */ private suspend fun pass(): Outcome { urgent = false while (true) { if (released || !store.isOwner(this) || state.current == SdkState.REFUSED) return Outcome.Idle - val event = store.head() ?: return Outcome.Idle + // A clear gives the lock back: it is taken again for what came after it. + if (!store.lockForSending()) return Outcome.Waiting + val generation = store.generation + val unreadable = ArrayList() + val head = store.head(unreadable) + unreadable.forEach { delivery.failure(it.listener, UNREADABLE) } + val event = when (head) { + QueueStore.Head.Empty -> return Outcome.Idle + // The disk failed to read it: later, not again at once. + QueueStore.Head.Unavailable -> return pause(null) + is QueueStore.Head.Event -> head.event + } if (clock.nowMillis() - event.createdAt > TTL_MS) { drop(event, LogReasons.EXPIRED, EXPIRED) continue @@ -361,26 +499,34 @@ internal class EventQueue( if (!connectivity.isConnected) return Outcome.Offline // Ready, and when the session's window ended, the /init of a new one first. if (init.awaitUsable(waitMs) != null || !init.session.isAlive()) return Outcome.Waiting + // Cleared while it waited (the customer was deleted): it does not go under the next one. + val body = store.ifCurrent(generation, event.id) { stamped(event) } ?: continue val sentAt = clock.nowMillis() - val result = sendOne(event) + val result = sendOne(event, body) // The answer is in: what it decides is done even if the queue is released meanwhile. val next = withContext(NonCancellable) { take(event, result, sentAt) } if (next != null) return next } } - private suspend fun sendOne(event: QueuedEvent): HttpResult { - val body = event.body - if (event.path == PATH_PUSH) source()?.let { body.put(FIELD_SOURCE, it) } - if (event.path == PATH_CUSTOM && !body.has(FIELD_TIME)) body.put(FIELD_TIME, event.createdAt / 1_000) - return try { - send(NetworkMethod.POST(event.path), body) - } catch (e: CancellationException) { - throw e - } catch (e: Exception) { - log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "POST ${event.path}: no answer", mapOf("method" to "POST", "path" to event.path), e) - HttpResult.Failure(SdkError.Network) - } + /** What goes for [event]: a copy of its fields with the queue's and the common ones. */ + private fun stamped(event: QueuedEvent): JSONObject = + stamp(withOwnFields(event.path, JSONObject(event.body.toString()), event.createdAt)) + + /** The stored source on `/push` and the time the event was made on `/push/custom`, unless it has its own. */ + private fun withOwnFields(path: String, body: JSONObject, createdAt: Long): JSONObject { + if (path == PATH_PUSH) source()?.let { body.put(FIELD_SOURCE, it) } + if (path == PATH_CUSTOM && !body.has(FIELD_TIME)) body.put(FIELD_TIME, createdAt / 1_000) + return body + } + + private suspend fun sendOne(event: QueuedEvent, body: JSONObject): HttpResult = try { + send(NetworkMethod.POST(event.path), body) + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + log(LogLevel.WARNING, LogCodes.REQUEST_FAILED, "POST ${event.path}: no answer", mapOf("method" to "POST", "path" to event.path), e) + HttpResult.Failure(SdkError.Network) } /** What the answer to [event] does; null to go on with the next event. */ @@ -412,6 +558,7 @@ internal class EventQueue( drop(event, LogReasons.ATTEMPTS, error, attempts) return null } + // A disk that refuses the count keeps it in memory: the pause comes all the same. store.update(event.copy(attempts = attempts)) return pause(result.retryAfterMs.takeIf { code in RETRY_AFTER_CODES }) } @@ -421,15 +568,16 @@ internal class EventQueue( drop(event, LogReasons.SERVICE_REFUSED, error) return null } - return when (init.checkShopAndAwait(waitMs)) { - SdkState.READY -> { - store.update(event.copy(shopChecked = true)) - null - } + val check = init.checkShopAndAwait(waitMs) + // The session its /init started woke the queue: not a trigger that may end the pause below. + urgent = false + return when (check) { + // A new answer found the shop fine: the event goes once more, after a pause if the disk failed. + CoreInit.ShopCheck.Fine -> if (store.update(event.copy(shopChecked = true))) null else pause(null) // Cleared by the state's own news; a released queue sends nothing more. - SdkState.REFUSED, SdkState.NOT_INITIALIZED -> Outcome.Idle - // The check told nothing yet: the event waits, the check goes again later. - else -> pause(null) + CoreInit.ShopCheck.Refused, CoreInit.ShopCheck.Released -> Outcome.Idle + // The check may not go yet: the event waits for it. Or it told nothing: a pause. + is CoreInit.ShopCheck.Unknown -> check.retryInMs?.let { Outcome.Pause(it, serverNamed = true) } ?: pause(null) } } else -> { @@ -445,7 +593,8 @@ internal class EventQueue( /** The event went: it leaves the queue and its listener hears the answer. */ private fun delivered(event: QueuedEvent, body: Any, sentAt: Long): Outcome? { failures = 0 - // Gone meanwhile (cleared, or dropped for room): its listener heard that already. + // Gone meanwhile (cleared, or dropped for room): its listener heard that already. A file the disk + // keeps is skipped from now on: the event is not sent again. val removed = store.remove(event.id) ?: return null val listener = removed.waiting?.listener ?: return null if (listener is EventAnswerListener) { @@ -475,7 +624,7 @@ internal class EventQueue( attempts?.let { put("attempts", it) } }, ) - waiting?.let { delivery.failure(it.listener, error.code, error.message) } + waiting?.let { delivery.failure(it.listener, error) } } private fun log(level: LogLevel, code: String, message: String, context: Map = emptyMap(), throwable: Throwable? = null) = @@ -501,11 +650,13 @@ internal class EventQueue( private const val FIELD_SOURCE = "source" private const val FIELD_TIME = "time" + private const val REASON_INVALID_JSON = "invalid_json" private val SHOP_REFUSALS = setOf(402, 403, 404) private val REPEATED_4XX = setOf(408, 410, 417, 426, 429) private val RETRY_AFTER_CODES = setOf(429, 503) private val EXPIRED = LegacyError(LegacyErrors.NO_ANSWER, "The event expired in the queue") private val OVERFLOWED = LegacyError(LegacyErrors.NO_ANSWER, "The event was dropped: the queue was full") + private val UNREADABLE = LegacyError(LegacyErrors.NO_ANSWER, "The event could not be read from the disk") } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyDelivery.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyDelivery.kt index 5a9f91aa..72248437 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyDelivery.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyDelivery.kt @@ -35,4 +35,6 @@ internal class LegacyDelivery( log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "onError of a request threw", e) } } + + fun failure(listener: OnApiCallbackListener, error: LegacyError) = failure(listener, error.code, error.message) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt index df50d11e..0e17beef 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt @@ -8,16 +8,22 @@ import kotlinx.coroutines.sync.Mutex import org.json.JSONException import org.json.JSONObject import java.io.File +import java.io.FileNotFoundException import java.io.FileOutputStream import java.io.IOException +import java.io.RandomAccessFile +import java.nio.channels.FileChannel +import java.nio.channels.FileLock +import java.nio.channels.OverlappingFileLockException import java.util.Locale +import kotlin.random.Random /** - * One event in a shop's queue: the request's own fields ([body], without the common ones, which go on - * at send time), when it was made, and what its answers so far did to it. + * One event in a shop's queue: the request's own fields ([body], without the common ones, which go on a + * copy at send time and never reach the disk), when it was made, and what its answers so far did to it. */ internal class QueuedEvent( - /** Its file's name without the extension: zero-padded, so the names sort in the queue's order. */ + /** Its file's name without the extension: it sorts in the queue's order. */ val id: String, val path: String, val body: JSONObject, @@ -65,23 +71,51 @@ internal class QueuedEvent( /** * The files of one shop's queue, in its `queue/` directory under `noBackupFilesDir`: one JSON file per - * event, named by a sequence so the names sort in order. One store per directory in the process, shared - * by every [EventQueue] of the shop, so two instances of one shop never send an event twice: + * event, named by the time it was made, a counter, the process and a random part, so the names sort in + * order and two processes never pick the same one. One store per directory in the process, shared by + * every [EventQueue] of the shop, so two instances of one shop never send an event twice: * - the queue made last is the [owner] and the only one that sends; the others write what they accept * and wake it; * - [sending] is held for an event from its send to its answer: a new owner waits for the old one's * event in flight; + * - only the process that holds the file lock of the directory ([lockForSending]) sends: another + * process of the app writes its events and leaves the sending to it; * - an event's old-style listener waits here, so whoever sends it calls it. * * A write goes to a temporary file first and is renamed over the event's file: a crash midway leaves * the old file or the new one, never a part. Disk failures are `storage_failed`; nothing here throws. + * An event whose file could not be rewritten keeps its new state in memory, and one whose file could + * not be removed after it went is skipped from then on. + * + * Three locks, never one inside another the other way round: [files] for the disk (held while a file is + * synced or the directory deleted), then [listenersLock]; [ownersLock] alone. The main thread takes only + * the last two, so it never waits for the disk. */ internal class QueueStore private constructor(private val directory: File, private val shopId: String?) { - private val lock = Any() + private val files = Any() + private val listenersLock = Any() + private val ownersLock = Any() + private val owners = ArrayList() private val listeners = HashMap() - private var sequence = 0L + + /** Events that went but whose file could not be removed: skipped, removed again by a later listing. */ + private val gone = HashSet() + + /** The state of events whose file could not be rewritten: kept for this process. */ + private val patched = HashMap() + + private var lastStamp = 0L + private var counter = 0 + + private var lockChannel: FileChannel? = null + private var processLock: FileLock? = null + + /** Moves on each [clear]: an event read before it is the cleared customer's and does not go. */ + @Volatile + var generation = 0L + private set /** Held from an event's send to its answer. */ val sending = Mutex() @@ -92,52 +126,150 @@ internal class QueueStore private constructor(private val directory: File, priva /** An event [remove] took off the queue, with its listener if it had one. */ class Removed(val waiting: Waiting?) + /** What [head] found. */ + sealed interface Head { + object Empty : Head + + /** The oldest event could not be read now (the disk failed): it stays for a later try. */ + object Unavailable : Head + class Event(val event: QueuedEvent) : Head + } + /** [queue] becomes the owner: it sends from now on. */ - fun claim(queue: EventQueue) = synchronized(lock) { + fun claim(queue: EventQueue) = synchronized(ownersLock) { owners -= queue owners += queue } /** [queue] stops: the queue made before it, if one is live, sends again. Returns the new owner. */ - fun leave(queue: EventQueue): EventQueue? = synchronized(lock) { + fun leave(queue: EventQueue): EventQueue? = synchronized(ownersLock) { owners -= queue owners.lastOrNull() } - val owner: EventQueue? get() = synchronized(lock) { owners.lastOrNull() } + val owner: EventQueue? get() = synchronized(ownersLock) { owners.lastOrNull() } - fun isOwner(queue: EventQueue): Boolean = synchronized(lock) { owners.lastOrNull() === queue } + fun isOwner(queue: EventQueue): Boolean = synchronized(ownersLock) { owners.lastOrNull() === queue } - /** The ids on disk in order; a temporary file left by a crash is removed. */ + /** + * Takes the directory's file lock for this process; true while it holds it. Another process of the + * app that holds it is the one that sends. + */ + fun lockForSending(): Boolean = synchronized(files) { + if (processLock?.isValid == true) return true + if (!directory.isDirectory && !directory.mkdirs() && !directory.isDirectory) return false + var channel: FileChannel? = null + try { + channel = RandomAccessFile(File(directory, LOCK_FILE), "rw").channel + val lock = channel.tryLock() + if (lock == null) { + channel.close() + return false + } + lockChannel = channel + processLock = lock + true + } catch (e: OverlappingFileLockException) { + // Held through another channel of this process, which only a test does. + channel?.close() + false + } catch (e: Exception) { + channel?.close() + failed(OP_LOCK, e) + false + } + } + + /** Gives the file lock back, for another process to send. */ + fun unlockForSending() = synchronized(files) { unlockLocked() } + + /** Must hold [files]. */ + private fun unlockLocked() { + try { + processLock?.release() + } catch (e: Exception) { + // A lock on a file deleted meanwhile, or a channel closed: nothing is held any more. + } + try { + lockChannel?.close() + } catch (e: Exception) { + // As above. + } + processLock = null + lockChannel = null + } + + /** + * Must hold [files]. The ids on disk in order. A temporary file is removed only once it is old: a + * fresh one may be another process's write in progress. An event that went but stayed on disk is + * removed again, and skipped while that fails. + */ private fun ids(): List { val names = directory.list() ?: return emptyList() + val now = System.currentTimeMillis() val ids = ArrayList(names.size) for (name in names) { when { - name.endsWith(EXTENSION) -> ids += name.removeSuffix(EXTENSION) - name.endsWith(TEMPORARY) -> File(directory, name).delete() + name.endsWith(EXTENSION) -> { + val id = name.removeSuffix(EXTENSION) + if (id in gone) { + val file = File(directory, name) + if (file.delete() || !file.exists()) gone -= id + } else { + ids += id + } + } + name.endsWith(TEMPORARY) -> File(directory, name).let { file -> + if (now - file.lastModified() > STALE_TEMPORARY_MS) file.delete() + } } } + gone.retainAll { File(directory, it + EXTENSION).exists() } ids.sort() return ids } - /** How many events wait. */ - fun size(): Int = synchronized(lock) { ids().size } - - /** The oldest event that can be read; a file that cannot is dropped (`queue_dropped`, `unreadable`). */ - fun head(): QueuedEvent? = synchronized(lock) { + /** + * The oldest event. A file that is not an event (it does not parse) is dropped (`queue_dropped`, + * `unreadable`) and its listener goes to [unreadable], to hear an error; a file the disk cannot read + * now stays, and the head is [Head.Unavailable]. + */ + fun head(unreadable: MutableList): Head = synchronized(files) { for (id in ids()) { - read(id)?.let { return it } - File(directory, id + EXTENSION).delete() - listeners.remove(id) + val file = File(directory, id + EXTENSION) + val text = try { + file.readText(Charsets.UTF_8) + } catch (e: FileNotFoundException) { + // Gone meanwhile (another process sent it), or the disk refused it. + if (!file.exists()) continue + failed(OP_READ, e) + return Head.Unavailable + } catch (e: IOException) { + failed(OP_READ, e) + return Head.Unavailable + } + val event = QueuedEvent.parse(id, text) + if (event != null) return Head.Event(patched[id] ?: event) + file.delete() + patched -= id + synchronized(listenersLock) { listeners.remove(id) }?.let { unreadable += it } SdkLogs.log(LogLevel.WARNING, LogCodes.QUEUE_DROPPED, "An event that could not be read was dropped", shopId, mapOf("reason" to REASON_UNREADABLE)) } - null + Head.Empty } - fun contains(id: String): Boolean = synchronized(lock) { File(directory, id + EXTENSION).isFile } + /** + * Runs [block] while the event [id] is still there and no [clear] came since [generation], and this + * process holds the file lock; null otherwise. The queue stamps an event's fields here, so a deletion + * of the customer either comes after them — the event goes as the deleted customer's — or before, + * and the event does not go. + */ + fun ifCurrent(generation: Long, id: String, block: () -> T): T? = synchronized(files) { + if (generation != this.generation || id in gone || processLock?.isValid != true) return null + if (!File(directory, id + EXTENSION).isFile) return null + block() + } /** * Writes a new event from [path], [body] and [createdAt] and keeps [listener] for it. Before, the @@ -152,60 +284,88 @@ internal class QueueStore private constructor(private val directory: File, priva listener: OnApiCallbackListener?, cap: Int, dropped: MutableList>, - ): QueuedEvent? = synchronized(lock) { + ): QueuedEvent? = synchronized(files) { val ids = ids() val over = ids.size - cap + 1 if (over > 0) { - ids.take(over).forEach { id -> dropped += read(id) to listeners.remove(id); File(directory, id + EXTENSION).delete() } + ids.take(over).forEach { id -> + val old = read(id) + File(directory, id + EXTENSION).delete() + patched -= id + dropped += old to synchronized(listenersLock) { listeners.remove(id) } + } } - val last = ids.lastOrNull()?.toLongOrNull() ?: 0L - sequence = maxOf(sequence, last) + 1 - val event = QueuedEvent(String.format(Locale.ROOT, ID_FORMAT, sequence), path, body, createdAt) + val event = QueuedEvent(newId(createdAt), path, body, createdAt) if (!write(event)) return null - if (listener != null) listeners[event.id] = Waiting(queue, listener) + if (listener != null) synchronized(listenersLock) { listeners[event.id] = Waiting(queue, listener) } event } - /** Rewrites [event] (its attempts, its check) if it is still there; false when it is gone or the disk refused. */ - fun update(event: QueuedEvent): Boolean = synchronized(lock) { - if (!File(directory, event.id + EXTENSION).isFile) return false - write(event) + /** Must hold [files]. Sorts by time and, within one millisecond of this process, by the order of the calls. */ + private fun newId(createdAt: Long): String { + val stamp = maxOf(createdAt, lastStamp) + counter = if (stamp == lastStamp) counter + 1 else 0 + lastStamp = stamp + return String.format(Locale.ROOT, ID_FORMAT, stamp, counter, PROCESS, Random.nextInt() and 0x7fffffff) } - /** Removes [id] with its listener; null when it was gone already (cleared, or dropped meanwhile). */ - fun remove(id: String): Removed? = synchronized(lock) { - val file = File(directory, id + EXTENSION) - if (!file.isFile) return null - if (!file.delete() && file.exists()) failed(OP_DELETE, null) - Removed(listeners.remove(id)) + /** + * Rewrites [event] (its attempts, its check) if it is still there. False when it is gone or the disk + * refused: the new state is kept for this process all the same. + */ + fun update(event: QueuedEvent): Boolean = synchronized(files) { + if (event.id in gone || !File(directory, event.id + EXTENSION).isFile) return false + patched[event.id] = event + if (!write(event)) return false + patched -= event.id + true } - /** The listener [id] has, without taking it. */ - fun listenerOf(id: String): Waiting? = synchronized(lock) { listeners[id] } + /** + * Removes [id] with its listener; null when it was gone already (cleared, or dropped meanwhile). A + * file the disk keeps is skipped from now on. + */ + fun remove(id: String): Removed? = synchronized(files) { + val file = File(directory, id + EXTENSION) + if (id in gone || !file.isFile) return null + if (!file.delete() && file.exists()) { + failed(OP_DELETE, null) + gone += id + } + patched -= id + Removed(synchronized(listenersLock) { listeners.remove(id) }) + } /** * Removes every event and hands back their listeners, then runs [then] while no event can be - * accepted (the customer's data goes with it, `deleteUserCredentials()`). Returns how many went and - * [then]'s result. + * accepted (the customer's data goes with it, `deleteUserCredentials()`). Moves the [generation] first, + * so an event read before does not go, and gives back the file lock, which [then] may delete. Returns + * how many went and [then]'s result. */ - fun clear(then: () -> T): Triple, T> = synchronized(lock) { + fun clear(then: () -> T): Triple, T> = synchronized(files) { + generation++ + unlockLocked() val ids = ids() ids.forEach { id -> val file = File(directory, id + EXTENSION) - if (!file.delete() && file.exists()) failed(OP_DELETE, null) + if (!file.delete() && file.exists()) { + failed(OP_DELETE, null) + gone += id + } } - val waiting = listeners.values.toList() - listeners.clear() + patched.clear() + val waiting = synchronized(listenersLock) { listeners.values.toList().also { listeners.clear() } } Triple(ids.size, waiting, then()) } /** Takes the listeners [queue] accepted: it stops, their events stay on disk for a later launch. */ - fun takeListeners(queue: EventQueue): List = synchronized(lock) { + fun takeListeners(queue: EventQueue): List = synchronized(listenersLock) { val mine = listeners.filterValues { it.queue === queue } mine.keys.forEach(listeners::remove) mine.values.toList() } + /** Must hold [files]. */ private fun read(id: String): QueuedEvent? = try { QueuedEvent.parse(id, File(directory, id + EXTENSION).readText(Charsets.UTF_8)) } catch (e: IOException) { @@ -213,7 +373,7 @@ internal class QueueStore private constructor(private val directory: File, priva null } - /** A temporary file synced to the disk, then renamed over the event's file. */ + /** Must hold [files]. A temporary file synced to the disk, then renamed over the event's file. */ private fun write(event: QueuedEvent): Boolean { if (!directory.isDirectory && !directory.mkdirs() && !directory.isDirectory) { failed(OP_WRITE, null) @@ -221,8 +381,10 @@ internal class QueueStore private constructor(private val directory: File, priva } val temporary = File(directory, event.id + TEMPORARY) return try { + // JSONObject.toString() gives null for a value JSON cannot hold: not written. + val text: String = event.toJson().toString() ?: throw IOException("The event is not JSON") FileOutputStream(temporary).use { stream -> - stream.write(event.toJson().toString().toByteArray(Charsets.UTF_8)) + stream.write(text.toByteArray(Charsets.UTF_8)) stream.fd.sync() } if (temporary.renameTo(File(directory, event.id + EXTENSION))) { @@ -232,7 +394,7 @@ internal class QueueStore private constructor(private val directory: File, priva failed(OP_WRITE, null) false } - } catch (e: IOException) { + } catch (e: Exception) { temporary.delete() failed(OP_WRITE, e) false @@ -246,13 +408,23 @@ internal class QueueStore private constructor(private val directory: File, priva internal companion object { private const val EXTENSION = ".json" private const val TEMPORARY = ".tmp" - private const val ID_FORMAT = "%019d" + const val LOCK_FILE = ".lock" + + /** Time, a counter, the process and a random part: sortable, and unique across processes. */ + private const val ID_FORMAT = "%013d-%06d-%d-%08x" + + /** A temporary file older than this is a crash's leftover, not a write in progress. */ + const val STALE_TEMPORARY_MS = 5 * 60 * 1000L + const val OP_READ = "read_queued_event" const val OP_WRITE = "write_queued_event" const val OP_DELETE = "delete_queued_event" + const val OP_LOCK = "lock_queue" private const val REASON_UNREADABLE = "unreadable" + private val PROCESS: Int by lazy { android.os.Process.myPid() } + private val stores = HashMap() /** The process's store of [directory]. */ @@ -261,6 +433,12 @@ internal class QueueStore private constructor(private val directory: File, priva } /** Forgets every store, as a new process would; for tests. */ - internal fun reset() = synchronized(stores) { stores.clear() } + internal fun reset() = synchronized(stores) { + stores.values.forEach { it.unlockForSending() } + stores.clear() + } + + /** A store of [directory] as another process has it; for tests. */ + internal fun otherProcess(directory: File, shopId: String?): QueueStore = QueueStore(directory, shopId) } } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt index af1efc8f..7c8aa144 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt @@ -79,6 +79,9 @@ class EventQueueTest { private class Sent(val path: String, val body: JSONObject, val sid: String) private var source: JSONObject? = null + + /** How many times the source throws before it answers: an unexpected failure while sending. */ + private var sourceFailures = 0 private val inits = mutableListOf() private val queues = mutableListOf() private val direct = mutableListOf() @@ -103,6 +106,7 @@ class EventQueueTest { holdEvents = false initAnswer = { ok() } source = null + sourceFailures = 0 SdkLogs.reset() SdkLogs.handlerExecutor = Executor { it.run() } SdkLogs.register(SHOP, LogLevel.DEBUG, "SDK") @@ -157,7 +161,14 @@ class EventQueueTest { }, connectivity = connectivity, lifecycle = lifecycle, - source = { source }, + source = { + if (sourceFailures > 0) { + sourceFailures-- + throw IllegalStateException("The source failed") + } + source + }, + stamp = { body -> CommonFields.addTo(body, settings) }, sendDirectly = { path, _, _ -> direct += path }, dispatchers = dispatchers, clock = clock, @@ -183,7 +194,8 @@ class EventQueueTest { private fun ids(): List = sent.map { it.body.optJSONArray("items")?.optJSONObject(0)?.optString("id").orEmpty() } - private fun files(): List = storage.queuePath.list()?.sorted().orEmpty() + /** The events' files and leftovers, not the queue's lock. */ + private fun files(): List = storage.queuePath.list()?.filter { it != QueueStore.LOCK_FILE }?.sorted().orEmpty() private fun codes(code: String) = logged.filter { it.code == code } @@ -257,17 +269,19 @@ class EventQueueTest { inits.forEach { it.release() } queues.clear() inits.clear() - // A new process: no store in memory, a temporary file a crash left, an unreadable event. + // A new process: no store in memory, a temporary file a crash left, an unreadable event, and a + // temporary file another process is writing right now. QueueStore.reset() - File(storage.queuePath, "0000000000000000099.tmp").writeText("{") - File(storage.queuePath, "0000000000000000000.json").writeText("not json") + File(storage.queuePath, "0000000000000000099.tmp").apply { writeText("{") }.setLastModified(System.currentTimeMillis() - QueueStore.STALE_TEMPORARY_MS - 1_000) + File(storage.queuePath, "0000000000000-000000-1-00000000.json").writeText("not json") + File(storage.queuePath, "9999999999999-000000-2-00000000.tmp").writeText("{") state = InstanceState(shopId = { SHOP }, dispatchers = dispatchers) connectivity.set(true) start() assertEquals(listOf("1", "2"), ids()) - assertTrue(files().isEmpty()) + assertEquals("another process's write stays", listOf("9999999999999-000000-2-00000000.tmp"), files()) assertEquals("unreadable", codes(LogCodes.QUEUE_DROPPED).single().context["reason"]) } @@ -452,19 +466,54 @@ class EventQueueTest { } @Test - fun `the check of the shop goes at most every ten minutes`() { + fun `the check of the shop goes at most every ten minutes, and an event waits for the next one`() { val queue = start() eventAnswers += http(404) eventAnswers += http(404) eventAnswers += http(404) + val heard = Heard() event(queue, "1") - event(queue, "2") + event(queue, "2", listener = heard) run() - // The first event: a 404, the check, a 404 again and it is dropped. The second: a 404 finds the - // shop checked less than ten minutes ago and fine, and it goes once more without another /init. + // The first event: a 404, the check, a 404 again and it is dropped. The second: a 404 while the + // shop may not be checked again yet: it is not marked checked, and it waits for the next check. assertEquals(2, initsSent) + assertEquals(listOf("1", "1", "2"), ids()) + lifecycle.set(false) + lifecycle.set(true) + after(CoreInit.CHECK_SHOP_INTERVAL_MS - 1_000) + assertEquals("no trigger ends the wait", listOf("1", "1", "2"), ids()) + + clock.advance(CoreInit.CHECK_SHOP_INTERVAL_MS) + after(1_000) assertEquals(listOf("1", "1", "2", "2"), ids()) + assertTrue(heard.calls.single().startsWith("success")) + } + + @Test + fun `a check of the shop whose answer does not come in time does not count as the shop found fine`() { + val queue = start() + holdInit = true + eventAnswers += http(402) + eventAnswers += http(402) + val heard = Heard() + event(queue, "1", listener = heard) + run() + + // The 402 asks the shop; /init does not answer within the queue's wait: a pause, and the event goes + // again unchecked — so its next 402 waits for the check again instead of dropping it. + after(10_000) + after(1_000) + assertEquals(listOf("1", "1"), ids()) + assertTrue(heard.calls.isEmpty()) + + // The check's answer comes: the shop is fine, the event goes once more. + initCalls.single().complete(ok()) + run() + assertEquals(listOf("1", "1", "1"), ids()) + assertTrue(heard.calls.single().startsWith("success")) + assertTrue(codes(LogCodes.QUEUE_DROPPED).isEmpty()) } @Test @@ -756,6 +805,7 @@ class EventQueueTest { val queue = start() storage.directory.mkdirs() // The queue's directory is a file: nothing can be written there. + storage.queuePath.deleteRecursively() storage.queuePath.writeText("in the way") event(queue, "1") run() @@ -825,6 +875,235 @@ class EventQueueTest { assertEquals(2, initsSent) } + // After the review of stages 4 and 5. + + @Test + fun `an event read before the customer was deleted does not go under the next customer's did`() { + val queue = start() + val init = inits.single() + // A deletion holds the instance; the event read meanwhile waits for it. + init.holdForRestart() + event(queue, "old") + run() + val cleared = CoroutineScope(StandardTestDispatcher(scheduler)).async { + queue.clear(LogReasons.DELETE_USER_CREDENTIALS, LegacyError(-1, "deleted")) { storage.deleteUserCredentials(includeQueue = true) } + } + run() + assertTrue(cleared.getCompleted()) + initAnswer = { ok(did = "D2", sid = "S2") } + init.restart() + run() + + assertTrue("the deleted customer's event does not go", sent.isEmpty()) + event(queue, "new") + run() + assertEquals(listOf("new"), ids()) + assertEquals("D2", sent.single().body.getString("did")) + } + + @Test + fun `a disk that keeps the shop's check or a sent event pauses or skips instead of sending again at once`() { + val queue = start() + eventAnswers += http(402) + connectivity.set(false) + event(queue, "1") + run() + assertTrue(storage.queuePath.setWritable(false)) + try { + connectivity.set(true) + run() + // The shop is fine, but its check could not be written: a pause, not a send at once. + assertEquals(1, sent.size) + assertTrue(codes(LogCodes.STORAGE_FAILED).isNotEmpty()) + after(1_000) + assertEquals(2, sent.size) + // It went, but its file stays: it is not sent again. + lifecycle.set(false) + lifecycle.set(true) + after(EventQueue.TIMER_MS) + assertEquals(2, sent.size) + assertEquals(1, files().size) + } finally { + storage.queuePath.setWritable(true) + } + // Once the disk takes it, the file goes. + lifecycle.set(false) + run() + assertTrue(files().isEmpty()) + assertEquals(2, sent.size) + } + + @Test + fun `fields JSON cannot hold are refused at once, and nothing stops the writer or the sender`() { + val queue = start() + val refused = Heard() + queue.accept("push/custom", JSONObject(mapOf("event" to "e", "value" to Double.NaN)), refused) + run() + assertEquals(listOf("error:-1"), refused.calls) + assertTrue(files().isEmpty()) + assertEquals("invalid_json", codes(LogCodes.INVALID_ARGUMENT).single().context["reason"]) + + // A clear whose deletion throws ends, and the writer takes the next event. + val cleared = CoroutineScope(StandardTestDispatcher(scheduler)).async { + queue.clear(LogReasons.DELETE_USER_CREDENTIALS, LegacyError(-1, "deleted")) { throw IllegalStateException("disk") } + } + run() + assertFalse(cleared.getCompleted()) + + // A send that throws: a pause, then it goes. + sourceFailures = 1 + event(queue, "1") + run() + assertTrue(sent.isEmpty()) + after(1_000) + assertEquals(listOf("1"), ids()) + assertNull(flush(queue).getCompleted()) + } + + @Test + fun `only the process that holds the queue's lock sends, and two processes never name two events alike`() { + val queue = start() + storage.queuePath.mkdirs() + val channel = java.io.RandomAccessFile(File(storage.queuePath, QueueStore.LOCK_FILE), "rw").channel + val other = channel.lock() + try { + event(queue, "1") + run() + assertTrue("another process sends", sent.isEmpty()) + assertEquals(1, files().size) + } finally { + other.release() + channel.close() + } + lifecycle.set(false) + run() + assertEquals(listOf("1"), ids()) + + // The same moment in two processes: names apart all the same. + connectivity.set(false) + val mine = QueueStore.of(storage.queuePath, SHOP) + val theirs = QueueStore.otherProcess(storage.queuePath, SHOP) + repeat(20) { + listOf(mine, theirs).forEach { store -> store.add(queue, "push", JSONObject(), clock.now, null, EventQueue.CAP, mutableListOf()) } + } + assertEquals(40, files().size) + } + + @Test + fun `a flush asked before release and handled after it ends with notInitialized`() { + val queue = start() + val flushed = CoroutineScope(kotlinx.coroutines.Dispatchers.Unconfined).async { + try { + queue.flush() + null + } catch (e: SdkException) { + e + } + } + queue.release() + run() + + assertEquals(SdkError.NotInitialized, (flushed.getCompleted() as SdkException).error) + } + + @Test + fun `an event accepted by the queue made first wakes the one that sends`() { + val first = start() + start() + event(first, "1") + run() + + assertEquals(listOf("1"), ids()) + } + + @Test + fun `nothing stamped reaches the disk, and a repeat after the source expired goes without it`() { + val queue = start() + source = JSONObject().put("from", "chain").put("code", "c1") + eventAnswers += http(500) + event(queue, "1") + run() + val stored = JSONObject(File(storage.queuePath, files().single()).readText()).getJSONObject("body") + listOf("source", "did", "sid", "seance", "shop_id", "segment", "stream").forEach { assertFalse(it, stored.has(it)) } + + source = null + after(1_000) + + assertEquals(listOf(true, false), sent.map { it.body.has("source") }) + assertTrue(sent.all { it.body.getString("did") == "D1" && it.body.getString("sid") == "S1" }) + } + + @Test + fun `an event the disk cannot read now stays for later, and one that is not an event tells its listener`() { + val queue = start() + connectivity.set(false) + val later = Heard() + event(queue, "1", listener = later) + run() + val file = File(storage.queuePath, files().single()) + assertTrue(file.setReadable(false)) + try { + connectivity.set(true) + run() + assertTrue(sent.isEmpty()) + assertTrue(file.exists()) + assertTrue(later.calls.isEmpty()) + assertTrue(codes(LogCodes.QUEUE_DROPPED).isEmpty()) + } finally { + file.setReadable(true) + } + after(1_000) + assertEquals(listOf("1"), ids()) + + connectivity.set(false) + val broken = Heard() + event(queue, "2", listener = broken) + run() + File(storage.queuePath, files().single()).writeText("not json") + connectivity.set(true) + run() + assertEquals(listOf("error:-1"), broken.calls) + assertEquals("unreadable", codes(LogCodes.QUEUE_DROPPED).single().context["reason"]) + } + + @Test + fun `an event the disk does not take goes with its source or its time, and accepted hears after the write`() { + val queue = start() + source = JSONObject().put("from", "chain").put("code", "c1") + val accepted = mutableListOf() + val bodies = mutableListOf() + queue.accept("push", JSONObject().put("event", "view"), null) { accepted += it } + assertTrue("not before the write", accepted.isEmpty()) + run() + assertEquals(listOf(null), accepted) + + storage.queuePath.deleteRecursively() + storage.queuePath.writeText("in the way") + try { + val direct = EventQueue( + shopId = SHOP, + store = QueueStore.otherProcess(storage.queuePath, SHOP), + init = inits.single(), + state = state, + send = { _, _ -> HttpResult.Success(JSONObject()) }, + connectivity = connectivity, + lifecycle = lifecycle, + source = { source }, + sendDirectly = { _, body, _ -> bodies += body }, + dispatchers = dispatchers, + clock = clock, + ).also { queues += it; it.start() } + direct.accept("push", JSONObject().put("event", "view"), null) + direct.accept("push/custom", JSONObject().put("event", "e"), null) + run() + } finally { + storage.queuePath.delete() + } + assertEquals("c1", bodies[0].getJSONObject("source").getString("code")) + assertEquals(clock.now / 1_000, bodies[1].getLong("time")) + assertFalse(bodies[1].has("source")) + } + private companion object { const val SHOP = "queue-shop" } From 546609bc161138977480eee9d01f446da6e09d3b Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:13:58 +0400 Subject: [PATCH 047/109] =?UTF-8?q?fix(core):=20the=20session=20and=20/ini?= =?UTF-8?q?t=20after=20review.=20An=20/init=20answer=20without=20a=20seanc?= =?UTF-8?q?e=20starts=20a=20session=20without=20a=20sid,=20alive=20for=20i?= =?UTF-8?q?ts=20window,=20so=20requests=20no=20longer=20send=20/init=20aft?= =?UTF-8?q?er=20/init=20for=20it.=20A=20later=20/init=20of=20a=20start=20w?= =?UTF-8?q?hose=20own=20/init=20succeeded=20=E2=80=94=20a=20new=20session'?= =?UTF-8?q?s,=20the=20shop's=20check=20=E2=80=94=20that=20fails=20leaves?= =?UTF-8?q?=20the=20state=20as=20it=20was,=20acts=20on=20the=20saved=20ans?= =?UTF-8?q?wer=20no=20more=20(no=20onAnswer=20with=20the=20saved=20answer,?= =?UTF-8?q?=20no=20refresh=20of=20popups=20v3),=20and=20is=20not=20waited?= =?UTF-8?q?=20for=20by=20requests=20while=20it=20is=20repeated;=20the=20wa?= =?UTF-8?q?rning=20that=20the=20SDK=20works=20on=20its=20did=20is=20logged?= =?UTF-8?q?=20again=20after=20a=20success.=20On=20the=20new=20core=20sessi?= =?UTF-8?q?onId=20is=20null=20once=20the=20session's=20window=20ended,=20n?= =?UTF-8?q?ot=20the=20sid=20of=20the=20session=20that=20ended.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/kotlin/com/personalization/SDK.kt | 12 ++- .../personalization/core/internal/CoreInit.kt | 28 +++++-- .../core/internal/SessionWindow.kt | 25 +++++- .../core/internal/CoreSessionTest.kt | 76 ++++++++++++++++++- 4 files changed, 129 insertions(+), 12 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 41b2063f..1eea39e2 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -262,11 +262,17 @@ open class SDK { /** * The `sid` of the current session; null while there is none, or before initialization. On the new - * core it is the server's (the `seance` of `/init`), and a session two hours without events ends: - * the next request or event starts a new one with `/init` first. + * core it is the server's (the `seance` of `/init`), and a session two hours without events ends — + * from then on this is null — and the next request or event starts a new one with `/init` first. */ val sessionId: String? - get() = if (::userSettingsRepository.isInitialized) userSettingsRepository.getSid().ifEmpty { null } else null + get() { + if (!::userSettingsRepository.isInitialized) return null + // On the new core a sid whose window ended is no session any more. + val session = (core as? CoreV2)?.init?.session + if (session != null && !session.isAlive()) return null + return userSettingsRepository.getSid().ifEmpty { null } + } /** The bridges' listeners of a new `sid` ([onSessionChange]). */ private val sessionListeners = CopyOnWriteArrayList<(String) -> Unit>() diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt index ac0ef3da..f8b24f96 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt @@ -136,6 +136,15 @@ internal class CoreInit( private var usable = false private var fallbackLogged = false + /** + * An `/init` of this start succeeded. A later one — a new session's, the shop's check — that fails + * changes nothing: the state stays, and the saved answer is not acted on again. + */ + private var startAnswered = false + + /** A later `/init` of this start failed and is being repeated: requests go on what the instance has meanwhile. */ + private var laterFailing = false + /** How many `/init` succeeded: [checkShopAndAwait] tells a success from another answer. */ private var successes = 0L private var lastCheckAt: Long? = null @@ -250,7 +259,8 @@ internal class CoreInit( session.forgetIfEnded() launchLocked() // Before the storage is prepared the start's own /init is about to go: that is the one to wait for. - state.current == SdkState.READY && (job?.isActive == true || !prepared) + // A new session's /init that failed and is repeated is not waited for: requests go on what there is. + state.current == SdkState.READY && !laterFailing && (job?.isActive == true || !prepared) } /** What [checkShopAndAwait] learnt of the shop. */ @@ -339,6 +349,8 @@ internal class CoreInit( job = null usable = false fallbackLogged = false + startAnswered = false + laterFailing = false publish(SdkState.INITIALIZING) } @@ -489,12 +501,16 @@ internal class CoreInit( if (settings.getDid().isEmpty()) settings.updateDid(did) // The server's sid replaces whatever went: a sid an earlier release made goes out only once. val seance = answer.optString(UserBasicParams.SEANCE) - if (seance.isNotEmpty()) session.start(seance) else session.extend() + if (seance.isNotEmpty()) session.start(seance) else session.startWithoutSid() storage.saveInitAnswer(answer) // The answer on disk is the new customer's now. if (storage.isWiped) storage.isWiped = false if (storage.isRefused) storage.isRefused = false + startAnswered = true + laterFailing = false successes++ + // A later failure of this start logs init_saved_answer again. + fallbackLogged = false publish(SdkState.READY) log(LogLevel.INFO, LogCodes.INIT_SUCCEEDED, "/init succeeded") if (advertisingIdValue != null) log(LogLevel.DEBUG, LogCodes.ADVERTISING_ID_SENT, "Advertising id sent with /init") @@ -514,8 +530,10 @@ internal class CoreInit( /** Must hold [lock]. */ private fun retry(attempt: Int, cause: Map, retryAfterMs: Long?): Step.Retry { - // A shop refused at an earlier launch stays refused until the server says otherwise. - if (state.current != SdkState.REFUSED) publish(SdkState.RETRYING) + // A shop refused at an earlier launch stays refused until the server says otherwise. A failed + // later /init leaves the state as this start's answer set it. + if (state.current != SdkState.REFUSED && !startAnswered) publish(SdkState.RETRYING) + if (startAnswered) laterFailing = true val hasDid = settings.getDid().isNotEmpty() val waitForNetwork = !hasDid && !connectivity.isConnected val delayMs = backoff.delayMs(attempt, retryAfterMs) @@ -526,7 +544,7 @@ internal class CoreInit( cause + mapOf("attempt" to attempt + 1) + (if (waitForNetwork) mapOf("reason" to LogReasons.OFFLINE) else mapOf("delay_ms" to delayMs)), ) - val fallback = state.current == SdkState.RETRYING && hasDid && !fallbackLogged + val fallback = !startAnswered && state.current == SdkState.RETRYING && hasDid && !fallbackLogged if (fallback) fallbackLogged = true return Step.Retry(delayMs, waitForNetwork, fallback) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/SessionWindow.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/SessionWindow.kt index ac0e2d6a..f7569b1f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/SessionWindow.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/SessionWindow.kt @@ -14,7 +14,9 @@ import java.util.concurrent.CopyOnWriteArrayList * * - The core never makes a `sid` of its own. A `sid` an earlier release made goes out once, with the * `/init` of a window still alive, and the server's answer replaces it. - * - The window is alive while there is a `sid` and the last activity is at most [windowMs] ago. + * - The window is alive while there is a `sid` and the last activity is at most [windowMs] ago. An `/init` + * whose answer has no `seance` ([startWithoutSid]) starts a session without one: alive for its window + * all the same, so requests do not send `/init` after `/init` for it. * - Only `/init` ([start]) and the events of the queue ([extend]) move the last activity; requests with * a result do not, as the server does not extend its own session on them either. * @@ -34,18 +36,22 @@ internal class SessionWindow( /** The last `sid` listeners heard of; a `sid` forgotten when its window ended still counts. */ private var lastKnown: String = settings.getSid() + /** A session `/init` started without a `sid`: alive for its window though it has none. In memory only. */ + private var withoutSid = false + /** Whether the session is alive at [now]: a `sid`, and its last activity at most [windowMs] before. */ fun isAlive(now: Long = clock.nowMillis()): Boolean = synchronized(lock) { alive(now) } private fun alive(now: Long): Boolean = - settings.getSid().isNotEmpty() && now - settings.getSidLastActTime() <= windowMs + (settings.getSid().isNotEmpty() || withoutSid) && now - settings.getSidLastActTime() <= windowMs /** * Ends a session whose window has passed: its `sid` and last activity go, so `/init` goes without * them and the server starts a new session. True when one ended. */ fun forgetIfEnded(now: Long = clock.nowMillis()): Boolean = synchronized(lock) { - if (settings.getSid().isEmpty() || alive(now)) return false + if ((settings.getSid().isEmpty() && !withoutSid) || alive(now)) return false + withoutSid = false storage.forgetSession() true } @@ -61,9 +67,22 @@ internal class SessionWindow( } } + /** + * `/init` answered without a `seance`: a session alive from now, keeping the `sid` it has if its window + * is alive, and without one otherwise. Listeners hear nothing: no `sid` came. + */ + fun startWithoutSid() { + synchronized(lock) { + val now = clock.nowMillis() + if (settings.getSid().isEmpty() || !alive(now)) withoutSid = true + storage.preferences.edit().putLong(StorageKeys.SID_LAST_ACT, now).apply() + } + } + /** `/init` answered with [sid]: the session is [sid] and its last activity now. */ fun start(sid: String) { val changed = synchronized(lock) { + withoutSid = false storage.preferences.edit() .putString(StorageKeys.SID, sid) .putLong(StorageKeys.SID_LAST_ACT, clock.nowMillis()) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt index 8f904bd2..e170b5a8 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt @@ -229,11 +229,85 @@ class CoreSessionTest { assertNull(waiting.getCompleted()) assertEquals("", settings.getSid()) - assertEquals(SdkState.RETRYING, state.current) + // A new session's /init that fails leaves the state as the start's answer set it. + assertEquals(SdkState.READY, state.current) // While init is repeated, a request does not wait for it. assertNull(request(init).getCompleted()) } + @Test + fun `an init answer without a seance starts a session without a sid, and requests do not send init after init`() { + val init = started() + answer(HttpResult.Success(JSONObject().put("did", "D1"))) + + assertTrue(init.session.isAlive()) + assertEquals("", settings.getSid()) + repeat(3) { assertNull(request(init).getCompleted()) } + assertEquals("one init, no storm", 1, calls.size) + + // Its window ends like any session's: the next request asks for a new one. + clock.advance(SessionWindow.WINDOW_MS + 1) + assertFalse(init.session.isAlive()) + request(init) + assertEquals(2, calls.size) + } + + @Test + fun `a failed init of a new session keeps the state and does not act on the saved answer again`() { + val answers = mutableListOf() + val init = CoreInit( + setup = InitSetup(SHOP), + send = { CompletableDeferred().also { calls += it }.await() }, + settings = settings, + storage = storage, + state = state, + connectivity = connectivity, + lifecycle = lifecycle, + hooks = object : InitHooks { + override fun onAnswer(answer: JSONObject, fresh: Boolean) { + answers += fresh + } + + override fun onUsable() = Unit + }, + dispatchers = dispatchers, + clock = clock, + backoff = Backoff(random = { 0.5 }), + session = SessionWindow(settings, storage, clock), + ).also { + inits += it + it.start() + scheduler.runCurrent() + } + answer(ok("S1")) + clock.advance(SessionWindow.WINDOW_MS + 1) + + request(init) + answer(HttpResult.Failure(SdkError.Http(503))) + scheduler.advanceTimeBy(1_000) + scheduler.runCurrent() + answer(HttpResult.Failure(SdkError.Network)) + + assertEquals(SdkState.READY, state.current) + assertEquals("only the start's answer, and it was fresh", listOf(true), answers) + } + + @Test + fun `a relaunch more than two hours after the last event is ready at once, and offline requests fail at once`() { + knownDevice("OLD", lastActivity = clock.now - SessionWindow.WINDOW_MS - 1) + connectivity.set(false) + val init = started() + + assertEquals(SdkState.READY, state.current) + // A request waits for the launch's /init, which finds no network at once: the request goes on at + // once too (and fails at once in the client), without waiting for a repeat. + val waiting = request(init) + assertFalse(waiting.isCompleted) + answer(HttpResult.Failure(SdkError.Network)) + assertNull(waiting.getCompleted()) + assertNull(request(init).getCompleted()) + } + @Test fun `a wait for the new session runs out with the request's timeout`() { val init = started() From b1026abb9daa7ade64b1cf44ceb6dfe28a73f40a Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:14:09 +0400 Subject: [PATCH 048/109] refactor(core): what nothing used goes: the ReadyGate interface, CoreInit.awaitReady and CoreInit.ready (requests wait in awaitUsable), and ShopStorage.queueDirectory (the queue's store makes its directory with its first event); the queue store's size, contains and listenerOf went with its rewrite. The KDoc of SDK.flush() says that an event waiting out the pause after a failure goes again at once. --- .../src/main/kotlin/com/personalization/SDK.kt | 5 +++-- .../personalization/core/internal/CoreInit.kt | 16 +--------------- .../personalization/core/internal/ShopStorage.kt | 15 +-------------- .../kotlin/com/personalization/SdkStorageTest.kt | 2 +- .../core/internal/CoreInitTest.kt | 2 +- .../core/internal/ShopStorageTest.kt | 13 +++++-------- 6 files changed, 12 insertions(+), 41 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 1eea39e2..d7ebf7d1 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -994,8 +994,9 @@ open class SDK { /** * Sends the events waiting in the queue now, without waiting for the queue's own triggers, and * returns once each of them was tried: sent, dropped, or held — by no network, by the instance not - * being ready yet, or by a server that asked to wait (`Retry-After`). It does not repeat a failed - * event at once and promises no delivery. On the legacy core, which has no queue, it returns at once. + * being ready yet, or by a server that asked to wait (`Retry-After`). An event waiting out the pause + * after a failure goes again at once; it promises no delivery. On the legacy core, which has no queue, + * it returns at once. * * @throws SdkException with [SdkError.NotInitialized] when the instance was never initialized or * was released, with [SdkError.ShopBlocked] when the shop refused the SDK. diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt index f8b24f96..77d41cc7 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt @@ -52,12 +52,6 @@ internal interface InitHooks { fun onUsable() } -/** Holds a request until the instance may send it. */ -internal fun interface ReadyGate { - /** Null once the request may go; otherwise why it may not: `shopBlocked`, `notInitialized` or `timeout`. */ - suspend fun awaitReady(timeoutMs: Long): SdkError? -} - /** * The new core's `/init` (`core/init.md`, decision 0002) and the [SdkState] it gives the instance. * @@ -102,7 +96,7 @@ internal class CoreInit( private val timeZone: () -> TimeZone = { TimeZone.getDefault() }, /** The shop's session; the instance's other parts read it too. */ internal val session: SessionWindow = SessionWindow(settings, storage, clock), -) : ReadyGate { +) { /** Whether a request may go: computed from the state on every change. */ private enum class Gate { WAIT, OPEN, BLOCKED, CLOSED } @@ -212,14 +206,6 @@ internal class CoreInit( if (becameReady) markUsable(start) } - override suspend fun awaitReady(timeoutMs: Long): SdkError? { - val decided = withTimeoutOrNull(timeoutMs) { gate.first { it != Gate.WAIT } } ?: return SdkError.Timeout - return errorOf(decided) - } - - /** Waits without a limit until a request may go or may not; the caller sets the deadline. */ - internal suspend fun ready(): SdkError? = errorOf(gate.first { it != Gate.WAIT }) - /** For a request with a result: [usable] within [timeoutMs]. */ suspend fun awaitUsable(timeoutMs: Long): SdkError? { val verdict = withTimeoutOrNull(timeoutMs) { Verdict(usable()) } ?: return SdkError.Timeout diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt index ee55cb2f..f4689634 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ShopStorage.kt @@ -110,21 +110,9 @@ internal class ShopStorage(context: Context, val layout: StorageLayout) { preferences.edit().putBoolean(StorageKeys.CREDENTIALS_WIPED, value).apply() } - /** Where the shop's event queue lives ([queueDirectory] creates it). */ + /** Where the shop's event queue lives; [QueueStore] creates it with its first event. */ val queuePath: File get() = File(directory, QUEUE_DIRECTORY) - /** The directory of the shop's event queue, created if needed; null when the disk refused it. */ - fun queueDirectory(): File? { - val queue = File(directory, QUEUE_DIRECTORY) - if (queue.isDirectory) return queue - return try { - if (queue.mkdirs() || queue.isDirectory) queue else null.also { failed(OP_CREATE_QUEUE, null) } - } catch (e: SecurityException) { - failed(OP_CREATE_QUEUE, e) - null - } - } - /** * Forgets the customer: [StorageKeys.USER_CREDENTIALS] and the saved `/init` answer, and with * [includeQueue] the queue's directory. The A/B group, the push tokens, the advertising id, the @@ -228,7 +216,6 @@ internal class ShopStorage(context: Context, val layout: StorageLayout) { const val OP_READ_INIT_ANSWER = "read_init_answer" const val OP_WRITE_INIT_ANSWER = "write_init_answer" - const val OP_CREATE_QUEUE = "create_queue_directory" const val OP_DELETE_CREDENTIALS = "delete_user_credentials" const val OP_DELETE_INIT_ANSWER = "delete_init_answer" const val OP_DELETE_QUEUE = "delete_queue" diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt index e6551642..7f748573 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkStorageTest.kt @@ -401,7 +401,7 @@ class SdkStorageTest { @Test fun `on the new core the deletion takes the queue too`() { val sdk = Rees46.initialize(context, config(coreV2 = true)) - val queue = sdk.storage?.queueDirectory() + val queue = sdk.storage?.queuePath?.also { it.mkdirs() } File(queue, "event").writeText("{}") runBlocking { sdk.deleteUserCredentials() } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt index bce3cd71..755fc293 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt @@ -793,7 +793,7 @@ class CoreInitTest { } private fun waitReady(init: CoreInit, timeoutMs: Long) = - CoroutineScope(StandardTestDispatcher(scheduler)).async { init.awaitReady(timeoutMs) }.also { scheduler.runCurrent() } + CoroutineScope(StandardTestDispatcher(scheduler)).async { init.awaitUsable(timeoutMs) }.also { scheduler.runCurrent() } private fun awaitReady(init: CoreInit, timeoutMs: Long): SdkError? { val waiting = waitReady(init, timeoutMs) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt index 53d080ea..5ed05de7 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/ShopStorageTest.kt @@ -103,17 +103,14 @@ class ShopStorageTest { } @Test - fun `the queue has a directory of the shop, made on first use`() { - val queue = storage().queueDirectory() + fun `the queue has a directory of the shop`() { + val queue = storage().queuePath - assertNotNull(queue) - assertTrue(queue?.isDirectory == true) assertEquals(File(context.noBackupFilesDir, "personalization_sdk/personalization_sdk_shop-1/queue"), queue) - assertEquals(queue, storage().queueDirectory()) // Another shop, another directory. assertEquals( File(context.noBackupFilesDir, "personalization_sdk/host_personalization_sdk_shop-1/queue"), - ShopStorage(context, StorageLayout(SHOP, "host")).queueDirectory() + ShopStorage(context, StorageLayout(SHOP, "host")).queuePath ) } @@ -123,7 +120,7 @@ class ShopStorageTest { val storage = storage() storage.saveInitAnswer(JSONObject("""{"did":"did-1"}""")) storage.isRefused = true - val queue = storage.queueDirectory() + val queue = storage.queuePath.also { it.mkdirs() } File(queue, "event-1").writeText("{}") assertTrue(runBlocking { LegacyCore(mockk(relaxed = true)) { "" }.deleteUserCredentials(storage) }) @@ -162,7 +159,7 @@ class ShopStorageTest { fun `on the new core the queue goes with the customer`() { seedShop() val storage = storage() - val queue = storage.queueDirectory() + val queue = storage.queuePath.also { it.mkdirs() } File(queue, "event-1").writeText("{}") val core = CoreV2(CoreHttpClient({ "https://example.invalid/" }, mockk(relaxed = true), FakeConnectivity(), SHOP), SHOP) From a67ff08785af93db8ab6b34d2c747bb418d03145 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:14:09 +0400 Subject: [PATCH 049/109] test(core): the session listener and the Java form of flush() are heard on the main thread through a main dispatcher the test runs itself, instead of polling the main looper against the wall clock --- .../com/personalization/SdkInstanceTest.kt | 13 ++++++------ .../com/personalization/TrackingQueueTest.kt | 17 ++++++++++------ .../core/internal/CoreFakes.kt | 20 +++++++++++++++++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt index 2d9039f1..d9075a86 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt @@ -9,7 +9,9 @@ import com.personalization.api.OnApiCallbackListener import com.personalization.core.SdkError import com.personalization.core.SdkException import com.personalization.core.SdkState +import com.personalization.core.internal.CoreDispatchers import com.personalization.core.internal.CoreSelection +import com.personalization.core.internal.HeldMain import com.personalization.core.internal.CoreV2 import com.personalization.core.internal.DeviceStorage import com.personalization.core.internal.LegacyCore @@ -457,17 +459,14 @@ class SdkInstanceTest { @Test fun `on the new core the session is the server's and the bridges hear it on the main thread`() { val heard = CopyOnWriteArrayList>() - val sdk = SDK() + val main = HeldMain() + val sdk = SDK().apply { dispatchers = CoreDispatchers(main = { main }) } sdk.onSessionChange { heard += it to (Looper.myLooper() == Looper.getMainLooper()) } sdk.initializeWith(context, config(coreV2 = true)) nextRequest("init") awaitState(sdk, SdkState.READY) - // Handed to the main thread from the io dispatcher: run the main looper until it arrives. - val deadline = System.currentTimeMillis() + 5_000 - while (heard.isEmpty() && System.currentTimeMillis() < deadline) { - shadowOf(Looper.getMainLooper()).idle() - Thread.sleep(5) - } + // Handed to the main thread from the io dispatcher: run it here, on the test's main thread. + while (heard.isEmpty() && main.runNext()) Unit assertEquals("S", sdk.sessionId) assertEquals(listOf("S" to true), heard) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt index e0e6fc4b..d19391ea 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt @@ -17,6 +17,8 @@ import com.personalization.core.SdkException import com.personalization.core.SdkResult import com.personalization.core.SdkState import com.personalization.core.internal.ContractFixtures +import com.personalization.core.internal.CoreDispatchers +import com.personalization.core.internal.HeldMain import com.personalization.core.internal.CoreSelection import com.personalization.core.internal.DeviceStorage import com.personalization.core.internal.LegacyCore @@ -236,15 +238,18 @@ class TrackingQueueTest { @Test fun `the Java form of flush ends once on the main thread`() { - val sdk = sdk() + val main = HeldMain() + val sdk = SDK().apply { dispatchers = CoreDispatchers(main = { main }) } + sdk.initializeWith( + context, + Rees46Config(shopId = SHOP, apiDomain = server.url("/").toString().trimEnd('/'), autoSendPushToken = false, coreV2 = true), + ) + runBlocking { withTimeout(5_000) { sdk.instanceState.changes.first { it == SdkState.READY } } } val ends = LinkedBlockingQueue, Boolean>>() sdk.flush(CompletionListener { ends += it to (Looper.myLooper() == Looper.getMainLooper()) }) - val deadline = System.currentTimeMillis() + 5_000 - while (ends.isEmpty() && System.currentTimeMillis() < deadline) { - shadowOf(Looper.getMainLooper()).idle() - Thread.sleep(5) - } + // Handed to the main thread: run it here, on the test's main thread. + while (ends.isEmpty() && main.runNext()) Unit assertEquals(SdkResult.Success(Unit) to true, ends.single()) } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt index 5832aa5b..fa5b550a 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreFakes.kt @@ -58,6 +58,26 @@ internal class FakeAppLifecycle(foreground: Boolean = true) : AppLifecycle { } } +/** + * The main thread for a test that runs on it (Robolectric's): what the SDK hands to "main" waits here + * until the test runs it, on its own thread, with [runNext] — a wait on the work itself, not on the clock. + */ +internal class HeldMain : kotlinx.coroutines.CoroutineDispatcher() { + private val tasks = java.util.concurrent.LinkedBlockingQueue() + + override fun dispatch(context: kotlin.coroutines.CoroutineContext, block: Runnable) { + tasks.put(block) + } + + /** Runs what was handed over, waiting up to [timeoutMs] for the first; false when nothing came. */ + fun runNext(timeoutMs: Long = 5_000): Boolean { + val first = tasks.poll(timeoutMs, java.util.concurrent.TimeUnit.MILLISECONDS) ?: return false + first.run() + while (true) (tasks.poll() ?: break).run() + return true + } +} + /** Every dispatcher of the core on one test scheduler: the test decides when work runs. */ @OptIn(ExperimentalCoroutinesApi::class) internal fun testCoreDispatchers(scheduler: TestCoroutineScheduler = TestCoroutineScheduler()): CoreDispatchers { From f86fb9ac6938636ded79d01f55c9ba5bad55f623 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:23:29 +0400 Subject: [PATCH 050/109] fix(core): the new core sends an event's numbers as JSON numbers, as the contract's fixtures have them: a cart line's amount and price, an order's price, a custom event's time and value and a popup's id that the older builders put in as text go as numbers when the event is sent; text that is not a whole plain number ("0123", "1e3", "2 pcs") stays text. The legacy core sends what it always has. --- .../core/internal/EventNumbers.kt | 58 +++++++++++++++++++ .../core/internal/EventQueue.kt | 5 +- .../com/personalization/TrackingQueueTest.kt | 7 ++- .../core/internal/EventQueueTest.kt | 32 ++++++++++ 4 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventNumbers.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventNumbers.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventNumbers.kt new file mode 100644 index 00000000..37c18734 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventNumbers.kt @@ -0,0 +1,58 @@ +package com.personalization.core.internal + +import org.json.JSONObject +import java.math.BigDecimal + +/** + * The new core sends an event's numbers as JSON numbers, the way the contract's fixtures have them: + * the older builders put a cart line's `amount` and `price`, an order's price and a popup's id in as + * text, which the server reads either way. Only text that is a whole plain number is turned into one: + * `"0123"`, `"1e3"`, `" 2"` or `"2 pcs"` stay as they are. The legacy core sends what it always has. + */ +internal object EventNumbers { + + /** Turns the numbers of [body], an event of [path], into JSON numbers; returns [body]. */ + fun normalize(path: String, body: JSONObject): JSONObject { + when (path) { + EventQueue.PATH_PUSH -> { + decimal(body, ORDER_PRICE) + val items = body.optJSONArray(ITEMS) ?: return body + for (index in 0 until items.length()) { + val item = items.optJSONObject(index) ?: continue + integer(item, AMOUNT) + decimal(item, PRICE) + } + } + EventQueue.PATH_CUSTOM -> { + integer(body, TIME) + integer(body, VALUE) + } + PATH_POPUP_SHOWED -> integer(body, POPUP) + } + return body + } + + private fun integer(json: JSONObject, key: String) { + val text = json.opt(key) as? String ?: return + val number = text.toLongOrNull()?.takeIf { it.toString() == text } ?: return + json.put(key, number) + } + + private fun decimal(json: JSONObject, key: String) { + val text = json.opt(key) as? String ?: return + if (!PLAIN_DECIMAL.matches(text)) return + // Kept as written: a BigDecimal goes out as its own digits, never in scientific notation. + json.put(key, BigDecimal(text)) + } + + private val PLAIN_DECIMAL = Regex("-?(0|[1-9][0-9]*)(\\.[0-9]+)?") + + private const val PATH_POPUP_SHOWED = "popup/showed" + private const val ORDER_PRICE = "order_price" + private const val ITEMS = "items" + private const val AMOUNT = "amount" + private const val PRICE = "price" + private const val TIME = "time" + private const val VALUE = "value" + private const val POPUP = "popup" +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt index 20ec1f29..d7405f4d 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt @@ -50,7 +50,8 @@ internal abstract class EventAnswerListener : OnApiCallbackListener() { * - **Stamping.** The common fields — `did`, the `sid` of the moment, `segment`, `stream` — go on a copy * when the event is sent ([stamp]), with the stored tracking source as a `source` object on `/push` only * and the time it was made as `time` on `/push/custom` only (the only path that reads it), unless the - * call set its own. Nothing stamped reaches the disk. + * call set its own. Numbers the older builders put in as text go as numbers ([EventNumbers]). Nothing + * stamped reaches the disk. * - **Sending.** One event at a time, oldest first, once the instance is ready and the session alive * (a session whose window ended gets its `/init` first), by the one process of the app that holds the * queue's file lock. An event cleared while it waited (the customer's deletion) does not go. Sending @@ -511,7 +512,7 @@ internal class EventQueue( /** What goes for [event]: a copy of its fields with the queue's and the common ones. */ private fun stamped(event: QueuedEvent): JSONObject = - stamp(withOwnFields(event.path, JSONObject(event.body.toString()), event.createdAt)) + stamp(withOwnFields(event.path, EventNumbers.normalize(event.path, JSONObject(event.body.toString())), event.createdAt)) /** The stored source on `/push` and the time the event was made on `/push/custom`, unless it has its own. */ private fun withOwnFields(path: String, body: JSONObject, createdAt: Long): JSONObject { diff --git a/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt index d19391ea..b196c39d 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/TrackingQueueTest.kt @@ -347,8 +347,11 @@ class TrackingQueueTest { assertTrue("$name: $actual is no array", actual is JSONArray && actual.length() == expected.length()) for (index in 0 until expected.length()) assertSubset("$name[$index]", expected.get(index), (actual as JSONArray).get(index)) } - // The SDK has always sent an item's amount as a string ("2"): the server reads both. - is Number -> assertEquals(name, expected.toDouble(), actual.toString().toDouble(), 0.0) + // The new core sends numbers as numbers, as the fixtures have them (the legacy one sent "2"). + is Number -> { + assertTrue("$name: $actual is no number", actual is Number) + assertEquals(name, expected.toDouble(), (actual as Number).toDouble(), 0.0) + } else -> assertEquals(name, expected, actual) } } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt index 7c8aa144..63d04c05 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt @@ -854,6 +854,38 @@ class EventQueueTest { assertEquals("new", sent.single().body.getJSONObject("source").getString("code")) } + @Test + fun `numbers the older builders put in as text go as numbers, and text that is not a plain number stays text`() { + val queue = start() + connectivity.set(false) + val items = org.json.JSONArray() + .put(JSONObject().put("id", "37").put("amount", "2").put("price", "999.50")) + .put(JSONObject().put("id", "38").put("amount", "0123").put("price", "1e3")) + .put(JSONObject().put("id", "39").put("amount", 3).put("price", "12345678.9")) + queue.accept("push", JSONObject().put("event", "cart").put("order_price", "2599.0").put("items", items), null) + queue.accept("push/custom", JSONObject().put("event", "e").put("value", "7").put("label", "8"), null) + queue.accept("popup/showed", JSONObject().put("popup", "42"), null) + queue.accept("track/stories", JSONObject().put("event", "view").put("story_id", 12).put("slide_id", "34"), null) + run() + connectivity.set(true) + run() + + val push = sent[0].body + val lines = push.getJSONArray("items") + assertEquals(2, lines.getJSONObject(0).get("amount")) + assertEquals(999.5, (lines.getJSONObject(0).get("price") as Number).toDouble(), 0.0) + assertEquals("0123", lines.getJSONObject(1).get("amount")) + assertEquals("1e3", lines.getJSONObject(1).get("price")) + assertEquals(3, lines.getJSONObject(2).get("amount")) + assertEquals(12345678.9, (lines.getJSONObject(2).get("price") as Number).toDouble(), 0.0) + assertEquals(2599.0, (push.get("order_price") as Number).toDouble(), 0.0) + assertEquals(7, sent[1].body.get("value")) + assertEquals("8", sent[1].body.get("label")) + assertEquals(42, sent[2].body.get("popup")) + // The slide's id is text by the contract. + assertEquals("34", sent[3].body.get("slide_id")) + } + @Test fun `an event extends a live session, and one that waited past the window goes with a new sid after init`() { val queue = start() From 9c0aa8ce4fd69dfbe6b85594b0bc8f9e2640e0e7 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:23:29 +0400 Subject: [PATCH 051/109] feat(core): push on the new core (stage 6, core/push.md, decision 0004). Rees46.handlePush(payload, PushEvent) hands a push to the shop its shop_id names, or the only shop, and returns that shop's id; a push without an id and a type is not the SDK's (push_dropped, not_sdk_push), and an unknown shop or several shops without a shop_id drop it. PushEvent DELIVERED, OPENED, CLICKED and CLOSED go through the shop's queue to track/delivered, track/opened, track/clicked and track/closed with code and type, without sid, seance or an empty field, once per message and event in a process: a repeat sends nothing, calls no callback and still returns the shop. The new core never sends track/received: the old handlePush(payload, PushEventType), notificationReceived and notificationClicked go to the new paths, and the legacy core keeps sending track/received as before. A tap sets the tracking source for 48 hours when its type is one of TrackingSourceType, and the /push events carry it as source. A notification the SDK draws (NotificationHelper) is reported as opened, and its dismissal (deleteIntent) as closed. Rees46.setPushCallbacks and SDK.setPushCallbacks take PushCallbacks (onDelivered, onOpened, onClicked, onClosed, onToken) with a parsed PushPayload; a shop's own callback of an event wins over the facade's, they run on the main thread, and one that throws is logged; the old message and token listeners keep working beside them. The token is the device's: one per provider in the device store, kept from FCM and HMS when the shop sends tokens by itself and always when the app passes one; each shop sends a new token at once and the same one at most once a week, only once the instance is ready and has a did, never from a push-only instance; the week is taken before the request, so two sends at once make one request whose answer both hear, and a failed send gives it back; setPushToken waits for the instance and answers success without a request inside the week; after each start's /init the shop sends the device's token when it is due. push_provider stays firebase or huawei. Logs: push_received, push_dropped, push_token_received, push_token_sent, push_token_skipped with its reason, push_token_failed. --- .../src/main/AndroidManifest.xml | 4 + .../personalization/HmsMessagingService.kt | 2 +- .../com/personalization/MessagingService.kt | 2 +- .../main/kotlin/com/personalization/Rees46.kt | 130 +++++- .../main/kotlin/com/personalization/SDK.kt | 169 ++++++- .../com/personalization/core/PushCallbacks.kt | 33 ++ .../com/personalization/core/PushEvent.kt | 19 + .../com/personalization/core/PushPayload.kt | 155 +++++++ .../core/internal/CoreHttpClient.kt | 9 +- .../personalization/core/internal/CoreInit.kt | 6 + .../core/internal/CorePushTokens.kt | 219 +++++++++ .../personalization/core/internal/CoreV2.kt | 9 +- .../core/internal/EventQueue.kt | 10 +- .../personalization/core/internal/PushHub.kt | 114 +++++ .../helpers/NotificationHelper.kt | 69 ++- .../helpers/NotificationNavigationHelper.kt | 7 + .../service/NotificationService.kt | 2 +- .../notifications/NotificationHandler.kt | 5 + .../push/PushDismissReceiver.kt | 43 ++ .../personalization/push/PushTokenManager.kt | 47 +- .../com/personalization/PushEventsTest.kt | 414 ++++++++++++++++++ .../personalization/core/PushPayloadTest.kt | 70 +++ .../core/internal/CorePushTokensTest.kt | 263 +++++++++++ 23 files changed, 1750 insertions(+), 51 deletions(-) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/PushCallbacks.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/PushEvent.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/PushPayload.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/push/PushDismissReceiver.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/PushPayloadTest.kt create mode 100644 personalization-sdk/src/test/kotlin/com/personalization/core/internal/CorePushTokensTest.kt diff --git a/personalization-sdk/src/main/AndroidManifest.xml b/personalization-sdk/src/main/AndroidManifest.xml index 8bb20826..057e32d0 100644 --- a/personalization-sdk/src/main/AndroidManifest.xml +++ b/personalization-sdk/src/main/AndroidManifest.xml @@ -21,6 +21,10 @@ + + , event: PushEvent): String? = try { + route(payload, event) + } catch (e: Exception) { + SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "handlePush: the push could not be handled", payload[PreferencesPartition.SHOP_ID_FIELD], throwable = e) + null + } + + private fun route(data: Map, event: PushEvent): String? { + val parsed = PushPayload.parse(data) + if (parsed == null) { + // Not the values: they carry the push's content. + SdkLogs.log( + LogLevel.DEBUG, + LogCodes.PUSH_DROPPED, + "handlePush: the push has no id or type, it is not a push of the SDK", + data[PreferencesPartition.SHOP_ID_FIELD], + mapOf("reason" to LogReasons.NOT_SDK_PUSH, "keys" to data.keys.sorted()), + ) + return null + } + val (resolution, shopId) = resolvePushTarget(data[PreferencesPartition.SHOP_ID_FIELD]) ?: return null + logHandedOver(shopId, event.name) + // Shown first, as before: the process-global listener needs no instance. + if (event == PushEvent.DELIVERED) SdkRegistry.dispatchMessage(shopId = shopId, data = data.toNotificationData()) + val target = targetInstance(resolution, shopId) + if (target == null) { + logDropped(shopId, LogReasons.UNKNOWN_SHOP) + return null + } + val fresh = when (event) { + PushEvent.DELIVERED -> target.onPushReceived(data) + else -> target.pushEvent(event, parsed.code, parsed.type) + } + if (fresh) PushHub.event(event, parsed.withShop(shopId), target.pushCallbacks) + return shopId + } + + /** + * The form of [handlePush] before [PushEvent]: [PushEventType.RECEIVED] is [PushEvent.DELIVERED] and + * [PushEventType.CLICKED] is [PushEvent.CLICKED]. * * A `RECEIVED` push is **shown first, with no initialization**: the parsed notification is dispatched * to the process-global listener ([setOnMessageListener]) so it appears even for a shop that is * registered but not yet initialized — the cold-process case, where FCM/HMS start the app just to - * deliver a data-only message and only eagerly-initialized shops are live. Tracking the delivery - * (`track/received`) then runs on the shop's own network, bringing a pending shop up with a light - * init ([materializeForPush]): no `/init` round-trip, no push-token re-registration, no profile - * call. A failure to track never undoes the shown notification. - * - * This is the router the SDK's own messaging services use; a host that owns its messaging service - * calls it directly. + * deliver a data-only message and only eagerly-initialized shops are live. As before, any push the + * shop resolves reaches that listener, one without `id` and `type` too. Tracking the delivery then runs + * on the shop's own network, bringing a pending shop up with a light init ([materializeForPush]); a + * failure to track never undoes the shown notification. The legacy core sends it to `track/received`, + * the new core to `track/delivered`. */ + @Deprecated( + message = "Use handlePush(payload, PushEvent): it returns the shop that took the push, and knows OPENED and CLOSED.", + replaceWith = ReplaceWith("Rees46.handlePush(payload, com.personalization.core.PushEvent.DELIVERED)", "com.personalization.Rees46"), + ) fun handlePush(payload: Map, event: PushEventType) { when (event) { PushEventType.RECEIVED -> receivePush(payload) @@ -227,22 +291,50 @@ object Rees46 { } } - private fun receivePush(payload: Map) { + /** A push the SDK's messaging services got: [handlePush] with [PushEventType.RECEIVED]. */ + internal fun receivePush(payload: Map) { val (resolution, shopId) = resolvePushTarget(payload[PreferencesPartition.SHOP_ID_FIELD]) ?: return - logHandedOver(shopId, PushEventType.RECEIVED) + logHandedOver(shopId, PushEventType.RECEIVED.name) // Show first — the process-global listener needs no instance, so a pending shop's push still // appears. Then track the delivery on the shop's own network (light bring-up if it isn't live); // a tracking/init failure must never undo the shown notification. SdkRegistry.dispatchMessage(shopId = shopId, data = payload.toNotificationData()) - runCatching { targetInstance(resolution, shopId)?.onPushReceived(payload) } - .onFailure { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "handlePush: the delivery was not tracked", shopId, throwable = it) } + runCatching { + val target = targetInstance(resolution, shopId) ?: return@runCatching + val fresh = target.onPushReceived(payload) + val parsed = PushPayload.parse(payload) + if (fresh && parsed != null) PushHub.event(PushEvent.DELIVERED, parsed.withShop(shopId), target.pushCallbacks) + }.onFailure { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "handlePush: the delivery was not tracked", shopId, throwable = it) } } + /** + * A tap in the old form: the legacy core tracks it from the payload's `NOTIFICATION_TYPE` and + * `NOTIFICATION_ID`, as before; the new core as [handlePush] with [PushEvent.CLICKED]. + */ private fun clickPush(payload: Map) { val (resolution, shopId) = resolvePushTarget(payload[PreferencesPartition.SHOP_ID_FIELD]) ?: return - logHandedOver(shopId, PushEventType.CLICKED) - runCatching { targetInstance(resolution, shopId)?.notificationClicked(payload.toBundle()) } - .onFailure { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "handlePush: the click was not tracked", shopId, throwable = it) } + logHandedOver(shopId, PushEventType.CLICKED.name) + runCatching { + val target = targetInstance(resolution, shopId) ?: return@runCatching + val parsed = PushPayload.parse(payload) + val fresh = if (parsed != null && target.core?.isV2 == true) { + target.pushEvent(PushEvent.CLICKED, parsed.code, parsed.type) + } else { + target.notificationClicked(payload.toBundle()) + true + } + if (fresh && parsed != null) PushHub.event(PushEvent.CLICKED, parsed.withShop(shopId), target.pushCallbacks) + }.onFailure { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "handlePush: the click was not tracked", shopId, throwable = it) } + } + + /** + * The app's callbacks on the pushes of every shop, the registered and not yet created among them: + * [PushPayload.shopId] tells the shop. A shop's own callback of an event ([SDK.setPushCallbacks]) wins + * over the one here. Replaces the callbacks set before; null clears them. [setOnMessageListener] keeps + * working beside them. + */ + fun setPushCallbacks(callbacks: PushCallbacks?) { + PushHub.callbacks = callbacks } /** @@ -280,8 +372,8 @@ object Rees46 { ) } - private fun logHandedOver(shopId: String, event: PushEventType) { - SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_RECEIVED, "Push handed to its shop", shopId, mapOf("event" to event.name.lowercase())) + private fun logHandedOver(shopId: String, event: String) { + SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_RECEIVED, "Push handed to its shop", shopId, mapOf("event" to event.lowercase())) } /** The live instance, or a pending shop brought up with a light push-context init. */ diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index d7ebf7d1..99cea347 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -12,6 +12,7 @@ import com.personalization.Params.InternalParameter import com.personalization.Params.TrackEvent import com.personalization.api.OnApiCallbackListener import com.personalization.api.models.purchase.PurchaseTrackingRequest +import com.personalization.api.models.tracking.TrackingSourceType import com.personalization.api.managers.CartManager import com.personalization.api.managers.CategoryManager import com.personalization.api.managers.CollectionManager @@ -27,6 +28,9 @@ import com.personalization.api.managers.TrackEventManager import com.personalization.api.managers.TrackingApi import com.personalization.api.params.ProfileParams import com.personalization.core.CompletionListener +import com.personalization.core.PushCallbacks +import com.personalization.core.PushEvent +import com.personalization.core.PushPayload import com.personalization.core.SdkError import com.personalization.core.SdkException import com.personalization.core.SdkResult @@ -38,13 +42,16 @@ import com.personalization.core.internal.Core import com.personalization.core.internal.CoreDispatchers import com.personalization.core.internal.CoreHttpClient import com.personalization.core.internal.CoreInit +import com.personalization.core.internal.CorePushTokens import com.personalization.core.internal.CoreSelection import com.personalization.core.internal.CoreV2 +import com.personalization.core.internal.DeviceStorage import com.personalization.core.internal.InitHooks import com.personalization.core.internal.InitSetup import com.personalization.core.internal.InstanceState import com.personalization.core.internal.EventQueue import com.personalization.core.internal.ProcessAppLifecycle +import com.personalization.core.internal.PushHub import com.personalization.core.internal.QueueStore import com.personalization.core.internal.SessionWindow import com.personalization.core.internal.ShopStorage @@ -55,6 +62,7 @@ import com.personalization.di.DaggerSdkComponent import com.personalization.features.inAppNotification.impl.ForegroundActivity import com.personalization.features.inAppNotification.impl.PopupPresentation import com.personalization.features.notification.data.mapper.toNotificationData +import com.personalization.features.notification.domain.model.NotificationConstants import com.personalization.features.notification.presentation.helpers.NotificationHelper import com.personalization.handlers.notifications.NotificationHandler import com.personalization.popups.PopupsV3 @@ -304,6 +312,26 @@ open class SDK { } } + /** The app's callbacks on this shop's pushes ([setPushCallbacks]). */ + @Volatile + internal var pushCallbacks: PushCallbacks? = null + private set + + /** + * The app's callbacks on this shop's pushes: delivered, opened, clicked, closed, and a new token. A + * callback set here wins over the one of the same event set with [Rees46.setPushCallbacks] for every + * shop. Replaces the callbacks set before; null or `PushCallbacks()` clears them. May be set before + * the instance is initialized. + * + * A callback runs on the main thread when the SDK sees its event: its messaging service, + * [Rees46.handlePush], or a notification the SDK drew ([NotificationHelper]). One event of one message + * is reported once per process. [setOnMessageListener] and [setOnPushTokenListener] keep working beside + * them. + */ + fun setPushCallbacks(callbacks: PushCallbacks?) { + pushCallbacks = callbacks + } + /** The level of this instance's log; [Rees46Config.logLevel] sets it for instances made through [Rees46]. */ internal var logLevel: LogLevel = LogLevel.WARNING @@ -479,7 +507,7 @@ open class SDK { enableAutoPopupPresentation = config.enableAutoPopupPresentation logLevel = config.logLevel coreV2Requested = config.coreV2 - initializeInternal(context, config, autoSendPushToken, needReInitialization, sendProfileOnInit = !pushOnly) + initializeInternal(context, config, autoSendPushToken, needReInitialization, pushOnly = pushOnly) this.pushOnly = pushOnly this.config = config } @@ -504,9 +532,10 @@ open class SDK { config: Rees46Config, autoSendPushToken: Boolean, needReInitialization: Boolean, - // When false, skip the background GAID + profile/set call (the push-context bring-up). - sendProfileOnInit: Boolean + // The push-context bring-up: no GAID + profile/set call and, on the new core, no push token. + pushOnly: Boolean, ) { + val sendProfileOnInit = !pushOnly val shopId = config.shopId // Initialized before (another shop, or released): the old start sends and takes nothing more. stopCurrent() @@ -526,7 +555,9 @@ open class SDK { SdkLogs.register(shopId = shopId, level = logLevel, tag = config.tag, owner = this) logShopId = shopId pushTokenManager.logShopId = shopId + pushTokenManager.callbacks = { pushCallbacks } storiesManager.logShopId = shopId + notificationHelper.shopId = shopId // Before the init request goes out: its response may already carry a popup. ForegroundActivity.install(context) @@ -564,8 +595,20 @@ open class SDK { lateinit var init: CoreInit // The core gets its /init and its queue before it takes a request: none goes without them. val core = networkRepository.useCoreV2(shopId, connectivity, dispatchers) { core -> - init = createCoreInit(core, connectivity, lifecycle, context, config, storage, autoSendPushToken, needReInitialization) + init = createCoreInit(core, connectivity, lifecycle, context, config, storage, autoSendPushToken, needReInitialization, pushOnly) core.init = init + // The token's rules are the new core's from the start: one FCM gives before /init waits for it. + pushTokenManager.core = CorePushTokens( + shopId = shopId, + device = DeviceStorage(context), + shop = storage.preferences, + send = { params, listener -> sendNetworkMethodUseCase.postAsync(CorePushTokens.PATH, params, listener) }, + ready = { init.ready() }, + hasDid = { userSettingsRepository.getDid().isNotEmpty() }, + autoSend = autoSendPushToken, + sends = !pushOnly, + dispatchers = dispatchers, + ) core.queue = EventQueue( shopId = shopId, store = QueueStore.of(storage.queuePath, shopId), @@ -637,6 +680,7 @@ open class SDK { */ private fun stopCurrent(cancelRequests: Boolean = true) { if (::registerManager.isInitialized) registerManager.stop() + if (::pushTokenManager.isInitialized) pushTokenManager.core?.release() if (::networkRepository.isInitialized) networkRepository.release(cancelRequests) } @@ -671,6 +715,7 @@ open class SDK { storage: ShopStorage, autoSendPushToken: Boolean, needReInitialization: Boolean, + pushOnly: Boolean, ): CoreInit { val contentResolver = context.contentResolver return CoreInit( @@ -704,7 +749,8 @@ open class SDK { override fun onUsable() { // What waited for a did on the legacy path (the stories v3 requests) goes now. networkRepository.executeQueueTasks() - pushTokenManager.initialize(context = context, autoSendPushToken = autoSendPushToken) + // A push-only instance sends no token: the shop's own instance does. + if (!pushOnly) pushTokenManager.initialize(context = context, autoSendPushToken = autoSendPushToken) } }, dispatchers = dispatchers, @@ -830,9 +876,24 @@ open class SDK { } /** + * Tracks a tap on a notification: [extras] carry its `NOTIFICATION_TYPE` and `NOTIFICATION_ID`. The tap + * attributes the next 48 hours of `/push` events as their `source`. On the new core the event goes + * through the queue, once per message in a process, and sets the source only for a type of + * [TrackingSourceType]. + * * @param extras from data notification */ fun notificationClicked(extras: Bundle?) { + if (isCoreV2) { + val type = extras?.getString(NotificationConstants.NOTIFICATION_TYPE) + val code = extras?.getString(NotificationConstants.NOTIFICATION_ID) + if (type.isNullOrBlank() || code.isNullOrBlank()) { + notSdkPush("The clicked push has no type or code", extras?.keySet()?.sorted()) + return + } + queuePushEvent(PushEvent.CLICKED, code, type) + return + } notificationHandler.notificationClicked( extras = extras, sendAsync = { method, params -> @@ -1658,9 +1719,32 @@ open class SDK { } /** + * Tracks a push that reached the app, from its `data`. On the legacy core it goes to `track/received`; + * on the new core to `track/delivered`, through the queue, once per message in a process, and only for + * a push with both an `id` and a `type`. + * * @param data from data notification */ fun notificationReceived(data: Map) { + trackDelivered(data) + } + + /** [notificationReceived]; false when the new core had the message's delivery already. */ + private fun trackDelivered(data: Map): Boolean { + if (isCoreV2) { + val code = data[ID_FIELD] + val type = data[TYPE_FIELD] + if (code.isNullOrBlank() || type.isNullOrBlank()) { + notSdkPush("The push has no id or type", data.keys.sorted()) + return false + } + return queuePushEvent(PushEvent.DELIVERED, code, type) + } + trackReceivedLegacy(data) + return true + } + + private fun trackReceivedLegacy(data: Map) { if (!isSdkInitialized) { log( LogLevel.WARNING, @@ -1688,17 +1772,71 @@ open class SDK { } } - private fun receiveMessage(data: Map) { - // Track the delivery on this instance's network and fire the deprecated per-instance listener. - // The process-global listener (Rees46.setOnMessageListener) is dispatched by the router - // ([Rees46.handlePush] / the [onMessage] companion) instead, so the notification shows even for a - // shop that has no live instance yet. - notificationReceived(data = data) + /** + * Tracks the delivery on this instance's network and fires the deprecated per-instance listener; + * false when the new core had the message's delivery already. The process-global listener + * (Rees46.setOnMessageListener) is dispatched by the router ([Rees46.handlePush] / the [onMessage] + * companion) instead, so the notification shows even for a shop that has no live instance yet. + */ + private fun receiveMessage(data: Map): Boolean { + val fresh = trackDelivered(data) onMessageListener?.onMessage(data = data.toNotificationData()) + return fresh } /** Entry point for [Rees46.handlePush]: track a received push on the already-resolved instance. */ - internal fun onPushReceived(data: Map) = receiveMessage(data = data) + internal fun onPushReceived(data: Map): Boolean = receiveMessage(data = data) + + /** Whether the instance runs on the new core. */ + private val isCoreV2: Boolean get() = core?.isV2 == true + + /** + * A push event of the message [code] of mailing [type] for [Rees46.handlePush]; false when it was a + * repeat. The new core queues it ([queuePushEvent]). The legacy core sends it as it always has: a tap + * through [notificationClicked], a delivery to `track/received`, and the shown and dismissed + * notifications, which it never reported before, to `track/opened` and `track/closed`. + */ + internal fun pushEvent(event: PushEvent, code: String, type: String): Boolean { + if (isCoreV2) return queuePushEvent(event, code, type) + val fields = mapOf(ID_FIELD to code, TYPE_FIELD to type) + when (event) { + PushEvent.DELIVERED -> trackReceivedLegacy(fields) + PushEvent.CLICKED -> notificationClicked(Bundle().apply { + putString(NotificationConstants.NOTIFICATION_TYPE, type) + putString(NotificationConstants.NOTIFICATION_ID, code) + }) + PushEvent.OPENED, PushEvent.CLOSED -> { + if (!isSdkInitialized) return false + sendNetworkMethodUseCase.postAsync(pathOf(event), JSONObject().put(TYPE_FIELD, type).put(CODE_FIELD, code)) + } + } + return true + } + + /** + * The new core's push event: `track/delivered`, `track/opened`, `track/clicked` or `track/closed` + * through the queue with `code` and `type`, once per message and event in a process (a repeat is a + * no-op, false). A tap also sets the tracking source for 48 hours, when its type is one of + * [TrackingSourceType]: the `/push` events carry it as `source`. + */ + private fun queuePushEvent(event: PushEvent, code: String, type: String): Boolean { + if (!PushHub.firstTime(shopId, event, code)) { + log(LogLevel.DEBUG, LogCodes.PUSH, "The push event was handled already", mapOf("event" to event.name.lowercase(), "reason" to REASON_REPEAT)) + return false + } + if (event == PushEvent.CLICKED && TrackingSourceType.entries.any { it.value == type }) { + notificationHandler.setClickSource(type = type, code = code) + } + sendNetworkMethodUseCase.postAsync(pathOf(event), JSONObject().put(CODE_FIELD, code).put(TYPE_FIELD, type)) + return true + } + + private fun pathOf(event: PushEvent): String = "track/" + event.name.lowercase(Locale.ROOT) + + private fun notSdkPush(message: String, keys: List?) { + // Not the keys' values: they carry the push's content. + log(LogLevel.DEBUG, LogCodes.PUSH_DROPPED, "$message: not a push of the SDK", mapOf("reason" to LogReasons.NOT_SDK_PUSH, "keys" to keys)) + } companion object { @@ -1738,6 +1876,7 @@ open class SDK { private const val ADD_FIELD = "add" private const val ID_FIELD = "id" private const val POPUPS_V3_TEST_MODE = "testmode" + private const val REASON_REPEAT = "repeat" /** * The SDK that receives push callbacks (see [onMessage]): whichever SDK was last passed to @@ -1842,9 +1981,11 @@ open class SDK { return } SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_RECEIVED, "Push handed to its shop", shopId, mapOf("event" to "received")) - // Show via the process-global listener, then track received on the instance. + // Show via the process-global listener, then track the delivery on the instance. SdkRegistry.dispatchMessage(shopId = shopId, data = data.toNotificationData()) - target.receiveMessage(data = data) + val fresh = target.receiveMessage(data = data) + val payload = PushPayload.parse(data) + if (fresh && payload != null) PushHub.event(PushEvent.DELIVERED, payload.withShop(shopId), target.pushCallbacks) } /** diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/PushCallbacks.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/PushCallbacks.kt new file mode 100644 index 00000000..716aac7b --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/PushCallbacks.kt @@ -0,0 +1,33 @@ +package com.personalization.core + +/** + * The app's callbacks on pushes: for every shop with `Rees46.setPushCallbacks`, for one shop with + * `SDK.setPushCallbacks`. A shop's own callback for an event wins over the facade's for that event. + * + * A callback runs on the main thread when the SDK sees its event: its messaging service, a notification + * it drew, or `Rees46.handlePush`. One event of one message (its `code`) is reported once per process. + * An exception a callback throws is logged as `callback_failed`. All fields are optional; + * `PushCallbacks()` with none clears them. + */ +class PushCallbacks @JvmOverloads constructor( + /** The push reached the app. */ + val onDelivered: PushListener? = null, + /** The notification was shown. */ + val onOpened: PushListener? = null, + /** The notification was tapped: what to open is [PushPayload.target]; the SDK opens nothing itself. */ + val onClicked: PushListener? = null, + /** The notification was dismissed without a tap. */ + val onClosed: PushListener? = null, + /** The SDK got a new token from FCM or HMS, once per token and provider in a process. */ + val onToken: PushTokenListener? = null, +) + +/** A push event's callback. */ +fun interface PushListener { + fun onPush(payload: PushPayload) +} + +/** A push token's callback. */ +fun interface PushTokenListener { + fun onToken(token: PushToken) +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/PushEvent.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/PushEvent.kt new file mode 100644 index 00000000..ef29e73b --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/PushEvent.kt @@ -0,0 +1,19 @@ +package com.personalization.core + +/** + * What happened to a push, for `Rees46.handlePush(payload, event)` (decision 0004). Each one is its own + * request: `track/delivered`, `track/opened`, `track/clicked`, `track/closed`. + */ +enum class PushEvent { + /** The push reached the app. */ + DELIVERED, + + /** The notification was shown: by the SDK itself, or by the app, which then reports it. */ + OPENED, + + /** The notification was tapped. */ + CLICKED, + + /** The notification was dismissed without a tap. */ + CLOSED, +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/PushPayload.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/PushPayload.kt new file mode 100644 index 00000000..fe33ef99 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/PushPayload.kt @@ -0,0 +1,155 @@ +package com.personalization.core + +import com.personalization.PushProvider +import org.json.JSONArray +import org.json.JSONObject + +/** + * A push of the SDK, parsed: what [PushCallbacks] receive. FCM and HMS send every value as a string, so + * `actions`, `action_urls` and `event` come as JSON text and are parsed here. + */ +class PushPayload internal constructor( + /** The shop the push was handed to; null when the push named none and was not routed. */ + val shopId: String?, + /** The message's code: the push's `id`. */ + val code: String, + /** The mailing's type: the push's `type` (`bulk`, `chain`, `transactional`, `digest`). */ + val type: String, + val title: String?, + val body: String?, + /** A link to the icon. */ + val icon: String?, + /** A link to the image; several are separated with commas. */ + val image: String?, + /** The notification's buttons; empty when it has none. */ + val actions: List, + /** What to open on a tap: the mailing's "additional data for the app"; null when it has none. */ + val target: PushTarget?, + /** The mailing's `utm_source`. */ + val source: String?, + /** The name of the sound file (`android_sound_file`). */ + val sound: String?, + /** The whole push as it came. */ + val data: Map, +) { + + override fun toString(): String = "PushPayload(shopId=$shopId, type=$type)" + + internal fun withShop(shopId: String): PushPayload = + PushPayload(shopId, code, type, title, body, icon, image, actions, target, source, sound, data) + + internal companion object { + /** + * The push in [data], or null when it is not the SDK's: every SDK push has an `id` and a `type`. + * A tap's intent extras (`NOTIFICATION_ID`, `NOTIFICATION_TYPE`) are read as well. Never throws: + * a field that does not parse is left empty. + */ + fun parse(data: Map): PushPayload? { + val code = (data[ID] ?: data[CLICK_ID])?.takeIf { it.isNotBlank() } ?: return null + val type = (data[TYPE] ?: data[CLICK_TYPE])?.takeIf { it.isNotBlank() } ?: return null + return PushPayload( + shopId = data[SHOP_ID]?.takeIf { it.isNotEmpty() }, + code = code, + type = type, + title = data[TITLE], + body = data[BODY], + icon = data[ICON], + image = data[IMAGE], + actions = actions(data[ACTIONS], data[ACTION_URLS]), + target = target(data[EVENT]), + source = data[SOURCE], + sound = data[SOUND], + data = data.toMap(), + ) + } + + private fun actions(actions: String?, urls: String?): List { + val list = array(actions) ?: return emptyList() + val links = array(urls) + return (0 until list.length()).mapNotNull { index -> + val action = list.optJSONObject(index) ?: return@mapNotNull null + PushAction( + id = action.optString(ACTION), + title = action.optString(TITLE), + url = links?.optString(index)?.takeIf { it.isNotEmpty() }, + ) + } + } + + private fun target(event: String?): PushTarget? { + if (event.isNullOrBlank()) return null + val json = try { + JSONObject(event) + } catch (e: Exception) { + return null + } + val payload = json.optJSONObject(PAYLOAD)?.let { fields -> fields.keys().asSequence().associateWith { fields.opt(it) } }.orEmpty() + return PushTarget(PushTargetType(json.optString(TYPE)), json.optString(URI), payload) + } + + private fun array(text: String?): JSONArray? = if (text.isNullOrBlank()) null else try { + JSONArray(text) + } catch (e: Exception) { + null + } + + const val SHOP_ID = "shop_id" + const val ID = "id" + const val TYPE = "type" + + /** The extras a tap's intent carries, as the SDK's notification helpers name them. */ + const val CLICK_ID = "NOTIFICATION_ID" + const val CLICK_TYPE = "NOTIFICATION_TYPE" + + private const val TITLE = "title" + private const val BODY = "body" + private const val ICON = "icon" + private const val IMAGE = "image" + private const val ACTIONS = "actions" + private const val ACTION = "action" + private const val ACTION_URLS = "action_urls" + private const val EVENT = "event" + private const val URI = "uri" + private const val PAYLOAD = "payload" + private const val SOURCE = "source" + private const val SOUND = "android_sound_file" + } +} + +/** A button of a notification. */ +class PushAction internal constructor( + /** `actions[i].action`. */ + val id: String, + val title: String, + /** The link of the same index in `action_urls`; null when there is none. */ + val url: String?, +) + +/** What a tap opens: the mailing's "additional data for the app" (`event`). */ +class PushTarget internal constructor( + val type: PushTargetType, + /** A link, a product's id, a category's id or the app's own link. */ + val uri: String, + /** A free field of the mailing. */ + val payload: Map, +) + +/** + * What [PushTarget.uri] is. An open set: the server understands `web` and `carousel` itself, the others + * are an agreement with the app, and a value the SDK does not know comes as it is. + */ +@JvmInline +value class PushTargetType(val value: String) { + companion object { + val WEB = PushTargetType("web") + val PRODUCT = PushTargetType("product") + val CATEGORY = PushTargetType("category") + val CAROUSEL = PushTargetType("carousel") + val CUSTOM = PushTargetType("custom") + } +} + +/** A push token and the provider that issued it. */ +data class PushToken(val token: String, val provider: PushProvider) { + override fun toString(): String = "PushToken(provider=$provider)" +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt index 895047ce..090ed456 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreHttpClient.kt @@ -73,7 +73,8 @@ internal object CoreHttp { * back with the failure, for the caller that repeats ([CoreInit]). * * `/init` carries `did`, `sid` and `seance` only when they have a value (an empty `did` is not a - * device), and no `segment`. + * device), and no `segment`. A push event (`track/delivered` and the others) carries no `sid` or `seance` + * and no empty field. */ internal class CoreHttpClient( private val baseUrl: () -> String, @@ -135,6 +136,12 @@ internal class CoreHttpClient( // `/init` does not read the A/B group (core/init.md): it is the events' field. fields.remove(UserBasicParams.SEGMENT) } + if (method.method in EventQueue.PUSH_EVENT_PATHS) { + // The server reads no sid in a push event (core/push.md), and core-v2 refuses an empty field. + fields.remove(UserBasicParams.SID) + fields.remove(UserBasicParams.SEANCE) + fields.keys().asSequence().toList().forEach { key -> if (fields.opt(key) == "") fields.remove(key) } + } val url = (baseUrl() + method.method).toHttpUrl() val builder = Request.Builder() .header(USER_AGENT, SDK.userAgent()) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt index 77d41cc7..11d0ca9a 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt @@ -206,6 +206,12 @@ internal class CoreInit( if (becameReady) markUsable(start) } + /** + * Waits without a limit until the instance is ready, or never will be: null once it is (it has a + * `did`), otherwise `shopBlocked` or `notInitialized`. The push token waits here (`core/push.md`). + */ + internal suspend fun ready(): SdkError? = errorOf(gate.first { it != Gate.WAIT }) + /** For a request with a result: [usable] within [timeoutMs]. */ suspend fun awaitUsable(timeoutMs: Long): SdkError? { val verdict = withTimeoutOrNull(timeoutMs) { Verdict(usable()) } ?: return SdkError.Timeout diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt new file mode 100644 index 00000000..68c41bc9 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt @@ -0,0 +1,219 @@ +package com.personalization.core.internal + +import android.content.SharedPreferences +import com.personalization.LogLevel +import com.personalization.PushProvider +import com.personalization.api.OnApiCallbackListener +import com.personalization.core.SdkError +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import kotlinx.coroutines.CoroutineExceptionHandler +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancel +import kotlinx.coroutines.launch +import org.json.JSONArray +import org.json.JSONObject + +/** + * The push token on the new core (`core/push.md`), for one shop. + * + * - The token is the device's: one per provider in [DeviceStorage], for every shop. A token from FCM or + * HMS is kept there when the shop sends tokens by itself (`autoSendPushToken`); one the app passes + * ([set]) always. + * - Each shop sends it to its own server (`POST mobile_push_tokens` with `platform=android` and + * `push_provider` `firebase` or `huawei`) and keeps, in its partition, which token it sent last and + * when. A token the shop has not sent goes at once; the same token at most once a week. + * - The week is taken before the request: two sends of one token at once make one request, the second + * hearing the first's answer. A failed send gives the week back. + * - A token goes only once the instance is ready ([ready]) and never without a `did`. After each start's + * `/init` ([sendStored]) the shop sends the device's token if it is due. + * - A push-only instance ([sends] false) sends no token. + */ +internal class CorePushTokens( + private val shopId: String, + private val device: DeviceStorage, + private val shop: SharedPreferences, + /** Sends the request's own fields through the instance's funnel. */ + private val send: (params: JSONObject, listener: OnApiCallbackListener) -> Unit, + /** Waits for the instance to be ready: null when it is, otherwise why it never will be. */ + private val ready: suspend () -> SdkError?, + private val hasDid: () -> Boolean, + /** Whether the shop takes the tokens FCM and HMS give it (`Rees46Config.autoSendPushToken`). */ + private val autoSend: Boolean, + private val sends: Boolean = true, + dispatchers: CoreDispatchers = CoreDispatchers.DEFAULT, + private val clock: Clock = Clock.SYSTEM, +) { + + private val scope = CoroutineScope(SupervisorJob() + dispatchers.io + CoroutineExceptionHandler { _, throwable -> + log(LogLevel.ERROR, LogCodes.PUSH_TOKEN_FAILED, "The push token could not be sent", throwable = throwable) + }) + + private val delivery = LegacyDelivery { level, code, message, throwable -> log(level, code, message, throwable = throwable) } + + private val lock = Any() + + /** The sends on their way, by `provider:token`, with the listeners waiting for their answer. */ + private val flights = HashMap() + + private class Flight(val claimedAt: Long, val previousToken: String?, val previousDate: Long) { + val waiting = ArrayList() + } + + /** The device's token of [provider], or the one this shop sent last. */ + fun token(provider: PushProvider): String? = device.pushToken(provider.id) ?: sentToken(provider) + + /** A token FCM or HMS gave: kept and sent, when the shop takes them, once the instance is ready. */ + fun received(token: String, provider: PushProvider) { + if (token.isEmpty() || !autoSend) return + device.savePushToken(provider.id, token) + scope.launch { if (ready() == null) sendIfDue(token, provider, null) } + } + + /** + * A token the app passes (`setPushToken`): kept for the device, then sent by the same rules once the + * instance is ready. [listener] hears the server's answer, success without a request when the token + * went this week, or the error. + */ + fun set(token: String, provider: PushProvider, listener: OnApiCallbackListener?) { + if (token.isBlank()) { + log(LogLevel.WARNING, LogCodes.INVALID_ARGUMENT, "setPushToken: the token is empty", mapOf("reason" to "empty_token")) + listener?.let { delivery.failure(it, LegacyErrors.of(SdkError.InvalidArgument("The token is empty"))) } + return + } + device.savePushToken(provider.id, token) + scope.launch { + val error = ready() + if (error != null) { + listener?.let { delivery.failure(it, LegacyErrors.of(error)) } + return@launch + } + sendIfDue(token, provider, listener) + } + } + + /** After a start's `/init`: the device's tokens go if they are due. */ + fun sendStored() { + PushProvider.entries.forEach { provider -> + val token = device.pushToken(provider.id) ?: return@forEach + scope.launch { sendIfDue(token, provider, null) } + } + } + + /** Sends [token] unless this shop sent it this week; [listener] hears how it ended. */ + internal fun sendIfDue(token: String, provider: PushProvider, listener: OnApiCallbackListener?) { + if (!sends) { + skipped(provider, REASON_PUSH_ONLY) + listener?.let { delivery.success(it, JSONObject()) } + return + } + if (!hasDid()) { + // Not before /init gave a did: the next start's /init sends it. + skipped(provider, REASON_NO_DID) + listener?.let { delivery.failure(it, LegacyErrors.of(SdkError.NotInitialized)) } + return + } + val key = "${provider.id}:$token" + val now = clock.nowMillis() + val flight = synchronized(lock) { + flights[key]?.let { going -> + // The same token on its way: one request, this caller hears its answer. + listener?.let { going.waiting += it } + return + } + val sentToken = sentToken(provider) + val sentAt = sentDate(provider) + if (sentToken == token && now - sentAt in 0 until WEEK_MS) { + null + } else { + // The week is taken before the request: a second send meanwhile finds it taken. + Flight(now, sentToken, sentAt).also { claimed -> + listener?.let { claimed.waiting += it } + flights[key] = claimed + shop.edit().putString(StorageKeys.pushToken(provider.id), token).putLong(StorageKeys.pushTokenDate(provider.id), now).apply() + } + } + } + if (flight == null) { + skipped(provider, REASON_SENT_THIS_WEEK) + listener?.let { delivery.success(it, JSONObject()) } + return + } + val params = JSONObject() + .put(FIELD_PLATFORM, PLATFORM_ANDROID) + .put(FIELD_TOKEN, token) + .put(FIELD_PUSH_PROVIDER, provider.serverValue) + send(params, object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) = landed { waiting -> + log(LogLevel.INFO, LogCodes.PUSH_TOKEN_SENT, "Push token sent", mapOf("provider" to provider.id)) + waiting.forEach { delivery.success(it, response ?: JSONObject()) } + } + + override fun onSuccess(response: JSONArray) = landed { waiting -> + log(LogLevel.INFO, LogCodes.PUSH_TOKEN_SENT, "Push token sent", mapOf("provider" to provider.id)) + waiting.forEach { delivery.success(it, response) } + } + + override fun onError(code: Int, msg: String?) { + landed(failed = true) { waiting -> + log(LogLevel.WARNING, LogCodes.PUSH_TOKEN_FAILED, "Push token was not sent", mapOf("provider" to provider.id, "status" to code)) + waiting.forEach { delivery.failure(it, code, msg) } + } + } + + private fun landed(failed: Boolean = false, tell: (List) -> Unit) { + val waiting = synchronized(lock) { + val ended = flights.remove(key) ?: return + if (failed) giveBack(provider, token, ended) + ended.waiting.toList() + } + tell(waiting) + } + }) + } + + /** Must hold [lock]. A failed send gives the week back, unless something else wrote the claim over meanwhile. */ + private fun giveBack(provider: PushProvider, token: String, flight: Flight) { + if (sentToken(provider) != token || sentDate(provider) != flight.claimedAt) return + val editor = shop.edit() + if (flight.previousToken == null) editor.remove(StorageKeys.pushToken(provider.id)) else editor.putString(StorageKeys.pushToken(provider.id), flight.previousToken) + if (flight.previousDate == 0L) editor.remove(StorageKeys.pushTokenDate(provider.id)) else editor.putLong(StorageKeys.pushTokenDate(provider.id), flight.previousDate) + editor.apply() + } + + /** The token this shop sent last, under the key of this release or of the ones before. */ + private fun sentToken(provider: PushProvider): String? = + shop.getString(StorageKeys.pushToken(provider.id), null)?.takeIf { it.isNotEmpty() } + ?: StorageKeys.LEGACY_PUSH_TOKEN[provider.id]?.let { shop.getString(it, null) }?.takeIf { it.isNotEmpty() } + + /** When this shop sent its token last; 0 when it never did or the customer was deleted since. */ + private fun sentDate(provider: PushProvider): Long { + val date = shop.getLong(StorageKeys.pushTokenDate(provider.id), 0L) + if (date != 0L) return date + return StorageKeys.LEGACY_PUSH_TOKEN_DATE[provider.id]?.let { shop.getLong(it, 0L) } ?: 0L + } + + private fun skipped(provider: PushProvider, reason: String) = + log(LogLevel.DEBUG, LogCodes.PUSH_TOKEN_SKIPPED, "Push token not sent", mapOf("provider" to provider.id, "reason" to reason)) + + /** Stops the sends waiting for the instance; a request on its way still ends. */ + fun release() = scope.cancel() + + private fun log(level: LogLevel, code: String, message: String, context: Map = emptyMap(), throwable: Throwable? = null) = + SdkLogs.log(level, code, message, shopId, context, throwable) + + internal companion object { + const val PATH = "mobile_push_tokens" + const val WEEK_MS = 7 * 24 * 60 * 60 * 1000L + + const val REASON_SENT_THIS_WEEK = "sent_this_week" + const val REASON_NO_DID = "no_did" + const val REASON_PUSH_ONLY = "push_only" + + private const val FIELD_PLATFORM = "platform" + private const val FIELD_TOKEN = "token" + private const val FIELD_PUSH_PROVIDER = "push_provider" + private const val PLATFORM_ANDROID = "android" + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt index 223a78c5..2b9dc0ea 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt @@ -91,7 +91,7 @@ internal class CoreV2( return } val queue = queue - if (queue != null && method is NetworkMethod.POST && method.method in EventQueue.PATHS) { + if (queue != null && method is NetworkMethod.POST && method.method in EVENT_PATHS) { queue.accept(method.method, params, listener) return } @@ -186,8 +186,11 @@ internal class CoreV2( return queue.clear(LogReasons.DELETE_USER_CREDENTIALS, CLEARED) { storage.deleteUserCredentials(includeQueue = true) } } - private companion object { - val CLEARED = LegacyError(LegacyErrors.NO_ANSWER, "The event was deleted with the customer's data") + internal companion object { + /** The paths whose events go through the queue. */ + val EVENT_PATHS: Set get() = EventQueue.PATHS + + private val CLEARED = LegacyError(LegacyErrors.NO_ANSWER, "The event was deleted with the customer's data") } private fun deliver(listener: OnApiCallbackListener, result: HttpResult) = when (result) { diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt index d7405f4d..b00c8bb3 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt @@ -40,7 +40,8 @@ internal abstract class EventAnswerListener : OnApiCallbackListener() { /** * The new core's event queue (`core/queue.md`, decision 0002): `/push`, `/push/custom`, `track/stories`, - * `track/slider` and `popup/showed` go through it, in order, and survive no network and a restart. + * `track/slider`, `popup/showed` and the push events (`track/delivered`, `track/opened`, `track/clicked`, + * `track/closed`) go through it, in order, and survive no network and a restart. * * - **Accepting.** [accept] answers a released instance with `notInitialized`, a refused shop with * `shopBlocked` and fields JSON cannot hold with `invalidArgument`, at once; otherwise it moves the @@ -635,8 +636,11 @@ internal class EventQueue( const val PATH_PUSH = "push" const val PATH_CUSTOM = "push/custom" - /** What the queue takes; the push events (`track/delivered` and the others) join with stage 6. */ - val PATHS: Set = setOf(PATH_PUSH, PATH_CUSTOM, "track/stories", "track/slider", "popup/showed") + /** The push events (decision 0004): the server reads no `sid` in them, and none goes. */ + val PUSH_EVENT_PATHS: Set = setOf("track/delivered", "track/opened", "track/clicked", "track/closed") + + /** What the queue takes. */ + val PATHS: Set = setOf(PATH_PUSH, PATH_CUSTOM, "track/stories", "track/slider", "popup/showed") + PUSH_EVENT_PATHS const val TTL_MS = 7 * 24 * 60 * 60 * 1000L const val CAP = 1_000 diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt new file mode 100644 index 00000000..e6de96ca --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt @@ -0,0 +1,114 @@ +package com.personalization.core.internal + +import android.os.Handler +import android.os.Looper +import com.personalization.LogLevel +import com.personalization.PushProvider +import com.personalization.core.PushCallbacks +import com.personalization.core.PushEvent +import com.personalization.core.PushListener +import com.personalization.core.PushPayload +import com.personalization.core.PushToken +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.Executor + +/** + * What the process knows of pushes beside the instances: the facade's [PushCallbacks], which events of + * which messages went already, and which tokens were reported. + * + * Callbacks run on the main thread ([main]); one that throws is logged as `callback_failed`. + */ +internal object PushHub { + + /** The facade's callbacks (`Rees46.setPushCallbacks`). */ + @Volatile + var callbacks: PushCallbacks? = null + + /** Where callbacks run: the main thread. */ + @Volatile + internal var main: Executor = MainThread + + private object MainThread : Executor { + private val handler by lazy { Handler(Looper.getMainLooper()) } + override fun execute(command: Runnable) { + handler.post(command) + } + } + + /** + * The events of the messages handled in this process, as `shop|event|code`: one event of one message + * goes once. Bounded: the oldest are forgotten past [SEEN_LIMIT]. + */ + private val seen = object : LinkedHashMap() { + override fun removeEldestEntry(eldest: MutableMap.MutableEntry?): Boolean = size > SEEN_LIMIT + } + + /** The last token of each provider the facade's `onToken` heard of. */ + private val facadeReported = ConcurrentHashMap() + + /** True the first time [event] of the message [code] comes for [shopId] in this process. */ + fun firstTime(shopId: String, event: PushEvent, code: String): Boolean = synchronized(seen) { + seen.put("$shopId|${event.name}|$code", true) == null + } + + /** + * Tells the callbacks about [event]: the shop's own one for it ([own]) when set, otherwise the + * facade's. + */ + fun event(event: PushEvent, payload: PushPayload, own: PushCallbacks?) { + val listener = listenerOf(own, event) ?: listenerOf(callbacks, event) ?: return + run(payload.shopId) { call(payload.shopId, "A push callback threw") { listener.onPush(payload) } } + } + + /** + * Tells the callbacks about a token an instance got from its provider: the instance's own `onToken` + * ([own], which keeps track of what it heard through [ownReported]) or, without one, the facade's; + * the facade hears each token once per process. + */ + fun token(token: PushToken, shopId: String?, own: PushCallbacks?, ownReported: MutableMap) { + val ownListener = own?.onToken + val ownNew = ownListener != null && ownReported.put(token.provider, token.token) != token.token + val facadeListener = if (ownListener == null) callbacks?.onToken else null + val facade = facadeListener?.takeIf { facadeReported.put(token.provider, token.token) != token.token } + if (!ownNew && facade == null) return + run(shopId) { + if (ownNew) ownListener?.let { call(shopId, "A push token callback threw") { it.onToken(token) } } + facade?.let { call(shopId, "A push token callback threw") { it.onToken(token) } } + } + } + + private fun listenerOf(callbacks: PushCallbacks?, event: PushEvent): PushListener? = when (event) { + PushEvent.DELIVERED -> callbacks?.onDelivered + PushEvent.OPENED -> callbacks?.onOpened + PushEvent.CLICKED -> callbacks?.onClicked + PushEvent.CLOSED -> callbacks?.onClosed + } + + private fun run(shopId: String?, block: () -> Unit) { + try { + main.execute(block) + } catch (e: Exception) { + SdkLogs.log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "A push callback could not be scheduled", shopId, throwable = e) + } + } + + private inline fun call(shopId: String?, message: String, block: () -> Unit) { + try { + block() + } catch (e: Exception) { + SdkLogs.log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, message, shopId, throwable = e) + } + } + + /** Test-only: forgets everything. */ + internal fun reset() { + callbacks = null + synchronized(seen) { seen.clear() } + facadeReported.clear() + main = MainThread + } + + private const val SEEN_LIMIT = 1_000 +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationHelper.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationHelper.kt index eb673973..5c9abe72 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationHelper.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationHelper.kt @@ -6,7 +6,10 @@ import android.content.pm.PackageManager import android.graphics.Bitmap import android.widget.RemoteViews import androidx.core.app.NotificationCompat +import com.personalization.Rees46 +import com.personalization.core.PushEvent import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_CHANNEL +import com.personalization.push.PushDismissReceiver import com.personalization.resources.NotificationResources import com.personalization.sdk.data.models.dto.notification.NotificationData import javax.inject.Inject @@ -15,12 +18,41 @@ class NotificationHelper @Inject constructor( private val notificationManager: NotificationManager ) { + /** The shop of the instance this helper belongs to: the notifications it draws are that shop's. */ + @Volatile + internal var shopId: String? = null + + /** + * Draws [data] as a notification. When it is a push of the SDK — it has an `id` and a `type` — the SDK + * reports it as shown (`Rees46.handlePush(push, PushEvent.OPENED)`: `track/opened` and the `onOpened` + * callback), and a dismissal without a tap as closed (`track/closed`, `onClosed`), for the shop of the + * instance whose helper this is. + */ fun createNotification( context: Context, data: NotificationData, images: List?, currentImageIndex: Int = 0, hasError: Boolean = false + ) = show(context, data, images, currentImageIndex, hasError, shopId ?: shopOf(data.id), reportShown = true) + + /** The same push drawn again, with another picture of its carousel: not shown once more. */ + internal fun redraw( + context: Context, + data: NotificationData, + images: List?, + currentImageIndex: Int, + hasError: Boolean, + ) = show(context, data, images, currentImageIndex, hasError, shopOf(data.id), reportShown = false) + + private fun show( + context: Context, + data: NotificationData, + images: List?, + currentImageIndex: Int, + hasError: Boolean, + shopId: String?, + reportShown: Boolean, ) { val view = RemoteViews( /* packageName = */ context.packageName, @@ -58,10 +90,28 @@ class NotificationHelper @Inject constructor( .setPriority(NotificationCompat.PRIORITY_HIGH) .setDefaults(NotificationCompat.DEFAULT_ALL) + val notificationId = (data.title + data.body).hashCode() + val push = pushOf(data, shopId) + push?.let { notificationBuilder.setDeleteIntent(PushDismissReceiver.intent(context, notificationId, it)) } + notificationManager.notify( - /* id = */ (data.title + data.body).hashCode(), + /* id = */ notificationId, /* notification = */ notificationBuilder.build() ) + if (push == null) return + if (shopId != null) remember(push.getValue(FIELD_ID), shopId) + if (reportShown) Rees46.handlePush(push, PushEvent.OPENED) + } + + /** What reports the push of [data]: its code, type and shop; null when it is not a push of the SDK. */ + private fun pushOf(data: NotificationData, shopId: String?): Map? { + val code = data.id?.takeIf { it.isNotBlank() } ?: return null + val type = data.type?.takeIf { it.isNotBlank() } ?: return null + return buildMap { + put(FIELD_ID, code) + put(FIELD_TYPE, type) + shopId?.let { put(FIELD_SHOP_ID, it) } + } } /** @@ -84,6 +134,23 @@ class NotificationHelper @Inject constructor( } private companion object { + const val FIELD_ID = "id" + const val FIELD_TYPE = "type" + const val FIELD_SHOP_ID = "shop_id" + private const val SHOPS_LIMIT = 100 + + /** + * The shop of each push drawn in this process, by its code: the carousel's service draws a push + * again without an instance, and its dismissal still belongs to the shop. + */ + private val shops = object : LinkedHashMap() { + override fun removeEldestEntry(eldest: MutableMap.MutableEntry?): Boolean = size > SHOPS_LIMIT + } + + fun remember(code: String, shopId: String) = synchronized(shops) { shops[code] = shopId } + + fun shopOf(code: String?): String? = code?.let { synchronized(shops) { shops[it] } } + // De-facto standard meta-data hosts use to declare a dedicated push notification icon. const val META_DATA_NOTIFICATION_ICON = "com.google.firebase.messaging.default_notification_icon" diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationNavigationHelper.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationNavigationHelper.kt index 7a951f30..72ebd146 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationNavigationHelper.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationNavigationHelper.kt @@ -6,7 +6,9 @@ import android.content.Intent import com.personalization.features.notification.domain.model.NotificationConstants.CURRENT_IMAGE_INDEX import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_BODY import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_IMAGE +import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_PARAM_ID import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_TITLE +import com.personalization.features.notification.domain.model.NotificationConstants.TYPE_PARAM import com.personalization.features.notification.service.NotificationService import com.personalization.sdk.data.models.dto.notification.NotificationData import java.util.concurrent.atomic.AtomicInteger @@ -28,6 +30,9 @@ object NotificationNavigationHelper { putExtra(NOTIFICATION_TITLE, data.title) putExtra(NOTIFICATION_BODY, data.body) putExtra(NOTIFICATION_IMAGE, data.image) + // The push it is, so the notification drawn again still reports its dismissal. + putExtra(NOTIFICATION_PARAM_ID, data.id) + putExtra(TYPE_PARAM, data.type) } return PendingIntent.getService( @@ -47,6 +52,8 @@ object NotificationNavigationHelper { putExtra(NOTIFICATION_IMAGE, data.image) putExtra(NOTIFICATION_TITLE, data.title) putExtra(NOTIFICATION_BODY, data.body) + putExtra(NOTIFICATION_PARAM_ID, data.id) + putExtra(TYPE_PARAM, data.type) } return PendingIntent.getService( context, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/service/NotificationService.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/service/NotificationService.kt index 63af571f..67908c24 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/service/NotificationService.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/service/NotificationService.kt @@ -104,7 +104,7 @@ class NotificationService : Service() { context = applicationContext, urls = data.image ) - notificationHelper.createNotification( + notificationHelper.redraw( context = this@NotificationService, data = NotificationData( id = data.id, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/handlers/notifications/NotificationHandler.kt b/personalization-sdk/src/main/kotlin/com/personalization/handlers/notifications/NotificationHandler.kt index a38d93b0..177d86a8 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/handlers/notifications/NotificationHandler.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/handlers/notifications/NotificationHandler.kt @@ -39,4 +39,9 @@ class NotificationHandler @Inject constructor( } ) } + + /** The source a tap on a push sets for the next 48 hours of `/push` events, as [notificationClicked] does. */ + internal fun setClickSource(type: String, code: String) { + setTrackingSourceUseCase(type = type, code = code) + } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/push/PushDismissReceiver.kt b/personalization-sdk/src/main/kotlin/com/personalization/push/PushDismissReceiver.kt new file mode 100644 index 00000000..f5ce9df7 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/push/PushDismissReceiver.kt @@ -0,0 +1,43 @@ +package com.personalization.push + +import android.app.PendingIntent +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import com.personalization.LogLevel +import com.personalization.Rees46 +import com.personalization.core.PushEvent +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs + +/** + * The `deleteIntent` of a notification the SDK drew: dismissed without a tap, it is + * `Rees46.handlePush(push, PushEvent.CLOSED)` — `track/closed` and the `onClosed` callback. + */ +internal class PushDismissReceiver : BroadcastReceiver() { + + override fun onReceive(context: Context, intent: Intent) { + try { + val push = intent.extras?.let { extras -> + extras.keySet().mapNotNull { key -> extras.getString(key)?.let { key to it } }.toMap() + } ?: return + Rees46.handlePush(push, PushEvent.CLOSED) + } catch (e: Exception) { + SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "A dismissed notification could not be reported", throwable = e) + } + } + + internal companion object { + /** The intent that reports the dismissal of the notification [notificationId] of [push]. */ + fun intent(context: Context, notificationId: Int, push: Map): PendingIntent { + val intent = Intent(context, PushDismissReceiver::class.java) + push.forEach { (key, value) -> intent.putExtra(key, value) } + return PendingIntent.getBroadcast( + context, + notificationId, + intent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, + ) + } + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt index 3df7ec71..a150f635 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/push/PushTokenManager.kt @@ -7,6 +7,10 @@ import android.content.Context import com.personalization.OnPushTokenListener import com.personalization.PushProvider import com.personalization.api.OnApiCallbackListener +import com.personalization.core.PushCallbacks +import com.personalization.core.PushToken +import com.personalization.core.internal.CorePushTokens +import com.personalization.core.internal.PushHub import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase import com.personalization.sdk.domain.usecases.preferences.GetPreferencesValueUseCase import com.personalization.sdk.domain.usecases.preferences.SavePreferencesValueUseCase @@ -29,6 +33,10 @@ import javax.inject.Singleton * device. Until the device has a `did` (a new install before `/init` answers, or the window after * `deleteUserCredentials()`) the request waits in the instance's queue and goes once the `did` is * stored; the host's listener hears of the token at once all the same. + * + * On the new core the rules of `core/push.md` are [CorePushTokens]'s: the token is the device's, each shop + * sends a new one at once and the same one at most once a week, after the instance is ready. Either way + * the app's `PushCallbacks.onToken` hears each new token once ([PushHub]). */ @Singleton class PushTokenManager @Inject constructor( @@ -45,6 +53,17 @@ class PushTokenManager @Inject constructor( @Volatile internal var logShopId: String? = null + /** The new core's rules for this shop's token; null on the legacy core. */ + @Volatile + internal var core: CorePushTokens? = null + + /** The instance's own push callbacks, for `onToken`. */ + @Volatile + internal var callbacks: () -> PushCallbacks? = { null } + + /** The tokens the instance's own `onToken` heard of, by provider. */ + private val reported = ConcurrentHashMap() + /** * Tokens whose request is already on the wire, as `provider:token`. * @@ -80,9 +99,9 @@ class PushTokenManager @Inject constructor( this.listener = listener } - /** Last token known for [provider] from the local cache, or null if none. */ + /** Last token known for [provider] from the local cache, or null if none: on the new core the device's. */ fun getToken(provider: PushProvider): String? = - getPreferencesValueUseCase.getPushToken(provider).takeIf { it.isNotEmpty() } + core?.token(provider) ?: getPreferencesValueUseCase.getPushToken(provider).takeIf { it.isNotEmpty() } /** * Detects available providers and proactively fetches their tokens. Tokens that are only @@ -91,6 +110,8 @@ class PushTokenManager @Inject constructor( fun initialize(context: Context, autoSendPushToken: Boolean) { this.context = context this.autoSendPushToken = autoSendPushToken + // The new core: what the device has goes once it is due, before a fetch brings it again. + core?.sendStored() sources.filter { it.isAvailable(context) }.forEach { source -> source.fetchToken(context) { result -> @@ -116,22 +137,34 @@ class PushTokenManager @Inject constructor( * `onNewToken`). Sends to the backend when changed, then always notifies the host listener. */ fun onTokenReceived(token: String, provider: PushProvider) { - if (token.isEmpty() || !::context.isInitialized) return + val v2 = core + if (token.isEmpty() || (v2 == null && !::context.isInitialized)) return // Never the token itself. SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_TOKEN_RECEIVED, "Push token received", logShopId, mapOf("provider" to provider.id)) - val currentDate = Date().time - if (shouldSendToken(provider, token, currentDate) && claimInFlight(provider, token)) { - sendTokenToServer(token, provider, currentDate, external = null) + if (v2 != null) { + // Kept for the device and sent once the instance is ready, even before it is. + v2.received(token, provider) + } else { + val currentDate = Date().time + if (shouldSendToken(provider, token, currentDate) && claimInFlight(provider, token)) { + sendTokenToServer(token, provider, currentDate, external = null) + } } listener?.onPushToken(token, provider) + PushHub.token(PushToken(token, provider), logShopId, callbacks(), reported) } /** * Manual registration entry point ([com.personalization.SDK.setPushToken]). Always sends to - * the backend regardless of [autoSendPushToken], because the host asked for it explicitly. + * the backend regardless of [autoSendPushToken], because the host asked for it explicitly. On the + * new core it waits for the instance and keeps the week like any token ([CorePushTokens.set]). */ fun sendToken(token: String, provider: PushProvider, listener: OnApiCallbackListener?) { + core?.let { + it.set(token, provider, listener) + return + } if (token.isEmpty()) return sendTokenToServer(token, provider, Date().time, external = listener) } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt new file mode 100644 index 00000000..fa88e9c8 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt @@ -0,0 +1,414 @@ +package com.personalization + +import android.content.Context +import android.net.ConnectivityManager +import android.net.NetworkCapabilities +import android.os.Bundle +import android.os.Looper +import androidx.test.core.app.ApplicationProvider +import com.personalization.core.PushCallbacks +import com.personalization.core.PushEvent +import com.personalization.core.PushPayload +import com.personalization.core.SdkState +import com.personalization.core.internal.CoreSelection +import com.personalization.core.internal.DeviceStorage +import com.personalization.core.internal.LegacyCore +import com.personalization.core.internal.PushHub +import com.personalization.core.internal.QueueStore +import com.personalization.core.internal.ShopStorage +import com.personalization.core.internal.StorageLayout +import com.personalization.core.internal.TrackingThreads +import com.personalization.features.notification.domain.model.NotificationConstants +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import com.personalization.sdk.data.models.dto.notification.NotificationData +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeout +import okhttp3.mockwebserver.Dispatcher +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import okhttp3.mockwebserver.RecordedRequest +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.ShadowNetworkCapabilities +import java.io.File +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.Executor +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit + +/** + * The push events of `core/push.md` and decision 0004 through `Rees46.handlePush` and the SDK, on a local + * server: each event through the new core's queue to its own `track/…`, never `track/received`, once per + * message; the shop the push names, or the only one; the callbacks of the shop and of the facade; and the + * legacy core as it was. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class PushEventsTest { + + private lateinit var context: Context + private var server = MockWebServer() + private val requests = LinkedBlockingQueue>() + private val logged = CopyOnWriteArrayList() + private val threads = TrackingThreads() + private val defaultExecutor = SdkLogs.handlerExecutor + + /** Holds the answer to /init while it is not null. */ + @Volatile + private var holdInit: java.util.concurrent.CountDownLatch? = null + + @Before + fun setUp() { + context = ApplicationProvider.getApplicationContext() + CoreSelection.reset() + SdkRegistry.reset() + Rees46.reset() + SdkLogs.reset() + QueueStore.reset() + PushHub.reset() + PushHub.main = Executor { it.run() } + SdkLogs.handlerExecutor = Executor { it.run() } + SdkLogs.handler = SdkLogHandler { logged += it } + listOf(SHOP, OTHER).forEach { shop -> + context.getSharedPreferences(StorageLayout(shop, null).partitionName, Context.MODE_PRIVATE).edit().clear().commit() + } + listOf(PreferencesPartition.LEGACY_KEY, DeviceStorage.NAME).forEach { + context.getSharedPreferences(it, Context.MODE_PRIVATE).edit().clear().commit() + } + File(context.noBackupFilesDir, ShopStorage.ROOT_DIRECTORY).deleteRecursively() + val manager = context.getSystemService(ConnectivityManager::class.java) + val capabilities = ShadowNetworkCapabilities.newInstance() + shadowOf(capabilities).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + shadowOf(manager).setNetworkCapabilities(manager.activeNetwork, capabilities) + LegacyCore.threads = threads + server = MockWebServer() + requests.clear() + logged.clear() + server.dispatcher = object : Dispatcher() { + override fun dispatch(request: RecordedRequest): MockResponse { + val url = request.requestUrl ?: return MockResponse().setResponseCode(400) + val path = url.encodedPath.removePrefix("/") + val body = if (request.method == "POST") JSONObject(request.body.readUtf8()) + else JSONObject().apply { url.queryParameterNames.forEach { put(it, url.queryParameter(it)) } } + requests += path to body + if (path == "init") { + holdInit?.await(5, TimeUnit.SECONDS) + return MockResponse().setBody("""{"did":"D1","seance":"S1"}""") + } + if (path == "segments/get") return MockResponse().setResponseCode(404) + return MockResponse().setBody("{}") + } + } + server.start() + } + + @After + fun tearDown() { + holdInit?.countDown() + SdkRegistry.all().forEach { it.release() } + server.shutdown() + LegacyCore.threads = LegacyCore.DEFAULT_THREADS + CoreSelection.reset() + SdkRegistry.reset() + Rees46.reset() + SdkLogs.reset() + SdkLogs.handlerExecutor = defaultExecutor + QueueStore.reset() + PushHub.reset() + } + + private fun config(shop: String = SHOP, coreV2: Boolean = true) = Rees46Config( + shopId = shop, + apiDomain = server.url("/").toString().trimEnd('/'), + autoSendPushToken = false, + coreV2 = coreV2, + logLevel = LogLevel.DEBUG, + ) + + private fun sdk(shop: String = SHOP, coreV2: Boolean = true): SDK { + val sdk = Rees46.initialize(context, config(shop, coreV2)) + runBlocking { withTimeout(5_000) { sdk.instanceState.changes.first { it == SdkState.READY } } } + return sdk + } + + private fun push(code: String = "m-1", type: String = "bulk", shop: String? = SHOP) = buildMap { + put("id", code) + put("type", type) + put("title", "Title") + put("body", "Body") + shop?.let { put("shop_id", it) } + } + + /** The next request to one of [paths], skipping others; null when none came in [timeoutMs]. */ + private fun next(vararg paths: String, timeoutMs: Long = 5_000): Pair? { + val deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMs) + while (true) { + val left = deadline - System.nanoTime() + if (left <= 0) return null + val request = requests.poll(left, TimeUnit.NANOSECONDS) ?: return null + if (request.first in paths) return request + } + } + + private fun flush(sdk: SDK) = runBlocking { withTimeout(5_000) { sdk.flush() } } + + // The new core. + + @Test + fun `each event goes through the queue to its own path with code, type, shop and did, and no sid`() { + val sdk = sdk() + listOf(PushEvent.DELIVERED, PushEvent.OPENED, PushEvent.CLICKED, PushEvent.CLOSED).forEach { event -> + assertEquals(SHOP, Rees46.handlePush(push(code = "m-${event.name}"), event)) + flush(sdk) + val (path, body) = next("track/delivered", "track/opened", "track/clicked", "track/closed", "track/received") + ?: throw AssertionError("no request for $event") + assertEquals("track/${event.name.lowercase()}", path) + assertEquals("m-${event.name}", body.getString("code")) + assertEquals("bulk", body.getString("type")) + assertEquals(SHOP, body.getString("shop_id")) + assertEquals("D1", body.getString("did")) + assertEquals("android", body.getString("stream")) + assertFalse("no sid in a push event", body.has("sid") || body.has("seance")) + assertFalse("no empty field", body.keys().asSequence().any { body.opt(it) == "" }) + } + } + + @Test + fun `the old forms go to the new paths on the new core, never track received`() { + val sdk = sdk() + @Suppress("DEPRECATION") + Rees46.handlePush(push(code = "a"), PushEventType.RECEIVED) + sdk.notificationReceived(push(code = "b")) + sdk.notificationClicked(Bundle().apply { + putString(NotificationConstants.NOTIFICATION_TYPE, "chain") + putString(NotificationConstants.NOTIFICATION_ID, "c") + }) + @Suppress("DEPRECATION") + Rees46.handlePush(mapOf("NOTIFICATION_TYPE" to "bulk", "NOTIFICATION_ID" to "d", "shop_id" to SHOP), PushEventType.CLICKED) + flush(sdk) + + val got = List(4) { next("track/delivered", "track/clicked", "track/received") } + assertEquals( + listOf("track/delivered" to "a", "track/delivered" to "b", "track/clicked" to "c", "track/clicked" to "d"), + got.map { it?.first to it?.second?.getString("code") }, + ) + assertNull(next("track/received", timeoutMs = 300)) + } + + @Test + fun `one event of one message goes once in a process, a repeat returns the shop and calls nothing`() { + val sdk = sdk() + val clicked = CopyOnWriteArrayList() + Rees46.setPushCallbacks(PushCallbacks(onClicked = { clicked += it.code })) + + assertEquals(SHOP, Rees46.handlePush(push(code = "m-1"), PushEvent.CLICKED)) + assertEquals(SHOP, Rees46.handlePush(push(code = "m-1"), PushEvent.CLICKED)) + sdk.notificationClicked(Bundle().apply { + putString(NotificationConstants.NOTIFICATION_TYPE, "bulk") + putString(NotificationConstants.NOTIFICATION_ID, "m-1") + }) + // Another event of the message is its own. + assertEquals(SHOP, Rees46.handlePush(push(code = "m-1"), PushEvent.CLOSED)) + flush(sdk) + + assertEquals("track/clicked", next("track/clicked", "track/closed")?.first) + assertEquals("track/closed", next("track/clicked", "track/closed")?.first) + assertNull(next("track/clicked", timeoutMs = 300)) + assertEquals(listOf("m-1"), clicked) + } + + @Test + fun `a tap sets the source of the next push events for a known type, and no from or code goes on the others`() { + val sdk = sdk() + Rees46.handlePush(push(code = "m-9", type = "chain"), PushEvent.CLICKED) + sdk.tracking.productView("37") + flush(sdk) + + val view = next("push")?.second ?: throw AssertionError("no push") + assertEquals("chain", view.getJSONObject("source").getString("from")) + assertEquals("m-9", view.getJSONObject("source").getString("code")) + assertFalse(view.has("from")) + + // A type the source does not know (digest) sets none. + sdk.tracking.setSource(com.personalization.api.models.tracking.TrackingSource(com.personalization.api.models.tracking.TrackingSourceType.BULK, "kept")) + Rees46.handlePush(push(code = "m-10", type = "digest"), PushEvent.CLICKED) + sdk.tracking.productView("38") + flush(sdk) + assertEquals("kept", next("push")?.second?.getJSONObject("source")?.getString("code")) + } + + // Routing. + + @Test + fun `the push goes to the shop it names, or the only one, and is dropped otherwise`() { + sdk(SHOP) + assertEquals(SHOP, Rees46.handlePush(push(shop = null), PushEvent.DELIVERED)) + assertNull(Rees46.handlePush(push(shop = "unknown"), PushEvent.DELIVERED)) + assertEquals("unknown_shop", logged.last { it.code == LogCodes.PUSH_DROPPED }.context["reason"]) + + sdk(OTHER) + assertEquals(OTHER, Rees46.handlePush(push(code = "x", shop = OTHER), PushEvent.DELIVERED)) + assertNull(Rees46.handlePush(push(code = "y", shop = null), PushEvent.DELIVERED)) + assertEquals("ambiguous_shop", logged.last { it.code == LogCodes.PUSH_DROPPED }.context["reason"]) + + // Not the SDK's: no id or no type. + assertNull(Rees46.handlePush(mapOf("shop_id" to SHOP, "type" to "bulk"), PushEvent.DELIVERED)) + val notOurs = logged.last { it.code == LogCodes.PUSH_DROPPED } + assertEquals("not_sdk_push", notOurs.context["reason"]) + assertEquals(LogLevel.DEBUG, notOurs.level) + } + + @Test + fun `a shop registered but not created takes its push with its stored did`() { + sdk(SHOP).release() + requests.clear() + Rees46.register(context, listOf(config(SHOP))) + assertFalse(Rees46.isInitialized(SHOP)) + + assertEquals(SHOP, Rees46.handlePush(push(code = "cold"), PushEvent.DELIVERED)) + val (path, body) = next("track/delivered") ?: throw AssertionError("no track/delivered") + assertEquals("track/delivered", path) + assertEquals("D1", body.getString("did")) + } + + // Callbacks. + + @Test + fun `a shop's own callback wins over the facade's for its event, on the main thread, and one that throws is logged`() { + PushHub.main = Executor { task -> android.os.Handler(Looper.getMainLooper()).post(task) } + val sdk = sdk() + val heard = CopyOnWriteArrayList() + val onMain = CopyOnWriteArrayList() + fun note(name: String, payload: PushPayload) { + heard += "$name:${payload.shopId}:${payload.code}" + onMain += Looper.myLooper() == Looper.getMainLooper() + } + Rees46.setPushCallbacks(PushCallbacks( + onDelivered = { note("facade-delivered", it) }, + onClicked = { note("facade-clicked", it) }, + onClosed = { throw IllegalStateException("the app's callback") }, + )) + sdk.setPushCallbacks(PushCallbacks(onClicked = { note("shop-clicked", it) })) + + Rees46.handlePush(push(code = "1", shop = null), PushEvent.DELIVERED) + Rees46.handlePush(push(code = "1"), PushEvent.CLICKED) + Rees46.handlePush(push(code = "1"), PushEvent.CLOSED) + shadowOf(Looper.getMainLooper()).idle() + + assertEquals(listOf("facade-delivered:$SHOP:1", "shop-clicked:$SHOP:1"), heard) + assertTrue(onMain.all { it }) + assertTrue(logged.any { it.code == LogCodes.CALLBACK_FAILED }) + } + + @Test + fun `the old message listeners keep hearing deliveries beside the callbacks`() { + val sdk = sdk() + val global = CopyOnWriteArrayList() + val own = CopyOnWriteArrayList() + val delivered = CopyOnWriteArrayList() + Rees46.setOnMessageListener { shopId, data -> global += "$shopId:${data.id}" } + @Suppress("DEPRECATION") + sdk.setOnMessageListener { own += it } + Rees46.setPushCallbacks(PushCallbacks(onDelivered = { delivered += it.code })) + + Rees46.handlePush(push(code = "1"), PushEvent.DELIVERED) + @Suppress("DEPRECATION") + Rees46.handlePush(push(code = "2"), PushEventType.RECEIVED) + + assertEquals(listOf("$SHOP:1", "$SHOP:2"), global) + assertEquals(listOf("1", "2"), own.map { it.id }) + assertEquals(listOf("1", "2"), delivered) + } + + // The legacy core. + + @Test + fun `the legacy core sends a delivery to track received, as it did, and the new events directly`() { + sdk(coreV2 = false) + @Suppress("DEPRECATION") + Rees46.handlePush(push(code = "old"), PushEventType.RECEIVED) + assertEquals("old", next("track/received", "track/delivered")?.takeIf { it.first == "track/received" }?.second?.getString("code")) + + assertEquals(SHOP, Rees46.handlePush(push(code = "new"), PushEvent.DELIVERED)) + assertEquals("track/received", next("track/received", "track/delivered")?.first) + Rees46.handlePush(push(code = "new"), PushEvent.OPENED) + assertEquals("track/opened", next("track/opened")?.first) + // No once-per-process rule on the legacy core. + Rees46.handlePush(push(code = "new"), PushEvent.OPENED) + assertEquals("track/opened", next("track/opened")?.first) + assertNull(next("track/delivered", timeoutMs = 300)) + } + + // The token. + + @Test + fun `on a new device the token waits for the did of init, goes once, and onToken hears it once`() { + val hold = java.util.concurrent.CountDownLatch(1).also { holdInit = it } + val heardTokens = CopyOnWriteArrayList() + Rees46.setPushCallbacks(PushCallbacks(onToken = { heardTokens += it.token })) + val sdk = Rees46.initialize(context, config().copy(autoSendPushToken = true)) + val answered = LinkedBlockingQueue() + + SDK.onPushTokenReceived("tok", PushProvider.FCM) + SDK.onPushTokenReceived("tok", PushProvider.FCM) + sdk.setPushToken("tok", PushProvider.FCM, object : com.personalization.api.OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) { + answered += "success" + } + + override fun onError(code: Int, msg: String?) { + answered += "error:$code" + } + }) + assertNull("no token without a did", next("mobile_push_tokens", timeoutMs = 300)) + + hold.countDown() + val body = next("mobile_push_tokens")?.second ?: throw AssertionError("no token") + assertEquals("D1", body.getString("did")) + assertEquals("tok", body.getString("token")) + assertEquals("firebase", body.getString("push_provider")) + assertEquals("android", body.getString("platform")) + assertEquals("success", answered.poll(5, TimeUnit.SECONDS)) + assertNull("once", next("mobile_push_tokens", timeoutMs = 300)) + assertEquals("tok", sdk.getPushToken(PushProvider.FCM)) + assertEquals(listOf("tok"), heardTokens) + } + + // A notification the SDK draws. + + @Test + fun `a notification the SDK draws is reported as opened, and its dismissal as closed`() { + val sdk = sdk() + val data = NotificationData(id = "drawn", title = "T", body = "B", icon = null, type = "bulk", actions = null, actionUrls = null, image = null, event = null) + sdk.notificationHelper.createNotification(context, data, images = null) + flush(sdk) + val opened = next("track/opened")?.second ?: throw AssertionError("no track/opened") + assertEquals("drawn", opened.getString("code")) + + val manager = context.getSystemService(android.app.NotificationManager::class.java) + val notification = shadowOf(manager).allNotifications.single() + val dismiss = shadowOf(notification.deleteIntent).savedIntent + com.personalization.push.PushDismissReceiver().onReceive(context, dismiss) + flush(sdk) + val closed = next("track/closed")?.second ?: throw AssertionError("no track/closed") + assertEquals("drawn", closed.getString("code")) + assertEquals(SHOP, closed.getString("shop_id")) + } + + private companion object { + const val SHOP = "push-shop" + const val OTHER = "push-other" + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/PushPayloadTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/PushPayloadTest.kt new file mode 100644 index 00000000..e29ef4e3 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/PushPayloadTest.kt @@ -0,0 +1,70 @@ +package com.personalization.core + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** A push of FCM or HMS parsed as contract/notifications.md has it: every value a string, JSON in text. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class PushPayloadTest { + + @Test + fun `a push of the SDK with its buttons, its target and its sound`() { + val data = mapOf( + "shop_id" to "shop-1", + "id" to "m-1", + "type" to "chain", + "title" to "Title", + "body" to "Body", + "icon" to "https://cdn/icon.png", + "image" to "https://cdn/1.png,https://cdn/2.png", + "actions" to """[{"action":"buy","title":"Buy"},{"action":"later","title":"Later"}]""", + "action_urls" to """["https://shop/buy"]""", + "event" to """{"type":"product","uri":"37","payload":{"k":"v"}}""", + "source" to "mailing", + "android_sound_file" to "chime", + ) + + val payload = PushPayload.parse(data) ?: throw AssertionError("not parsed") + + assertEquals("shop-1", payload.shopId) + assertEquals("m-1", payload.code) + assertEquals("chain", payload.type) + assertEquals(listOf("buy" to "https://shop/buy", "later" to null), payload.actions.map { it.id to it.url }) + assertEquals(PushTargetType.PRODUCT, payload.target?.type) + assertEquals("37", payload.target?.uri) + assertEquals(mapOf("k" to "v"), payload.target?.payload) + assertEquals("mailing", payload.source) + assertEquals("chime", payload.sound) + assertEquals(data, payload.data) + // Its content stays out of logs. + assertTrue(!payload.toString().contains("Title")) + } + + @Test + fun `an unknown target type comes as it is, broken JSON leaves its field empty, and a tap's extras are read`() { + val payload = PushPayload.parse(mapOf( + "NOTIFICATION_ID" to "m-2", + "NOTIFICATION_TYPE" to "bulk", + "actions" to "not json", + "event" to """{"type":"story","uri":"7"}""", + )) ?: throw AssertionError("not parsed") + + assertEquals("m-2", payload.code) + assertNull(payload.shopId) + assertTrue(payload.actions.isEmpty()) + assertEquals("story", payload.target?.type?.value) + } + + @Test + fun `without an id or a type it is not a push of the SDK`() { + assertNull(PushPayload.parse(mapOf("type" to "bulk"))) + assertNull(PushPayload.parse(mapOf("id" to "m-3"))) + assertNull(PushPayload.parse(mapOf("id" to " ", "type" to "bulk"))) + } +} diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CorePushTokensTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CorePushTokensTest.kt new file mode 100644 index 00000000..b95d8966 --- /dev/null +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CorePushTokensTest.kt @@ -0,0 +1,263 @@ +package com.personalization.core.internal + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import com.personalization.LogLevel +import com.personalization.PushProvider +import com.personalization.SdkLogHandler +import com.personalization.SdkLogMessage +import com.personalization.api.OnApiCallbackListener +import com.personalization.core.SdkError +import com.personalization.logs.LogCodes +import com.personalization.logs.SdkLogs +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestCoroutineScheduler +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.Executor + +/** + * The push token on the new core (`core/push.md`): one per device, sent by each shop on its own week, a + * new one at once, only once the instance is ready and has a did, the week taken before the request and + * given back when it fails, and `push_provider` for FCM and HMS. + */ +@OptIn(ExperimentalCoroutinesApi::class) +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class CorePushTokensTest { + + private lateinit var context: Context + private val scheduler = TestCoroutineScheduler() + private val dispatchers = testCoreDispatchers(scheduler) + private val clock = FakeClock() + private val logged = CopyOnWriteArrayList() + private val defaultExecutor = SdkLogs.handlerExecutor + + /** What went, by shop, and the listeners waiting for an answer the test gives. */ + private val sent = mutableListOf>() + private val answers = mutableListOf() + + /** Whether the instance is ready; until it is, [ready] waits. */ + private var readiness = CompletableDeferred() + private var did = "D1" + + @Before + fun setUp() { + context = ApplicationProvider.getApplicationContext() + listOf(DeviceStorage.NAME, partition(SHOP), partition(OTHER)).forEach { + context.getSharedPreferences(it, Context.MODE_PRIVATE).edit().clear().commit() + } + SdkLogs.reset() + SdkLogs.handlerExecutor = Executor { it.run() } + SdkLogs.register(SHOP, LogLevel.DEBUG, "SDK") + SdkLogs.register(OTHER, LogLevel.DEBUG, "SDK") + SdkLogs.handler = SdkLogHandler { logged += it } + readiness = CompletableDeferred().apply { complete(null) } + } + + @After + fun tearDown() { + SdkLogs.reset() + SdkLogs.handlerExecutor = defaultExecutor + } + + private fun partition(shop: String) = StorageLayout(shop, null).partitionName + + private fun tokens(shop: String = SHOP, autoSend: Boolean = true, sends: Boolean = true) = CorePushTokens( + shopId = shop, + device = DeviceStorage(context), + shop = context.getSharedPreferences(partition(shop), Context.MODE_PRIVATE), + send = { params, listener -> + sent += shop to params + answers += listener + }, + ready = { readiness.await() }, + hasDid = { did.isNotEmpty() }, + autoSend = autoSend, + sends = sends, + dispatchers = dispatchers, + clock = clock, + ) + + private fun run() = scheduler.runCurrent() + + private fun ok() { + answers.removeAt(0).onSuccess(JSONObject().put("status", "success")) + run() + } + + private fun fail(code: Int) { + answers.removeAt(0).onError(code, null) + run() + } + + private class Heard : OnApiCallbackListener() { + val calls = CopyOnWriteArrayList() + override fun onSuccess(response: JSONObject?) { + calls += "success" + } + + override fun onError(code: Int, msg: String?) { + calls += "error:$code" + } + } + + private fun reasons() = logged.filter { it.code == LogCodes.PUSH_TOKEN_SKIPPED }.map { it.context["reason"] } + + @Test + fun `a new token goes at once with its provider, the same one at most once a week`() { + val tokens = tokens() + tokens.received("t1", PushProvider.FCM) + run() + ok() + assertEquals(JSONObject().put("platform", "android").put("token", "t1").put("push_provider", "firebase").toString(), sent.single().second.toString()) + + tokens.received("t1", PushProvider.FCM) + run() + assertEquals("the same token this week", 1, sent.size) + assertEquals(listOf(CorePushTokens.REASON_SENT_THIS_WEEK), reasons()) + + tokens.received("t2", PushProvider.FCM) + run() + ok() + assertEquals("a new token at once", 2, sent.size) + + clock.advance(CorePushTokens.WEEK_MS) + tokens.sendStored() + run() + ok() + assertEquals("a week later", listOf("t1", "t2", "t2"), sent.map { it.second.getString("token") }) + + tokens.received("h1", PushProvider.HMS) + run() + assertEquals("huawei", sent.last().second.getString("push_provider")) + assertTrue(logged.any { it.code == LogCodes.PUSH_TOKEN_SENT }) + } + + @Test + fun `the token is the device's and each shop sends it on its own week`() { + val shop = tokens(SHOP) + val other = tokens(OTHER) + shop.received("t1", PushProvider.FCM) + run() + ok() + + assertEquals("t1", DeviceStorage(context).pushToken("fcm")) + assertEquals("t1", other.token(PushProvider.FCM)) + other.sendStored() + run() + ok() + shop.sendStored() + other.sendStored() + run() + + assertEquals(listOf(SHOP, OTHER), sent.map { it.first }) + } + + @Test + fun `nothing goes before the instance is ready, nor without a did, nor from a push-only instance`() { + readiness = CompletableDeferred() + val tokens = tokens() + tokens.received("t1", PushProvider.FCM) + run() + assertTrue("waits for ready", sent.isEmpty()) + readiness.complete(null) + run() + assertEquals(1, sent.size) + + did = "" + tokens(OTHER).sendStored() + run() + assertEquals(1, sent.size) + + did = "D1" + tokens(OTHER, sends = false).sendStored() + run() + assertEquals(1, sent.size) + assertEquals(listOf(CorePushTokens.REASON_NO_DID, CorePushTokens.REASON_PUSH_ONLY), reasons()) + } + + @Test + fun `two sends of one token at once make one request, and both hear its answer`() { + val tokens = tokens() + val first = Heard() + val second = Heard() + tokens.set("t1", PushProvider.FCM, first) + tokens.set("t1", PushProvider.FCM, second) + tokens.received("t1", PushProvider.FCM) + run() + + assertEquals(1, sent.size) + ok() + assertEquals(listOf("success"), first.calls) + assertEquals(listOf("success"), second.calls) + } + + @Test + fun `a failed send gives the week back, so the token goes again`() { + val tokens = tokens() + val heard = Heard() + tokens.set("t1", PushProvider.FCM, heard) + run() + fail(500) + assertEquals(listOf("error:500"), heard.calls) + assertTrue(logged.any { it.code == LogCodes.PUSH_TOKEN_FAILED }) + + tokens.sendStored() + run() + assertEquals(2, sent.size) + } + + @Test + fun `setPushToken waits for ready, says success without a request inside the week, and refuses an empty token or a refused shop`() { + val tokens = tokens() + val first = Heard() + tokens.set("t1", PushProvider.HMS, first) + run() + ok() + val again = Heard() + tokens.set("t1", PushProvider.HMS, again) + run() + assertEquals(1, sent.size) + assertEquals(listOf("success"), again.calls) + + val empty = Heard() + tokens.set("", PushProvider.FCM, empty) + assertEquals(listOf("error:-1"), empty.calls) + + readiness = CompletableDeferred().apply { complete(SdkError.ShopBlocked) } + val blocked = Heard() + tokens.set("t9", PushProvider.FCM, blocked) + run() + assertEquals(listOf("error:402"), blocked.calls) + assertEquals("kept for the device all the same", "t9", tokens.token(PushProvider.FCM)) + } + + @Test + fun `a shop that does not send tokens by itself keeps none from FCM, and sends the one the app passes`() { + val tokens = tokens(autoSend = false) + tokens.received("from-fcm", PushProvider.FCM) + run() + assertNull(DeviceStorage(context).pushToken("fcm")) + assertTrue(sent.isEmpty()) + + tokens.set("from-app", PushProvider.FCM, null) + run() + assertEquals("from-app", sent.single().second.getString("token")) + } + + private companion object { + const val SHOP = "token-shop" + const val OTHER = "token-other" + } +} From 359dbd3ee60f5eb4c89be63ee28e9e279a1eaa27 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:23:30 +0400 Subject: [PATCH 052/109] feat(core): an API for the Flutter and React Native bridges (stage 7), behind the opt-in marker BridgeApi. SDK.onSessionChange hears every new sid of the new core; Rees46.onPushEvent and Rees46.onPushToken hear every push event and every new token the SDK sees, once, on the main thread, beside the app's callbacks; BridgeErrors gives the whole SdkError (type, HTTP code, serverCode) behind any old onError(code, message) while it runs, rebuilds one from the code on the legacy core, and makes old-style listeners that report SdkResult; SDK.queueEvent puts a tracking event through the queue and completes once it is written, with invalidArgument for a path the queue does not take, notInitialized and shopBlocked, and on the legacy core with the server's answer. The new core's failures carry their SdkError to the old listeners for this: a dropped or expired event, a cleared queue, a refused argument. The instance's state needs nothing new: onStateChange tells the current state, then every change. --- .../main/kotlin/com/personalization/Rees46.kt | 19 +++++ .../main/kotlin/com/personalization/SDK.kt | 48 ++++++++++-- .../com/personalization/core/BridgeApi.kt | 39 ++++++++++ .../com/personalization/core/BridgeErrors.kt | 50 +++++++++++++ .../core/BridgePushListener.kt | 10 +++ .../core/internal/CorePushTokens.kt | 4 +- .../personalization/core/internal/CoreV2.kt | 18 ++++- .../core/internal/CurrentError.kt | 26 +++++++ .../core/internal/EventQueue.kt | 6 +- .../core/internal/LegacyCore.kt | 2 +- .../core/internal/LegacyDelivery.kt | 13 +++- .../core/internal/LegacyErrors.kt | 13 ++-- .../personalization/core/internal/PushHub.kt | 46 ++++++++++-- .../features/tracking/impl/TrackingApiImpl.kt | 3 +- .../com/personalization/PushEventsTest.kt | 75 +++++++++++++++++++ .../com/personalization/SdkInstanceTest.kt | 2 + 16 files changed, 345 insertions(+), 29 deletions(-) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/BridgeApi.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/BridgeErrors.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/BridgePushListener.kt create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/CurrentError.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt index c66a536a..f71917f5 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt @@ -5,9 +5,12 @@ import com.personalization.logs.LogReasons import com.personalization.logs.SdkLogs import android.content.Context import android.os.Bundle +import com.personalization.core.BridgeApi +import com.personalization.core.BridgePushListener import com.personalization.core.PushCallbacks import com.personalization.core.PushEvent import com.personalization.core.PushPayload +import com.personalization.core.PushTokenListener import com.personalization.core.internal.CoreSelection import com.personalization.core.internal.PushHub import com.personalization.features.inAppNotification.impl.ForegroundActivity @@ -337,6 +340,22 @@ object Rees46 { PushHub.callbacks = callbacks } + /** + * For the bridges: [listener] hears every push event the SDK handles, of every shop, on the main + * thread, beside the app's [PushCallbacks] and whatever they are. Once per event of a message, like + * them. Until the handle is cancelled. + */ + @BridgeApi + fun onPushEvent(listener: BridgePushListener): Cancellable = PushHub.addEventSink(listener) + + /** + * For the bridges: [listener] hears every new push token the SDK gets from FCM or HMS, once per token + * and provider in a process, on the main thread, beside the app's `onToken`. Until the handle is + * cancelled. + */ + @BridgeApi + fun onPushToken(listener: PushTokenListener): Cancellable = PushHub.addTokenSink(listener) + /** * Resolves the push target as ([InstanceResolver.Resolution] to shopId), or null when the push * should be dropped. Mirrors [instance] resolution (live wins over pending; a single registered diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 99cea347..c4a28fcf 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -27,6 +27,8 @@ import com.personalization.api.managers.SearchManager import com.personalization.api.managers.TrackEventManager import com.personalization.api.managers.TrackingApi import com.personalization.api.params.ProfileParams +import com.personalization.core.BridgeApi +import com.personalization.core.BridgeErrors import com.personalization.core.CompletionListener import com.personalization.core.PushCallbacks import com.personalization.core.PushEvent @@ -112,7 +114,7 @@ private const val GAID_KEY = "google_advertising_id" * [deleteUserCredentials] — may clash with members of the same name in such a subclass: a subclass * that declares one of them needs to rename its own. */ -@OptIn(InternalPersonalizationV3Api::class) +@OptIn(InternalPersonalizationV3Api::class, BridgeApi::class) open class SDK { internal lateinit var context: Context @@ -286,11 +288,12 @@ open class SDK { private val sessionListeners = CopyOnWriteArrayList<(String) -> Unit>() /** - * For the bridges (stage 7): [listener] hears every new `sid` of the new core, on the main thread, - * until the handle is cancelled — instead of asking [sessionId] on a timer. The legacy core does not - * call it. May be set before the instance is initialized. + * For the bridges: [listener] hears every new `sid` of the new core, on the main thread, until the + * handle is cancelled — instead of asking [sessionId] on a timer. The legacy core does not call it. + * May be set before the instance is initialized. An exception it throws is logged as `callback_failed`. */ - internal fun onSessionChange(listener: (sessionId: String) -> Unit): Cancellable { + @BridgeApi + fun onSessionChange(listener: (sessionId: String) -> Unit): Cancellable { sessionListeners += listener return Cancellable { sessionListeners -= listener } } @@ -1838,6 +1841,41 @@ open class SDK { log(LogLevel.DEBUG, LogCodes.PUSH_DROPPED, "$message: not a push of the SDK", mapOf("reason" to LogReasons.NOT_SDK_PUSH, "keys" to keys)) } + /** + * For the bridges: [body] as an event of [path] through the queue, as the tracking methods send it — + * `push`, `push/custom`, `track/stories`, `track/slider`, `popup/showed` and the push events + * `track/delivered`, `track/opened`, `track/clicked`, `track/closed`. The queue adds the common fields + * and, on `/push`, the stored source when the event goes. + * + * [listener] hears once, on the main thread: [SdkResult.Success] once the event is accepted — written + * to disk, the way the contract's tracking methods complete — or [SdkResult.Failure] with + * [SdkError.InvalidArgument] for another path, [SdkError.NotInitialized], or [SdkError.ShopBlocked]. On + * the legacy core, which has no queue, the event goes at once and [listener] hears the server's answer. + */ + @BridgeApi + fun queueEvent(path: String, body: JSONObject, listener: CompletionListener) { + val core = core + val error = when { + path !in CoreV2.EVENT_PATHS -> SdkError.InvalidArgument("$path is not an event of the queue") + core == null || released -> SdkError.NotInitialized + else -> null + } + if (error != null || core == null) { + calls.launch { complete(listener, SdkResult.Failure(error ?: SdkError.NotInitialized)) } + return + } + val copy = JSONObject(body.toString()) + if (core is CoreV2) { + core.accept(path, copy) { refused -> + calls.launch { complete(listener, if (refused == null) SdkResult.Success(Unit) else SdkResult.Failure(refused)) } + } + return + } + sendNetworkMethodUseCase.postAsync(path, copy, BridgeErrors.listener { result -> + calls.launch { complete(listener, if (result is SdkResult.Failure) result else SdkResult.Success(Unit)) } + }) + } + companion object { var TAG = "SDK" diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/BridgeApi.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/BridgeApi.kt new file mode 100644 index 00000000..914be0f6 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/BridgeApi.kt @@ -0,0 +1,39 @@ +package com.personalization.core + +/** + * Members for the Flutter and React Native bridges, not for apps: the bridges forward the native SDK's + * events and errors to Dart and JavaScript without losing anything, and apps get the same things + * through the public API (`onStateChange`, `PushCallbacks`, the old listeners). + * + * What it marks may change in any release, without a deprecation. A bridge opts in once per module: + * + * ```groovy + * kotlinOptions { + * freeCompilerArgs += ['-opt-in=com.personalization.core.BridgeApi'] + * } + * ``` + * + * The members: + * - `SDK.onSessionChange` — every new `sid` of the new core; + * - `Rees46.onPushEvent` and `Rees46.onPushToken` — every push event and token the SDK sees, beside the + * app's `PushCallbacks`; + * - `BridgeErrors` — the whole [SdkError] behind an old `onError(code, message)`, and listeners that + * report [SdkResult]; + * - `SDK.queueEvent` — a tracking event through the queue, completed once it is accepted. + * + * The instance's state needs nothing of its own: `SDK.onStateChange` tells the current state at once, + * then every change. + */ +@RequiresOptIn( + level = RequiresOptIn.Level.ERROR, + message = "For the SDK's Flutter and React Native bridges, not for apps; it may change in any release. " + + "Opt in with @OptIn(BridgeApi::class) or -opt-in=com.personalization.core.BridgeApi.", +) +@Retention(AnnotationRetention.BINARY) +@Target( + AnnotationTarget.CLASS, + AnnotationTarget.FUNCTION, + AnnotationTarget.PROPERTY, + AnnotationTarget.TYPEALIAS, +) +annotation class BridgeApi diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/BridgeErrors.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/BridgeErrors.kt new file mode 100644 index 00000000..fb903bb9 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/BridgeErrors.kt @@ -0,0 +1,50 @@ +package com.personalization.core + +import com.personalization.api.OnApiCallbackListener +import com.personalization.core.internal.CurrentError +import org.json.JSONArray +import org.json.JSONObject + +/** + * For the bridges: the whole [SdkError] — its type, HTTP code and `serverCode` — behind an old + * `onError(code, message)`, so a bridge hands Dart and JavaScript the error the core had, not a code. + * + * Every old method keeps calling `onError(code, message)`; while the SDK calls it, [currentError] is the + * error behind it, on that thread. It reaches a bridge's listener through the listeners the SDK's managers + * wrap around it, which forward on the same thread. On the legacy core, which has no [SdkError], it is + * null and [errorOf] rebuilds one from the code. + */ +@BridgeApi +object BridgeErrors { + + /** The error behind the `onError` the SDK is calling on this thread; null outside one or on the legacy core. */ + @JvmStatic + fun currentError(): SdkError? = CurrentError.get() + + /** + * The error of an `onError(code, message)` the SDK is calling: [currentError] when the core knows it, + * otherwise one made from [code] — [SdkError.Http] for an HTTP status, [SdkError.Network] for `-1`. + */ + @JvmStatic + fun errorOf(code: Int, message: String?): SdkError = currentError() ?: when (code) { + in HTTP_STATUSES -> SdkError.Http(code) + else -> SdkError.Network + } + + /** + * An old-style listener that reports the whole result to [onResult], once, on the thread the SDK + * answers on: [SdkResult.Success] with the answer (a [JSONObject], a [JSONArray] or null), or + * [SdkResult.Failure] with [errorOf] the failure. Pass it to any method that takes an + * [OnApiCallbackListener]. + */ + @JvmStatic + fun listener(onResult: (SdkResult) -> Unit): OnApiCallbackListener = object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) = onResult(SdkResult.Success(response)) + + override fun onSuccess(response: JSONArray) = onResult(SdkResult.Success(response)) + + override fun onError(code: Int, msg: String?) = onResult(SdkResult.Failure(errorOf(code, msg))) + } + + private val HTTP_STATUSES = 100..599 +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/BridgePushListener.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/BridgePushListener.kt new file mode 100644 index 00000000..cbd96a90 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/BridgePushListener.kt @@ -0,0 +1,10 @@ +package com.personalization.core + +/** + * For the bridges: every push event the SDK sees, of every shop, beside the app's [PushCallbacks] — + * `Rees46.onPushEvent`. [PushPayload.shopId] is the shop the push went to. + */ +@BridgeApi +fun interface BridgePushListener { + fun onPushEvent(event: PushEvent, payload: PushPayload) +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt index 68c41bc9..c07698b7 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt @@ -156,9 +156,11 @@ internal class CorePushTokens( } override fun onError(code: Int, msg: String?) { + // The whole error, while this runs on the funnel's thread. + val error = CurrentError.get() landed(failed = true) { waiting -> log(LogLevel.WARNING, LogCodes.PUSH_TOKEN_FAILED, "Push token was not sent", mapOf("provider" to provider.id, "status" to code)) - waiting.forEach { delivery.failure(it, code, msg) } + waiting.forEach { delivery.failure(it, code, msg, error) } } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt index 2b9dc0ea..f65f39ed 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt @@ -98,6 +98,20 @@ internal class CoreV2( sendNow(method, params, answer) } + /** + * Takes an event of [path] for the queue without an answer listener: [accepted] hears, once, null when + * it is written to disk (or, when the disk refused it, sent once as a request), otherwise why it was + * refused — `notInitialized` after [release], `shopBlocked` for a refused shop. + */ + fun accept(path: String, params: JSONObject, accepted: (SdkError?) -> Unit) { + val queue = queue + if (released || queue == null) { + endings.launch { accepted(SdkError.NotInitialized) } + return + } + queue.accept(path, params, listener = null, accepted = accepted) + } + /** Sends an event the queue's disk would not take, once, as a request. */ internal fun sendDirectly(path: String, params: JSONObject, listener: OnApiCallbackListener?) { val answered = AtomicBoolean(false) @@ -190,11 +204,11 @@ internal class CoreV2( /** The paths whose events go through the queue. */ val EVENT_PATHS: Set get() = EventQueue.PATHS - private val CLEARED = LegacyError(LegacyErrors.NO_ANSWER, "The event was deleted with the customer's data") + private val CLEARED = LegacyError(LegacyErrors.NO_ANSWER, "The event was deleted with the customer's data", SdkError.Cancelled) } private fun deliver(listener: OnApiCallbackListener, result: HttpResult) = when (result) { is HttpResult.Success -> delivery.success(listener, result.body) - is HttpResult.Failure -> LegacyErrors.of(result.error, result.errorBody).let { delivery.failure(listener, it.code, it.message) } + is HttpResult.Failure -> delivery.failure(listener, LegacyErrors.of(result.error, result.errorBody)) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CurrentError.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CurrentError.kt new file mode 100644 index 00000000..a8c35416 --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CurrentError.kt @@ -0,0 +1,26 @@ +package com.personalization.core.internal + +import com.personalization.core.SdkError + +/** + * The [SdkError] behind the old `onError(code, message)` running on this thread: set by [LegacyDelivery] + * around the call, so it reaches a bridge's listener through the listeners the SDK wraps around it, + * which forward on the same thread. + */ +internal object CurrentError { + + private val local = ThreadLocal() + + fun get(): SdkError? = local.get() + + /** Runs [block] with [error] as the thread's error; the one before comes back after it. */ + fun with(error: SdkError?, block: () -> T): T { + val previous = local.get() + local.set(error) + try { + return block() + } finally { + local.set(previous) + } + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt index b00c8bb3..31320d94 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt @@ -660,8 +660,8 @@ internal class EventQueue( private val REPEATED_4XX = setOf(408, 410, 417, 426, 429) private val RETRY_AFTER_CODES = setOf(429, 503) - private val EXPIRED = LegacyError(LegacyErrors.NO_ANSWER, "The event expired in the queue") - private val OVERFLOWED = LegacyError(LegacyErrors.NO_ANSWER, "The event was dropped: the queue was full") - private val UNREADABLE = LegacyError(LegacyErrors.NO_ANSWER, "The event could not be read from the disk") + private val EXPIRED = LegacyError(LegacyErrors.NO_ANSWER, "The event expired in the queue", SdkError.Timeout) + private val OVERFLOWED = LegacyError(LegacyErrors.NO_ANSWER, "The event was dropped: the queue was full", SdkError.Cancelled) + private val UNREADABLE = LegacyError(LegacyErrors.NO_ANSWER, "The event could not be read from the disk", SdkError.Decoding) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt index 4bb4562a..0337fb5d 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyCore.kt @@ -52,7 +52,7 @@ internal class LegacyCore( // Off the caller's thread, as every answer of this path. if (listener != null) { val error = LegacyErrors.of(SdkError.NotInitialized) - threads.execute { delivery.failure(listener, error.code, error.message) } + threads.execute { delivery.failure(listener, error) } } return } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyDelivery.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyDelivery.kt index 72248437..224f6bdb 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyDelivery.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyDelivery.kt @@ -2,6 +2,7 @@ package com.personalization.core.internal import com.personalization.LogLevel import com.personalization.api.OnApiCallbackListener +import com.personalization.core.SdkError import com.personalization.logs.LogCodes import org.json.JSONArray import org.json.JSONObject @@ -24,17 +25,21 @@ internal class LegacyDelivery( } } catch (e: Exception) { log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "onSuccess of a request threw", e) - failure(listener, LegacyErrors.NO_ANSWER, e.message) + failure(listener, LegacyErrors.NO_ANSWER, e.message, SdkError.Decoding) } } - fun failure(listener: OnApiCallbackListener, code: Int, message: String?) { + /** + * Calls `onError(code, message)`; while it runs, [error] is the thread's [CurrentError], so a bridge's + * listener — or a listener the SDK wrapped around it and forwards from — can read the whole error. + */ + fun failure(listener: OnApiCallbackListener, code: Int, message: String?, error: SdkError? = null) { try { - listener.onError(code, message) + CurrentError.with(error) { listener.onError(code, message) } } catch (e: Exception) { log(LogLevel.ERROR, LogCodes.CALLBACK_FAILED, "onError of a request threw", e) } } - fun failure(listener: OnApiCallbackListener, error: LegacyError) = failure(listener, error.code, error.message) + fun failure(listener: OnApiCallbackListener, error: LegacyError) = failure(listener, error.code, error.message, error.error) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyErrors.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyErrors.kt index eee7837b..9e90d056 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyErrors.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/LegacyErrors.kt @@ -2,8 +2,11 @@ package com.personalization.core.internal import com.personalization.core.SdkError -/** What an old `OnApiCallbackListener.onError(code, msg)` receives. */ -internal data class LegacyError(val code: Int, val message: String?) +/** + * What an old `OnApiCallbackListener.onError(code, msg)` receives, and the [error] behind it when the + * new core knows it: the bridges read it while the callback runs (`BridgeErrors.currentError`). + */ +internal data class LegacyError(val code: Int, val message: String?, val error: SdkError? = null) /** * The one mapping from [SdkError] to the `onError(Int, String?)` of the old methods, so they keep the @@ -23,13 +26,13 @@ internal object LegacyErrors { * apps may show the server's text from it. It is never written to the log. */ fun of(error: SdkError, errorBody: String? = null): LegacyError = when (error) { - is SdkError.Http -> LegacyError(error.code, errorBody ?: error.message) - SdkError.ShopBlocked -> LegacyError(SHOP_BLOCKED, error.message) + is SdkError.Http -> LegacyError(error.code, errorBody ?: error.message, error) + SdkError.ShopBlocked -> LegacyError(SHOP_BLOCKED, error.message, error) SdkError.Network, SdkError.Timeout, SdkError.Decoding, SdkError.NotInitialized, SdkError.Cancelled, - is SdkError.InvalidArgument -> LegacyError(NO_ANSWER, error.message) + is SdkError.InvalidArgument -> LegacyError(NO_ANSWER, error.message, error) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt index e6de96ca..97c380ee 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt @@ -2,30 +2,39 @@ package com.personalization.core.internal import android.os.Handler import android.os.Looper +import com.personalization.Cancellable import com.personalization.LogLevel import com.personalization.PushProvider +import com.personalization.core.BridgeApi +import com.personalization.core.BridgePushListener import com.personalization.core.PushCallbacks import com.personalization.core.PushEvent import com.personalization.core.PushListener import com.personalization.core.PushPayload import com.personalization.core.PushToken +import com.personalization.core.PushTokenListener import com.personalization.logs.LogCodes import com.personalization.logs.SdkLogs import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.Executor /** - * What the process knows of pushes beside the instances: the facade's [PushCallbacks], which events of - * which messages went already, and which tokens were reported. + * What the process knows of pushes beside the instances: the facade's [PushCallbacks], the bridges' + * listeners, which events of which messages went already, and which tokens were reported. * * Callbacks run on the main thread ([main]); one that throws is logged as `callback_failed`. */ +@OptIn(BridgeApi::class) internal object PushHub { /** The facade's callbacks (`Rees46.setPushCallbacks`). */ @Volatile var callbacks: PushCallbacks? = null + private val eventSinks = CopyOnWriteArrayList() + private val tokenSinks = CopyOnWriteArrayList() + /** Where callbacks run: the main thread. */ @Volatile internal var main: Executor = MainThread @@ -48,6 +57,9 @@ internal object PushHub { /** The last token of each provider the facade's `onToken` heard of. */ private val facadeReported = ConcurrentHashMap() + /** The last token of each provider the bridges heard of. */ + private val sinkReported = ConcurrentHashMap() + /** True the first time [event] of the message [code] comes for [shopId] in this process. */ fun firstTime(shopId: String, event: PushEvent, code: String): Boolean = synchronized(seen) { seen.put("$shopId|${event.name}|$code", true) == null @@ -55,30 +67,47 @@ internal object PushHub { /** * Tells the callbacks about [event]: the shop's own one for it ([own]) when set, otherwise the - * facade's. + * facade's; and every bridge listener. */ fun event(event: PushEvent, payload: PushPayload, own: PushCallbacks?) { - val listener = listenerOf(own, event) ?: listenerOf(callbacks, event) ?: return - run(payload.shopId) { call(payload.shopId, "A push callback threw") { listener.onPush(payload) } } + val listener = listenerOf(own, event) ?: listenerOf(callbacks, event) + val sinks = eventSinks.toList() + if (listener == null && sinks.isEmpty()) return + run(payload.shopId) { + listener?.let { call(payload.shopId, "A push callback threw") { it.onPush(payload) } } + sinks.forEach { sink -> call(payload.shopId, "A bridge's push listener threw") { sink.onPushEvent(event, payload) } } + } } /** * Tells the callbacks about a token an instance got from its provider: the instance's own `onToken` * ([own], which keeps track of what it heard through [ownReported]) or, without one, the facade's; - * the facade hears each token once per process. + * the facade and the bridges hear each token once per process. */ fun token(token: PushToken, shopId: String?, own: PushCallbacks?, ownReported: MutableMap) { val ownListener = own?.onToken val ownNew = ownListener != null && ownReported.put(token.provider, token.token) != token.token val facadeListener = if (ownListener == null) callbacks?.onToken else null val facade = facadeListener?.takeIf { facadeReported.put(token.provider, token.token) != token.token } - if (!ownNew && facade == null) return + val sinks = if (sinkReported.put(token.provider, token.token) != token.token) tokenSinks.toList() else emptyList() + if (!ownNew && facade == null && sinks.isEmpty()) return run(shopId) { if (ownNew) ownListener?.let { call(shopId, "A push token callback threw") { it.onToken(token) } } facade?.let { call(shopId, "A push token callback threw") { it.onToken(token) } } + sinks.forEach { sink -> call(shopId, "A bridge's token listener threw") { sink.onToken(token) } } } } + fun addEventSink(sink: BridgePushListener): Cancellable { + eventSinks += sink + return Cancellable { eventSinks -= sink } + } + + fun addTokenSink(sink: PushTokenListener): Cancellable { + tokenSinks += sink + return Cancellable { tokenSinks -= sink } + } + private fun listenerOf(callbacks: PushCallbacks?, event: PushEvent): PushListener? = when (event) { PushEvent.DELIVERED -> callbacks?.onDelivered PushEvent.OPENED -> callbacks?.onOpened @@ -105,8 +134,11 @@ internal object PushHub { /** Test-only: forgets everything. */ internal fun reset() { callbacks = null + eventSinks.clear() + tokenSinks.clear() synchronized(seen) { seen.clear() } facadeReported.clear() + sinkReported.clear() main = MainThread } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/tracking/impl/TrackingApiImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/tracking/impl/TrackingApiImpl.kt index 1c1e66b3..9fd2b6dd 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/tracking/impl/TrackingApiImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/tracking/impl/TrackingApiImpl.kt @@ -11,6 +11,7 @@ import com.personalization.api.models.tracking.TrackingItem import com.personalization.api.models.tracking.TrackingSource import com.personalization.api.models.tracking.putSource import com.personalization.api.params.ProductItemParams +import com.personalization.core.SdkError import com.personalization.core.internal.CoreDispatchers import com.personalization.core.internal.LegacyDelivery import com.personalization.core.internal.LegacyErrors @@ -51,7 +52,7 @@ internal class TrackingApiImpl @Inject constructor( if (value.isNotBlank() || !checksArguments()) return false val message = "$name is empty" SdkLogs.log(LogLevel.WARNING, LogCodes.INVALID_ARGUMENT, "A tracking call was refused: $message", null, mapOf("reason" to "empty_$name")) - listener?.let { refusals.launch { delivery.failure(it, LegacyErrors.NO_ANSWER, message) } } + listener?.let { refusals.launch { delivery.failure(it, LegacyErrors.NO_ANSWER, message, SdkError.InvalidArgument(message)) } } return true } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt index fa88e9c8..f6268307 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt @@ -407,6 +407,81 @@ class PushEventsTest { assertEquals(SHOP, closed.getString("shop_id")) } + // The bridges. + + @OptIn(com.personalization.core.BridgeApi::class) + @Test + fun `a bridge gets the whole error behind an old onError, and a result instead of a code`() { + val sdk = sdk() + val results = LinkedBlockingQueue>() + val current = LinkedBlockingQueue() + + sdk.getCurrentSegment(object : com.personalization.api.OnApiCallbackListener() { + override fun onError(code: Int, msg: String?) { + current += com.personalization.core.BridgeErrors.currentError() ?: "none" + } + }) + assertEquals(com.personalization.core.SdkError.Http(404), current.poll(5, TimeUnit.SECONDS)) + assertNull("only inside onError", com.personalization.core.BridgeErrors.currentError()) + + sdk.getCurrentSegment(com.personalization.core.BridgeErrors.listener { results += it }) + assertEquals(com.personalization.core.SdkResult.Failure(com.personalization.core.SdkError.Http(404)), results.poll(5, TimeUnit.SECONDS)) + + sdk.tracking.categoryView("", com.personalization.core.BridgeErrors.listener { results += it }) + assertEquals( + com.personalization.core.SdkResult.Failure(com.personalization.core.SdkError.InvalidArgument("categoryId is empty")), + results.poll(5, TimeUnit.SECONDS), + ) + + // Without the core's error (the legacy core), one made from the code. + assertEquals(com.personalization.core.SdkError.Http(500), com.personalization.core.BridgeErrors.errorOf(500, null)) + assertEquals(com.personalization.core.SdkError.Network, com.personalization.core.BridgeErrors.errorOf(-1, null)) + } + + @OptIn(com.personalization.core.BridgeApi::class) + @Test + fun `a bridge's event completes once it is accepted, and its push and token streams hear everything once`() { + // The completions are handed to the main thread: the test runs them on its own. + val main = com.personalization.core.internal.HeldMain() + val sdk = SDK().apply { dispatchers = com.personalization.core.internal.CoreDispatchers(main = { main }) } + sdk.initializeWith(context, config()) + runBlocking { withTimeout(5_000) { sdk.instanceState.changes.first { it == SdkState.READY } } } + val ends = LinkedBlockingQueue>() + val completions = com.personalization.core.CompletionListener { ends += it } + + sdk.queueEvent("push/custom", JSONObject().put("event", "from_dart"), completions) + assertEquals(com.personalization.core.SdkResult.Success(Unit), takeOnMain(main, ends)) + sdk.queueEvent("profile/set", JSONObject(), completions) + val refused = takeOnMain(main, ends) + assertTrue(refused is com.personalization.core.SdkResult.Failure && refused.error is com.personalization.core.SdkError.InvalidArgument) + flush(sdk) + assertEquals("from_dart", next("push/custom")?.second?.getString("event")) + + val events = CopyOnWriteArrayList() + val tokens = CopyOnWriteArrayList() + val pushes = Rees46.onPushEvent { event, payload -> events += "${event.name}:${payload.shopId}:${payload.code}" } + Rees46.onPushToken { tokens += it.token } + Rees46.handlePush(push(code = "b1"), PushEvent.DELIVERED) + Rees46.handlePush(push(code = "b1"), PushEvent.DELIVERED) + SDK.onPushTokenReceived("bridge-token", PushProvider.HMS) + SDK.onPushTokenReceived("bridge-token", PushProvider.HMS) + pushes.cancel() + Rees46.handlePush(push(code = "b2"), PushEvent.DELIVERED) + + assertEquals(listOf("DELIVERED:$SHOP:b1"), events) + assertEquals(listOf("bridge-token"), tokens) + + sdk.release() + sdk.queueEvent("push", JSONObject(), completions) + assertEquals(com.personalization.core.SdkResult.Failure(com.personalization.core.SdkError.NotInitialized), takeOnMain(main, ends)) + } + + /** The next of [queue], running what [main] was handed until it comes. */ + private fun takeOnMain(main: com.personalization.core.internal.HeldMain, queue: LinkedBlockingQueue): T? { + while (queue.isEmpty() && main.runNext()) Unit + return queue.poll() + } + private companion object { const val SHOP = "push-shop" const val OTHER = "push-other" diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt index d9075a86..4eb9066b 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt @@ -9,6 +9,7 @@ import com.personalization.api.OnApiCallbackListener import com.personalization.core.SdkError import com.personalization.core.SdkException import com.personalization.core.SdkState +import com.personalization.core.BridgeApi import com.personalization.core.internal.CoreDispatchers import com.personalization.core.internal.CoreSelection import com.personalization.core.internal.HeldMain @@ -456,6 +457,7 @@ class SdkInstanceTest { assertTrue(logged.any { it.code == LogCodes.DEVICE_ID_RESET && it.context["reason"] == "need_reinitialization" }) } + @OptIn(BridgeApi::class) @Test fun `on the new core the session is the server's and the bridges hear it on the main thread`() { val heard = CopyOnWriteArrayList>() From 9922bea20077a346752b4ba28dd017a231082921 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:28:37 +0400 Subject: [PATCH 053/109] fix(core): an ordinary pause between /init repeats ends when the network comes back, when the app comes to the foreground and when the shop is to be checked: the init machine follows the network itself, so a session that ended gets its /init as soon as there is a network. A pause a server's Retry-After set is waited in full whatever comes. --- .../personalization/core/internal/CoreInit.kt | 48 +++++++++++++++---- .../core/internal/CoreInitTest.kt | 44 +++++++++++++++++ .../core/internal/CoreSessionTest.kt | 12 +++++ 3 files changed, 94 insertions(+), 10 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt index 11d0ca9a..096688c4 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt @@ -16,6 +16,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel +import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.combine @@ -143,6 +144,13 @@ internal class CoreInit( private var successes = 0L private var lastCheckAt: Long? = null private var foreground: Cancellable? = null + private var network: Cancellable? = null + + /** + * Ends an ordinary pause between repeats: the network coming back, the foreground, [checkShop]. A + * server's `Retry-After` is waited in full all the same. + */ + private val wake = Channel(Channel.CONFLATED) /** How many `/init` went out; for tests. */ @Volatile @@ -181,10 +189,17 @@ internal class CoreInit( } if (ready) markUsable(generation()) val handle = lifecycle.addListener(::onForegroundChanged) - // A release that came meanwhile does not leave the observer behind. - val kept = synchronized(lock) { if (released) false else true.also { foreground = handle } } + val networkHandle = connectivity.addListener { connected -> if (connected) wake.trySend(Unit) } + // A release that came meanwhile does not leave the observers behind. + val kept = synchronized(lock) { + if (released) false else true.also { + foreground = handle + network = networkHandle + } + } if (!kept) { handle.cancel() + networkHandle.cancel() return } val start = generation() @@ -320,7 +335,12 @@ internal class CoreInit( * True when `/init` went out. */ internal fun checkShop(): Boolean = synchronized(lock) { - if (released || held || !prepared || state.current == SdkState.REFUSED || job?.isActive == true) return false + if (released || held || !prepared || state.current == SdkState.REFUSED) return false + if (job?.isActive == true) { + // Being repeated: an ordinary pause ends, and the next attempt's answer is the check's. + wake.trySend(Unit) + return false + } val now = clock.nowMillis() val last = lastCheckAt if (last != null && now - last < CHECK_SHOP_INTERVAL_MS) return false @@ -362,6 +382,8 @@ internal class CoreInit( job?.cancel() job = null gate.value = Gate.CLOSED + network?.cancel() + network = null foreground.also { foreground = null } } listening?.cancel() @@ -405,7 +427,7 @@ internal class CoreInit( private sealed interface Step { object Done : Step class Answered(val answer: JSONObject) : Step - class Retry(val delayMs: Long, val waitForNetwork: Boolean, val fallback: Boolean) : Step + class Retry(val delayMs: Long, val waitForNetwork: Boolean, val fallback: Boolean, val serverNamed: Boolean) : Step } private suspend fun run(start: Int) { @@ -451,7 +473,7 @@ internal class CoreInit( if (step.waitForNetwork) { waitForNetwork() } else { - pause(step.delayMs) + pause(step.delayMs, step.serverNamed) attempt++ } } @@ -538,7 +560,7 @@ internal class CoreInit( ) val fallback = !startAnswered && state.current == SdkState.RETRYING && hasDid && !fallbackLogged if (fallback) fallbackLogged = true - return Step.Retry(delayMs, waitForNetwork, fallback) + return Step.Retry(delayMs, waitForNetwork, fallback, serverNamed = retryAfterMs != null) } /** Must hold [lock]. */ @@ -581,6 +603,7 @@ internal class CoreInit( /** Heard on the main thread; handled on [CoreDispatchers.io], where waiting for [lock] blocks nothing. */ private fun onForegroundChanged(foreground: Boolean) { if (!foreground) return + wake.trySend(Unit) scope.launch { onForeground() } } @@ -612,12 +635,17 @@ internal class CoreInit( } } - /** A pause between repeats; the network coming back ends a pause it set off. */ - private suspend fun pause(delayMs: Long) { - if (connectivity.isConnected) { + /** + * A pause between repeats. A server's `Retry-After` ([serverNamed]) is waited in full; an ordinary one + * ends when the network comes back, the app comes to the foreground or the shop is to be checked. + */ + private suspend fun pause(delayMs: Long, serverNamed: Boolean) { + // What woke the machine before this pause began is old news. + while (wake.tryReceive().isSuccess) Unit + if (serverNamed) { delay(delayMs) } else { - withTimeoutOrNull(delayMs) { awaitNetwork() } + withTimeoutOrNull(delayMs) { wake.receive() } } } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt index 755fc293..06509541 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreInitTest.kt @@ -278,6 +278,50 @@ class CoreInitTest { assertEquals(4, sent.size) } + @Test + fun `an ordinary pause of init ends on the network's return, the foreground or a check of the shop`() { + val init = started() + answer(ok()) + // The session's window ended: the new session's /init fails and waits out its pause. + clock.advance(SessionWindow.WINDOW_MS + 1) + init.renewSession() + scheduler.runCurrent() + answer(http(500)) + after(500) + assertEquals(2, sent.size) + + connectivity.set(false) + connectivity.set(true) + scheduler.runCurrent() + assertEquals("the network back sends it at once", 3, sent.size) + + answer(http(500)) + lifecycle.set(false) + lifecycle.set(true) + scheduler.runCurrent() + assertEquals("so does the foreground", 4, sent.size) + + answer(http(500)) + init.checkShop() + scheduler.runCurrent() + assertEquals("and a check of the shop", 5, sent.size) + } + + @Test + fun `a pause the server named is waited in full whatever comes`() { + val init = started() + answer(http(503, retryAfterMs = 60_000)) + connectivity.set(false) + connectivity.set(true) + lifecycle.set(false) + lifecycle.set(true) + init.checkShop() + after(59_999) + assertEquals(1, sent.size) + after(1) + assertEquals(2, sent.size) + } + @Test fun `a Retry-After waits at least a second and at most five minutes`() { started() diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt index e170b5a8..738d232a 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CoreSessionTest.kt @@ -252,6 +252,18 @@ class CoreSessionTest { assertEquals(2, calls.size) } + @Test + fun `an init answer without a seance keeps the live sid`() { + knownDevice("LIVE", lastActivity = clock.now - 60_000) + val init = started() + clock.advance(1_000) + answer(HttpResult.Success(JSONObject().put("did", "D1"))) + + assertEquals("LIVE", settings.getSid()) + assertEquals(clock.now, lastActivity) + assertTrue(init.session.isAlive()) + } + @Test fun `a failed init of a new session keeps the state and does not act on the saved answer again`() { val answers = mutableListOf() From 7d55cbe64c13e98067cfd59927e8ad13ec7c7058 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:28:38 +0400 Subject: [PATCH 054/109] fix(core): a 402, 403 or 404 to an event less than ten minutes after a check found the shop fine, while it is ready, drops the event at once as the service's refusal (service_refused), with no repeat and no hold of the queue. An answer that says an event went still takes it off the disk when it comes after release(): the request of the event in flight ends apart from the sending, so the event is not sent again at the next launch. Event files are named by time (in microseconds, rising within the process), the process and a random part, without a counter. --- .../personalization/core/internal/CoreInit.kt | 20 ++++++++-- .../core/internal/EventQueue.kt | 34 ++++++++++++++--- .../core/internal/QueueStore.kt | 20 +++++----- .../core/internal/EventQueueTest.kt | 38 ++++++++++++------- 4 files changed, 81 insertions(+), 31 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt index 096688c4..3fc7bd7a 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt @@ -152,6 +152,9 @@ internal class CoreInit( */ private val wake = Channel(Channel.CONFLATED) + /** [successes] when the last check of the shop went: a success since found the shop fine. */ + private var successesAtCheck = 0L + /** How many `/init` went out; for tests. */ @Volatile internal var sent = 0 @@ -282,8 +285,15 @@ internal class CoreInit( object Released : ShopCheck /** - * No new answer told: the check could not go yet — [retryInMs] until it may, after one less than - * 10 minutes ago — or its answer did not come in time or was not a success (null). + * A check less than 10 minutes ago found the shop fine and it is ready: this refusal is the + * service's, not the shop's. + */ + object RecentlyFine : ShopCheck + + /** + * No answer told: the check could not go yet — [retryInMs] until it may, after one less than 10 + * minutes ago that did not find the shop fine — or its answer did not come in time or was not a + * success (null). */ class Unknown(val retryInMs: Long?) : ShopCheck } @@ -292,7 +302,8 @@ internal class CoreInit( * Asks the server about the shop after a `402`, `403` or `404` to an event ([checkShop]), and waits * up to [timeoutMs] for the answer of the `/init` that went, or of the one already on its way. Only a * new answer tells: [ShopCheck.Fine] when it was a success, [ShopCheck.Refused] when the shop refused; - * a wait that ends first, a failure, or a check that may not go yet is [ShopCheck.Unknown]. + * a check less than 10 minutes ago that found the shop fine is [ShopCheck.RecentlyFine]; a wait that + * ends first, a failure, or a check that may not go yet is [ShopCheck.Unknown]. */ internal suspend fun checkShopAndAwait(timeoutMs: Long): ShopCheck { val seen: Long @@ -306,6 +317,8 @@ internal class CoreInit( if (!went && job?.isActive != true) { val last = lastCheckAt val wait = if (last == null) null else (CHECK_SHOP_INTERVAL_MS - (clock.nowMillis() - last)).takeIf { it > 0 } + // Checked less than 10 minutes ago, and fine. + if (wait != null && state.current == SdkState.READY && successes > successesAtCheck) return ShopCheck.RecentlyFine return ShopCheck.Unknown(wait) } } @@ -345,6 +358,7 @@ internal class CoreInit( val last = lastCheckAt if (last != null && now - last < CHECK_SHOP_INTERVAL_MS) return false lastCheckAt = now + successesAtCheck = successes launchLocked() } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt index 31320d94..bba9eb0e 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt @@ -17,6 +17,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.async import kotlinx.coroutines.cancel import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.coroutineScope @@ -288,10 +289,11 @@ internal class EventQueue( } /** - * Stops sending; the events stay on disk for the next launch. With [cancel] the event in flight is - * cancelled and the listeners of this queue's events hear `-1`; without, as when a push-only - * instance gives way to the app's, the event in flight finishes and the listeners stay for the - * queue that sends next. Events accepted before are still written; a flush asked before it and not + * Stops sending; the events stay on disk for the next launch. With [cancel] the sending stops at once + * and the listeners of this queue's events hear `-1`; the request of the event in flight still ends, + * and an answer that says it went takes the event off the disk. Without, as when a push-only instance + * gives way to the app's, the event in flight finishes and the listeners stay for the queue that + * sends next. Events accepted before are still written; a flush asked before it and not * handled yet ends with `notInitialized`. */ fun release(cancel: Boolean = true) { @@ -522,7 +524,24 @@ internal class EventQueue( return body } - private suspend fun sendOne(event: QueuedEvent, body: JSONObject): HttpResult = try { + /** + * Sends [event]. The request runs apart from the sending: a [release] that cancels the sending lets it + * end, and an answer that says it went still takes the event off the disk, so it does not go twice. + */ + private suspend fun sendOne(event: QueuedEvent, body: JSONObject): HttpResult { + val call = writing.async { request(event, body) } + try { + return call.await() + } catch (e: CancellationException) { + writing.launch { + val result = call.await() + if (result is HttpResult.Success || (result as? HttpResult.Failure)?.error == SdkError.Decoding) store.remove(event.id) + } + throw e + } + } + + private suspend fun request(event: QueuedEvent, body: JSONObject): HttpResult = try { send(NetworkMethod.POST(event.path), body) } catch (e: CancellationException) { throw e @@ -576,6 +595,11 @@ internal class EventQueue( return when (check) { // A new answer found the shop fine: the event goes once more, after a pause if the disk failed. CoreInit.ShopCheck.Fine -> if (store.update(event.copy(shopChecked = true))) null else pause(null) + // Found fine less than 10 minutes ago: the service refuses, not the shop. + CoreInit.ShopCheck.RecentlyFine -> { + drop(event, LogReasons.SERVICE_REFUSED, error) + null + } // Cleared by the state's own news; a released queue sends nothing more. CoreInit.ShopCheck.Refused, CoreInit.ShopCheck.Released -> Outcome.Idle // The check may not go yet: the event waits for it. Or it told nothing: a pause. diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt index 0e17beef..11f7b4ba 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt @@ -71,8 +71,8 @@ internal class QueuedEvent( /** * The files of one shop's queue, in its `queue/` directory under `noBackupFilesDir`: one JSON file per - * event, named by the time it was made, a counter, the process and a random part, so the names sort in - * order and two processes never pick the same one. One store per directory in the process, shared by + * event, named by the time it was made (in microseconds, rising within the process), the process and a + * random part: the names sort in order, and two instances or processes never pick the same one. One store per directory in the process, shared by * every [EventQueue] of the shop, so two instances of one shop never send an event twice: * - the queue made last is the [owner] and the only one that sends; the others write what they accept * and wake it; @@ -106,8 +106,8 @@ internal class QueueStore private constructor(private val directory: File, priva /** The state of events whose file could not be rewritten: kept for this process. */ private val patched = HashMap() + /** The time of the last name, in microseconds: names of one process rise with the calls. */ private var lastStamp = 0L - private var counter = 0 private var lockChannel: FileChannel? = null private var processLock: FileLock? = null @@ -301,12 +301,14 @@ internal class QueueStore private constructor(private val directory: File, priva event } - /** Must hold [files]. Sorts by time and, within one millisecond of this process, by the order of the calls. */ + /** + * Must hold [files]. Sorts by time: the event's in microseconds, kept above the last name's so that + * two events of one millisecond keep the order of the calls. + */ private fun newId(createdAt: Long): String { - val stamp = maxOf(createdAt, lastStamp) - counter = if (stamp == lastStamp) counter + 1 else 0 + val stamp = maxOf(createdAt * 1_000, lastStamp + 1) lastStamp = stamp - return String.format(Locale.ROOT, ID_FORMAT, stamp, counter, PROCESS, Random.nextInt() and 0x7fffffff) + return String.format(Locale.ROOT, ID_FORMAT, stamp, PROCESS, Random.nextInt() and 0x7fffffff) } /** @@ -410,8 +412,8 @@ internal class QueueStore private constructor(private val directory: File, priva private const val TEMPORARY = ".tmp" const val LOCK_FILE = ".lock" - /** Time, a counter, the process and a random part: sortable, and unique across processes. */ - private const val ID_FORMAT = "%013d-%06d-%d-%08x" + /** Time, the process and a random part: sortable, and unique across instances and processes. */ + private const val ID_FORMAT = "%016d-%d-%08x" /** A temporary file older than this is a crash's leftover, not a write in progress. */ const val STALE_TEMPORARY_MS = 5 * 60 * 1000L diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt index 63d04c05..3317f751 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt @@ -273,15 +273,15 @@ class EventQueueTest { // temporary file another process is writing right now. QueueStore.reset() File(storage.queuePath, "0000000000000000099.tmp").apply { writeText("{") }.setLastModified(System.currentTimeMillis() - QueueStore.STALE_TEMPORARY_MS - 1_000) - File(storage.queuePath, "0000000000000-000000-1-00000000.json").writeText("not json") - File(storage.queuePath, "9999999999999-000000-2-00000000.tmp").writeText("{") + File(storage.queuePath, "0000000000000000-1-00000000.json").writeText("not json") + File(storage.queuePath, "9999999999999999-2-00000000.tmp").writeText("{") state = InstanceState(shopId = { SHOP }, dispatchers = dispatchers) connectivity.set(true) start() assertEquals(listOf("1", "2"), ids()) - assertEquals("another process's write stays", listOf("9999999999999-000000-2-00000000.tmp"), files()) + assertEquals("another process's write stays", listOf("9999999999999999-2-00000000.tmp"), files()) assertEquals("unreadable", codes(LogCodes.QUEUE_DROPPED).single().context["reason"]) } @@ -466,7 +466,7 @@ class EventQueueTest { } @Test - fun `the check of the shop goes at most every ten minutes, and an event waits for the next one`() { + fun `a refusal less than ten minutes after a check that found the shop fine drops the event as the service's`() { val queue = start() eventAnswers += http(404) eventAnswers += http(404) @@ -477,18 +477,11 @@ class EventQueueTest { run() // The first event: a 404, the check, a 404 again and it is dropped. The second: a 404 while the - // shop may not be checked again yet: it is not marked checked, and it waits for the next check. + // shop was found fine less than ten minutes ago: dropped at once, without another /init. assertEquals(2, initsSent) assertEquals(listOf("1", "1", "2"), ids()) - lifecycle.set(false) - lifecycle.set(true) - after(CoreInit.CHECK_SHOP_INTERVAL_MS - 1_000) - assertEquals("no trigger ends the wait", listOf("1", "1", "2"), ids()) - - clock.advance(CoreInit.CHECK_SHOP_INTERVAL_MS) - after(1_000) - assertEquals(listOf("1", "1", "2", "2"), ids()) - assertTrue(heard.calls.single().startsWith("success")) + assertEquals(listOf("error:404"), heard.calls) + assertEquals(listOf(LogReasons.SERVICE_REFUSED, LogReasons.SERVICE_REFUSED), codes(LogCodes.QUEUE_DROPPED).map { it.context["reason"] }) } @Test @@ -741,6 +734,23 @@ class EventQueueTest { assertEquals(listOf("error:-1"), later.calls) } + @Test + fun `an answer that comes after release still takes the event off the disk`() { + val queue = start() + holdEvents = true + event(queue, "1") + run() + assertEquals(1, eventCalls.size) + + queue.release() + run() + assertEquals(1, files().size) + eventCalls.single().complete(HttpResult.Success(JSONObject())) + run() + + assertTrue("it went: not sent again at the next launch", files().isEmpty()) + } + @Test fun `a popup is shown only for an event sent within ten seconds and in the foreground`() { val queue = start() From cf700f6cc7673c71dcb6cdb5babf1e6e4e779eb7 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:28:38 +0400 Subject: [PATCH 055/109] =?UTF-8?q?fix(core):=20on=20the=20new=20core=20th?= =?UTF-8?q?e=20popups=20v3=20and=20stories=20v3=20clients,=20which=20make?= =?UTF-8?q?=20their=20own=20requests,=20wait=20before=20each=20as=20a=20re?= =?UTF-8?q?quest=20of=20the=20core=20does=20=E2=80=94=20for=20the=20instan?= =?UTF-8?q?ce=20and,=20when=20the=20session's=20window=20ended,=20for=20th?= =?UTF-8?q?e=20/init=20of=20a=20new=20one=20=E2=80=94=20and=20send=20the?= =?UTF-8?q?=20live=20sid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/kotlin/com/personalization/SDK.kt | 12 +++++++++++ .../personalization/core/internal/CoreV2.kt | 7 +++++++ .../ui/widgets/StoriesApiV3.kt | 4 +++- .../com/personalization/SdkInstanceTest.kt | 20 +++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index c4a28fcf..59294223 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -97,6 +97,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import java.util.Locale import java.util.concurrent.CopyOnWriteArrayList @@ -284,6 +285,17 @@ open class SDK { return userSettingsRepository.getSid().ifEmpty { null } } + /** + * For the clients that make their own requests (popups v3, stories v3), on their background thread: + * on the new core, waits as a request of the core does, within its read timeout, for the instance and + * for the `/init` of a new session when the window ended, so [sessionId] is the live `sid` after it. + * The legacy core does not wait. + */ + internal fun awaitLiveSession() { + val core = core as? CoreV2 ?: return + runBlocking { core.awaitUsable() } + } + /** The bridges' listeners of a new `sid` ([onSessionChange]). */ private val sessionListeners = CopyOnWriteArrayList<(String) -> Unit>() diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt index f65f39ed..c4d28401 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt @@ -167,6 +167,13 @@ internal class CoreV2( return if (waited == null) SdkError.Timeout else waited.error } + /** + * For the clients that make their own requests (popups v3, stories v3): the wait a request of the core + * makes — for the instance and, when the session's window ended, for the `/init` of a new one — so they + * go with the live `sid`. Null when they may go. + */ + internal suspend fun awaitUsable(): SdkError? = awaitInstance() + /** What a wait decided; null [error] when the request may go. */ private class Verdict(val error: SdkError?) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt index 3040e94d..ddbba46c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/ui/widgets/StoriesApiV3.kt @@ -143,13 +143,15 @@ internal object StoriesApiV3 { /** * Задача — когда у SDK есть did: сразу или из очереди `getAsync` после init. Init мог * закончиться между проверкой и постановкой в очередь — тогда задача стартует сама; дважды - * она не выполнится. + * она не выполнится. В новом ядре перед задачей — проверка окна сессии, как у любого запроса + * ядра: если окно кончилось, сначала `/init`, и задача берёт живой `sid`. */ fun whenDid(sdk: SDK, task: () -> Unit) { val once = AtomicBoolean(false) val run = Runnable { if (!once.compareAndSet(false, true)) return@Runnable try { + sdk.awaitLiveSession() task() } catch (throwable: Throwable) { SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES_V3, "stories v3: request failed", sdk.logShopId, throwable = throwable) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt index 4eb9066b..0a1d2f56 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/SdkInstanceTest.kt @@ -11,6 +11,7 @@ import com.personalization.core.SdkException import com.personalization.core.SdkState import com.personalization.core.BridgeApi import com.personalization.core.internal.CoreDispatchers +import com.personalization.core.internal.CoreInit import com.personalization.core.internal.CoreSelection import com.personalization.core.internal.HeldMain import com.personalization.core.internal.CoreV2 @@ -474,6 +475,25 @@ class SdkInstanceTest { assertEquals(listOf("S" to true), heard) } + @Test + fun `on the new core the v3 clients check the session's window and take the live sid`() { + var seance = "S1" + initAnswer = { _ -> MockResponse().setBody(JSONObject().put("did", "D1").put("seance", seance).toString()) } + val sdk = Rees46.initialize(context, config(coreV2 = true)) + nextRequest("init") + awaitState(sdk, SdkState.READY) + // Two hours without an event: the window ended, and the sid with it. + partition().edit().putLong("sid_last_act", System.currentTimeMillis() - CoreInit.SESSION_WINDOW_MS - 1).commit() + assertNull(sdk.sessionId) + seance = "S2" + + val seen = java.util.concurrent.LinkedBlockingQueue() + com.personalization.ui.widgets.StoriesApiV3.whenDid(sdk) { seen += sdk.getSid() } + + nextRequest("init") + assertEquals("S2", seen.poll(5, java.util.concurrent.TimeUnit.SECONDS)) + } + @Test fun `on the new core a stored did is ready at once and its init still goes`() { partition().edit().putString("did", "stored-did").commit() From 09df57cb15647ecc639facc1d3852f8a9fd18fac Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 02:28:38 +0400 Subject: [PATCH 056/109] docs(core): the tracking methods say that on the new core an event's listener is kept until the event is sent, up to the 7 days the queue keeps it, and hears the server's answer or the error that dropped it then --- .../src/main/kotlin/com/personalization/SDK.kt | 3 ++- .../com/personalization/api/managers/TrackEventManager.kt | 8 ++++++++ .../com/personalization/api/managers/TrackingApi.kt | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 59294223..a0c92847 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -1381,7 +1381,8 @@ open class SDK { } /** - * Custom event tracking. + * Custom event tracking. On the new core the event goes through the queue and [listener] is kept until + * it is sent, up to 7 days ([TrackingApi]). * * @param event Event key * @param time Optional UNIX time in seconds diff --git a/personalization-sdk/src/main/kotlin/com/personalization/api/managers/TrackEventManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/api/managers/TrackEventManager.kt index 19de60ae..44aadd58 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/api/managers/TrackEventManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/api/managers/TrackEventManager.kt @@ -5,6 +5,14 @@ import com.personalization.Params.TrackEvent import com.personalization.api.OnApiCallbackListener import com.personalization.api.models.purchase.PurchaseTrackingRequest +/** + * The tracking methods of earlier releases; [TrackingApi] (`sdk.tracking`) replaces them. + * + * On the new core (`Rees46Config.coreV2`) every event goes through the queue: the method returns at + * once, and a `listener` is kept until the event is sent — after a launch without a network that may be + * as long as the queue keeps an event, 7 days — then hears the server's answer, or the error that + * dropped the event. Whatever the listener holds stays alive that long. + */ interface TrackEventManager { /** diff --git a/personalization-sdk/src/main/kotlin/com/personalization/api/managers/TrackingApi.kt b/personalization-sdk/src/main/kotlin/com/personalization/api/managers/TrackingApi.kt index baf9ae91..7e957175 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/api/managers/TrackingApi.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/api/managers/TrackingApi.kt @@ -9,6 +9,13 @@ import com.personalization.api.models.tracking.TrackingSource * Standard tracking events — the `tracking` namespace of the SDK. * * Reached through an SDK instance: `Rees46.instance().tracking.productView("sku-1")`. + * + * On the new core (`Rees46Config.coreV2`) every event goes through the queue: the method returns at + * once, and a `listener` is kept until the event is sent — after a launch without a network that may be + * as long as the queue keeps an event, 7 days — then hears the server's answer, or the error that + * dropped the event. An event the queue refuses at once (an empty id, a released instance, a refused + * shop) tells the listener at once. Whatever the listener holds stays alive that long: keep it free of + * screens and other short-lived objects. */ interface TrackingApi { From 1bef78d1413341b9e8953175fe10fff5a3dd21c3 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 03:04:40 +0400 Subject: [PATCH 057/109] fix(push): a notification the SDK draws is reported only on the new core, for the push's own shop and with the whole push On the legacy core NotificationHelper.createNotification sends neither track/opened nor track/closed and sets no dismissal intent, as in 2.49.0, so a dismissal never starts the app; Rees46.handlePush(payload, OPENED or CLOSED) sends nothing there either. On the new core the shown and the dismissed notification go to the shop the push names in its shop_id, not to the shop of the helper's instance, and onOpened, onClosed and the bridges' onPushEvent get the whole push as the SDK received it: title, body, target, actions and data. A push the SDK did not see come in is rebuilt from the notification's data for the helper's shop. The dismissal intent and the carousel's buttons carry the push under one extra (at most 4096 characters; a larger one keeps id, type, shop_id, title and body), so a carousel drawn again in a process its button started still reports the dismissal for the push's shop. PushDismissReceiver works off the main thread with goAsync() and ends the broadcast once the event is on the disk of the shop's queue. The legacy core's tap through handlePush(payload, CLICKED) goes through notificationClicked as before, and SDK.pushEvent loses its unreachable delivery and legacy tap branches. --- .../main/kotlin/com/personalization/Rees46.kt | 31 +++- .../main/kotlin/com/personalization/SDK.kt | 38 ++--- .../core/internal/CoreSelection.kt | 3 + .../personalization/core/internal/CoreV2.kt | 5 + .../core/internal/EventQueue.kt | 15 ++ .../personalization/core/internal/PushHub.kt | 23 +++ .../helpers/NotificationHelper.kt | 101 ++++++++---- .../helpers/NotificationNavigationHelper.kt | 21 ++- .../helpers/NotificationViewHelper.kt | 36 ++++- .../service/NotificationService.kt | 11 +- .../push/PushDismissReceiver.kt | 48 +++++- .../com/personalization/push/PushExtras.kt | 47 ++++++ .../network/NetworkRepositoryImpl.kt | 11 ++ .../com/personalization/PushEventsTest.kt | 148 +++++++++++++++--- 14 files changed, 441 insertions(+), 97 deletions(-) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/push/PushExtras.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt index f71917f5..62c30b0a 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt @@ -255,6 +255,9 @@ object Rees46 { } val (resolution, shopId) = resolvePushTarget(data[PreferencesPartition.SHOP_ID_FIELD]) ?: return null logHandedOver(shopId, event.name) + // Kept before it is shown: a notification the SDK draws for it reports the whole push for its shop. + val payload = parsed.withShop(shopId) + PushHub.remember(payload) // Shown first, as before: the process-global listener needs no instance. if (event == PushEvent.DELIVERED) SdkRegistry.dispatchMessage(shopId = shopId, data = data.toNotificationData()) val target = targetInstance(resolution, shopId) @@ -262,11 +265,16 @@ object Rees46 { logDropped(shopId, LogReasons.UNKNOWN_SHOP) return null } - val fresh = when (event) { - PushEvent.DELIVERED -> target.onPushReceived(data) + val fresh = when { + event == PushEvent.DELIVERED -> target.onPushReceived(data) + // The legacy core's tap, as it always went: from the extras of a tap's intent. + event == PushEvent.CLICKED && target.core?.isV2 != true -> { + target.notificationClicked(tapExtras(parsed.code, parsed.type)) + true + } else -> target.pushEvent(event, parsed.code, parsed.type) } - if (fresh) PushHub.event(event, parsed.withShop(shopId), target.pushCallbacks) + if (fresh) PushHub.event(event, payload, target.pushCallbacks) return shopId } @@ -298,6 +306,9 @@ object Rees46 { internal fun receivePush(payload: Map) { val (resolution, shopId) = resolvePushTarget(payload[PreferencesPartition.SHOP_ID_FIELD]) ?: return logHandedOver(shopId, PushEventType.RECEIVED.name) + val parsed = PushPayload.parse(payload)?.withShop(shopId) + // Kept before it is shown: a notification the SDK draws for it reports the whole push for its shop. + parsed?.let(PushHub::remember) // Show first — the process-global listener needs no instance, so a pending shop's push still // appears. Then track the delivery on the shop's own network (light bring-up if it isn't live); // a tracking/init failure must never undo the shown notification. @@ -305,8 +316,7 @@ object Rees46 { runCatching { val target = targetInstance(resolution, shopId) ?: return@runCatching val fresh = target.onPushReceived(payload) - val parsed = PushPayload.parse(payload) - if (fresh && parsed != null) PushHub.event(PushEvent.DELIVERED, parsed.withShop(shopId), target.pushCallbacks) + if (fresh && parsed != null) PushHub.event(PushEvent.DELIVERED, parsed, target.pushCallbacks) }.onFailure { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "handlePush: the delivery was not tracked", shopId, throwable = it) } } @@ -319,14 +329,15 @@ object Rees46 { logHandedOver(shopId, PushEventType.CLICKED.name) runCatching { val target = targetInstance(resolution, shopId) ?: return@runCatching - val parsed = PushPayload.parse(payload) + val parsed = PushPayload.parse(payload)?.withShop(shopId) + parsed?.let(PushHub::remember) val fresh = if (parsed != null && target.core?.isV2 == true) { target.pushEvent(PushEvent.CLICKED, parsed.code, parsed.type) } else { target.notificationClicked(payload.toBundle()) true } - if (fresh && parsed != null) PushHub.event(PushEvent.CLICKED, parsed.withShop(shopId), target.pushCallbacks) + if (fresh && parsed != null) PushHub.event(PushEvent.CLICKED, parsed, target.pushCallbacks) }.onFailure { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "handlePush: the click was not tracked", shopId, throwable = it) } } @@ -438,6 +449,12 @@ object Rees46 { SdkLogs.handler = handler } + /** The extras of a tap's intent, as the SDK's notification helpers name them: what the legacy core reads. */ + private fun tapExtras(code: String, type: String): Bundle = Bundle().apply { + putString(PushPayload.CLICK_TYPE, type) + putString(PushPayload.CLICK_ID, code) + } + private fun Map.toBundle(): Bundle = Bundle().also { bundle -> forEach { (key, value) -> bundle.putString(key, value) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index a0c92847..d4887a8d 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -99,6 +99,7 @@ import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeoutOrNull import java.util.Locale import java.util.concurrent.CopyOnWriteArrayList import javax.inject.Inject @@ -1807,28 +1808,27 @@ open class SDK { private val isCoreV2: Boolean get() = core?.isV2 == true /** - * A push event of the message [code] of mailing [type] for [Rees46.handlePush]; false when it was a - * repeat. The new core queues it ([queuePushEvent]). The legacy core sends it as it always has: a tap - * through [notificationClicked], a delivery to `track/received`, and the shown and dismissed - * notifications, which it never reported before, to `track/opened` and `track/closed`. + * A shown, tapped or dismissed push of the message [code] of mailing [type] for [Rees46.handlePush]; + * false when it was a repeat. The new core queues it ([queuePushEvent]). The legacy core reports neither + * a shown nor a dismissed notification, as before; [Rees46.handlePush] takes its taps through + * [notificationClicked] and its deliveries through [onPushReceived]. */ internal fun pushEvent(event: PushEvent, code: String, type: String): Boolean { if (isCoreV2) return queuePushEvent(event, code, type) - val fields = mapOf(ID_FIELD to code, TYPE_FIELD to type) - when (event) { - PushEvent.DELIVERED -> trackReceivedLegacy(fields) - PushEvent.CLICKED -> notificationClicked(Bundle().apply { - putString(NotificationConstants.NOTIFICATION_TYPE, type) - putString(NotificationConstants.NOTIFICATION_ID, code) - }) - PushEvent.OPENED, PushEvent.CLOSED -> { - if (!isSdkInitialized) return false - sendNetworkMethodUseCase.postAsync(pathOf(event), JSONObject().put(TYPE_FIELD, type).put(CODE_FIELD, code)) - } - } + log(LogLevel.DEBUG, LogCodes.PUSH, "The legacy core does not report this push event", mapOf("event" to event.name.lowercase(Locale.ROOT))) return true } + /** + * Returns once the events this instance accepted before the call are written to the disk of its + * queue, or after [timeoutMs]; at once on the legacy core, which has no queue. Blocks: not for the + * main thread. + */ + internal fun awaitEventsWritten(timeoutMs: Long) { + val core = core as? CoreV2 ?: return + runBlocking { withTimeoutOrNull(timeoutMs) { core.awaitWrites() } } + } + /** * The new core's push event: `track/delivered`, `track/opened`, `track/clicked` or `track/closed` * through the queue with `code` and `type`, once per message and event in a process (a repeat is a @@ -2032,11 +2032,13 @@ open class SDK { return } SdkLogs.log(LogLevel.DEBUG, LogCodes.PUSH_RECEIVED, "Push handed to its shop", shopId, mapOf("event" to "received")) + val payload = PushPayload.parse(data)?.withShop(shopId) + // Kept before it is shown: a notification the SDK draws for it reports the whole push. + payload?.let(PushHub::remember) // Show via the process-global listener, then track the delivery on the instance. SdkRegistry.dispatchMessage(shopId = shopId, data = data.toNotificationData()) val fresh = target.receiveMessage(data = data) - val payload = PushPayload.parse(data) - if (fresh && payload != null) PushHub.event(PushEvent.DELIVERED, payload.withShop(shopId), target.pushCallbacks) + if (fresh && payload != null) PushHub.event(PushEvent.DELIVERED, payload, target.pushCallbacks) } /** diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreSelection.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreSelection.kt index a7b8e211..906f458c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreSelection.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreSelection.kt @@ -33,6 +33,9 @@ internal object CoreSelection { return decided } + /** The process's choice, without making it: true for the new core, null before the first call. */ + fun decided(): Boolean? = synchronized(this) { coreV2 } + /** Test-only: the next call decides again. */ internal fun reset() = synchronized(this) { coreV2 = null diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt index c4d28401..33f28435 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreV2.kt @@ -191,6 +191,11 @@ internal class CoreV2( queue?.release(cancel = cancelRequests) } + /** Returns once the events accepted before the call are written to disk ([EventQueue.awaitWrites]). */ + suspend fun awaitWrites() { + queue?.awaitWrites() + } + /** Sends the queue now ([EventQueue.flush]). */ suspend fun flush() { if (released) throw SdkException(SdkError.NotInitialized) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt index bba9eb0e..02bf9cc4 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt @@ -149,6 +149,9 @@ internal class EventQueue( class Clear(val reason: String, val error: LegacyError, val then: () -> Boolean, val done: CompletableDeferred?) : Command class Flush(val done: CompletableDeferred) : Command + + /** Done once every command sent before it was handled. */ + class Barrier(val done: CompletableDeferred) : Command } private sealed interface Outcome { @@ -270,6 +273,16 @@ internal class EventQueue( done.await() } + /** + * Returns once every event accepted before the call is written to disk, or refused: the writer handled + * what came before. At once without a writer (released). + */ + suspend fun awaitWrites() { + val done = CompletableDeferred() + if (writer?.isActive != true || commands.trySend(Command.Barrier(done)).isFailure) return + done.await() + } + /** * Empties the queue for [reason] and runs [then] while no event can be accepted: an event accepted * before this call goes, one accepted after it stays. The listeners waiting hear [error]. Returns @@ -353,6 +366,7 @@ internal class EventQueue( } ) is Command.Flush -> command.done.completeExceptionally(SdkException(SdkError.NotInitialized)) + is Command.Barrier -> command.done.complete(Unit) } } @@ -377,6 +391,7 @@ internal class EventQueue( kick(urgent = true) } } + is Command.Barrier -> command.done.complete(Unit) } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt index 97c380ee..00bb53b8 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt @@ -54,6 +54,14 @@ internal object PushHub { override fun removeEldestEntry(eldest: MutableMap.MutableEntry?): Boolean = size > SEEN_LIMIT } + /** + * The pushes handed to a shop in this process, as they came and with the shop they went to, by + * `type|code`: a notification the SDK draws reports them whole ([received]). Bounded like [seen]. + */ + private val pushes = object : LinkedHashMap>() { + override fun removeEldestEntry(eldest: MutableMap.MutableEntry>?): Boolean = size > PUSHES_LIMIT + } + /** The last token of each provider the facade's `onToken` heard of. */ private val facadeReported = ConcurrentHashMap() @@ -65,6 +73,16 @@ internal object PushHub { seen.put("$shopId|${event.name}|$code", true) == null } + /** Keeps [payload], handed to its shop ([PushPayload.shopId]), for [received]. */ + fun remember(payload: PushPayload) { + val shopId = payload.shopId ?: return + val push = if (payload.data[PushPayload.SHOP_ID].isNullOrEmpty()) payload.data + (PushPayload.SHOP_ID to shopId) else payload.data + synchronized(pushes) { pushes["${payload.type}|${payload.code}"] = push } + } + + /** The push of mailing [type] and message [code] as it came in this process, with its `shop_id`; null when it did not. */ + fun received(code: String, type: String): Map? = synchronized(pushes) { pushes["$type|$code"] } + /** * Tells the callbacks about [event]: the shop's own one for it ([own]) when set, otherwise the * facade's; and every bridge listener. @@ -137,10 +155,15 @@ internal object PushHub { eventSinks.clear() tokenSinks.clear() synchronized(seen) { seen.clear() } + forgetPushes() facadeReported.clear() sinkReported.clear() main = MainThread } + /** Test-only: forgets the pushes that came, as a new process would. */ + internal fun forgetPushes() = synchronized(pushes) { pushes.clear() } + private const val SEEN_LIMIT = 1_000 + private const val PUSHES_LIMIT = 100 } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationHelper.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationHelper.kt index 5c9abe72..dbaae3ab 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationHelper.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationHelper.kt @@ -8,25 +8,47 @@ import android.widget.RemoteViews import androidx.core.app.NotificationCompat import com.personalization.Rees46 import com.personalization.core.PushEvent +import com.personalization.core.internal.CoreSelection +import com.personalization.core.internal.PushHub +import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_ACTION +import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_ACTIONS +import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_ACTION_URLS +import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_BODY import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_CHANNEL +import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_EVENT +import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_ICON +import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_IMAGE +import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_PAYLOAD +import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_TITLE +import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_URI +import com.personalization.features.notification.domain.model.NotificationConstants.TYPE_PARAM +import com.personalization.features.notification.event.model.NotificationEvent import com.personalization.push.PushDismissReceiver import com.personalization.resources.NotificationResources import com.personalization.sdk.data.models.dto.notification.NotificationData +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject import javax.inject.Inject class NotificationHelper @Inject constructor( private val notificationManager: NotificationManager ) { - /** The shop of the instance this helper belongs to: the notifications it draws are that shop's. */ + /** The shop of the instance this helper belongs to: the shop of a push the SDK did not see come in. */ @Volatile internal var shopId: String? = null /** - * Draws [data] as a notification. When it is a push of the SDK — it has an `id` and a `type` — the SDK - * reports it as shown (`Rees46.handlePush(push, PushEvent.OPENED)`: `track/opened` and the `onOpened` - * callback), and a dismissal without a tap as closed (`track/closed`, `onClosed`), for the shop of the - * instance whose helper this is. + * Draws [data] as a notification. + * + * On the new core ([com.personalization.Rees46Config.coreV2]), a push of the SDK — one with an `id` and + * a `type` — is reported as shown (`Rees46.handlePush(push, PushEvent.OPENED)`: `track/opened` and the + * `onOpened` callback) and its dismissal without a tap as closed (`track/closed`, `onClosed`), for the + * shop the push names in its `shop_id`. The callbacks get the whole push as the SDK received it. A push + * the SDK did not see come in is rebuilt from [data], for the shop of this helper's instance. + * + * The legacy core reports neither and sets no dismissal intent, as before. */ fun createNotification( context: Context, @@ -34,16 +56,21 @@ class NotificationHelper @Inject constructor( images: List?, currentImageIndex: Int = 0, hasError: Boolean = false - ) = show(context, data, images, currentImageIndex, hasError, shopId ?: shopOf(data.id), reportShown = true) + ) = show(context, data, images, currentImageIndex, hasError, push = if (CoreSelection.decided() == true) pushOf(data) else null, reportShown = true) - /** The same push drawn again, with another picture of its carousel: not shown once more. */ + /** + * The same push drawn again, with another picture of its carousel: not reported as shown once more. + * [push] is what the carousel's button carried ([com.personalization.push.PushExtras]): the dismissal of the notification drawn + * again is still reported for the push's shop, in a process the button started. Null on the legacy core. + */ internal fun redraw( context: Context, data: NotificationData, images: List?, currentImageIndex: Int, hasError: Boolean, - ) = show(context, data, images, currentImageIndex, hasError, shopOf(data.id), reportShown = false) + push: Map?, + ) = show(context, data, images, currentImageIndex, hasError, push, reportShown = false) private fun show( context: Context, @@ -51,7 +78,7 @@ class NotificationHelper @Inject constructor( images: List?, currentImageIndex: Int, hasError: Boolean, - shopId: String?, + push: Map?, reportShown: Boolean, ) { val view = RemoteViews( @@ -74,7 +101,8 @@ class NotificationHelper @Inject constructor( data = data, currentIndex = currentImageIndex, imageCount = images?.size ?: 0, - hasError = hasError + hasError = hasError, + push = push, ) val notificationBuilder = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL) @@ -91,26 +119,54 @@ class NotificationHelper @Inject constructor( .setDefaults(NotificationCompat.DEFAULT_ALL) val notificationId = (data.title + data.body).hashCode() - val push = pushOf(data, shopId) push?.let { notificationBuilder.setDeleteIntent(PushDismissReceiver.intent(context, notificationId, it)) } notificationManager.notify( /* id = */ notificationId, /* notification = */ notificationBuilder.build() ) - if (push == null) return - if (shopId != null) remember(push.getValue(FIELD_ID), shopId) - if (reportShown) Rees46.handlePush(push, PushEvent.OPENED) + if (reportShown && push != null) Rees46.handlePush(push, PushEvent.OPENED) } - /** What reports the push of [data]: its code, type and shop; null when it is not a push of the SDK. */ - private fun pushOf(data: NotificationData, shopId: String?): Map? { + /** + * The push behind [data], for its events: as the SDK received it, with its own `shop_id` and every + * field ([PushHub.received]); or, for a push the SDK did not see come in, rebuilt from [data] for the + * shop of this helper's instance. Null when it is not a push of the SDK. + */ + private fun pushOf(data: NotificationData): Map? { val code = data.id?.takeIf { it.isNotBlank() } ?: return null val type = data.type?.takeIf { it.isNotBlank() } ?: return null + PushHub.received(code, type)?.let { return it } return buildMap { put(FIELD_ID, code) put(FIELD_TYPE, type) shopId?.let { put(FIELD_SHOP_ID, it) } + data.title?.let { put(NOTIFICATION_TITLE, it) } + data.body?.let { put(NOTIFICATION_BODY, it) } + data.icon?.takeIf { it.isNotEmpty() }?.let { put(NOTIFICATION_ICON, it) } + data.image?.takeIf { it.isNotEmpty() }?.let { put(NOTIFICATION_IMAGE, it) } + data.actions?.takeIf { it.isNotEmpty() }?.let { actions -> + val list = JSONArray() + actions.forEach { list.put(JSONObject().put(NOTIFICATION_ACTION, it.action).put(NOTIFICATION_TITLE, it.title)) } + put(NOTIFICATION_ACTIONS, list.toString()) + } + data.actionUrls?.takeIf { it.isNotEmpty() }?.let { urls -> put(NOTIFICATION_ACTION_URLS, JSONArray(urls).toString()) } + data.event?.let(::eventOf)?.let { put(NOTIFICATION_EVENT, it) } + } + } + + /** The `event` of a push as JSON text; null for the empty event the mapper gives a push without one. */ + private fun eventOf(event: NotificationEvent): String? { + val payload = event.payload.orEmpty() + if (event.type.isNullOrEmpty() && event.uri.isNullOrEmpty() && payload.isEmpty()) return null + return try { + JSONObject() + .put(TYPE_PARAM, event.type.orEmpty()) + .put(NOTIFICATION_URI, event.uri.orEmpty()) + .apply { if (payload.isNotEmpty()) put(NOTIFICATION_PAYLOAD, JSONObject(payload)) } + .toString() + } catch (e: JSONException) { + null } } @@ -137,19 +193,6 @@ class NotificationHelper @Inject constructor( const val FIELD_ID = "id" const val FIELD_TYPE = "type" const val FIELD_SHOP_ID = "shop_id" - private const val SHOPS_LIMIT = 100 - - /** - * The shop of each push drawn in this process, by its code: the carousel's service draws a push - * again without an instance, and its dismissal still belongs to the shop. - */ - private val shops = object : LinkedHashMap() { - override fun removeEldestEntry(eldest: MutableMap.MutableEntry?): Boolean = size > SHOPS_LIMIT - } - - fun remember(code: String, shopId: String) = synchronized(shops) { shops[code] = shopId } - - fun shopOf(code: String?): String? = code?.let { synchronized(shops) { shops[it] } } // De-facto standard meta-data hosts use to declare a dedicated push notification icon. const val META_DATA_NOTIFICATION_ICON = diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationNavigationHelper.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationNavigationHelper.kt index 72ebd146..4cbf2ff0 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationNavigationHelper.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationNavigationHelper.kt @@ -10,6 +10,7 @@ import com.personalization.features.notification.domain.model.NotificationConsta import com.personalization.features.notification.domain.model.NotificationConstants.NOTIFICATION_TITLE import com.personalization.features.notification.domain.model.NotificationConstants.TYPE_PARAM import com.personalization.features.notification.service.NotificationService +import com.personalization.push.PushExtras import com.personalization.sdk.data.models.dto.notification.NotificationData import java.util.concurrent.atomic.AtomicInteger import kotlin.math.abs @@ -23,6 +24,15 @@ object NotificationNavigationHelper { data: NotificationData, newIndex: Int, action: String + ): PendingIntent = createNavigationPendingIntent(context, data, newIndex, action, push = null) + + /** [createNavigationPendingIntent] that also carries [push], the push behind [data] ([PushExtras]). */ + internal fun createNavigationPendingIntent( + context: Context, + data: NotificationData, + newIndex: Int, + action: String, + push: Map?, ): PendingIntent { val intent = Intent(context, NotificationService::class.java).apply { this.action = action @@ -30,9 +40,10 @@ object NotificationNavigationHelper { putExtra(NOTIFICATION_TITLE, data.title) putExtra(NOTIFICATION_BODY, data.body) putExtra(NOTIFICATION_IMAGE, data.image) - // The push it is, so the notification drawn again still reports its dismissal. + // The push it is, with its shop, so the notification drawn again still reports its dismissal. putExtra(NOTIFICATION_PARAM_ID, data.id) putExtra(TYPE_PARAM, data.type) + push?.let { PushExtras.put(this, it) } } return PendingIntent.getService( @@ -46,6 +57,13 @@ object NotificationNavigationHelper { fun createRetryPendingIntent( context: Context, data: NotificationData + ): PendingIntent = createRetryPendingIntent(context, data, push = null) + + /** [createRetryPendingIntent] that also carries [push], the push behind [data] ([PushExtras]). */ + internal fun createRetryPendingIntent( + context: Context, + data: NotificationData, + push: Map?, ): PendingIntent { val retryIntent = Intent(context, NotificationService::class.java).apply { putExtra(CURRENT_IMAGE_INDEX, 0) @@ -54,6 +72,7 @@ object NotificationNavigationHelper { putExtra(NOTIFICATION_BODY, data.body) putExtra(NOTIFICATION_PARAM_ID, data.id) putExtra(TYPE_PARAM, data.type) + push?.let { PushExtras.put(this, it) } } return PendingIntent.getService( context, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationViewHelper.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationViewHelper.kt index 8caad17e..1779c5e3 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationViewHelper.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/presentation/helpers/NotificationViewHelper.kt @@ -17,6 +17,21 @@ object NotificationViewHelper { currentIndex: Int, imageCount: Int, hasError: Boolean + ) = setNavigationActions(customView, context, data, currentIndex, imageCount, hasError, push = null) + + /** + * [setNavigationActions] whose buttons carry [push], the push behind [data] on the new core: the + * notification drawn again still reports its dismissal for the push's shop, in a process started by + * the button. + */ + internal fun setNavigationActions( + customView: RemoteViews, + context: Context, + data: NotificationData, + currentIndex: Int, + imageCount: Int, + hasError: Boolean, + push: Map?, ) { setActionButton( customView = customView, @@ -25,7 +40,8 @@ object NotificationViewHelper { actionId = R.id.action1, action = ACTION_PREVIOUS_IMAGE, newIndex = currentIndex - 1, - isVisible = currentIndex > 0 + isVisible = currentIndex > 0, + push = push, ) setActionButton( @@ -35,14 +51,16 @@ object NotificationViewHelper { actionId = R.id.action2, action = ACTION_NEXT_IMAGE, newIndex = currentIndex + 1, - isVisible = currentIndex < imageCount - 1 + isVisible = currentIndex < imageCount - 1, + push = push, ) setRetryButtonVisibility( customView = customView, context = context, data = data, - hasError = hasError + hasError = hasError, + push = push, ) } @@ -53,13 +71,15 @@ object NotificationViewHelper { actionId: Int, action: String, newIndex: Int, - isVisible: Boolean + isVisible: Boolean, + push: Map?, ) { val pendingIntent = NotificationNavigationHelper.createNavigationPendingIntent( context = context, data = data, newIndex = newIndex, - action = action + action = action, + push = push, ) customView.setOnClickPendingIntent( /* viewId = */ actionId, @@ -75,12 +95,14 @@ object NotificationViewHelper { customView: RemoteViews, context: Context, data: NotificationData, - hasError: Boolean + hasError: Boolean, + push: Map?, ) { if (hasError) { val retryPendingIntent = NotificationNavigationHelper.createRetryPendingIntent( context = context, - data = data + data = data, + push = push, ) customView.setOnClickPendingIntent( /* viewId = */ R.id.retryButton, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/service/NotificationService.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/service/NotificationService.kt index 67908c24..69e1d8e6 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/notification/service/NotificationService.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/notification/service/NotificationService.kt @@ -16,6 +16,7 @@ import com.personalization.sdk.data.models.dto.notification.NotificationData import com.personalization.LogLevel import com.personalization.logs.LogCodes import com.personalization.logs.SdkLogs +import com.personalization.push.PushExtras import java.io.IOException import javax.inject.Inject import kotlinx.coroutines.CoroutineScope @@ -58,7 +59,9 @@ class NotificationService : Service() { data != null -> updateNotification( data, currentIndex = currentIndex, - startId = startId + startId = startId, + // The push behind it, with its shop, on the new core: the dismissal is still reported. + push = PushExtras.read(intent), ) else -> onStopService(startId) @@ -96,7 +99,8 @@ class NotificationService : Service() { private fun updateNotification( data: NotificationData, currentIndex: Int, - startId: Int + startId: Int, + push: Map?, ) { serviceScope.launch { try { @@ -119,7 +123,8 @@ class NotificationService : Service() { ), images = loadedImages, currentImageIndex = currentIndex, - hasError = hasError + hasError = hasError, + push = push, ) } catch (ioException: IOException) { showToast( diff --git a/personalization-sdk/src/main/kotlin/com/personalization/push/PushDismissReceiver.kt b/personalization-sdk/src/main/kotlin/com/personalization/push/PushDismissReceiver.kt index f5ce9df7..045d57bb 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/push/PushDismissReceiver.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/push/PushDismissReceiver.kt @@ -6,32 +6,64 @@ import android.content.Context import android.content.Intent import com.personalization.LogLevel import com.personalization.Rees46 +import com.personalization.SdkRegistry import com.personalization.core.PushEvent import com.personalization.logs.LogCodes import com.personalization.logs.SdkLogs +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.asExecutor +import java.util.concurrent.Executor /** - * The `deleteIntent` of a notification the SDK drew: dismissed without a tap, it is - * `Rees46.handlePush(push, PushEvent.CLOSED)` — `track/closed` and the `onClosed` callback. + * The `deleteIntent` of a notification the SDK drew on the new core: dismissed without a tap, it is + * `Rees46.handlePush(push, PushEvent.CLOSED)` — `track/closed` and the `onClosed` callback, for the shop the + * push names, with the whole push ([PushExtras]). + * + * The work runs off the main thread ([goAsync]): the broadcast ends once the event is on the disk of the + * shop's queue, or the SDK gave it up, so a process the dismissal started is not stopped before. */ internal class PushDismissReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { + val push = PushExtras.read(intent) ?: return + // Null when onReceive is called outside a broadcast. + val pending: PendingResult? = goAsync() try { - val push = intent.extras?.let { extras -> - extras.keySet().mapNotNull { key -> extras.getString(key)?.let { key to it } }.toMap() - } ?: return - Rees46.handlePush(push, PushEvent.CLOSED) + work.execute { + try { + report(push) + } finally { + pending?.finish() + } + } } catch (e: Exception) { - SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "A dismissed notification could not be reported", throwable = e) + SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "A dismissed notification could not be reported", push["shop_id"], throwable = e) + pending?.finish() } } internal companion object { + /** How long the broadcast waits for the event to reach the disk: well inside the system's 10 s. */ + const val WRITE_WAIT_MS = 5_000L + + /** Where the dismissal is handled; a test runs it in place. */ + @Volatile + internal var work: Executor = Dispatchers.IO.asExecutor() + + /** Hands the dismissal of [push] to its shop and returns once the event is written or given up. */ + internal fun report(push: Map) { + try { + val shopId = Rees46.handlePush(push, PushEvent.CLOSED) ?: return + SdkRegistry.byShopId(shopId)?.awaitEventsWritten(WRITE_WAIT_MS) + } catch (e: Exception) { + SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "A dismissed notification could not be reported", push["shop_id"], throwable = e) + } + } + /** The intent that reports the dismissal of the notification [notificationId] of [push]. */ fun intent(context: Context, notificationId: Int, push: Map): PendingIntent { val intent = Intent(context, PushDismissReceiver::class.java) - push.forEach { (key, value) -> intent.putExtra(key, value) } + PushExtras.put(intent, push) return PendingIntent.getBroadcast( context, notificationId, diff --git a/personalization-sdk/src/main/kotlin/com/personalization/push/PushExtras.kt b/personalization-sdk/src/main/kotlin/com/personalization/push/PushExtras.kt new file mode 100644 index 00000000..e0144b0d --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/push/PushExtras.kt @@ -0,0 +1,47 @@ +package com.personalization.push + +import android.content.Intent +import android.os.Bundle + +/** + * The push an intent of a notification the SDK drew carries — its dismissal, the buttons of its carousel — + * under one extra: the push's fields as they came, its `shop_id` among them. A later process, started by + * the intent itself, reports the dismissal for the push's own shop and hands the callbacks the whole push. + * + * A push of FCM or HMS holds at most 4 KB, so it fits as it is. One larger than [MAX_CHARS] (built by the + * app) keeps only what routes it and what a callback shows first: `id`, `type`, `shop_id`, `title` and + * `body`. + */ +internal object PushExtras { + + const val EXTRA = "com.personalization.push.PUSH" + + /** The most characters of keys and values an intent carries for a push. */ + const val MAX_CHARS = 4_096 + + private val ESSENTIAL = setOf("id", "type", "shop_id", "title", "body") + + fun put(intent: Intent, push: Map) { + val bundle = Bundle() + fit(push).forEach { (key, value) -> bundle.putString(key, value) } + intent.putExtra(EXTRA, bundle) + } + + /** The push [intent] carries; null when it carries none. */ + fun read(intent: Intent?): Map? { + val bundle = try { + intent?.getBundleExtra(EXTRA) + } catch (e: RuntimeException) { + // An extra that does not unparcel: not one the SDK wrote. + null + } ?: return null + val push = bundle.keySet().mapNotNull { key -> bundle.getString(key)?.let { key to it } }.toMap() + return push.takeIf { it.isNotEmpty() } + } + + /** [push] as an intent may carry it. */ + fun fit(push: Map): Map { + val size = push.entries.sumOf { (key, value) -> key.length + value.length } + return if (size <= MAX_CHARS) push else push.filterKeys { it in ESSENTIAL } + } +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt index 7eb91775..f953c199 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/sdk/data/repositories/network/NetworkRepositoryImpl.kt @@ -48,6 +48,13 @@ class NetworkRepositoryImpl @Inject constructor( private val queueLock = Any() private val queue = ArrayList() + /** + * Hears the path of each request handed to the funnel, on the caller's thread, before anything else + * happens to it; for tests, which then know what went without waiting for it. + */ + @Volatile + internal var onRequest: ((path: String) -> Unit)? = null + /** How many requests wait for a `did` on the legacy core; for tests. */ internal val waitingForDid: Int get() = synchronized(queueLock) { queue.size } @@ -83,6 +90,7 @@ class NetworkRepositoryImpl @Inject constructor( params: JSONObject, listener: OnApiCallbackListener? ) { + onRequest?.invoke(method) sendMethod( networkMethod = NetworkMethod.POST(method), params = params, @@ -95,6 +103,7 @@ class NetworkRepositoryImpl @Inject constructor( params: JSONObject, listener: OnApiCallbackListener? ) { + onRequest?.invoke(method) sendAsync { sendMethod( networkMethod = NetworkMethod.POST(method), @@ -117,6 +126,7 @@ class NetworkRepositoryImpl @Inject constructor( params: JSONObject, listener: OnApiCallbackListener? ) { + onRequest?.invoke(method) sendMethod( networkMethod = NetworkMethod.GET(method), params = params, @@ -129,6 +139,7 @@ class NetworkRepositoryImpl @Inject constructor( params: JSONObject, listener: OnApiCallbackListener? ) { + onRequest?.invoke(method) sendAsync { sendMethod( networkMethod = NetworkMethod.GET(method), diff --git a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt index f6268307..4529ba4b 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt @@ -18,7 +18,13 @@ import com.personalization.core.internal.QueueStore import com.personalization.core.internal.ShopStorage import com.personalization.core.internal.StorageLayout import com.personalization.core.internal.TrackingThreads +import com.personalization.features.notification.data.mapper.toNotificationData import com.personalization.features.notification.domain.model.NotificationConstants +import com.personalization.features.notification.presentation.helpers.NotificationHelper +import com.personalization.features.notification.presentation.helpers.NotificationNavigationHelper +import com.personalization.features.notification.service.mapper.toNotificationData +import com.personalization.push.PushDismissReceiver +import com.personalization.push.PushExtras import com.personalization.logs.LogCodes import com.personalization.logs.SdkLogs import com.personalization.sdk.data.models.dto.notification.NotificationData @@ -64,6 +70,7 @@ class PushEventsTest { private val logged = CopyOnWriteArrayList() private val threads = TrackingThreads() private val defaultExecutor = SdkLogs.handlerExecutor + private val defaultWork = PushDismissReceiver.work /** Holds the answer to /init while it is not null. */ @Volatile @@ -127,6 +134,7 @@ class PushEventsTest { SdkLogs.handlerExecutor = defaultExecutor QueueStore.reset() PushHub.reset() + PushDismissReceiver.work = defaultWork } private fun config(shop: String = SHOP, coreV2: Boolean = true) = Rees46Config( @@ -164,6 +172,9 @@ class PushEventsTest { private fun flush(sdk: SDK) = runBlocking { withTimeout(5_000) { sdk.flush() } } + /** What the server got and the test has not taken yet. */ + private fun drained(): List> = buildList { while (true) add(requests.poll() ?: break) } + // The new core. @Test @@ -335,20 +346,29 @@ class PushEventsTest { // The legacy core. @Test - fun `the legacy core sends a delivery to track received, as it did, and the new events directly`() { - sdk(coreV2 = false) + fun `the legacy core sends a delivery to track received and a tap as it did, and reports no shown or dismissed notification`() { + val sdk = sdk(coreV2 = false) + val handed = CopyOnWriteArrayList() + sdk.networkRepository.onRequest = { handed += it } @Suppress("DEPRECATION") Rees46.handlePush(push(code = "old"), PushEventType.RECEIVED) - assertEquals("old", next("track/received", "track/delivered")?.takeIf { it.first == "track/received" }?.second?.getString("code")) + assertEquals("old", next("track/received")?.second?.getString("code")) assertEquals(SHOP, Rees46.handlePush(push(code = "new"), PushEvent.DELIVERED)) - assertEquals("track/received", next("track/received", "track/delivered")?.first) - Rees46.handlePush(push(code = "new"), PushEvent.OPENED) - assertEquals("track/opened", next("track/opened")?.first) - // No once-per-process rule on the legacy core. - Rees46.handlePush(push(code = "new"), PushEvent.OPENED) - assertEquals("track/opened", next("track/opened")?.first) - assertNull(next("track/delivered", timeoutMs = 300)) + assertEquals("new", next("track/received")?.second?.getString("code")) + // Neither event reaches the network: nothing is handed to the funnel for them. + assertEquals(SHOP, Rees46.handlePush(push(code = "new"), PushEvent.OPENED)) + assertEquals(SHOP, Rees46.handlePush(push(code = "new"), PushEvent.CLOSED)) + assertEquals(SHOP, Rees46.handlePush(push(code = "new"), PushEvent.CLICKED)) + assertEquals("new", next("track/clicked")?.second?.getString("code")) + + // A notification the SDK draws: no dismissal intent, nothing reported, as in 2.49.0. + val data = NotificationData(id = "drawn", title = "T", body = "B", icon = null, type = "bulk", actions = null, actionUrls = null, image = null, event = null) + sdk.notificationHelper.createNotification(context, data, images = null) + val notification = shadowOf(context.getSystemService(android.app.NotificationManager::class.java)).allNotifications.single() + assertNull(notification.deleteIntent) + + assertEquals(listOf("track/received", "track/received", "track/clicked"), handed.filter { it.startsWith("track/") }) } // The token. @@ -388,23 +408,103 @@ class PushEventsTest { // A notification the SDK draws. + /** A push of [shop] with every field a callback reads. */ + private fun fullPush(code: String, shop: String) = mapOf( + "id" to code, + "type" to "bulk", + "shop_id" to shop, + "title" to "Sale", + "body" to "Today only", + "image" to "https://example.com/i.png", + "actions" to """[{"action":"buy","title":"Buy"}]""", + "action_urls" to """["https://example.com/buy"]""", + "event" to """{"type":"product","uri":"37","payload":{"k":"v"}}""", + "custom" to "kept", + ) + + private fun assertWhole(payload: PushPayload?, shop: String) { + if (payload == null) throw AssertionError("no payload") + assertEquals(shop, payload.shopId) + assertEquals("Sale", payload.title) + assertEquals("Today only", payload.body) + assertEquals(com.personalization.core.PushTargetType.PRODUCT, payload.target?.type) + assertEquals("37", payload.target?.uri) + assertEquals("v", payload.target?.payload?.get("k")) + assertEquals("buy", payload.actions.single().id) + assertEquals("https://example.com/buy", payload.actions.single().url) + assertEquals("kept", payload.data["custom"]) + } + + @OptIn(com.personalization.core.BridgeApi::class) @Test - fun `a notification the SDK draws is reported as opened, and its dismissal as closed`() { - val sdk = sdk() - val data = NotificationData(id = "drawn", title = "T", body = "B", icon = null, type = "bulk", actions = null, actionUrls = null, image = null, event = null) - sdk.notificationHelper.createNotification(context, data, images = null) - flush(sdk) - val opened = next("track/opened")?.second ?: throw AssertionError("no track/opened") - assertEquals("drawn", opened.getString("code")) + fun `a notification the SDK draws is reported as opened and its dismissal as closed, for the push's own shop, whole`() { + val shop = sdk(SHOP) + val other = sdk(OTHER) + val opened = CopyOnWriteArrayList() + val closed = CopyOnWriteArrayList() + val bridge = CopyOnWriteArrayList>() + Rees46.setPushCallbacks(PushCallbacks(onOpened = { opened += it }, onClosed = { closed += it })) + Rees46.onPushEvent { event, payload -> bridge += event to payload } + val push = fullPush("drawn", OTHER) + Rees46.handlePush(push, PushEvent.DELIVERED) + + // Drawn by the helper of another shop's instance: the push names its own. + shop.notificationHelper.createNotification(context, push.toNotificationData(), images = null) + flush(other) + val shown = next("track/opened")?.second ?: throw AssertionError("no track/opened") + assertEquals("drawn", shown.getString("code")) + assertEquals(OTHER, shown.getString("shop_id")) + assertWhole(opened.single(), OTHER) + assertWhole(bridge.single { it.first == PushEvent.OPENED }.second, OTHER) + + // The dismissal is handled off the receiver's thread, and the broadcast ends once the event is written. + val work = LinkedBlockingQueue() + PushDismissReceiver.work = Executor { work += it } + val notification = shadowOf(context.getSystemService(android.app.NotificationManager::class.java)).allNotifications.single() + PushDismissReceiver().onReceive(context, shadowOf(notification.deleteIntent).savedIntent) + assertTrue("nothing handled on the receiver's thread", closed.isEmpty()) + work.single().run() + flush(other) + val dismissed = next("track/closed")?.second ?: throw AssertionError("no track/closed") + assertEquals(OTHER, dismissed.getString("shop_id")) + assertWhole(closed.single(), OTHER) + assertWhole(bridge.single { it.first == PushEvent.CLOSED }.second, OTHER) + } + @Test + fun `a carousel drawn again in a new process still reports its dismissal for the push's shop, whole`() { + sdk(SHOP) + val other = sdk(OTHER) + val closed = CopyOnWriteArrayList() + Rees46.setPushCallbacks(PushCallbacks(onClosed = { closed += it })) + val push = fullPush("carousel", OTHER) + val data = push.toNotificationData() + // The carousel's button carries the push. + val button = shadowOf( + NotificationNavigationHelper.createNavigationPendingIntent(context, data, 1, NotificationConstants.ACTION_NEXT_IMAGE, push) + ).savedIntent + + // A new process: nothing of the push in memory, and the helper the service makes belongs to no instance. + PushHub.forgetPushes() val manager = context.getSystemService(android.app.NotificationManager::class.java) - val notification = shadowOf(manager).allNotifications.single() - val dismiss = shadowOf(notification.deleteIntent).savedIntent - com.personalization.push.PushDismissReceiver().onReceive(context, dismiss) - flush(sdk) - val closed = next("track/closed")?.second ?: throw AssertionError("no track/closed") - assertEquals("drawn", closed.getString("code")) - assertEquals(SHOP, closed.getString("shop_id")) + NotificationHelper(manager).redraw(context, button.toNotificationData(), images = null, currentImageIndex = 1, hasError = false, push = PushExtras.read(button)) + PushDismissReceiver.work = Executor { it.run() } + PushDismissReceiver().onReceive(context, shadowOf(shadowOf(manager).allNotifications.single().deleteIntent).savedIntent) + flush(other) + + val dismissed = next("track/closed")?.second ?: throw AssertionError("no track/closed") + assertEquals("carousel", dismissed.getString("code")) + assertEquals(OTHER, dismissed.getString("shop_id")) + assertWhole(closed.single(), OTHER) + // Drawn again, not shown again: flushed, the queue sent everything it had. + assertFalse(drained().any { it.first == "track/opened" }) + } + + @Test + fun `a push larger than an intent carries keeps what routes it and what a callback shows first`() { + val big = fullPush("big", OTHER) + ("extra" to "x".repeat(PushExtras.MAX_CHARS)) + assertEquals(setOf("id", "type", "shop_id", "title", "body"), PushExtras.fit(big).keys) + assertEquals(fullPush("small", OTHER), PushExtras.fit(fullPush("small", OTHER))) } // The bridges. From 5d7d51ff84818feec2e642b671e4f4e8984ab68d Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 03:09:02 +0400 Subject: [PATCH 058/109] fix(push): a push asks no /init. Its events need only the stored did, and a shop brought up for a push never sends /init On the new core the queue sends track/delivered, track/opened, track/clicked and track/closed once the instance has a did: they neither wait for a live session nor start one, so a push that comes after the session's window ended sends no /init. A shop registered but not created that Rees46.handlePush brings up for a push sends no /init, no push token and no profile and shows no popup, on both cores: it is ready on the shop's stored did, its queue sends the push events alone and leaves the others on disk, and a refusal to an event waits for the app's instance instead of checking the shop. Without a stored did the push is dropped (push_dropped, no_did). The app's first Rees46.instance or Rees46.awaitInstance of the shop puts a full instance with a full start in its place, as Rees46.initialize did, while the push's events in flight finish; initializing that object itself makes it a full instance. Nobody waiting for the shop's instance is handed the one brought up for a push. --- .../main/kotlin/com/personalization/Rees46.kt | 25 +++- .../main/kotlin/com/personalization/SDK.kt | 49 ++++++-- .../kotlin/com/personalization/SdkRegistry.kt | 5 +- .../personalization/core/internal/CoreInit.kt | 52 +++++++-- .../core/internal/EventQueue.kt | 20 +++- .../core/internal/QueueStore.kt | 13 ++- .../com/personalization/logs/SdkLogs.kt | 3 + .../com/personalization/PushEventsTest.kt | 107 ++++++++++++++++-- .../core/internal/EventQueueTest.kt | 62 +++++++++- 9 files changed, 289 insertions(+), 47 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt index 62c30b0a..55ef92a2 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt @@ -128,7 +128,7 @@ object Rees46 { ) return when (resolution) { is InstanceResolver.Resolution.Existing -> - SdkRegistry.byShopId(resolution.shopId) + SdkRegistry.byShopId(resolution.shopId)?.let(::upgraded) ?: throw UnknownShopIdException(missingMessage(resolution.shopId)) is InstanceResolver.Resolution.Pending -> materialize(resolution.shopId) @@ -176,7 +176,7 @@ object Rees46 { ) return when (resolution) { is InstanceResolver.Resolution.Existing -> { - SdkRegistry.byShopId(resolution.shopId)?.let(onReady) + SdkRegistry.byShopId(resolution.shopId)?.let { onReady(upgraded(it)) } Cancellable.NOOP } @@ -265,6 +265,7 @@ object Rees46 { logDropped(shopId, LogReasons.UNKNOWN_SHOP) return null } + if (target.dropsPushWithoutDid()) return null val fresh = when { event == PushEvent.DELIVERED -> target.onPushReceived(data) // The legacy core's tap, as it always went: from the extras of a tap's intent. @@ -315,6 +316,7 @@ object Rees46 { SdkRegistry.dispatchMessage(shopId = shopId, data = payload.toNotificationData()) runCatching { val target = targetInstance(resolution, shopId) ?: return@runCatching + if (target.dropsPushWithoutDid()) return@runCatching val fresh = target.onPushReceived(payload) if (fresh && parsed != null) PushHub.event(PushEvent.DELIVERED, parsed, target.pushCallbacks) }.onFailure { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "handlePush: the delivery was not tracked", shopId, throwable = it) } @@ -329,6 +331,7 @@ object Rees46 { logHandedOver(shopId, PushEventType.CLICKED.name) runCatching { val target = targetInstance(resolution, shopId) ?: return@runCatching + if (target.dropsPushWithoutDid()) return@runCatching val parsed = PushPayload.parse(payload)?.withShop(shopId) parsed?.let(PushHub::remember) val fresh = if (parsed != null && target.core?.isV2 == true) { @@ -414,10 +417,9 @@ object Rees46 { } /** - * Brings up a registered-but-pending shop just enough to track a push, via [SDK.initializeForPush]: - * no `/init` round-trip, no push-token re-registration, no profile call. A shop that was never - * initialized has no did and the backend never sent it a push, so this is not reached for real - * deliveries. + * Brings up a registered-but-pending shop just enough to send a push's event, via + * [SDK.initializeForPush]: no `/init`, no push token, no profile, no popup; the event goes with the shop's + * stored `did`. The app's first [instance] or [initialize] of the shop puts a full instance in its place. */ private fun materializeForPush(shopId: String): SDK? = synchronized(lock) { SdkRegistry.byShopId(shopId)?.let { return it } @@ -425,6 +427,17 @@ object Rees46 { SDK().apply { initializeForPush(pendingInit.context, pendingInit.config) }.also { pending.remove(shopId) } } + /** + * The app's own instance of [sdk]'s shop: [sdk] itself, or, for an instance brought up only for a push + * ([SDK.initializeForPush]), a full one with a full start (`/init`, the push token, everything) that + * takes its place while the push's events in flight finish. + */ + private fun upgraded(sdk: SDK): SDK { + if (!sdk.pushOnly) return sdk + val config = sdk.config ?: return sdk + return initialize(sdk.context, config) + } + /** * Sets a single process-global listener invoked for every received push, on whatever shop it * routes to, with that shop's id. Set it once — typically in `Application.onCreate` — instead of diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index d4887a8d..2cc2c293 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -495,10 +495,12 @@ open class SDK { ) /** - * Light bring-up for handling a push in a (possibly cold) background process: does not re-register - * the push token (it is already on the server — that is why the push arrived) and skips the GAID + - * profile/set call. Just enough to track the delivery and reach the message listener. Used by - * [Rees46.handlePush] to materialize a lazily-registered shop without the full startup work. + * Light bring-up for handling a push in a (possibly cold) background process, used by + * [Rees46.handlePush] for a shop registered but not created. On both cores it never sends `/init`: the + * instance is ready on the shop's stored `did` and sends the push's event with it. It shows no popup and + * sends neither the push token nor the profile. Without a stored `did` the event is dropped + * (`push_dropped`, `no_did`). The app's first [Rees46.instance] or [Rees46.initialize] of the shop puts a + * full instance in its place, and an initialize of this object makes it one. */ internal fun initializeForPush(context: Context, config: Rees46Config) = start( context, @@ -516,18 +518,33 @@ open class SDK { pushOnly: Boolean, ): Unit = synchronized(initLock) { val current = this.config - if (current != null && isLive && current.shopId == config.shopId) { + val sameShop = current != null && isLive && current.shopId == config.shopId + // Brought up for a push and now started by the app: it becomes a full instance, and the push's + // events in flight finish. + val upgrade = sameShop && this.pushOnly && !pushOnly + if (sameShop && !upgrade) { keepConfig(config) return } enableAutoPopupPresentation = config.enableAutoPopupPresentation logLevel = config.logLevel coreV2Requested = config.coreV2 - initializeInternal(context, config, autoSendPushToken, needReInitialization, pushOnly = pushOnly) + // Before the instance registers: a push-only one wakes nobody waiting for the shop's instance. this.pushOnly = pushOnly + initializeInternal(context, config, autoSendPushToken, needReInitialization, pushOnly = pushOnly, cancelRequests = !upgrade) this.config = config } + /** + * A push-only instance ([initializeForPush]) without a stored `did` cannot send a push's event: true + * when this is one, and the push is dropped (`push_dropped`, `no_did`). + */ + internal fun dropsPushWithoutDid(): Boolean { + if (!pushOnly || deviceId != null) return false + log(LogLevel.WARNING, LogCodes.PUSH_DROPPED, "The push's event is dropped: the shop has no did yet", mapOf("reason" to LogReasons.NO_DID)) + return true + } + /** Initialized and not released. */ private val isLive: Boolean get() = isSdkInitialized && !released @@ -548,13 +565,15 @@ open class SDK { config: Rees46Config, autoSendPushToken: Boolean, needReInitialization: Boolean, - // The push-context bring-up: no GAID + profile/set call and, on the new core, no push token. + // The push-context bring-up: no /init, no GAID + profile/set call and no push token. pushOnly: Boolean, + // False when a push-only instance becomes a full one: the push's events in flight finish. + cancelRequests: Boolean = true, ) { val sendProfileOnInit = !pushOnly val shopId = config.shopId // Initialized before (another shop, or released): the old start sends and takes nothing more. - stopCurrent() + stopCurrent(cancelRequests) val sdkComponent = DaggerSdkComponent.factory().create( appModule = AppModule(applicationContext = context) @@ -638,13 +657,14 @@ open class SDK { source = ::trackingSource, sendDirectly = core::sendDirectly, dispatchers = dispatchers, + pushEventsOnly = pushOnly, ).also { it.start() } } this.core = core init.start() } else { core = networkRepository.core - startLegacy(context, config, storage, autoSendPushToken, needReInitialization) + if (pushOnly) startLegacyForPush(config, storage) else startLegacy(context, config, storage, autoSendPushToken, needReInitialization) } // Register this instance so pushes delivered to the (process-global) messaging services are @@ -721,6 +741,15 @@ open class SDK { ) } + /** + * The legacy path brought up only for a push: no `/init`, no session of its own, no popup, no token. + * Ready on the stored `did`, with which the push's event goes; without one it stays initializing. + */ + private fun startLegacyForPush(config: Rees46Config, storage: ShopStorage) { + storage.adoptConfiguredDid(config.deviceId, userSettingsRepository) + instanceState.set(if (userSettingsRepository.getDid().isNotEmpty()) SdkState.READY else SdkState.INITIALIZING) + } + /** The new core's `/init` and state ([CoreInit]); requests wait for it in [CoreV2]. Started by the caller. */ private fun createCoreInit( core: CoreV2, @@ -742,6 +771,7 @@ open class SDK { advertisingId = if (config.sendAdvertisingId) ::readAdvertisingId else null, extraParams = ::popupsV3InitParams, skipInit = { RegisterManager.isPreLaunchDevice(contentResolver) }, + pushOnly = pushOnly, ), send = { params -> core.client.send(NetworkMethod.GET(CoreHttpClient.INIT_PATH), params) }, settings = userSettingsRepository, @@ -2037,6 +2067,7 @@ open class SDK { payload?.let(PushHub::remember) // Show via the process-global listener, then track the delivery on the instance. SdkRegistry.dispatchMessage(shopId = shopId, data = data.toNotificationData()) + if (target.dropsPushWithoutDid()) return val fresh = target.receiveMessage(data = data) if (fresh && payload != null) PushHub.event(PushEvent.DELIVERED, payload, target.pushCallbacks) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SdkRegistry.kt b/personalization-sdk/src/main/kotlin/com/personalization/SdkRegistry.kt index 174bdec7..6d6771d4 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SdkRegistry.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SdkRegistry.kt @@ -64,7 +64,8 @@ internal object SdkRegistry { instances.remove(previous) } current = sdk - toNotify = takeMatchingAwaiters(shopId) + // An instance brought up only for a push is not the one they wait for: the app's own is. + toNotify = if (sdk.pushOnly) emptyList() else takeMatchingAwaiters(shopId) } toNotify.forEach { it.onReady(sdk) } } @@ -89,7 +90,7 @@ internal object SdkRegistry { val alreadyLive: SDK? val awaiter: Awaiter? synchronized(lock) { - alreadyLive = if (shopId != null) byShop[shopId] else current + alreadyLive = (if (shopId != null) byShop[shopId] else current)?.takeUnless { it.pushOnly } if (alreadyLive == null) { awaiter = Awaiter(shopId, onReady) awaiting.add(awaiter) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt index 3fc7bd7a..800f546c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt @@ -39,6 +39,12 @@ internal class InitSetup( val extraParams: () -> Map = { emptyMap() }, /** True on a Google Play pre-launch report device, where `/init` is not sent. */ val skipInit: () -> Boolean = { false }, + /** + * Brought up only to send a push's event (`Rees46.handlePush` for a shop registered but not created): + * `/init` never goes. The instance is ready on the stored `did`, and a new session, a check of the shop + * or the foreground ask nothing of the server: the app's own instance of the shop does. + */ + val pushOnly: Boolean = false, ) /** What the instance does with `/init`. Called on a background thread; an exception is logged. */ @@ -72,6 +78,8 @@ internal interface InitHooks { * foreground; and on [checkShop]. A refused shop is not asked again before the next launch. * - **Session.** `/init` carries the `sid` only while its window is alive; the `seance` of the answer is * the session from then on. The core never makes a `sid` of its own. + * - **Push only** ([InitSetup.pushOnly]). `/init` never goes: the instance is ready on the stored `did`, + * and neither a new session, a check of the shop nor the foreground sends one. * * Requests with a result wait in [awaitUsable]: for the instance to be ready and, when the window * ended, for the `/init` of a new session. A stale answer (an `/init` of a start [restart] or [release] @@ -122,6 +130,9 @@ internal class CoreInit( /** Storage prepared for the first `/init`; none goes before. */ private var prepared = false + /** Completed once the start's storage is prepared, or the instance released; for tests. */ + private val preparedSignal = CompletableDeferred() + /** `/init` is not sent on this device (a Google Play pre-launch report device). */ @Volatile private var skipped = false @@ -169,6 +180,9 @@ internal class CoreInit( synchronized(lock) { job }?.join() } + /** Returns once [start] prepared the storage, and with it decided whether `/init` goes; for tests. */ + internal suspend fun awaitPrepared() = preparedSignal.await() + /** Read once and reused: the advertising id goes with every `/init` of the launch. */ @Volatile private var advertisingIdValue: String? = null @@ -211,15 +225,19 @@ internal class CoreInit( /** On [CoreDispatchers.io]: the disk work of the start, then the first `/init`. */ private fun prepare(start: Int) { - val becameReady = synchronized(lock) { - if (released) return - // A deletion that began meanwhile has the customer forgotten: nothing of theirs comes back. - if (start == generation) prepareStorage() - prepared = true - val ready = state.current == SdkState.INITIALIZING && !held && settings.getDid().isNotEmpty() - if (ready) publish(SdkState.READY) - launchLocked() - ready + val becameReady = try { + synchronized(lock) { + if (released) return + // A deletion that began meanwhile has the customer forgotten: nothing of theirs comes back. + if (start == generation) prepareStorage() + prepared = true + val ready = state.current == SdkState.INITIALIZING && !held && settings.getDid().isNotEmpty() + if (ready) publish(SdkState.READY) + launchLocked() + ready + } + } finally { + preparedSignal.complete(Unit) } if (becameReady) markUsable(start) } @@ -230,6 +248,15 @@ internal class CoreInit( */ internal suspend fun ready(): SdkError? = errorOf(gate.first { it != Gate.WAIT }) + /** + * [ready] within [timeoutMs]: what a push's event waits for. It needs a `did` and no session — the + * server reads no `sid` in it — so it never starts one. `timeout` when the wait runs out. + */ + internal suspend fun awaitReady(timeoutMs: Long): SdkError? { + val verdict = withTimeoutOrNull(timeoutMs) { Verdict(ready()) } ?: return SdkError.Timeout + return verdict.error + } + /** For a request with a result: [usable] within [timeoutMs]. */ suspend fun awaitUsable(timeoutMs: Long): SdkError? { val verdict = withTimeoutOrNull(timeoutMs) { Verdict(usable()) } ?: return SdkError.Timeout @@ -265,7 +292,7 @@ internal class CoreInit( * instance works on what it has and nobody waits; a refused or released instance renews nothing. */ internal fun renewSession(): Boolean = synchronized(lock) { - if (released || skipped || state.current == SdkState.REFUSED || session.isAlive()) return false + if (released || skipped || setup.pushOnly || state.current == SdkState.REFUSED || session.isAlive()) return false session.forgetIfEnded() launchLocked() // Before the storage is prepared the start's own /init is about to go: that is the one to wait for. @@ -311,6 +338,8 @@ internal class CoreInit( synchronized(lock) { if (released) return ShopCheck.Released if (state.current == SdkState.REFUSED) return ShopCheck.Refused + // Only the app's own instance asks the server: the event waits for it. + if (setup.pushOnly) return ShopCheck.Unknown(CHECK_SHOP_INTERVAL_MS) seen = answers.value seenSuccesses = successes val went = checkShop() @@ -402,6 +431,7 @@ internal class CoreInit( } listening?.cancel() scope.cancel() + preparedSignal.complete(Unit) } private fun generation(): Int = synchronized(lock) { generation } @@ -432,7 +462,7 @@ internal class CoreInit( * a deletion holds the instance; true when it went. */ private fun launchLocked(): Boolean { - if (released || held || !prepared || job?.isActive == true) return false + if (released || held || !prepared || setup.pushOnly || job?.isActive == true) return false val start = generation job = scope.launch { run(start) } return true diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt index 02bf9cc4..0aa6bfed 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt @@ -56,7 +56,8 @@ internal abstract class EventAnswerListener : OnApiCallbackListener() { * stamped reaches the disk. * - **Sending.** One event at a time, oldest first, once the instance is ready and the session alive * (a session whose window ended gets its `/init` first), by the one process of the app that holds the - * queue's file lock. An event cleared while it waited (the customer's deletion) does not go. Sending + * queue's file lock. A push's event needs only the `did`: it neither waits for a session nor starts one. + * The queue of an instance brought up only for a push sends the push events alone. An event cleared while it waited (the customer's deletion) does not go. Sending * starts on [SdkState.READY], on the app's foreground and background, on the network's return, on * [flush] and on a timer. * - **Answers.** `2xx` removes the event. No network waits for it and a timeout repeats it after a pause, @@ -99,6 +100,11 @@ internal class EventQueue( private val waitMs: Long = 10_000, /** At most this many events wait; tests make it small. */ private val cap: Int = CAP, + /** + * The queue of an instance brought up only for a push ([InitSetup.pushOnly]): it sends the push events + * and leaves the others on disk for the app's own instance, which starts their session. + */ + private val pushEventsOnly: Boolean = false, ) { private val handler = CoroutineExceptionHandler { _, throwable -> @@ -503,7 +509,7 @@ internal class EventQueue( if (!store.lockForSending()) return Outcome.Waiting val generation = store.generation val unreadable = ArrayList() - val head = store.head(unreadable) + val head = store.head(unreadable) { event -> pushEventsOnly && event.path !in PUSH_EVENT_PATHS } unreadable.forEach { delivery.failure(it.listener, UNREADABLE) } val event = when (head) { QueueStore.Head.Empty -> return Outcome.Idle @@ -516,8 +522,14 @@ internal class EventQueue( continue } if (!connectivity.isConnected) return Outcome.Offline - // Ready, and when the session's window ended, the /init of a new one first. - if (init.awaitUsable(waitMs) != null || !init.session.isAlive()) return Outcome.Waiting + if (event.path in PUSH_EVENT_PATHS) { + // A push's event needs a did and no session: the server reads no sid in it. One that came + // after the window ended starts no /init. + if (init.awaitReady(waitMs) != null) return Outcome.Waiting + } else { + // Ready, and when the session's window ended, the /init of a new one first. + if (init.awaitUsable(waitMs) != null || !init.session.isAlive()) return Outcome.Waiting + } // Cleared while it waited (the customer was deleted): it does not go under the next one. val body = store.ifCurrent(generation, event.id) { stamped(event) } ?: continue val sentAt = clock.nowMillis() diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt index 11f7b4ba..4b687ba6 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/QueueStore.kt @@ -230,11 +230,11 @@ internal class QueueStore private constructor(private val directory: File, priva } /** - * The oldest event. A file that is not an event (it does not parse) is dropped (`queue_dropped`, - * `unreadable`) and its listener goes to [unreadable], to hear an error; a file the disk cannot read - * now stays, and the head is [Head.Unavailable]. + * The oldest event that is not [skipped]. A file that is not an event (it does not parse) is dropped + * (`queue_dropped`, `unreadable`) and its listener goes to [unreadable], to hear an error; a file the + * disk cannot read now stays, and the head is [Head.Unavailable]. */ - fun head(unreadable: MutableList): Head = synchronized(files) { + fun head(unreadable: MutableList, skipped: (QueuedEvent) -> Boolean = { false }): Head = synchronized(files) { for (id in ids()) { val file = File(directory, id + EXTENSION) val text = try { @@ -249,7 +249,10 @@ internal class QueueStore private constructor(private val directory: File, priva return Head.Unavailable } val event = QueuedEvent.parse(id, text) - if (event != null) return Head.Event(patched[id] ?: event) + if (event != null) { + if (skipped(event)) continue + return Head.Event(patched[id] ?: event) + } file.delete() patched -= id synchronized(listenersLock) { listeners.remove(id) }?.let { unreadable += it } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt index 6ee1b8a1..736f00e2 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/logs/SdkLogs.kt @@ -229,6 +229,9 @@ internal object LogReasons { const val ALREADY_INITIALIZED = "already_initialized" const val NOT_READY = "not_ready" + /** A shop brought up only for a push has no `did`: its event cannot go (`push_dropped`). */ + const val NO_DID = "no_did" + // Why the queue dropped events (`queue_dropped`). /** Older than the queue keeps an event (7 days). */ const val EXPIRED = "expired" diff --git a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt index 4529ba4b..e4cff7ae 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt @@ -26,6 +26,7 @@ import com.personalization.features.notification.service.mapper.toNotificationDa import com.personalization.push.PushDismissReceiver import com.personalization.push.PushExtras import com.personalization.logs.LogCodes +import com.personalization.logs.LogReasons import com.personalization.logs.SdkLogs import com.personalization.sdk.data.models.dto.notification.NotificationData import kotlinx.coroutines.flow.first @@ -67,6 +68,9 @@ class PushEventsTest { private lateinit var context: Context private var server = MockWebServer() private val requests = LinkedBlockingQueue>() + + /** Every request the server got, in order, whatever the test took from [requests]. */ + private val seen = CopyOnWriteArrayList>() private val logged = CopyOnWriteArrayList() private val threads = TrackingThreads() private val defaultExecutor = SdkLogs.handlerExecutor @@ -102,6 +106,7 @@ class PushEventsTest { LegacyCore.threads = threads server = MockWebServer() requests.clear() + seen.clear() logged.clear() server.dispatcher = object : Dispatcher() { override fun dispatch(request: RecordedRequest): MockResponse { @@ -110,6 +115,7 @@ class PushEventsTest { val body = if (request.method == "POST") JSONObject(request.body.readUtf8()) else JSONObject().apply { url.queryParameterNames.forEach { put(it, url.queryParameter(it)) } } requests += path to body + seen += path to body if (path == "init") { holdInit?.await(5, TimeUnit.SECONDS) return MockResponse().setBody("""{"did":"D1","seance":"S1"}""") @@ -281,17 +287,104 @@ class PushEventsTest { assertEquals(LogLevel.DEBUG, notOurs.level) } + /** The partition of [shop], where an earlier launch left its did. */ + private fun partition(shop: String = SHOP) = + context.getSharedPreferences(StorageLayout(shop, null).partitionName, Context.MODE_PRIVATE) + @Test - fun `a shop registered but not created takes its push with its stored did`() { - sdk(SHOP).release() - requests.clear() - Rees46.register(context, listOf(config(SHOP))) + fun `a shop registered but not created sends its push with its stored did and no init, and becomes a full instance on use`() { + // An earlier launch's did; the server would hand out another one. + partition().edit().putString("did", "STORED").commit() + Rees46.register(context, listOf(config(SHOP).copy(autoSendPushToken = true))) assertFalse(Rees46.isInitialized(SHOP)) assertEquals(SHOP, Rees46.handlePush(push(code = "cold"), PushEvent.DELIVERED)) - val (path, body) = next("track/delivered") ?: throw AssertionError("no track/delivered") - assertEquals("track/delivered", path) - assertEquals("D1", body.getString("did")) + val light = SdkRegistry.byShopId(SHOP) ?: throw AssertionError("no instance") + assertTrue(light.pushOnly) + assertEquals(SdkState.READY, light.state) + val init = (light.core as com.personalization.core.internal.CoreV2).init ?: throw AssertionError("no init") + runBlocking { withTimeout(5_000) { init.awaitPrepared() } } + flush(light) + + assertEquals("STORED", next("track/delivered")?.second?.getString("did")) + assertEquals("no /init went, nor will", 0, init.sent) + assertFalse(init.isRunning) + assertEquals(emptyList(), drained().map { it.first }.filter { it in listOf("init", "mobile_push_tokens", "profile/set") }) + + // The app's first use: a full instance with a full start takes its place. + val full = Rees46.instance(SHOP) + assertTrue(full !== light) + assertFalse(full.pushOnly) + assertEquals(SdkState.NOT_INITIALIZED, light.state) + assertEquals("STORED", next("init")?.second?.getString("did")) + assertTrue(Rees46.instance(SHOP) === full) + } + + @Test + fun `initializing the instance brought up for a push makes it a full one, and its push still goes`() { + partition().edit().putString("did", "STORED").commit() + Rees46.register(context, listOf(config(SHOP))) + Rees46.handlePush(push(code = "cold"), PushEvent.DELIVERED) + val light = SdkRegistry.byShopId(SHOP) ?: throw AssertionError("no instance") + + light.initializeWith(context, config(SHOP)) + + assertFalse(light.pushOnly) + assertTrue(SdkRegistry.byShopId(SHOP) === light) + assertEquals("STORED", next("init")?.second?.getString("did")) + flush(light) + assertEquals(listOf("cold"), seen.filter { it.first == "track/delivered" }.map { it.second.getString("code") }) + } + + @Test + fun `a push live two hours after the last event goes without an init`() { + val sdk = sdk() + val init = (sdk.core as com.personalization.core.internal.CoreV2).init ?: throw AssertionError("no init") + runBlocking { withTimeout(5_000) { init.awaitIdle() } } + assertEquals(1, init.sent) + // The app sat in the background past the session's window. + partition().edit().putLong("sid_last_act", System.currentTimeMillis() - com.personalization.core.internal.SessionWindow.WINDOW_MS - 1).commit() + assertNull(sdk.sessionId) + + assertEquals(SHOP, Rees46.handlePush(push(code = "late"), PushEvent.DELIVERED)) + flush(sdk) + + assertEquals("D1", next("track/delivered")?.second?.getString("did")) + assertEquals(1, init.sent) + assertFalse(init.isRunning) + assertFalse(drained().any { it.first == "init" }) + } + + @Test + fun `a shop registered but not created and without a did drops its push`() { + Rees46.register(context, listOf(config(SHOP))) + + assertNull(Rees46.handlePush(push(code = "no-did"), PushEvent.DELIVERED)) + val dropped = logged.last { it.code == LogCodes.PUSH_DROPPED } + assertEquals(LogReasons.NO_DID, dropped.context["reason"]) + val light = SdkRegistry.byShopId(SHOP) ?: throw AssertionError("no instance") + val init = (light.core as com.personalization.core.internal.CoreV2).init ?: throw AssertionError("no init") + runBlocking { withTimeout(5_000) { init.awaitPrepared() } } + assertEquals(0, init.sent) + assertEquals(SdkState.INITIALIZING, light.state) + } + + @Test + fun `on the legacy core a shop registered but not created sends its push with its stored did and no init`() { + partition().edit().putString("did", "STORED").commit() + Rees46.register(context, listOf(config(SHOP, coreV2 = false))) + + assertEquals(SHOP, Rees46.handlePush(push(code = "cold"), PushEvent.DELIVERED)) + assertEquals("STORED", next("track/received")?.second?.getString("did")) + // The legacy /init goes out on the caller's thread: had it gone, the server has it by now. + threads.awaitIdle() + assertFalse(drained().any { it.first in listOf("init", "mobile_push_tokens", "profile/set") }) + assertEquals(SdkState.READY, SdkRegistry.byShopId(SHOP)?.state) + + // Without a did the push is dropped. + Rees46.register(context, listOf(config(OTHER, coreV2 = false))) + assertNull(Rees46.handlePush(push(code = "cold", shop = OTHER), PushEvent.DELIVERED)) + assertEquals(LogReasons.NO_DID, logged.last { it.code == LogCodes.PUSH_DROPPED }.context["reason"]) } // Callbacks. diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt index 3317f751..7674f844 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt @@ -127,10 +127,13 @@ class EventQueueTest { private fun ok(did: String = "D1", sid: String = "S1") = HttpResult.Success(JSONObject().put("did", did).put("seance", sid)) private fun http(code: Int, retryAfterMs: Long? = null) = HttpResult.Failure(SdkError.Http(code), """{"status":"error"}""", retryAfterMs) - /** An instance as an app starts it: its `/init`, then its queue over the shop's files. */ - private fun start(cap: Int = EventQueue.CAP): EventQueue { + /** + * An instance as an app starts it: its `/init`, then its queue over the shop's files. [pushOnly]: as + * `Rees46.handlePush` brings up a shop registered but not created. + */ + private fun start(cap: Int = EventQueue.CAP, pushOnly: Boolean = false): EventQueue { val init = CoreInit( - setup = InitSetup(SHOP), + setup = InitSetup(SHOP, pushOnly = pushOnly), send = { initsSent++ if (holdInit) CompletableDeferred().also { initCalls += it }.await() else initAnswer() @@ -174,6 +177,7 @@ class EventQueueTest { clock = clock, backoff = Backoff(random = { 0.5 }), cap = cap, + pushEventsOnly = pushOnly, ).also { queues += it } queue.start() init.start() @@ -917,6 +921,58 @@ class EventQueueTest { assertEquals(2, initsSent) } + // After the review of stages 6 and 7. + + @Test + fun `a push's event after the session's window ended goes with the did and starts no init`() { + val queue = start() + assertEquals(1, initsSent) + clock.advance(SessionWindow.WINDOW_MS + 1) + assertFalse(inits.single().session.isAlive()) + + queue.accept("track/delivered", JSONObject().put("code", "m-1").put("type", "bulk"), null) + run() + + assertEquals(listOf("track/delivered"), sent.map { it.path }) + assertEquals("no /init for a push's event", 1, initsSent) + assertFalse(inits.single().session.isAlive()) + + // An event of the session asks for a new one first. + event(queue, "1") + run() + assertEquals(2, initsSent) + assertEquals("push", sent.last().path) + } + + @Test + fun `the queue of an instance brought up for a push sends its push events alone, ready on the stored did, and asks the server nothing`() { + settings.updateDid("STORED") + val queue = start(pushOnly = true) + assertEquals(SdkState.READY, state.current) + event(queue, "1") + queue.accept("track/delivered", JSONObject().put("code", "m-1").put("type", "bulk"), null) + run() + + assertEquals(listOf("track/delivered"), sent.map { it.path }) + assertEquals("STORED", sent.single().body.getString("did")) + assertEquals("the app's instance sends the other events", 1, files().size) + + // A refusal to a push's event: no check of the shop, the event waits for the app's instance. + eventAnswers += http(404) + queue.accept("track/clicked", JSONObject().put("code", "m-1").put("type", "bulk"), null) + run() + after(CoreInit.CHECK_SHOP_INTERVAL_MS - 1) + clock.advance(SessionWindow.WINDOW_MS + 1) + lifecycle.set(false) + lifecycle.set(true) + run() + assertEquals(0, initsSent) + assertEquals(listOf("track/delivered", "track/clicked"), sent.map { it.path }) + after(1) + assertEquals(listOf("track/delivered", "track/clicked", "track/clicked"), sent.map { it.path }) + assertEquals(0, initsSent) + } + // After the review of stages 4 and 5. @Test From 81b3f428bf4dd60e80437d3f3f98e3de123ba7e0 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 03:11:37 +0400 Subject: [PATCH 059/109] fix(core): flush() ends however the pass it waits for ends, and a request cut off by release() keeps its event until it ends A flush taken by a pass of the queue now ends when the pass ends for any reason: it went through, it threw, or a release() that cancels the sending cut it off; one handed over while the queue is being released ends at once. The request of the event in flight that a cancelling release() lets run keeps the queue's sending lock and the process's file lock until its answer comes: the queue of an instance made right after, or another process, does not send that event again meanwhile, then sends it if it did not go. --- .../core/internal/EventQueue.kt | 88 ++++++++++++------- .../core/internal/EventQueueTest.kt | 69 +++++++++++++++ 2 files changed, 127 insertions(+), 30 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt index 0aa6bfed..995a6c5f 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt @@ -24,7 +24,6 @@ import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.selects.select -import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext import kotlinx.coroutines.withTimeoutOrNull import org.json.JSONObject @@ -310,15 +309,17 @@ internal class EventQueue( /** * Stops sending; the events stay on disk for the next launch. With [cancel] the sending stops at once * and the listeners of this queue's events hear `-1`; the request of the event in flight still ends, - * and an answer that says it went takes the event off the disk. Without, as when a push-only instance - * gives way to the app's, the event in flight finishes and the listeners stay for the queue that - * sends next. Events accepted before are still written; a flush asked before it and not - * handled yet ends with `notInitialized`. + * holding the queue until then so that the queue made next does not send its event again, and an + * answer that says it went takes the event off the disk. Without, as when a push-only instance gives + * way to the app's, the event in flight finishes and the listeners stay for the queue that sends next. + * Events accepted before are still written; a flush asked before it and not handled yet ends with + * `notInitialized`, and one a pass had taken ends. */ fun release(cancel: Boolean = true) { val heard = synchronized(subscriptions) { if (released) return - released = true + // Under the flushes' lock too: a flush handed over after this sees it and ends at once. + synchronized(flushLock) { released = true } subscriptions.toList().also { subscriptions.clear() } } heard.forEach { it.cancel() } @@ -393,8 +394,7 @@ internal class EventQueue( if (owner != null && owner !== this) { owner.flushFromOther(command.done) } else { - synchronized(flushLock) { flushes += command.done } - kick(urgent = true) + addFlush(command.done) } } is Command.Barrier -> command.done.complete(Unit) @@ -402,13 +402,12 @@ internal class EventQueue( } /** A flush asked of another queue of the shop: this one sends. */ - private fun flushFromOther(done: CompletableDeferred) { - if (released) { - done.complete(Unit) - return - } - synchronized(flushLock) { flushes += done } - kick(urgent = true) + private fun flushFromOther(done: CompletableDeferred) = addFlush(done) + + /** Hands [done] to the next pass; a released queue sends nothing more, so it ends at once. */ + private fun addFlush(done: CompletableDeferred) { + val added = synchronized(flushLock) { if (released) false else flushes.add(done) } + if (added) kick(urgent = true) else done.complete(Unit) } private fun write(accept: Command.Accept) { @@ -443,17 +442,18 @@ internal class EventQueue( private suspend fun work() { while (!released) { + val tried = synchronized(flushLock) { flushes.toList().also { flushes.clear() } } val outcome = try { - val tried = synchronized(flushLock) { flushes.toList().also { flushes.clear() } } - val outcome = if (store.isOwner(this)) store.sending.withLock { passHoldingLock() } else Outcome.Idle - tried.forEach { it.complete(Unit) } - outcome + if (store.isOwner(this)) sendingPass() else Outcome.Idle } catch (e: CancellationException) { throw e } catch (e: Exception) { // Never a tight loop: the next pass comes after a pause. log(LogLevel.ERROR, LogCodes.SDK_LOG, "Sending the event queue failed", throwable = e) pause(null) + } finally { + // The flushes this pass took end however it ended: done, failed, or cut off by a release. + tried.forEach { it.complete(Unit) } } when (outcome) { Outcome.Idle -> wake.receive() @@ -490,18 +490,38 @@ internal class EventQueue( done.forEach { it.complete(Unit) } } - /** One pass while this process holds the queue's file lock; when another process holds it, that one sends. */ - private suspend fun passHoldingLock(): Outcome { - if (!store.lockForSending()) return Outcome.Waiting + /** + * Whether the request of the event in flight took over the pass's hold of the queue: a cancelling + * [release] cut the pass off while the request runs ([sendOne]). + */ + private class Hold { + @Volatile + var handedOver = false + } + + /** + * One pass holding [QueueStore.sending] and, while this process holds the queue's file lock, sending; + * when another process holds it, that one sends. A request a cancelling [release] cut off keeps both + * until it ends, so neither the queue made after this one nor another process sends its event again + * meanwhile. + */ + private suspend fun sendingPass(): Outcome { + store.sending.lock() + val hold = Hold() try { - return pass() + if (!store.lockForSending()) return Outcome.Waiting + try { + return pass(hold) + } finally { + if (!hold.handedOver) store.unlockForSending() + } } finally { - store.unlockForSending() + if (!hold.handedOver) store.sending.unlock() } } /** Sends events oldest first until the queue is empty or cannot send now. */ - private suspend fun pass(): Outcome { + private suspend fun pass(hold: Hold): Outcome { urgent = false while (true) { if (released || !store.isOwner(this) || state.current == SdkState.REFUSED) return Outcome.Idle @@ -533,7 +553,7 @@ internal class EventQueue( // Cleared while it waited (the customer was deleted): it does not go under the next one. val body = store.ifCurrent(generation, event.id) { stamped(event) } ?: continue val sentAt = clock.nowMillis() - val result = sendOne(event, body) + val result = sendOne(event, body, hold) // The answer is in: what it decides is done even if the queue is released meanwhile. val next = withContext(NonCancellable) { take(event, result, sentAt) } if (next != null) return next @@ -553,16 +573,24 @@ internal class EventQueue( /** * Sends [event]. The request runs apart from the sending: a [release] that cancels the sending lets it - * end, and an answer that says it went still takes the event off the disk, so it does not go twice. + * end, holding [hold]'s claim on the queue — the sending's lock and the file lock — until then, so the + * event is not sent twice at once; an answer that says it went still takes the event off the disk, so it + * does not go again later either. */ - private suspend fun sendOne(event: QueuedEvent, body: JSONObject): HttpResult { + private suspend fun sendOne(event: QueuedEvent, body: JSONObject, hold: Hold): HttpResult { val call = writing.async { request(event, body) } try { return call.await() } catch (e: CancellationException) { + hold.handedOver = true writing.launch { - val result = call.await() - if (result is HttpResult.Success || (result as? HttpResult.Failure)?.error == SdkError.Decoding) store.remove(event.id) + try { + val result = call.await() + if (result is HttpResult.Success || (result as? HttpResult.Failure)?.error == SdkError.Decoding) store.remove(event.id) + } finally { + store.unlockForSending() + store.sending.unlock() + } } throw e } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt index 7674f844..f7b06e86 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt @@ -973,6 +973,75 @@ class EventQueueTest { assertEquals(0, initsSent) } + @Test + fun `a flush ends however the pass it waits for ends, a release that cuts it off or an exception in it`() { + val queue = start() + holdEvents = true + event(queue, "1") + // Taken by the pass that sends 1 and waits for its answer. + val cutOff = flush(queue) + assertEquals(1, eventCalls.size) + assertFalse(cutOff.isCompleted) + queue.release() + run() + assertTrue("ended by the release", cutOff.isCompleted) + assertNull(cutOff.getCompleted()) + eventCalls.single().complete(HttpResult.Success(JSONObject())) + run() + + // A pass that throws: the flush ends the pause of the first failure, and the pass it is taken by + // throws too. + val next = start() + holdEvents = false + sourceFailures = 2 + event(next, "2") + val failed = flush(next) + assertEquals(0, sourceFailures) + assertTrue("ended by the failed pass", failed.isCompleted) + assertEquals(listOf("1"), ids()) + after(2_000) + assertEquals(listOf("1", "2"), ids()) + } + + @Test + fun `a request cut off by release keeps its event until it ends, and the queue made next does not send it again meanwhile`() { + val queue = start() + holdEvents = true + event(queue, "1") + run() + assertEquals(1, eventCalls.size) + + queue.release() + run() + // The app initializes the shop again: a new queue over the same files. + holdEvents = false + val next = start() + event(next, "2") + run() + flush(next) + assertEquals("1 is still on its way", listOf("1"), ids()) + + // It failed: the new queue sends it, then the next one. + eventCalls.single().complete(HttpResult.Failure(SdkError.Timeout)) + run() + assertEquals(listOf("1", "1", "2"), ids()) + assertTrue(files().isEmpty()) + + // Cut off again, and it went: nobody sends it twice. + holdEvents = true + event(next, "3") + run() + next.release() + holdEvents = false + val third = start() + run() + eventCalls.last().complete(HttpResult.Success(JSONObject())) + run() + flush(third) + assertEquals(listOf("1", "1", "2", "3"), ids()) + assertTrue(files().isEmpty()) + } + // After the review of stages 4 and 5. @Test From 34997a0e5c1142026a1a855bf85b2dcfe284d2bd Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 03:14:12 +0400 Subject: [PATCH 060/109] fix(push): setPushToken around release() answers notInitialized, a claim of a dead process frees the week, and a late bridge hears the token MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On the new core a setPushToken that waits for the instance to be ready when it is released now hears notInitialized instead of nothing, and one made after release() hears notInitialized and keeps nothing for the device; an empty token is refused off the caller's thread too. The week of a token is claimed in the shop's partition with the time of the claim, and the token and the date of the send are written only once the server took it: a failed send gives the claim back, and a claim older than three minutes without a send — its process died before the answer — no longer keeps the token from going for a week. deleteUserCredentials() wipes the claims with the dates. A bridge's onPushToken listener registered after the SDK got a token hears that token at once, once per provider. --- .../core/internal/CorePushTokens.kt | 136 ++++++++++++++---- .../personalization/core/internal/PushHub.kt | 19 ++- .../core/internal/StorageKeys.kt | 15 +- .../com/personalization/PushEventsTest.kt | 34 +++++ .../core/internal/CorePushTokensTest.kt | 52 +++++++ 5 files changed, 225 insertions(+), 31 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt index c07698b7..b6d8b393 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt @@ -9,11 +9,16 @@ import com.personalization.logs.LogCodes import com.personalization.logs.SdkLogs import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import org.json.JSONArray import org.json.JSONObject +import java.util.concurrent.atomic.AtomicBoolean /** * The push token on the new core (`core/push.md`), for one shop. @@ -25,10 +30,14 @@ import org.json.JSONObject * `push_provider` `firebase` or `huawei`) and keeps, in its partition, which token it sent last and * when. A token the shop has not sent goes at once; the same token at most once a week. * - The week is taken before the request: two sends of one token at once make one request, the second - * hearing the first's answer. A failed send gives the week back. + * hearing the first's answer. The claim is kept in the partition with its time, and the token and the + * date of the send only once the server took it. A failed send gives the claim back, and a claim older + * than [CLAIM_TTL_MS] without a send — its process died — is free. * - A token goes only once the instance is ready ([ready]) and never without a `did`. After each start's * `/init` ([sendStored]) the shop sends the device's token if it is due. * - A push-only instance ([sends] false) sends no token. + * - After [release] nothing is sent or kept: [set] answers `notInitialized`, and so does one that was + * waiting for the instance to be ready. */ internal class CorePushTokens( private val shopId: String, @@ -46,58 +55,87 @@ internal class CorePushTokens( private val clock: Clock = Clock.SYSTEM, ) { - private val scope = CoroutineScope(SupervisorJob() + dispatchers.io + CoroutineExceptionHandler { _, throwable -> + private val handler = CoroutineExceptionHandler { _, throwable -> log(LogLevel.ERROR, LogCodes.PUSH_TOKEN_FAILED, "The push token could not be sent", throwable = throwable) - }) + } + + /** The sends waiting for the instance; [release] cancels them. */ + private val scope = CoroutineScope(SupervisorJob() + dispatchers.io + handler) + + /** Answers that come from no running send: a refusal, a send [release] cut off. Never cancelled. */ + private val endings = CoroutineScope(SupervisorJob() + dispatchers.io + handler) private val delivery = LegacyDelivery { level, code, message, throwable -> log(level, code, message, throwable = throwable) } private val lock = Any() + /** Guarded by [lock]: after [release] nothing is kept or sent. */ + private var released = false + /** The sends on their way, by `provider:token`, with the listeners waiting for their answer. */ private val flights = HashMap() - private class Flight(val claimedAt: Long, val previousToken: String?, val previousDate: Long) { + private class Flight(val claimedAt: Long) { val waiting = ArrayList() } + /** Sends waiting for the instance and requests on their way; for [awaitIdle]. */ + private val busy = MutableStateFlow(0) + /** The device's token of [provider], or the one this shop sent last. */ fun token(provider: PushProvider): String? = device.pushToken(provider.id) ?: sentToken(provider) /** A token FCM or HMS gave: kept and sent, when the shop takes them, once the instance is ready. */ fun received(token: String, provider: PushProvider) { if (token.isEmpty() || !autoSend) return - device.savePushToken(provider.id, token) - scope.launch { if (ready() == null) sendIfDue(token, provider, null) } + synchronized(lock) { + if (released) return + device.savePushToken(provider.id, token) + } + launch { if (ready() == null) sendIfDue(token, provider, null) } } /** * A token the app passes (`setPushToken`): kept for the device, then sent by the same rules once the * instance is ready. [listener] hears the server's answer, success without a request when the token - * went this week, or the error. + * went this week, or the error — `notInitialized` after [release], even while it waited for the + * instance; then nothing is kept. */ fun set(token: String, provider: PushProvider, listener: OnApiCallbackListener?) { if (token.isBlank()) { log(LogLevel.WARNING, LogCodes.INVALID_ARGUMENT, "setPushToken: the token is empty", mapOf("reason" to "empty_token")) - listener?.let { delivery.failure(it, LegacyErrors.of(SdkError.InvalidArgument("The token is empty"))) } + refuse(listener, SdkError.InvalidArgument("The token is empty")) + return + } + val kept = synchronized(lock) { + if (!released) device.savePushToken(provider.id, token) + !released + } + if (!kept) { + refuse(listener, SdkError.NotInitialized) return } - device.savePushToken(provider.id, token) - scope.launch { + // Who answers [listener]: the send, or the release that cut it off before it went. + val handed = AtomicBoolean(false) + val job = launch { val error = ready() + if (!handed.compareAndSet(false, true)) return@launch if (error != null) { listener?.let { delivery.failure(it, LegacyErrors.of(error)) } return@launch } sendIfDue(token, provider, listener) } + job.invokeOnCompletion { cause -> + if (cause != null && handed.compareAndSet(false, true)) refuse(listener, SdkError.NotInitialized) + } } /** After a start's `/init`: the device's tokens go if they are due. */ fun sendStored() { PushProvider.entries.forEach { provider -> val token = device.pushToken(provider.id) ?: return@forEach - scope.launch { sendIfDue(token, provider, null) } + launch { sendIfDue(token, provider, null) } } } @@ -122,16 +160,18 @@ internal class CorePushTokens( listener?.let { going.waiting += it } return } - val sentToken = sentToken(provider) - val sentAt = sentDate(provider) - if (sentToken == token && now - sentAt in 0 until WEEK_MS) { + val sentThisWeek = sentToken(provider) == token && now - sentDate(provider) in 0 until WEEK_MS + // Taken by another process of the app moments ago; one whose process died is free. + val claimed = claimedToken(provider) == token && now - claimedAt(provider) in 0 until CLAIM_TTL_MS + if (sentThisWeek || claimed) { null } else { // The week is taken before the request: a second send meanwhile finds it taken. - Flight(now, sentToken, sentAt).also { claimed -> - listener?.let { claimed.waiting += it } - flights[key] = claimed - shop.edit().putString(StorageKeys.pushToken(provider.id), token).putLong(StorageKeys.pushTokenDate(provider.id), now).apply() + Flight(now).also { taken -> + listener?.let { taken.waiting += it } + flights[key] = taken + busy.update { it + 1 } + shop.edit().putString(StorageKeys.pushTokenClaim(provider.id), token).putLong(StorageKeys.pushTokenClaimedAt(provider.id), now).apply() } } } @@ -167,23 +207,56 @@ internal class CorePushTokens( private fun landed(failed: Boolean = false, tell: (List) -> Unit) { val waiting = synchronized(lock) { val ended = flights.remove(key) ?: return - if (failed) giveBack(provider, token, ended) + settle(provider, token, ended, sent = !failed) ended.waiting.toList() } - tell(waiting) + try { + tell(waiting) + } finally { + busy.update { it - 1 } + } } }) } - /** Must hold [lock]. A failed send gives the week back, unless something else wrote the claim over meanwhile. */ - private fun giveBack(provider: PushProvider, token: String, flight: Flight) { - if (sentToken(provider) != token || sentDate(provider) != flight.claimedAt) return + /** + * Must hold [lock]. The claim of [flight] ends: a send that went is recorded as the shop's, and the + * claim goes unless another one wrote over it meanwhile. + */ + private fun settle(provider: PushProvider, token: String, flight: Flight, sent: Boolean) { val editor = shop.edit() - if (flight.previousToken == null) editor.remove(StorageKeys.pushToken(provider.id)) else editor.putString(StorageKeys.pushToken(provider.id), flight.previousToken) - if (flight.previousDate == 0L) editor.remove(StorageKeys.pushTokenDate(provider.id)) else editor.putLong(StorageKeys.pushTokenDate(provider.id), flight.previousDate) + if (sent) { + editor.putString(StorageKeys.pushToken(provider.id), token).putLong(StorageKeys.pushTokenDate(provider.id), flight.claimedAt) + } + if (claimedToken(provider) == token && claimedAt(provider) == flight.claimedAt) { + editor.remove(StorageKeys.pushTokenClaim(provider.id)).remove(StorageKeys.pushTokenClaimedAt(provider.id)) + } editor.apply() } + private fun claimedToken(provider: PushProvider): String? = shop.getString(StorageKeys.pushTokenClaim(provider.id), null) + + private fun claimedAt(provider: PushProvider): Long = shop.getLong(StorageKeys.pushTokenClaimedAt(provider.id), 0L) + + /** Runs [block] on the sends' scope, counted for [awaitIdle]. */ + private fun launch(block: suspend () -> Unit): Job { + busy.update { it + 1 } + val job = scope.launch { block() } + job.invokeOnCompletion { busy.update { it - 1 } } + return job + } + + /** Hands [listener] [error] off the caller's thread. */ + private fun refuse(listener: OnApiCallbackListener?, error: SdkError) { + listener ?: return + endings.launch { delivery.failure(listener, LegacyErrors.of(error)) } + } + + /** Returns once nothing waits for the instance and no request is on its way; for tests. */ + internal suspend fun awaitIdle() { + busy.first { it == 0 } + } + /** The token this shop sent last, under the key of this release or of the ones before. */ private fun sentToken(provider: PushProvider): String? = shop.getString(StorageKeys.pushToken(provider.id), null)?.takeIf { it.isNotEmpty() } @@ -199,8 +272,14 @@ internal class CorePushTokens( private fun skipped(provider: PushProvider, reason: String) = log(LogLevel.DEBUG, LogCodes.PUSH_TOKEN_SKIPPED, "Push token not sent", mapOf("provider" to provider.id, "reason" to reason)) - /** Stops the sends waiting for the instance; a request on its way still ends. */ - fun release() = scope.cancel() + /** + * Stops the sends waiting for the instance — a [set] among them hears `notInitialized` — and keeps or + * sends nothing more; a request on its way still ends. + */ + fun release() { + synchronized(lock) { released = true } + scope.cancel() + } private fun log(level: LogLevel, code: String, message: String, context: Map = emptyMap(), throwable: Throwable? = null) = SdkLogs.log(level, code, message, shopId, context, throwable) @@ -209,6 +288,9 @@ internal class CorePushTokens( const val PATH = "mobile_push_tokens" const val WEEK_MS = 7 * 24 * 60 * 60 * 1000L + /** A claim this old without a send is free: the process that took it died before the answer. */ + const val CLAIM_TTL_MS = 3 * 60 * 1000L + const val REASON_SENT_THIS_WEEK = "sent_this_week" const val REASON_NO_DID = "no_did" const val REASON_PUSH_ONLY = "push_only" diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt index 00bb53b8..dd70512d 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt @@ -65,7 +65,7 @@ internal object PushHub { /** The last token of each provider the facade's `onToken` heard of. */ private val facadeReported = ConcurrentHashMap() - /** The last token of each provider the bridges heard of. */ + /** The last token of each provider the SDK got in this process: what the bridges heard, or hear on joining. */ private val sinkReported = ConcurrentHashMap() /** True the first time [event] of the message [code] comes for [shopId] in this process. */ @@ -107,7 +107,10 @@ internal object PushHub { val ownNew = ownListener != null && ownReported.put(token.provider, token.token) != token.token val facadeListener = if (ownListener == null) callbacks?.onToken else null val facade = facadeListener?.takeIf { facadeReported.put(token.provider, token.token) != token.token } - val sinks = if (sinkReported.put(token.provider, token.token) != token.token) tokenSinks.toList() else emptyList() + // With the sinks' lock: a sink that joins meanwhile hears this token once, here or on joining. + val sinks = synchronized(tokenSinks) { + if (sinkReported.put(token.provider, token.token) != token.token) tokenSinks.toList() else emptyList() + } if (!ownNew && facade == null && sinks.isEmpty()) return run(shopId) { if (ownNew) ownListener?.let { call(shopId, "A push token callback threw") { it.onToken(token) } } @@ -121,8 +124,18 @@ internal object PushHub { return Cancellable { eventSinks -= sink } } + /** + * Adds a bridge's token listener. A token the SDK got before it joined comes to it at once, once per + * provider, on the main thread: a bridge that registers after the token came still hears it. + */ fun addTokenSink(sink: PushTokenListener): Cancellable { - tokenSinks += sink + val current = synchronized(tokenSinks) { + tokenSinks += sink + sinkReported.map { (provider, token) -> PushToken(token, provider) } + } + if (current.isNotEmpty()) { + run(null) { current.forEach { token -> call(null, "A bridge's token listener threw") { sink.onToken(token) } } } + } return Cancellable { tokenSinks -= sink } } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt index 2fa37cc0..c2f72c4a 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/StorageKeys.kt @@ -29,6 +29,15 @@ internal object StorageKeys { /** When the shop last sent the push token of a provider. */ fun pushTokenDate(provider: String): String = "push_token_date_$provider" + /** + * The token of a provider a send of the new core took the week for, before the server answered: the + * claim, with its time ([pushTokenClaimedAt]). Gone once the send ends. + */ + fun pushTokenClaim(provider: String): String = "push_token_claim_$provider" + + /** When the claim of [pushTokenClaim] was taken. */ + fun pushTokenClaimedAt(provider: String): String = "push_token_claimed_at_$provider" + /** The token keys of releases before the provider was part of the key; still read as a fallback. */ val LEGACY_PUSH_TOKEN: Map = mapOf("fcm" to "token", "hms" to "hms_token") @@ -98,7 +107,11 @@ internal object StorageKeys { add(TRACKING_SOURCE_TYPE) add(TRACKING_SOURCE_CODE) add(TRACKING_SOURCE_SAVED_AT) - PUSH_PROVIDERS.forEach { add(pushTokenDate(it)) } + PUSH_PROVIDERS.forEach { + add(pushTokenDate(it)) + add(pushTokenClaim(it)) + add(pushTokenClaimedAt(it)) + } addAll(LEGACY_PUSH_TOKEN_DATE.values) add(REFUSED) } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt index e4cff7ae..59aa702c 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt @@ -499,6 +499,40 @@ class PushEventsTest { assertEquals(listOf("tok"), heardTokens) } + @OptIn(com.personalization.core.BridgeApi::class) + @Test + fun `setPushToken after release answers notInitialized and keeps nothing`() { + val sdk = sdk() + sdk.release() + val heard = LinkedBlockingQueue() + + sdk.setPushToken("after", PushProvider.FCM, object : com.personalization.api.OnApiCallbackListener() { + override fun onError(code: Int, msg: String?) { + heard += code + heard += com.personalization.core.BridgeErrors.currentError() ?: "none" + } + }) + + assertEquals(-1, heard.poll(5, TimeUnit.SECONDS)) + assertEquals(com.personalization.core.SdkError.NotInitialized, heard.poll(5, TimeUnit.SECONDS)) + assertNull(DeviceStorage(context).pushToken("fcm")) + } + + @OptIn(com.personalization.core.BridgeApi::class) + @Test + fun `a bridge that registers after the token came hears it once on registration`() { + sdk() + SDK.onPushTokenReceived("early", PushProvider.FCM) + val tokens = CopyOnWriteArrayList() + + Rees46.onPushToken { tokens += it.token } + assertEquals(listOf("early"), tokens) + + SDK.onPushTokenReceived("early", PushProvider.FCM) + SDK.onPushTokenReceived("next", PushProvider.FCM) + assertEquals(listOf("early", "next"), tokens) + } + // A notification the SDK draws. /** A push of [shop] with every field a callback reads. */ diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CorePushTokensTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CorePushTokensTest.kt index b95d8966..041077bd 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CorePushTokensTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CorePushTokensTest.kt @@ -103,12 +103,14 @@ class CorePushTokensTest { private class Heard : OnApiCallbackListener() { val calls = CopyOnWriteArrayList() + val errors = CopyOnWriteArrayList() override fun onSuccess(response: JSONObject?) { calls += "success" } override fun onError(code: Int, msg: String?) { calls += "error:$code" + errors += CurrentError.get() } } @@ -233,6 +235,8 @@ class CorePushTokensTest { val empty = Heard() tokens.set("", PushProvider.FCM, empty) + assertTrue("off the caller's thread", empty.calls.isEmpty()) + run() assertEquals(listOf("error:-1"), empty.calls) readiness = CompletableDeferred().apply { complete(SdkError.ShopBlocked) } @@ -256,6 +260,54 @@ class CorePushTokensTest { assertEquals("from-app", sent.single().second.getString("token")) } + @Test + fun `setPushToken waiting for ready when the instance is released hears notInitialized, and one after release keeps and sends nothing`() { + readiness = CompletableDeferred() + val tokens = tokens() + val waiting = Heard() + tokens.set("t1", PushProvider.FCM, waiting) + run() + assertTrue(waiting.calls.isEmpty()) + + tokens.release() + run() + assertEquals(listOf("error:-1"), waiting.calls) + assertEquals(listOf(SdkError.NotInitialized), waiting.errors) + + val after = Heard() + tokens.set("t2", PushProvider.FCM, after) + run() + assertEquals(listOf("error:-1"), after.calls) + assertEquals(listOf(SdkError.NotInitialized), after.errors) + assertEquals("the token of before release", "t1", DeviceStorage(context).pushToken("fcm")) + readiness.complete(null) + run() + assertTrue(sent.isEmpty()) + } + + @Test + fun `a claim whose process died before the answer frees the week after a few minutes`() { + tokens().received("t1", PushProvider.FCM) + run() + assertEquals(1, sent.size) + // The process dies before the answer: the next one finds the claim. + answers.clear() + val next = tokens() + next.sendStored() + run() + assertEquals("a claim moments old is another process's send", 1, sent.size) + + clock.advance(CorePushTokens.CLAIM_TTL_MS) + next.sendStored() + run() + assertEquals(2, sent.size) + ok() + next.sendStored() + run() + assertEquals("the send that went counts the week", 2, sent.size) + assertEquals(listOf(CorePushTokens.REASON_SENT_THIS_WEEK, CorePushTokens.REASON_SENT_THIS_WEEK), reasons()) + } + private companion object { const val SHOP = "token-shop" const val OTHER = "token-other" From bb2d9680fb544e850e4c2dba3c1b6cbea0e2cd3f Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 03:15:58 +0400 Subject: [PATCH 061/109] fix(bridge): the checks of the tracking managers reach a bridge as invalidArgument with their message, and queueEvent refuses fields JSON cannot hold A purchase, a custom event or a story event the SDK refuses before anything goes (an empty order, reserved custom keys, no stories code) now reports through the same path as the core's failures: the old onError(code, message) keeps its code and message, and BridgeErrors gives invalidArgument with the message behind it, on both cores, instead of a network error made from the code. On the new core the refusal comes off the caller's thread, once, like every other failure; the legacy core answers on the caller's thread as before. SDK.queueEvent with fields JSON cannot hold (NaN, Infinity) answers invalidArgument through its listener instead of throwing at the bridge. --- .../main/kotlin/com/personalization/SDK.kt | 16 +++++++- .../core/internal/ArgumentRefusals.kt | 40 +++++++++++++++++++ .../trackEvent/impl/TrackEventManagerImpl.kt | 15 +++++-- .../personalization/stories/StoriesManager.kt | 12 +++++- .../com/personalization/PushEventsTest.kt | 28 +++++++++++++ 5 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 personalization-sdk/src/main/kotlin/com/personalization/core/internal/ArgumentRefusals.kt diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 2cc2c293..1fab3d3a 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -1892,7 +1892,8 @@ open class SDK { * * [listener] hears once, on the main thread: [SdkResult.Success] once the event is accepted — written * to disk, the way the contract's tracking methods complete — or [SdkResult.Failure] with - * [SdkError.InvalidArgument] for another path, [SdkError.NotInitialized], or [SdkError.ShopBlocked]. On + * [SdkError.InvalidArgument] for another path or fields JSON cannot hold (NaN, Infinity), + * [SdkError.NotInitialized], or [SdkError.ShopBlocked]. It never throws. On * the legacy core, which has no queue, the event goes at once and [listener] hears the server's answer. */ @BridgeApi @@ -1907,7 +1908,18 @@ open class SDK { calls.launch { complete(listener, SdkResult.Failure(error ?: SdkError.NotInitialized)) } return } - val copy = JSONObject(body.toString()) + // A copy of the fields as they are now; null for fields JSON cannot hold (NaN, Infinity). + val copy = try { + val text: String? = body.toString() + text?.let(::JSONObject) + } catch (e: Exception) { + null + } + if (copy == null) { + log(LogLevel.WARNING, LogCodes.INVALID_ARGUMENT, "A bridge's event was refused: its fields are not JSON", mapOf("reason" to "invalid_json", "path" to path)) + calls.launch { complete(listener, SdkResult.Failure(SdkError.InvalidArgument("The event's fields cannot be written as JSON"))) } + return + } if (core is CoreV2) { core.accept(path, copy) { refused -> calls.launch { complete(listener, if (refused == null) SdkResult.Success(Unit) else SdkResult.Failure(refused)) } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ArgumentRefusals.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ArgumentRefusals.kt new file mode 100644 index 00000000..07c4a92c --- /dev/null +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/ArgumentRefusals.kt @@ -0,0 +1,40 @@ +package com.personalization.core.internal + +import com.personalization.api.OnApiCallbackListener +import com.personalization.core.SdkError +import com.personalization.logs.SdkLogs +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.launch + +/** + * A call the SDK refuses before anything goes — an argument it cannot take — reported to the old listener + * as the core reports its failures: `onError(code, message)` with [SdkError.InvalidArgument] and the + * message behind it ([CurrentError]), so a bridge hands on `invalidArgument` with its text and not a + * network error made from the code. + * + * The new core answers off the caller's thread, once, as it answers every failure; an exception the + * listener throws is logged. The legacy core answers on the caller's thread, as it always did. + */ +internal object ArgumentRefusals { + + private val scope = CoroutineScope(SupervisorJob() + CoreDispatchers.DEFAULT.io) + + private val delivery = LegacyDelivery { level, code, message, throwable -> SdkLogs.log(level, code, message, throwable = throwable) } + + /** + * Tells [listener] `onError([code], [message])`; [offThread] on the new core. The error behind it is + * `invalidArgument` with [message], or a general text when there is none. + */ + fun refuse(listener: OnApiCallbackListener?, code: Int, message: String?, offThread: Boolean) { + listener ?: return + val error = SdkError.InvalidArgument(message ?: DEFAULT_MESSAGE) + if (offThread) { + scope.launch { delivery.failure(listener, code, message, error) } + } else { + CurrentError.with(error) { listener.onError(code, message) } + } + } + + private const val DEFAULT_MESSAGE = "The call's arguments are not valid" +} diff --git a/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt b/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt index 9c8ebb3a..d973ff49 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/features/trackEvent/impl/TrackEventManagerImpl.kt @@ -12,6 +12,7 @@ import com.personalization.api.managers.InAppNotificationManager import com.personalization.api.managers.TrackEventManager import com.personalization.api.models.purchase.PurchaseTrackingRequest import com.personalization.api.params.ProductItemParams +import com.personalization.core.internal.ArgumentRefusals import com.personalization.core.internal.EventAnswerListener import com.personalization.sdk.data.mappers.popup.PopupDtoMapper import com.personalization.sdk.data.models.params.SdkInitializationParams.PARAM_POPUP @@ -65,7 +66,7 @@ internal class TrackEventManagerImpl @Inject constructor( override fun trackPurchase(request: PurchaseTrackingRequest, listener: OnApiCallbackListener?) { val body = PurchaseTrackingJsonBuilder.buildOrError(request).getOrElse { error -> - listener?.onError(PurchaseTrackingJsonBuilder.CLIENT_VALIDATION_ERROR_CODE, error.message) + refuse(listener, PurchaseTrackingJsonBuilder.CLIENT_VALIDATION_ERROR_CODE, error.message) return } popups()?.onPurchase() @@ -99,7 +100,7 @@ internal class TrackEventManagerImpl @Inject constructor( ) { val effectiveCustom = TrackCustomEventPayloadHelper.effectiveCustomFields(customFields) TrackCustomEventPayloadHelper.validateNoReservedKeyCollisions(effectiveCustom)?.let { msg -> - listener?.onError(TrackCustomEventPayloadHelper.CLIENT_VALIDATION_ERROR_CODE, msg) + refuse(listener, TrackCustomEventPayloadHelper.CLIENT_VALIDATION_ERROR_CODE, msg) return } @@ -129,7 +130,8 @@ internal class TrackEventManagerImpl @Inject constructor( } body.put(PAYLOAD_PARAMETER, payload) } catch (e: JSONException) { - listener?.onError( + refuse( + listener, TrackCustomEventPayloadHelper.CLIENT_VALIDATION_ERROR_CODE, "trackEvent: failed to build custom fields payload: ${e.message}" ) @@ -180,6 +182,13 @@ internal class TrackEventManagerImpl @Inject constructor( postCustomEvent(params.build(), listener) } + /** + * An argument the call cannot take: `invalidArgument` behind the old code ([ArgumentRefusals]), off the + * caller's thread on the new core. + */ + private fun refuse(listener: OnApiCallbackListener?, code: Int, message: String?) = + ArgumentRefusals.refuse(listener, code, message, offThread = sendNetworkMethodUseCase.queuesEvents) + override fun trackPopupShown(popupId: Int, listener: OnApiCallbackListener?) { val params = Params() params.put(UserBasicParams.SHOP_ID, getUserSettingsValueUseCase.getShopId()) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/stories/StoriesManager.kt b/personalization-sdk/src/main/kotlin/com/personalization/stories/StoriesManager.kt index 8046f49e..d16bc39c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/stories/StoriesManager.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/stories/StoriesManager.kt @@ -7,6 +7,7 @@ import android.os.Handler import android.os.Looper import com.personalization.SDK import com.personalization.api.OnApiCallbackListener +import com.personalization.core.internal.ArgumentRefusals import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase import com.personalization.sdk.domain.usecases.trackingSource.SetTrackingSourceUseCase import com.personalization.stories.models.Story @@ -137,7 +138,7 @@ class StoriesManager @Inject constructor( if (effectiveCode == null) { val message = "trackStory($event): no stories code given and no block loaded yet" SdkLogs.log(LogLevel.WARNING, LogCodes.STORIES, message, logShopId) - listener?.onError(CLIENT_VALIDATION_ERROR_CODE, message) + refuse(listener, message) return } try { @@ -152,10 +153,17 @@ class StoriesManager @Inject constructor( sendNetworkMethodUseCase.postAsync(TRACK_STORIES_METHOD, params, listener) } catch (e: JSONException) { SdkLogs.log(LogLevel.ERROR, LogCodes.STORIES, "Stories failed", logShopId, throwable = e) - listener?.onError(CLIENT_VALIDATION_ERROR_CODE, e.message) + refuse(listener, e.message) } } + /** + * A story event that cannot go: `invalidArgument` behind the old code ([ArgumentRefusals]), off the + * caller's thread on the new core. + */ + private fun refuse(listener: OnApiCallbackListener?, message: String?) = + ArgumentRefusals.refuse(listener, CLIENT_VALIDATION_ERROR_CODE, message, offThread = sendNetworkMethodUseCase.queuesEvents) + /** * A numeric id keeps going on the wire as a number, the way `trackStory(storyId: Int)` always sent * it. Converted only when the number renders back to the same text, so ids like `"0123"` — which diff --git a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt index 59aa702c..e813a235 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt @@ -660,11 +660,35 @@ class PushEventsTest { results.poll(5, TimeUnit.SECONDS), ) + // The checks of the tracking managers: invalidArgument with their message, off the caller's thread. + val caller = Thread.currentThread() + val threads = CopyOnWriteArrayList() + sdk.tracking.custom("e", null, null, null, null, mapOf("did" to "mine"), com.personalization.core.BridgeErrors.listener { threads += Thread.currentThread(); results += it }) + val reserved = results.poll(5, TimeUnit.SECONDS) + assertTrue(reserved is com.personalization.core.SdkResult.Failure && reserved.error is com.personalization.core.SdkError.InvalidArgument) + assertTrue(((reserved as com.personalization.core.SdkResult.Failure).error as com.personalization.core.SdkError.InvalidArgument).message.contains("did")) + sdk.tracking.storyView("42", "3", null, com.personalization.core.BridgeErrors.listener { threads += Thread.currentThread(); results += it }) + val noCode = results.poll(5, TimeUnit.SECONDS) + assertTrue(((noCode as com.personalization.core.SdkResult.Failure).error as com.personalization.core.SdkError.InvalidArgument).message.contains("no stories code")) + assertTrue(threads.none { it === caller }) + // Without the core's error (the legacy core), one made from the code. assertEquals(com.personalization.core.SdkError.Http(500), com.personalization.core.BridgeErrors.errorOf(500, null)) assertEquals(com.personalization.core.SdkError.Network, com.personalization.core.BridgeErrors.errorOf(-1, null)) } + @OptIn(com.personalization.core.BridgeApi::class) + @Test + fun `on the legacy core a bridge gets invalidArgument with its message for a check of the tracking managers, on the caller's thread as before`() { + val sdk = sdk(coreV2 = false) + val results = CopyOnWriteArrayList>() + + sdk.tracking.custom("e", null, null, null, null, mapOf("did" to "mine"), com.personalization.core.BridgeErrors.listener { results += it }) + + val reserved = results.single() + assertTrue(reserved is com.personalization.core.SdkResult.Failure && reserved.error is com.personalization.core.SdkError.InvalidArgument) + } + @OptIn(com.personalization.core.BridgeApi::class) @Test fun `a bridge's event completes once it is accepted, and its push and token streams hear everything once`() { @@ -681,6 +705,10 @@ class PushEventsTest { sdk.queueEvent("profile/set", JSONObject(), completions) val refused = takeOnMain(main, ends) assertTrue(refused is com.personalization.core.SdkResult.Failure && refused.error is com.personalization.core.SdkError.InvalidArgument) + // Fields JSON cannot hold: refused through the listener, never thrown at the bridge. + sdk.queueEvent("push/custom", JSONObject(mapOf("event" to "nan", "value" to Double.NaN)), completions) + val notJson = takeOnMain(main, ends) + assertTrue(notJson is com.personalization.core.SdkResult.Failure && notJson.error is com.personalization.core.SdkError.InvalidArgument) flush(sdk) assertEquals("from_dart", next("push/custom")?.second?.getString("event")) From c9ff3d2d1e571a49e2b0777e3dd0dfef0173ffd0 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 03:16:48 +0400 Subject: [PATCH 062/109] fix(core): an /init answer without a seance wakes the queue that waits for the session at once The session's listeners now hear an /init that answers without a seance too, with the sid it keeps and as no change of the session: an event queue that stopped waiting for the new session's /init goes on at once instead of on its one-minute timer. The bridges' onSessionChange still hears only a new sid. --- .../core/internal/SessionWindow.kt | 19 ++++++++++++++----- .../core/internal/EventQueueTest.kt | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/SessionWindow.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/SessionWindow.kt index f7569b1f..f380c80c 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/SessionWindow.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/SessionWindow.kt @@ -20,8 +20,10 @@ import java.util.concurrent.CopyOnWriteArrayList * - Only `/init` ([start]) and the events of the queue ([extend]) move the last activity; requests with * a result do not, as the server does not extend its own session on them either. * - * Listeners hear every `sid` `/init` brings, and whether it differs from the one before ("the session - * changed", for the bridges). They are called on the thread that stored it and must not block. + * Listeners hear every `/init` that answers: the `sid` it brings and whether it differs from the one + * before ("the session changed", for the bridges), or, from an answer without a `seance`, the `sid` kept + * (empty when none) and `changed` false — a queue waiting for the session goes on at once either way. + * They are called on the thread that stored it and must not block. */ internal class SessionWindow( private val settings: UserSettingsRepository, @@ -69,14 +71,17 @@ internal class SessionWindow( /** * `/init` answered without a `seance`: a session alive from now, keeping the `sid` it has if its window - * is alive, and without one otherwise. Listeners hear nothing: no `sid` came. + * is alive, and without one otherwise. Listeners hear the `sid` kept, not as a change: no `sid` came, + * but whoever waits for the session may go on. */ fun startWithoutSid() { - synchronized(lock) { + val kept = synchronized(lock) { val now = clock.nowMillis() if (settings.getSid().isEmpty() || !alive(now)) withoutSid = true storage.preferences.edit().putLong(StorageKeys.SID_LAST_ACT, now).apply() + settings.getSid() } + tell(kept, changed = false) } /** `/init` answered with [sid]: the session is [sid] and its last activity now. */ @@ -89,6 +94,10 @@ internal class SessionWindow( .apply() (sid != lastKnown).also { lastKnown = sid } } + tell(sid, changed) + } + + private fun tell(sid: String, changed: Boolean) { listeners.forEach { listener -> try { listener(sid, changed) @@ -98,7 +107,7 @@ internal class SessionWindow( } } - /** Calls [listener] with each `sid` `/init` brings until the handle is cancelled. */ + /** Calls [listener] on each answer of `/init` ([start], [startWithoutSid]) until the handle is cancelled. */ fun addListener(listener: (sid: String, changed: Boolean) -> Unit): Cancellable { listeners += listener return Cancellable { listeners -= listener } diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt index f7b06e86..70a33e98 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt @@ -973,6 +973,24 @@ class EventQueueTest { assertEquals(0, initsSent) } + @Test + fun `an init answer without a seance wakes the queue that waits for the session at once`() { + val queue = start() + clock.advance(SessionWindow.WINDOW_MS + 1) + holdInit = true + event(queue, "1") + run() + assertEquals("the new session's /init is on its way", 1, initCalls.size) + // The wait for the instance runs out: the queue waits for a trigger or its timer. + after(10_000) + assertTrue(sent.isEmpty()) + + initCalls.single().complete(HttpResult.Success(JSONObject().put("did", "D1"))) + run() + + assertEquals("at once, not on the timer", listOf("1"), ids()) + } + @Test fun `a flush ends however the pass it waits for ends, a release that cuts it off or an exception in it`() { val queue = start() From 21b5de630511522f3d5d3451737658c5dafbe190 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 03:17:52 +0400 Subject: [PATCH 063/109] fix(push): a push event counts as sent once the queue accepted it, so a refused one goes when it comes again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The rule of one event of one message per process now holds the event only while the queue decides and keeps it once the queue accepted it. An event the queue refused — the instance released, the shop refused — is forgotten and logged (push, "The push event was not accepted"), so the same event of the message can come again later and go. A repeat while the first is being accepted still sends nothing. --- .../main/kotlin/com/personalization/SDK.kt | 17 +++++++++---- .../personalization/core/internal/PushHub.kt | 10 +++++++- .../com/personalization/PushEventsTest.kt | 25 +++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 1fab3d3a..63101333 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -1862,18 +1862,25 @@ open class SDK { /** * The new core's push event: `track/delivered`, `track/opened`, `track/clicked` or `track/closed` * through the queue with `code` and `type`, once per message and event in a process (a repeat is a - * no-op, false). A tap also sets the tracking source for 48 hours, when its type is one of - * [TrackingSourceType]: the `/push` events carry it as `source`. + * no-op, false). The event counts as sent once the queue accepted it: one the queue refused (the + * instance released, the shop refused) may come again and go. A tap also sets the tracking source for + * 48 hours, when its type is one of [TrackingSourceType]: the `/push` events carry it as `source`. */ private fun queuePushEvent(event: PushEvent, code: String, type: String): Boolean { - if (!PushHub.firstTime(shopId, event, code)) { - log(LogLevel.DEBUG, LogCodes.PUSH, "The push event was handled already", mapOf("event" to event.name.lowercase(), "reason" to REASON_REPEAT)) + val core = core as? CoreV2 ?: return false + val shop = shopId + if (!PushHub.firstTime(shop, event, code)) { + log(LogLevel.DEBUG, LogCodes.PUSH, "The push event was handled already", mapOf("event" to event.name.lowercase(Locale.ROOT), "reason" to REASON_REPEAT)) return false } if (event == PushEvent.CLICKED && TrackingSourceType.entries.any { it.value == type }) { notificationHandler.setClickSource(type = type, code = code) } - sendNetworkMethodUseCase.postAsync(pathOf(event), JSONObject().put(CODE_FIELD, code).put(TYPE_FIELD, type)) + core.accept(pathOf(event), JSONObject().put(CODE_FIELD, code).put(TYPE_FIELD, type)) { refused -> + if (refused == null) return@accept + PushHub.forget(shop, event, code) + log(LogLevel.WARNING, LogCodes.PUSH, "The push event was not accepted", mapOf("event" to event.name.lowercase(Locale.ROOT), "error" to refused.javaClass.simpleName)) + } return true } diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt index dd70512d..c808d218 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/PushHub.kt @@ -68,11 +68,19 @@ internal object PushHub { /** The last token of each provider the SDK got in this process: what the bridges heard, or hear on joining. */ private val sinkReported = ConcurrentHashMap() - /** True the first time [event] of the message [code] comes for [shopId] in this process. */ + /** + * True the first time [event] of the message [code] comes for [shopId] in this process: it is taken, + * and a repeat is not sent. [forget] gives it back when the queue did not accept the event. + */ fun firstTime(shopId: String, event: PushEvent, code: String): Boolean = synchronized(seen) { seen.put("$shopId|${event.name}|$code", true) == null } + /** [event] of the message [code] was not accepted for [shopId]: it may come again and go. */ + fun forget(shopId: String, event: PushEvent, code: String) { + synchronized(seen) { seen.remove("$shopId|${event.name}|$code") } + } + /** Keeps [payload], handed to its shop ([PushPayload.shopId]), for [received]. */ fun remember(payload: PushPayload) { val shopId = payload.shopId ?: return diff --git a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt index e813a235..a7c8a58f 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt @@ -76,6 +76,10 @@ class PushEventsTest { private val defaultExecutor = SdkLogs.handlerExecutor private val defaultWork = PushDismissReceiver.work + /** The status of the answer to /init. */ + @Volatile + private var initStatus = 200 + /** Holds the answer to /init while it is not null. */ @Volatile private var holdInit: java.util.concurrent.CountDownLatch? = null @@ -105,6 +109,7 @@ class PushEventsTest { shadowOf(manager).setNetworkCapabilities(manager.activeNetwork, capabilities) LegacyCore.threads = threads server = MockWebServer() + initStatus = 200 requests.clear() seen.clear() logged.clear() @@ -118,6 +123,7 @@ class PushEventsTest { seen += path to body if (path == "init") { holdInit?.await(5, TimeUnit.SECONDS) + if (initStatus != 200) return MockResponse().setResponseCode(initStatus) return MockResponse().setBody("""{"did":"D1","seance":"S1"}""") } if (path == "segments/get") return MockResponse().setResponseCode(404) @@ -246,6 +252,25 @@ class PushEventsTest { assertEquals(listOf("m-1"), clicked) } + @Test + fun `a push event the queue refused counts as not sent, and goes when it comes again`() { + initStatus = 403 + val refused = Rees46.initialize(context, config()) + runBlocking { withTimeout(5_000) { refused.instanceState.changes.first { it == SdkState.REFUSED } } } + assertEquals(SHOP, Rees46.handlePush(push(code = "again"), PushEvent.CLICKED)) + // The queue's refusal comes off the caller's thread. + val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5) + while (logged.none { it.message == "The push event was not accepted" } && System.nanoTime() < deadline) Thread.sleep(10) + assertTrue(logged.any { it.message == "The push event was not accepted" }) + refused.release() + + initStatus = 200 + val sdk = sdk() + assertEquals(SHOP, Rees46.handlePush(push(code = "again"), PushEvent.CLICKED)) + flush(sdk) + assertEquals("again", next("track/clicked")?.second?.getString("code")) + } + @Test fun `a tap sets the source of the next push events for a known type, and no from or code goes on the others`() { val sdk = sdk() From a516bb0684cd1725904bbe8b329aca5436bdbb6c Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 03:20:15 +0400 Subject: [PATCH 064/109] test(push): what must not go is checked once the work it would come from has ended, not after waiting 300 ms The push tests no longer wait 300 ms for a request that should not come. After flush() every event of the queue went, so the requests the server got are checked as a whole; a request sent around the queue would pass the funnel, whose hook the tests read; the legacy core's requests are checked through the same hook; the push token is checked once nothing of it waits for the instance or is on its way (CorePushTokens.awaitIdle), with one send carrying the did of /init. --- .../com/personalization/PushEventsTest.kt | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt index a7c8a58f..759f1901 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt @@ -211,6 +211,9 @@ class PushEventsTest { @Test fun `the old forms go to the new paths on the new core, never track received`() { val sdk = sdk() + // A request the core sends directly passes the funnel first: none is a delivery's track/received. + val handed = CopyOnWriteArrayList() + sdk.networkRepository.onRequest = { handed += it } @Suppress("DEPRECATION") Rees46.handlePush(push(code = "a"), PushEventType.RECEIVED) sdk.notificationReceived(push(code = "b")) @@ -227,7 +230,9 @@ class PushEventsTest { listOf("track/delivered" to "a", "track/delivered" to "b", "track/clicked" to "c", "track/clicked" to "d"), got.map { it?.first to it?.second?.getString("code") }, ) - assertNull(next("track/received", timeoutMs = 300)) + // Flushed: every event of the queue went, and nothing went around it. + assertFalse(seen.any { it.first == "track/received" }) + assertFalse(handed.contains("track/received")) } @Test @@ -246,9 +251,8 @@ class PushEventsTest { assertEquals(SHOP, Rees46.handlePush(push(code = "m-1"), PushEvent.CLOSED)) flush(sdk) - assertEquals("track/clicked", next("track/clicked", "track/closed")?.first) - assertEquals("track/closed", next("track/clicked", "track/closed")?.first) - assertNull(next("track/clicked", timeoutMs = 300)) + // Flushed: every event of the queue went. + assertEquals(listOf("track/clicked", "track/closed"), seen.map { it.first }.filter { it == "track/clicked" || it == "track/closed" }) assertEquals(listOf("m-1"), clicked) } @@ -510,7 +514,7 @@ class PushEventsTest { answered += "error:$code" } }) - assertNull("no token without a did", next("mobile_push_tokens", timeoutMs = 300)) + assertEquals(SdkState.INITIALIZING, sdk.state) hold.countDown() val body = next("mobile_push_tokens")?.second ?: throw AssertionError("no token") @@ -519,7 +523,10 @@ class PushEventsTest { assertEquals("firebase", body.getString("push_provider")) assertEquals("android", body.getString("platform")) assertEquals("success", answered.poll(5, TimeUnit.SECONDS)) - assertNull("once", next("mobile_push_tokens", timeoutMs = 300)) + // Nothing of the token waits or is on its way any more: every send it made reached the server, once, + // and none of them before the did. + runBlocking { withTimeout(5_000) { sdk.pushTokenManager.core?.awaitIdle() } } + assertEquals(listOf("D1"), seen.filter { it.first == "mobile_push_tokens" }.map { it.second.optString("did") }) assertEquals("tok", sdk.getPushToken(PushProvider.FCM)) assertEquals(listOf("tok"), heardTokens) } From 3a73ee12264afee837344b2380c24510434e6c03 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 03:21:54 +0400 Subject: [PATCH 065/109] fix(core): the event queue sends in two lanes, push events and tracking, and starts no session from the background Each lane keeps its own order. The push lane needs only the did and never waits for a session. A tracking event at the head that needs a new session no longer holds the push events behind it: the queue sends them while the tracking lane waits. A tracking event whose session's window ended starts the /init of a new session from the foreground only and waits for its answer, which wakes the queue; from the background no /init goes for it, and the lane goes once the app comes back. A push that comes in the background after the window ended goes past the queued tracking events with no /init. --- .../personalization/core/internal/CoreInit.kt | 2 +- .../core/internal/EventQueue.kt | 43 +++++++++++++---- .../core/internal/EventQueueTest.kt | 47 +++++++++++++++++++ 3 files changed, 82 insertions(+), 10 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt index 800f546c..ab8922f1 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CoreInit.kt @@ -74,7 +74,7 @@ internal interface InitHooks { * - **Without an answer** an instance with a `did` works as [SdkState.READY] (`init_saved_answer`); one * without waits for the network instead of failing again. * - **Later `/init`.** When the session's window ended ([SessionWindow], `core/session.md`): found by - * a request ([awaitUsable]), by the queue before an event ([renewSession]) or on coming back to the + * a request ([awaitUsable]), by the queue before a tracking event in the foreground ([renewSession]) or on coming back to the * foreground; and on [checkShop]. A refused shop is not asked again before the next launch. * - **Session.** `/init` carries the `sid` only while its window is alive; the `seance` of the answer is * the session from then on. The core never makes a `sid` of its own. diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt index 995a6c5f..843cdefd 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt @@ -53,10 +53,12 @@ internal abstract class EventAnswerListener : OnApiCallbackListener() { * and the time it was made as `time` on `/push/custom` only (the only path that reads it), unless the * call set its own. Numbers the older builders put in as text go as numbers ([EventNumbers]). Nothing * stamped reaches the disk. - * - **Sending.** One event at a time, oldest first, once the instance is ready and the session alive - * (a session whose window ended gets its `/init` first), by the one process of the app that holds the - * queue's file lock. A push's event needs only the `did`: it neither waits for a session nor starts one. - * The queue of an instance brought up only for a push sends the push events alone. An event cleared while it waited (the customer's deletion) does not go. Sending + * - **Sending.** One event at a time, by the one process of the app that holds the queue's file lock, in + * two lanes that each keep their order. The push events need only the `did`: they neither wait for a + * session nor start one. The tracking events need the instance ready and the session alive: a session + * whose window ended gets its `/init` first, from the foreground only, and while they wait for it the + * push events behind them go. The queue of an instance brought up only for a push sends the push + * events alone. An event cleared while it waited (the customer's deletion) does not go. Sending * starts on [SdkState.READY], on the app's foreground and background, on the network's return, on * [flush] and on a timer. * - **Answers.** `2xx` removes the event. No network waits for it and a timeout repeats it after a pause, @@ -520,19 +522,26 @@ internal class EventQueue( } } - /** Sends events oldest first until the queue is empty or cannot send now. */ + /** + * Sends events oldest first until the queue is empty or cannot send now, in two lanes that each keep + * their order: the push events, which need only a `did`, and the tracking events, which need a live + * session. A tracking event that waits for its session does not hold the push events behind it: the + * pass goes on with the push lane alone. + */ private suspend fun pass(hold: Hold): Outcome { urgent = false + // What the tracking lane waits for, once its head was found unable to go now. + var trackingHeld: Outcome? = null while (true) { if (released || !store.isOwner(this) || state.current == SdkState.REFUSED) return Outcome.Idle // A clear gives the lock back: it is taken again for what came after it. if (!store.lockForSending()) return Outcome.Waiting val generation = store.generation val unreadable = ArrayList() - val head = store.head(unreadable) { event -> pushEventsOnly && event.path !in PUSH_EVENT_PATHS } + val head = store.head(unreadable) { event -> event.path !in PUSH_EVENT_PATHS && (pushEventsOnly || trackingHeld != null) } unreadable.forEach { delivery.failure(it.listener, UNREADABLE) } val event = when (head) { - QueueStore.Head.Empty -> return Outcome.Idle + QueueStore.Head.Empty -> return trackingHeld ?: Outcome.Idle // The disk failed to read it: later, not again at once. QueueStore.Head.Unavailable -> return pause(null) is QueueStore.Head.Event -> head.event @@ -547,8 +556,11 @@ internal class EventQueue( // after the window ended starts no /init. if (init.awaitReady(waitMs) != null) return Outcome.Waiting } else { - // Ready, and when the session's window ended, the /init of a new one first. - if (init.awaitUsable(waitMs) != null || !init.session.isAlive()) return Outcome.Waiting + val held = trackingWait() + if (held != null) { + trackingHeld = held + continue + } } // Cleared while it waited (the customer was deleted): it does not go under the next one. val body = store.ifCurrent(generation, event.id) { stamped(event) } ?: continue @@ -560,6 +572,19 @@ internal class EventQueue( } } + /** + * Null when the tracking lane's head may go now: the instance is ready and the session alive. When the + * session's window ended, a new session starts from the foreground only — its `/init` goes, and its + * answer wakes the queue — and never from the background: the lane waits for the app to come back. + */ + private suspend fun trackingWait(): Outcome? { + if (init.session.isAlive()) { + return if (init.awaitReady(waitMs) == null && init.session.isAlive()) null else Outcome.Waiting + } + if (lifecycle.isForeground) init.renewSession() + return Outcome.Waiting + } + /** What goes for [event]: a copy of its fields with the queue's and the common ones. */ private fun stamped(event: QueuedEvent): JSONObject = stamp(withOwnFields(event.path, EventNumbers.normalize(event.path, JSONObject(event.body.toString())), event.createdAt)) diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt index 70a33e98..4871f52d 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/EventQueueTest.kt @@ -944,6 +944,53 @@ class EventQueueTest { assertEquals("push", sent.last().path) } + @Test + fun `a tracking event that waits for its new session does not hold the push events behind it`() { + val queue = start() + clock.advance(SessionWindow.WINDOW_MS + 1) + holdInit = true + event(queue, "1") + queue.accept("track/clicked", JSONObject().put("code", "m-1").put("type", "bulk"), null) + run() + + assertEquals(listOf("track/clicked"), sent.map { it.path }) + assertEquals("the new session's /init is on its way", 2, initsSent) + initCalls.single().complete(ok(sid = "S2")) + run() + assertEquals(listOf("track/clicked", "push"), sent.map { it.path }) + assertEquals("S2", sent.last().sid) + } + + @Test + fun `a push in the background after the window ended goes past the tracking events, and no init goes until the foreground`() { + val queue = start() + connectivity.set(false) + event(queue, "1") + event(queue, "2") + run() + clock.advance(SessionWindow.WINDOW_MS + 1) + lifecycle.set(false) + connectivity.set(true) + run() + + queue.accept("track/delivered", JSONObject().put("code", "m-1").put("type", "bulk"), null) + run() + assertEquals(listOf("track/delivered"), sent.map { it.path }) + // The timer looks again: still in the background, still no new session. + after(EventQueue.TIMER_MS) + assertEquals(listOf("track/delivered"), sent.map { it.path }) + assertEquals(1, initsSent) + + // The app comes back: a new session, then the tracking lane in its order. + initAnswer = { ok(sid = "S2") } + lifecycle.set(true) + run() + assertEquals(2, initsSent) + assertEquals(listOf("track/delivered", "push", "push"), sent.map { it.path }) + assertEquals(listOf("1", "2"), sent.drop(1).map { it.body.getJSONArray("items").getJSONObject(0).getString("id") }) + assertTrue(sent.drop(1).all { it.sid == "S2" }) + } + @Test fun `the queue of an instance brought up for a push sends its push events alone, ready on the stored did, and asks the server nothing`() { settings.updateDid("STORED") From d03c1064c7271c5dddb03e3e99df6e66ed395605 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 03:23:50 +0400 Subject: [PATCH 066/109] fix(push): the push callbacks and the bridges' onPushEvent hear an event once the queue accepted it, and queueEvent refuses the push paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On the new core PushCallbacks (the shop's and the facade's) and Rees46.onPushEvent are called for a push event only once its queue accepted it; an event the queue refused — the instance released, the shop refused — calls none, and it calls them when it comes again and is accepted. The legacy core, which has no queue, calls them at once as before. SDK.queueEvent answers invalidArgument for track/delivered, track/opened, track/clicked, track/closed and track/received: a bridge hands pushes to Rees46.handlePush, which keeps their rules. --- .../main/kotlin/com/personalization/Rees46.kt | 30 +++---- .../main/kotlin/com/personalization/SDK.kt | 87 +++++++++++-------- .../com/personalization/PushEventsTest.kt | 21 ++++- 3 files changed, 87 insertions(+), 51 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt index 55ef92a2..0d52bd41 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/Rees46.kt @@ -229,9 +229,10 @@ object Rees46 { * a repeat sends nothing and still returns the shop. A tap also sets the tracking source for 48 hours, * which the `/push` events carry as `source`. The legacy core sends a delivery to `track/received` * and a tap to `track/clicked`, as before. - * 4. The shop's callback of the event runs on the main thread: the one of [SDK.setPushCallbacks], or - * else the one of [setPushCallbacks]. For [PushEvent.DELIVERED] the old listeners - * ([setOnMessageListener], [SDK.setOnMessageListener]) hear the push too, as before. + * 4. The shop's callback of the event runs on the main thread, on the new core once the queue accepted + * the event: the one of [SDK.setPushCallbacks], or else the one of [setPushCallbacks]. An event the + * queue refused (the instance released, the shop refused) calls none. For [PushEvent.DELIVERED] the + * old listeners ([setOnMessageListener], [SDK.setOnMessageListener]) hear the push too, as before. */ fun handlePush(payload: Map, event: PushEvent): String? = try { route(payload, event) @@ -266,16 +267,17 @@ object Rees46 { return null } if (target.dropsPushWithoutDid()) return null - val fresh = when { - event == PushEvent.DELIVERED -> target.onPushReceived(data) + // The callbacks hear the event once it is taken: on the new core, once its queue accepted it. + val tell = { PushHub.event(event, payload, target.pushCallbacks) } + when { + event == PushEvent.DELIVERED -> target.onPushReceived(data, tell) // The legacy core's tap, as it always went: from the extras of a tap's intent. event == PushEvent.CLICKED && target.core?.isV2 != true -> { target.notificationClicked(tapExtras(parsed.code, parsed.type)) - true + tell() } - else -> target.pushEvent(event, parsed.code, parsed.type) + else -> target.pushEvent(event, parsed.code, parsed.type, tell) } - if (fresh) PushHub.event(event, payload, target.pushCallbacks) return shopId } @@ -317,8 +319,7 @@ object Rees46 { runCatching { val target = targetInstance(resolution, shopId) ?: return@runCatching if (target.dropsPushWithoutDid()) return@runCatching - val fresh = target.onPushReceived(payload) - if (fresh && parsed != null) PushHub.event(PushEvent.DELIVERED, parsed, target.pushCallbacks) + target.onPushReceived(payload) { parsed?.let { PushHub.event(PushEvent.DELIVERED, it, target.pushCallbacks) } } }.onFailure { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "handlePush: the delivery was not tracked", shopId, throwable = it) } } @@ -334,13 +335,12 @@ object Rees46 { if (target.dropsPushWithoutDid()) return@runCatching val parsed = PushPayload.parse(payload)?.withShop(shopId) parsed?.let(PushHub::remember) - val fresh = if (parsed != null && target.core?.isV2 == true) { - target.pushEvent(PushEvent.CLICKED, parsed.code, parsed.type) + if (parsed != null && target.core?.isV2 == true) { + target.pushEvent(PushEvent.CLICKED, parsed.code, parsed.type) { PushHub.event(PushEvent.CLICKED, parsed, target.pushCallbacks) } } else { target.notificationClicked(payload.toBundle()) - true + parsed?.let { PushHub.event(PushEvent.CLICKED, it, target.pushCallbacks) } } - if (fresh && parsed != null) PushHub.event(PushEvent.CLICKED, parsed, target.pushCallbacks) }.onFailure { SdkLogs.log(LogLevel.ERROR, LogCodes.PUSH_FAILED, "handlePush: the click was not tracked", shopId, throwable = it) } } @@ -357,7 +357,7 @@ object Rees46 { /** * For the bridges: [listener] hears every push event the SDK handles, of every shop, on the main * thread, beside the app's [PushCallbacks] and whatever they are. Once per event of a message, like - * them. Until the handle is cancelled. + * them, and on the new core once the queue accepted the event. Until the handle is cancelled. */ @BridgeApi fun onPushEvent(listener: BridgePushListener): Cancellable = PushHub.addEventSink(listener) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 63101333..8aff5a30 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -340,9 +340,9 @@ open class SDK { * the instance is initialized. * * A callback runs on the main thread when the SDK sees its event: its messaging service, - * [Rees46.handlePush], or a notification the SDK drew ([NotificationHelper]). One event of one message - * is reported once per process. [setOnMessageListener] and [setOnPushTokenListener] keep working beside - * them. + * [Rees46.handlePush], or a notification the SDK drew ([NotificationHelper]); on the new core once the + * queue accepted the event. One event of one message is reported once per process. [setOnMessageListener] + * and [setOnPushTokenListener] keep working beside them. */ fun setPushCallbacks(callbacks: PushCallbacks?) { pushCallbacks = callbacks @@ -937,7 +937,7 @@ open class SDK { notSdkPush("The clicked push has no type or code", extras?.keySet()?.sorted()) return } - queuePushEvent(PushEvent.CLICKED, code, type) + queuePushEvent(PushEvent.CLICKED, code, type, accepted = {}) return } notificationHandler.notificationClicked( @@ -1773,22 +1773,27 @@ open class SDK { * @param data from data notification */ fun notificationReceived(data: Map) { - trackDelivered(data) + trackDelivered(data, accepted = {}) } - /** [notificationReceived]; false when the new core had the message's delivery already. */ - private fun trackDelivered(data: Map): Boolean { + /** + * [notificationReceived]; [accepted] runs once the delivery is taken: on the new core once its queue + * accepted it — not for a repeat, nor for a push without an `id` and a `type` — and on the legacy core + * at once. + */ + private fun trackDelivered(data: Map, accepted: () -> Unit) { if (isCoreV2) { val code = data[ID_FIELD] val type = data[TYPE_FIELD] if (code.isNullOrBlank() || type.isNullOrBlank()) { notSdkPush("The push has no id or type", data.keys.sorted()) - return false + return } - return queuePushEvent(PushEvent.DELIVERED, code, type) + queuePushEvent(PushEvent.DELIVERED, code, type, accepted) + return } trackReceivedLegacy(data) - return true + accepted() } private fun trackReceivedLegacy(data: Map) { @@ -1821,32 +1826,38 @@ open class SDK { /** * Tracks the delivery on this instance's network and fires the deprecated per-instance listener; - * false when the new core had the message's delivery already. The process-global listener + * [accepted] runs once the delivery is taken ([trackDelivered]). The process-global listener * (Rees46.setOnMessageListener) is dispatched by the router ([Rees46.handlePush] / the [onMessage] * companion) instead, so the notification shows even for a shop that has no live instance yet. */ - private fun receiveMessage(data: Map): Boolean { - val fresh = trackDelivered(data) + private fun receiveMessage(data: Map, accepted: () -> Unit) { + trackDelivered(data, accepted) onMessageListener?.onMessage(data = data.toNotificationData()) - return fresh } - /** Entry point for [Rees46.handlePush]: track a received push on the already-resolved instance. */ - internal fun onPushReceived(data: Map): Boolean = receiveMessage(data = data) + /** + * Entry point for [Rees46.handlePush]: track a received push on the already-resolved instance; + * [accepted] runs once the delivery is taken, for the push callbacks. + */ + internal fun onPushReceived(data: Map, accepted: () -> Unit) = receiveMessage(data, accepted) /** Whether the instance runs on the new core. */ private val isCoreV2: Boolean get() = core?.isV2 == true /** * A shown, tapped or dismissed push of the message [code] of mailing [type] for [Rees46.handlePush]; - * false when it was a repeat. The new core queues it ([queuePushEvent]). The legacy core reports neither - * a shown nor a dismissed notification, as before; [Rees46.handlePush] takes its taps through - * [notificationClicked] and its deliveries through [onPushReceived]. + * [accepted] runs once it is taken, for the push callbacks. The new core queues it ([queuePushEvent]) + * and runs [accepted] once the queue accepted it. The legacy core reports neither a shown nor a + * dismissed notification, as before, and runs [accepted] at once; [Rees46.handlePush] takes its taps + * through [notificationClicked] and its deliveries through [onPushReceived]. */ - internal fun pushEvent(event: PushEvent, code: String, type: String): Boolean { - if (isCoreV2) return queuePushEvent(event, code, type) + internal fun pushEvent(event: PushEvent, code: String, type: String, accepted: () -> Unit) { + if (isCoreV2) { + queuePushEvent(event, code, type, accepted) + return + } log(LogLevel.DEBUG, LogCodes.PUSH, "The legacy core does not report this push event", mapOf("event" to event.name.lowercase(Locale.ROOT))) - return true + accepted() } /** @@ -1862,26 +1873,29 @@ open class SDK { /** * The new core's push event: `track/delivered`, `track/opened`, `track/clicked` or `track/closed` * through the queue with `code` and `type`, once per message and event in a process (a repeat is a - * no-op, false). The event counts as sent once the queue accepted it: one the queue refused (the - * instance released, the shop refused) may come again and go. A tap also sets the tracking source for - * 48 hours, when its type is one of [TrackingSourceType]: the `/push` events carry it as `source`. + * no-op). The event counts as sent once the queue accepted it, and only then [accepted] runs: one the + * queue refused (the instance released, the shop refused) may come again and go. A tap also sets the + * tracking source for 48 hours, when its type is one of [TrackingSourceType]: the `/push` events carry + * it as `source`. */ - private fun queuePushEvent(event: PushEvent, code: String, type: String): Boolean { - val core = core as? CoreV2 ?: return false + private fun queuePushEvent(event: PushEvent, code: String, type: String, accepted: () -> Unit) { + val core = core as? CoreV2 ?: return val shop = shopId if (!PushHub.firstTime(shop, event, code)) { log(LogLevel.DEBUG, LogCodes.PUSH, "The push event was handled already", mapOf("event" to event.name.lowercase(Locale.ROOT), "reason" to REASON_REPEAT)) - return false + return } if (event == PushEvent.CLICKED && TrackingSourceType.entries.any { it.value == type }) { notificationHandler.setClickSource(type = type, code = code) } core.accept(pathOf(event), JSONObject().put(CODE_FIELD, code).put(TYPE_FIELD, type)) { refused -> - if (refused == null) return@accept + if (refused == null) { + accepted() + return@accept + } PushHub.forget(shop, event, code) log(LogLevel.WARNING, LogCodes.PUSH, "The push event was not accepted", mapOf("event" to event.name.lowercase(Locale.ROOT), "error" to refused.javaClass.simpleName)) } - return true } private fun pathOf(event: PushEvent): String = "track/" + event.name.lowercase(Locale.ROOT) @@ -1893,9 +1907,10 @@ open class SDK { /** * For the bridges: [body] as an event of [path] through the queue, as the tracking methods send it — - * `push`, `push/custom`, `track/stories`, `track/slider`, `popup/showed` and the push events - * `track/delivered`, `track/opened`, `track/clicked`, `track/closed`. The queue adds the common fields - * and, on `/push`, the stored source when the event goes. + * `push`, `push/custom`, `track/stories`, `track/slider` and `popup/showed`. The queue adds the common + * fields and, on `/push`, the stored source when the event goes. A push event (`track/delivered`, + * `track/opened`, `track/clicked`, `track/closed`, `track/received`) is [SdkError.InvalidArgument]: a + * bridge hands the push to [Rees46.handlePush], which keeps its rules (once per message, the callbacks). * * [listener] hears once, on the main thread: [SdkResult.Success] once the event is accepted — written * to disk, the way the contract's tracking methods complete — or [SdkResult.Failure] with @@ -1907,6 +1922,8 @@ open class SDK { fun queueEvent(path: String, body: JSONObject, listener: CompletionListener) { val core = core val error = when { + path in EventQueue.PUSH_EVENT_PATHS || path == TRACK_RECEIVED -> + SdkError.InvalidArgument("$path is a push event: hand the push to Rees46.handlePush") path !in CoreV2.EVENT_PATHS -> SdkError.InvalidArgument("$path is not an event of the queue") core == null || released -> SdkError.NotInitialized else -> null @@ -2087,8 +2104,8 @@ open class SDK { // Show via the process-global listener, then track the delivery on the instance. SdkRegistry.dispatchMessage(shopId = shopId, data = data.toNotificationData()) if (target.dropsPushWithoutDid()) return - val fresh = target.receiveMessage(data = data) - if (fresh && payload != null) PushHub.event(PushEvent.DELIVERED, payload, target.pushCallbacks) + // The callbacks hear the delivery once the queue accepted it. + target.receiveMessage(data) { payload?.let { PushHub.event(PushEvent.DELIVERED, it, target.pushCallbacks) } } } /** diff --git a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt index 759f1901..f47cb143 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt @@ -256,8 +256,13 @@ class PushEventsTest { assertEquals(listOf("m-1"), clicked) } + @OptIn(com.personalization.core.BridgeApi::class) @Test - fun `a push event the queue refused counts as not sent, and goes when it comes again`() { + fun `a push event the queue refused counts as not sent and calls no callback, and goes when it comes again`() { + val clicked = CopyOnWriteArrayList() + val bridge = CopyOnWriteArrayList() + Rees46.setPushCallbacks(PushCallbacks(onClicked = { clicked += it.code })) + Rees46.onPushEvent { _, payload -> bridge += payload.code } initStatus = 403 val refused = Rees46.initialize(context, config()) runBlocking { withTimeout(5_000) { refused.instanceState.changes.first { it == SdkState.REFUSED } } } @@ -266,6 +271,7 @@ class PushEventsTest { val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5) while (logged.none { it.message == "The push event was not accepted" } && System.nanoTime() < deadline) Thread.sleep(10) assertTrue(logged.any { it.message == "The push event was not accepted" }) + assertTrue("no callback for an event the queue refused", clicked.isEmpty() && bridge.isEmpty()) refused.release() initStatus = 200 @@ -273,6 +279,8 @@ class PushEventsTest { assertEquals(SHOP, Rees46.handlePush(push(code = "again"), PushEvent.CLICKED)) flush(sdk) assertEquals("again", next("track/clicked")?.second?.getString("code")) + assertEquals(listOf("again"), clicked) + assertEquals(listOf("again"), bridge) } @Test @@ -438,6 +446,8 @@ class PushEventsTest { Rees46.handlePush(push(code = "1", shop = null), PushEvent.DELIVERED) Rees46.handlePush(push(code = "1"), PushEvent.CLICKED) Rees46.handlePush(push(code = "1"), PushEvent.CLOSED) + // The callbacks come once the queue accepted the events. + sdk.awaitEventsWritten(5_000) shadowOf(Looper.getMainLooper()).idle() assertEquals(listOf("facade-delivered:$SHOP:1", "shop-clicked:$SHOP:1"), heard) @@ -459,6 +469,7 @@ class PushEventsTest { Rees46.handlePush(push(code = "1"), PushEvent.DELIVERED) @Suppress("DEPRECATION") Rees46.handlePush(push(code = "2"), PushEventType.RECEIVED) + sdk.awaitEventsWritten(5_000) assertEquals(listOf("$SHOP:1", "$SHOP:2"), global) assertEquals(listOf("1", "2"), own.map { it.id }) @@ -737,6 +748,12 @@ class PushEventsTest { sdk.queueEvent("profile/set", JSONObject(), completions) val refused = takeOnMain(main, ends) assertTrue(refused is com.personalization.core.SdkResult.Failure && refused.error is com.personalization.core.SdkError.InvalidArgument) + // A push event: the bridge hands the push to handlePush. + listOf("track/delivered", "track/opened", "track/clicked", "track/closed", "track/received").forEach { path -> + sdk.queueEvent(path, JSONObject().put("code", "x").put("type", "bulk"), completions) + val pushPath = takeOnMain(main, ends) + assertTrue(path, pushPath is com.personalization.core.SdkResult.Failure && pushPath.error is com.personalization.core.SdkError.InvalidArgument) + } // Fields JSON cannot hold: refused through the listener, never thrown at the bridge. sdk.queueEvent("push/custom", JSONObject(mapOf("event" to "nan", "value" to Double.NaN)), completions) val notJson = takeOnMain(main, ends) @@ -750,10 +767,12 @@ class PushEventsTest { Rees46.onPushToken { tokens += it.token } Rees46.handlePush(push(code = "b1"), PushEvent.DELIVERED) Rees46.handlePush(push(code = "b1"), PushEvent.DELIVERED) + sdk.awaitEventsWritten(5_000) SDK.onPushTokenReceived("bridge-token", PushProvider.HMS) SDK.onPushTokenReceived("bridge-token", PushProvider.HMS) pushes.cancel() Rees46.handlePush(push(code = "b2"), PushEvent.DELIVERED) + sdk.awaitEventsWritten(5_000) assertEquals(listOf("DELIVERED:$SHOP:b1"), events) assertEquals(listOf("bridge-token"), tokens) From 9245e12a8f5ffb6b352fb3df3ae88f092694f34f Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 03:25:19 +0400 Subject: [PATCH 067/109] fix(push): a claim of the token's week is free after five minutes, a token the app passes waits for the did, the legacy core's push without a did starts as before, and bridges get shopBlocked for a refused shop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The claim of a token's week without a send is free after five minutes; the week, the claim and the send are kept per provider (FCM, HMS). A token the app passes on the new core that finds the instance ready but the did gone (the customer deleted meanwhile) waits for the next did and answers once it went: waiting for a did is no error. On the legacy core a shop registered but not created whose push comes without a stored did starts as it always did — /init for a did, then the push's event with it, no token — and is no longer dropped; the drop with no_did is the new core's only. BridgeErrors.errorOf(state) gives the error an instance in a state gives every call: shopBlocked for a refused shop, notInitialized before initialization and after release(), what a bridge reports for an initialize whose shop refused the SDK. --- .../main/kotlin/com/personalization/SDK.kt | 33 +++++++++++-------- .../com/personalization/core/BridgeErrors.kt | 12 +++++++ .../core/internal/CorePushTokens.kt | 22 ++++++++++--- .../com/personalization/PushEventsTest.kt | 12 ++++--- .../core/internal/CorePushTokensTest.kt | 27 ++++++++++++++- 5 files changed, 83 insertions(+), 23 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt index 8aff5a30..4c7250f9 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/SDK.kt @@ -496,11 +496,12 @@ open class SDK { /** * Light bring-up for handling a push in a (possibly cold) background process, used by - * [Rees46.handlePush] for a shop registered but not created. On both cores it never sends `/init`: the - * instance is ready on the shop's stored `did` and sends the push's event with it. It shows no popup and - * sends neither the push token nor the profile. Without a stored `did` the event is dropped - * (`push_dropped`, `no_did`). The app's first [Rees46.instance] or [Rees46.initialize] of the shop puts a - * full instance in its place, and an initialize of this object makes it one. + * [Rees46.handlePush] for a shop registered but not created. On both cores, with a stored `did`, it never + * sends `/init`: the instance is ready on that `did` and sends the push's event with it. It shows no + * popup and sends neither the push token nor the profile. Without a stored `did` the new core drops the + * event (`push_dropped`, `no_did`), and the legacy core starts as it always did. The app's first + * [Rees46.instance] or [Rees46.initialize] of the shop puts a full instance in its place, and an + * initialize of this object makes it one. */ internal fun initializeForPush(context: Context, config: Rees46Config) = start( context, @@ -536,11 +537,12 @@ open class SDK { } /** - * A push-only instance ([initializeForPush]) without a stored `did` cannot send a push's event: true - * when this is one, and the push is dropped (`push_dropped`, `no_did`). + * A push-only instance ([initializeForPush]) of the new core without a stored `did` cannot send a push's + * event: true when this is one, and the push is dropped (`push_dropped`, `no_did`). */ internal fun dropsPushWithoutDid(): Boolean { - if (!pushOnly || deviceId != null) return false + // The legacy core asks /init for one first, as it always did. + if (!pushOnly || !isCoreV2 || deviceId != null) return false log(LogLevel.WARNING, LogCodes.PUSH_DROPPED, "The push's event is dropped: the shop has no did yet", mapOf("reason" to LogReasons.NO_DID)) return true } @@ -664,7 +666,7 @@ open class SDK { init.start() } else { core = networkRepository.core - if (pushOnly) startLegacyForPush(config, storage) else startLegacy(context, config, storage, autoSendPushToken, needReInitialization) + if (pushOnly) startLegacyForPush(context, config, storage) else startLegacy(context, config, storage, autoSendPushToken, needReInitialization) } // Register this instance so pushes delivered to the (process-global) messaging services are @@ -742,12 +744,17 @@ open class SDK { } /** - * The legacy path brought up only for a push: no `/init`, no session of its own, no popup, no token. - * Ready on the stored `did`, with which the push's event goes; without one it stays initializing. + * The legacy path brought up only for a push: with a stored `did`, no `/init`, no session of its own, no + * popup, no token; ready on that `did`, with which the push's event goes. Without one it starts as it + * always did (`/init` for a `did`, which the push's event then waits for), only sending no token. */ - private fun startLegacyForPush(config: Rees46Config, storage: ShopStorage) { + private fun startLegacyForPush(context: Context, config: Rees46Config, storage: ShopStorage) { storage.adoptConfiguredDid(config.deviceId, userSettingsRepository) - instanceState.set(if (userSettingsRepository.getDid().isNotEmpty()) SdkState.READY else SdkState.INITIALIZING) + if (userSettingsRepository.getDid().isEmpty()) { + startLegacy(context, config, storage, autoSendPushToken = false, needReInitialization = false) + return + } + instanceState.set(SdkState.READY) } /** The new core's `/init` and state ([CoreInit]); requests wait for it in [CoreV2]. Started by the caller. */ diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/BridgeErrors.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/BridgeErrors.kt index fb903bb9..22468bee 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/BridgeErrors.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/BridgeErrors.kt @@ -31,6 +31,18 @@ object BridgeErrors { else -> SdkError.Network } + /** + * The error an instance in [state] gives every call: [SdkError.ShopBlocked] for a shop that refused the + * SDK ([SdkState.REFUSED]), [SdkError.NotInitialized] before initialization and after `release()`; null + * while calls may go. What a bridge reports for an `initialize` whose shop refused the SDK. + */ + @JvmStatic + fun errorOf(state: SdkState): SdkError? = when (state) { + SdkState.REFUSED -> SdkError.ShopBlocked + SdkState.NOT_INITIALIZED -> SdkError.NotInitialized + SdkState.INITIALIZING, SdkState.READY, SdkState.RETRYING -> null + } + /** * An old-style listener that reports the whole result to [onResult], once, on the thread the SDK * answers on: [SdkResult.Success] with the answer (a [JSONObject], a [JSONArray] or null), or diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt index b6d8b393..c499b8fd 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/CorePushTokens.kt @@ -12,6 +12,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel +import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.update @@ -32,9 +33,11 @@ import java.util.concurrent.atomic.AtomicBoolean * - The week is taken before the request: two sends of one token at once make one request, the second * hearing the first's answer. The claim is kept in the partition with its time, and the token and the * date of the send only once the server took it. A failed send gives the claim back, and a claim older - * than [CLAIM_TTL_MS] without a send — its process died — is free. - * - A token goes only once the instance is ready ([ready]) and never without a `did`. After each start's - * `/init` ([sendStored]) the shop sends the device's token if it is due. + * than [CLAIM_TTL_MS] (five minutes) without a send — its process died — is free. The week, the claim and + * the send are kept for each provider (FCM, HMS) on its own. + * - A token goes only once the instance is ready ([ready]) and never without a `did`. A token the app + * passes waits for the `did` and its listener hears the answer of the send: waiting is no error. After + * each start's `/init` ([sendStored]) the shop sends the device's token if it is due. * - A push-only instance ([sends] false) sends no token. * - After [release] nothing is sent or kept: [set] answers `notInitialized`, and so does one that was * waiting for the instance to be ready. @@ -118,7 +121,13 @@ internal class CorePushTokens( // Who answers [listener]: the send, or the release that cut it off before it went. val handed = AtomicBoolean(false) val job = launch { - val error = ready() + var error = ready() + // Ready, yet the did went (the customer was deleted meanwhile): the token waits for the next one + // and goes then; waiting for a did is no error. + while (error == null && !hasDid()) { + delay(DID_RECHECK_MS) + error = ready() + } if (!handed.compareAndSet(false, true)) return@launch if (error != null) { listener?.let { delivery.failure(it, LegacyErrors.of(error)) } @@ -289,7 +298,10 @@ internal class CorePushTokens( const val WEEK_MS = 7 * 24 * 60 * 60 * 1000L /** A claim this old without a send is free: the process that took it died before the answer. */ - const val CLAIM_TTL_MS = 3 * 60 * 1000L + const val CLAIM_TTL_MS = 5 * 60 * 1000L + + /** How often a token the app passed looks again for the did a deletion of the customer took. */ + const val DID_RECHECK_MS = 100L const val REASON_SENT_THIS_WEEK = "sent_this_week" const val REASON_NO_DID = "no_did" diff --git a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt index f47cb143..87c58d5b 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/PushEventsTest.kt @@ -272,6 +272,8 @@ class PushEventsTest { while (logged.none { it.message == "The push event was not accepted" } && System.nanoTime() < deadline) Thread.sleep(10) assertTrue(logged.any { it.message == "The push event was not accepted" }) assertTrue("no callback for an event the queue refused", clicked.isEmpty() && bridge.isEmpty()) + // What a bridge reports for the initialize of a refused shop. + assertEquals(com.personalization.core.SdkError.ShopBlocked, com.personalization.core.BridgeErrors.errorOf(refused.state)) refused.release() initStatus = 200 @@ -407,7 +409,7 @@ class PushEventsTest { } @Test - fun `on the legacy core a shop registered but not created sends its push with its stored did and no init`() { + fun `on the legacy core a shop registered but not created sends its push with its stored did and no init, and without one starts as before`() { partition().edit().putString("did", "STORED").commit() Rees46.register(context, listOf(config(SHOP, coreV2 = false))) @@ -418,10 +420,12 @@ class PushEventsTest { assertFalse(drained().any { it.first in listOf("init", "mobile_push_tokens", "profile/set") }) assertEquals(SdkState.READY, SdkRegistry.byShopId(SHOP)?.state) - // Without a did the push is dropped. + // Without a did it starts as it always did: /init for a did, then the push's event with it. Rees46.register(context, listOf(config(OTHER, coreV2 = false))) - assertNull(Rees46.handlePush(push(code = "cold", shop = OTHER), PushEvent.DELIVERED)) - assertEquals(LogReasons.NO_DID, logged.last { it.code == LogCodes.PUSH_DROPPED }.context["reason"]) + assertEquals(OTHER, Rees46.handlePush(push(code = "cold", shop = OTHER), PushEvent.DELIVERED)) + assertEquals(OTHER, next("init")?.second?.getString("shop_id")) + assertEquals("D1", next("track/received")?.second?.getString("did")) + assertFalse(logged.any { it.code == LogCodes.PUSH_DROPPED && it.context["reason"] == LogReasons.NO_DID }) } // Callbacks. diff --git a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CorePushTokensTest.kt b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CorePushTokensTest.kt index 041077bd..d10cd948 100644 --- a/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CorePushTokensTest.kt +++ b/personalization-sdk/src/test/kotlin/com/personalization/core/internal/CorePushTokensTest.kt @@ -297,6 +297,13 @@ class CorePushTokensTest { run() assertEquals("a claim moments old is another process's send", 1, sent.size) + // The claim is the provider's: an HMS token goes all the same. + next.received("h1", PushProvider.HMS) + run() + assertEquals(listOf("t1", "h1"), sent.map { it.second.getString("token") }) + answers.clear() + sent.removeAt(1) + clock.advance(CorePushTokens.CLAIM_TTL_MS) next.sendStored() run() @@ -305,7 +312,25 @@ class CorePushTokensTest { next.sendStored() run() assertEquals("the send that went counts the week", 2, sent.size) - assertEquals(listOf(CorePushTokens.REASON_SENT_THIS_WEEK, CorePushTokens.REASON_SENT_THIS_WEEK), reasons()) + assertEquals(CorePushTokens.REASON_SENT_THIS_WEEK, reasons().last()) + } + + @Test + fun `a token the app passes waits for the did and answers once it went, as no error`() { + did = "" + val tokens = tokens() + val heard = Heard() + tokens.set("t1", PushProvider.FCM, heard) + run() + assertTrue(heard.calls.isEmpty()) + assertTrue(sent.isEmpty()) + + did = "D1" + scheduler.advanceTimeBy(CorePushTokens.DID_RECHECK_MS) + run() + assertEquals(1, sent.size) + ok() + assertEquals(listOf("success"), heard.calls) } private companion object { From c08a43cd96cc3053f0d40c67b409f9f722952696 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 03:29:30 +0400 Subject: [PATCH 068/109] fix(core): an instance that is not ready holds the queue's two lanes once, not once per lane A tracking event at the head whose session is alive waits for the instance to be ready as before; when it is not, the pass ends there instead of waiting again for the push lane, which needs the instance ready as well. A session whose window ended while the event waited is looked at again as an ended one: a new session from the foreground, the push lane meanwhile. --- .../core/internal/EventQueue.kt | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt index 843cdefd..32dd9680 100644 --- a/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt +++ b/personalization-sdk/src/main/kotlin/com/personalization/core/internal/EventQueue.kt @@ -555,12 +555,18 @@ internal class EventQueue( // A push's event needs a did and no session: the server reads no sid in it. One that came // after the window ended starts no /init. if (init.awaitReady(waitMs) != null) return Outcome.Waiting - } else { - val held = trackingWait() - if (held != null) { - trackingHeld = held - continue - } + } else if (!init.session.isAlive()) { + // The session's window ended: a new one starts from the foreground only — its /init goes, + // and its answer wakes the queue — never from the background. The push lane goes meanwhile. + if (lifecycle.isForeground) init.renewSession() + trackingHeld = Outcome.Waiting + continue + } else if (init.awaitReady(waitMs) != null) { + // Not ready: neither lane can go. + return Outcome.Waiting + } else if (!init.session.isAlive()) { + // The window ended while it waited: the head is looked at again, as a session's that ended. + continue } // Cleared while it waited (the customer was deleted): it does not go under the next one. val body = store.ifCurrent(generation, event.id) { stamped(event) } ?: continue @@ -572,19 +578,6 @@ internal class EventQueue( } } - /** - * Null when the tracking lane's head may go now: the instance is ready and the session alive. When the - * session's window ended, a new session starts from the foreground only — its `/init` goes, and its - * answer wakes the queue — and never from the background: the lane waits for the app to come back. - */ - private suspend fun trackingWait(): Outcome? { - if (init.session.isAlive()) { - return if (init.awaitReady(waitMs) == null && init.session.isAlive()) null else Outcome.Waiting - } - if (lifecycle.isForeground) init.renewSession() - return Outcome.Waiting - } - /** What goes for [event]: a copy of its fields with the queue's and the common ones. */ private fun stamped(event: QueuedEvent): JSONObject = stamp(withOwnFields(event.path, EventNumbers.normalize(event.path, JSONObject(event.body.toString())), event.createdAt)) From 98596fa032eeec9a17b18b1cd7741ff37488c550 Mon Sep 17 00:00:00 2001 From: xyzcancer Date: Sat, 14 Nov 2026 05:03:43 +0400 Subject: [PATCH 069/109] test(core): a live run of both cores against the demo shop, and the new core's queue across a restart without a network CoreLiveE2ETest initializes the demo shop on a fresh install, takes an item from a live search, tracks a product view, an add to cart, a purchase and a custom event, and checks that every request the SDK logged got a 2xx and every listener succeeded, once per core. Two more runs queue a purchase offline, kill the process, and check that a new process sends the queue when the network returns, without a flush. DemoTestRunner skips the application's onCreate with cleanProcess=true, so the test makes the process's first initialization and picks its core; without the argument it is AndroidJUnitRunner. --- demo-app/build.gradle | 2 +- .../personalization/demo/CoreLiveE2ETest.kt | 428 ++++++++++++++++++ .../personalization/demo/DemoTestRunner.kt | 38 ++ 3 files changed, 467 insertions(+), 1 deletion(-) create mode 100644 demo-app/src/androidTest/java/com/personalization/demo/CoreLiveE2ETest.kt create mode 100644 demo-app/src/androidTest/java/com/personalization/demo/DemoTestRunner.kt diff --git a/demo-app/build.gradle b/demo-app/build.gradle index f1a075e2..8d3f7c89 100644 --- a/demo-app/build.gradle +++ b/demo-app/build.gradle @@ -45,7 +45,7 @@ android { versionName "1.0" multiDexEnabled true - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + testInstrumentationRunner "com.personalization.demo.DemoTestRunner" // Add shop ID to BuildConfig buildConfigField "String", "SHOP_ID", "\"${shopId}\"" diff --git a/demo-app/src/androidTest/java/com/personalization/demo/CoreLiveE2ETest.kt b/demo-app/src/androidTest/java/com/personalization/demo/CoreLiveE2ETest.kt new file mode 100644 index 00000000..5eb3383b --- /dev/null +++ b/demo-app/src/androidTest/java/com/personalization/demo/CoreLiveE2ETest.kt @@ -0,0 +1,428 @@ +package com.personalization.demo + +import android.content.Context +import android.net.ConnectivityManager +import android.net.NetworkCapabilities +import android.os.ParcelFileDescriptor +import android.util.Log +import androidx.test.core.app.ActivityScenario +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import com.personalization.LogLevel +import com.personalization.Rees46 +import com.personalization.Rees46Config +import com.personalization.SDK +import com.personalization.SdkLogHandler +import com.personalization.SdkLogMessage +import com.personalization.api.OnApiCallbackListener +import com.personalization.api.models.purchase.PurchaseItemRequest +import com.personalization.api.models.purchase.PurchaseTrackingRequest +import com.personalization.api.models.tracking.TrackingItem +import com.personalization.core.SdkResult +import com.personalization.core.SdkState +import java.io.File +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import org.json.JSONArray +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertTrue +import org.junit.Assume.assumeTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Live end-to-end check of the SDK's cores against the real API and the demo shop. + * + * A process has one core, decided by its first initialization, and [DemoApplication] initializes the + * demo shop itself; so these tests run only in a process the application left alone + * (`cleanProcess=true`, see [DemoTestRunner]), one core per run, and each is selected by its own + * argument. Without them every test here is skipped. + * + * [roundTrip], with `coreV2=true` or `coreV2=false`: a fresh install initializes the demo shop, runs a + * live search, tracks a product view, an add to cart, a purchase and a custom event, and checks that + * every request got a `2xx`: + * ``` + * ./gradlew :demo-app:connectedRees46DebugAndroidTest \ + * -Pandroid.testInstrumentationRunnerArguments.class=com.personalization.demo.CoreLiveE2ETest \ + * -Pandroid.testInstrumentationRunnerArguments.cleanProcess=true \ + * -Pandroid.testInstrumentationRunnerArguments.coreV2=true + * ``` + * + * The new core's queue across a restart without a network, in two runs of `am instrument` (the end of + * a run kills the process; the network stays off between them): + * - `offlineStep=1` ([offlineRestart1_queueWithoutNetwork]): initializes online, turns the network + * off, tracks a purchase and a custom event, and checks they wait on disk; + * - `offlineStep=2` ([offlineRestart2_deliverAfterRestart]): a new process initializes offline on the + * stored `did`, turns the network on, and checks that the queue goes without a flush. + * ``` + * adb shell am instrument -w -e cleanProcess true -e offlineStep 1 \ + * -e class com.personalization.demo.CoreLiveE2ETest \ + * com.personalization.demo.test/com.personalization.demo.DemoTestRunner + * ``` + * + * Every purchase is a real order of the demo shop, named `sdk-core-e2e--