Skip to content

Commit

Permalink
Merge pull request #674 from praveek/dev
Browse files Browse the repository at this point in the history
Fix strict mode violations
  • Loading branch information
praveek authored Jun 3, 2024
2 parents f1a544e + 65b0fd9 commit 17aca7e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
34 changes: 22 additions & 12 deletions code/core/src/phone/java/com/adobe/marketing/mobile/MobileCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,28 @@ 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());
}

// Initialize event history
EventHub.Companion.getShared().initializeEventHistory();
// 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;
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 17aca7e

Please sign in to comment.