From 9c32b489c53c0e2651dd06fd85c3b31d0712bae3 Mon Sep 17 00:00:00 2001 From: praveek Date: Tue, 28 May 2024 13:21:42 -0700 Subject: [PATCH 1/3] Fix strict mode violations --- .../mobile/internal/eventhub/EventHub.kt | 7 +++++ .../adobe/marketing/mobile/MobileCore.java | 28 +++++++++++-------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/code/core/src/main/java/com/adobe/marketing/mobile/internal/eventhub/EventHub.kt b/code/core/src/main/java/com/adobe/marketing/mobile/internal/eventhub/EventHub.kt index 74ea39b48..de401e2a6 100644 --- a/code/core/src/main/java/com/adobe/marketing/mobile/internal/eventhub/EventHub.kt +++ b/code/core/src/main/java/com/adobe/marketing/mobile/internal/eventhub/EventHub.kt @@ -199,6 +199,13 @@ internal class EventHub { ).get() } + /** + * Submits a task to be executed in the event hub executor. + */ + fun executeInEventHubExecutor(task: () -> Unit) { + eventHubExecutor.submit(task) + } + /** * Initializes event history. This must be called after the SDK has application context. */ diff --git a/code/core/src/phone/java/com/adobe/marketing/mobile/MobileCore.java b/code/core/src/phone/java/com/adobe/marketing/mobile/MobileCore.java index 16667eb12..fb04a354b 100644 --- a/code/core/src/phone/java/com/adobe/marketing/mobile/MobileCore.java +++ b/code/core/src/phone/java/com/adobe/marketing/mobile/MobileCore.java @@ -123,18 +123,24 @@ public static void setApplication(@NonNull final Application application) { ServiceProvider.getInstance().getAppContextService().setApplication(application); App.INSTANCE.registerActivityResumedListener(MobileCore::collectLaunchInfo); - try { - V4Migrator migrator = new V4Migrator(); - migrator.migrate(); - } catch (Exception e) { - Log.error( - CoreConstants.LOG_TAG, - LOG_TAG, - "Migration from V4 SDK failed with error - " + e.getLocalizedMessage()); - } + // Migration and EventHistory operations must complete in a background thread before any extensions are registered. + // To ensure these tasks are completed before any registerExtension calls are made, + // reuse the eventHubExecutor instead of using a separate executor instance. + EventHub.Companion.getShared().executeInEventHubExecutor(() -> { + try { + V4Migrator migrator = new V4Migrator(); + migrator.migrate(); + } catch (Exception e) { + Log.error( + CoreConstants.LOG_TAG, + LOG_TAG, + "Migration from V4 SDK failed with error - " + e.getLocalizedMessage()); + } - // Initialize event history - EventHub.Companion.getShared().initializeEventHistory(); + // Initialize event history + EventHub.Companion.getShared().initializeEventHistory(); + return null; + }); } /** From 59c5ed55d893b6dd35012db1ccf2f8d1e0e1bf2c Mon Sep 17 00:00:00 2001 From: praveek Date: Tue, 28 May 2024 13:25:03 -0700 Subject: [PATCH 2/3] Fix lint errors --- .../adobe/marketing/mobile/MobileCore.java | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/code/core/src/phone/java/com/adobe/marketing/mobile/MobileCore.java b/code/core/src/phone/java/com/adobe/marketing/mobile/MobileCore.java index fb04a354b..626cb20fa 100644 --- a/code/core/src/phone/java/com/adobe/marketing/mobile/MobileCore.java +++ b/code/core/src/phone/java/com/adobe/marketing/mobile/MobileCore.java @@ -123,24 +123,28 @@ public static void setApplication(@NonNull final Application application) { ServiceProvider.getInstance().getAppContextService().setApplication(application); App.INSTANCE.registerActivityResumedListener(MobileCore::collectLaunchInfo); - // Migration and EventHistory operations must complete in a background thread before any extensions are registered. - // To ensure these tasks are completed before any registerExtension calls are made, + // Migration and EventHistory operations must complete in a background thread before any + // extensions are registered. + // To ensure these tasks are completed before any registerExtension calls are made, // reuse the eventHubExecutor instead of using a separate executor instance. - EventHub.Companion.getShared().executeInEventHubExecutor(() -> { - try { - V4Migrator migrator = new V4Migrator(); - migrator.migrate(); - } catch (Exception e) { - Log.error( - CoreConstants.LOG_TAG, - LOG_TAG, - "Migration from V4 SDK failed with error - " + e.getLocalizedMessage()); - } - - // Initialize event history - EventHub.Companion.getShared().initializeEventHistory(); - return null; - }); + EventHub.Companion.getShared() + .executeInEventHubExecutor( + () -> { + try { + V4Migrator migrator = new V4Migrator(); + migrator.migrate(); + } catch (Exception e) { + Log.error( + CoreConstants.LOG_TAG, + LOG_TAG, + "Migration from V4 SDK failed with error - " + + e.getLocalizedMessage()); + } + + // Initialize event history + EventHub.Companion.getShared().initializeEventHistory(); + return null; + }); } /** From 65b0fd9513d195f093f1b576d39a0267b614d308 Mon Sep 17 00:00:00 2001 From: praveek Date: Mon, 3 Jun 2024 15:12:57 -0700 Subject: [PATCH 3/3] Add test --- .../mobile/internal/eventhub/EventHubTests.kt | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/code/core/src/test/java/com/adobe/marketing/mobile/internal/eventhub/EventHubTests.kt b/code/core/src/test/java/com/adobe/marketing/mobile/internal/eventhub/EventHubTests.kt index 5bb0f3953..9fe9e4d5d 100644 --- a/code/core/src/test/java/com/adobe/marketing/mobile/internal/eventhub/EventHubTests.kt +++ b/code/core/src/test/java/com/adobe/marketing/mobile/internal/eventhub/EventHubTests.kt @@ -43,6 +43,7 @@ import java.lang.UnsupportedOperationException import java.util.Locale import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicBoolean import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertNotNull @@ -148,6 +149,23 @@ internal class EventHubTests { } } + private class TestExtension_InitCallback(api: ExtensionApi) : Extension(api) { + companion object { + const val EXTENSION_NAME = "TestExtension_InitCallback" + + // Calls this during initialization + var initCallback: (() -> Unit)? = null + } + + init { + initCallback?.invoke() + } + + override fun getName(): String { + return EXTENSION_NAME + } + } + private lateinit var eventHub: EventHub private val eventType = "Type" private val eventSource = "Source" @@ -199,6 +217,26 @@ internal class EventHubTests { assertEquals(EventHubError.None, ret) } + @Test + fun testExecutionOrderBeforeExtensionInitialization() { + val latch = CountDownLatch(1) + val flag = AtomicBoolean(false) + TestExtension_InitCallback.initCallback = { + if (flag.get()) { + latch.countDown() + } + } + + // This should complete before the extension instance is created. + eventHub.executeInEventHubExecutor { + flag.set(true) + } + val ret = registerExtension(TestExtension_InitCallback::class.java) + assertEquals(EventHubError.None, ret) + + assertTrue { latch.await(250, TimeUnit.MILLISECONDS) } + } + @Test fun testRegisterExtensionFailure_DuplicateExtension() { var ret = registerExtension(TestExtension2::class.java)