Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

staging -> main (Core 3.0.2) #677

Merged
merged 11 commits into from
Jun 4, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void testGetDataQueue_DataQueueMigrationFromCacheDirectory() {
FileUtils.deleteFile(context.getDatabasePath(TEST_DATABASE_NAME).getParentFile(), true);
assertFalse(context.getDatabasePath(TEST_DATABASE_NAME).exists());
File cacheDatabaseFile = new File(context.getCacheDir(), TEST_DATABASE_NAME);
DataQueue dataQueue = new SQLiteDataQueue(cacheDatabaseFile.getPath());
DataQueue dataQueue = new SQLiteDataQueue(TEST_DATABASE_NAME, cacheDatabaseFile.getPath());
dataQueue.add(new DataEntity("test_data_1"));
DataQueue dataQueueExisting = new DataQueueService().getDataQueue(TEST_DATABASE_NAME);
Assert.assertEquals("test_data_1", dataQueueExisting.peek().getData());
Expand All @@ -102,7 +102,7 @@ public void testGetDataQueue_DataQueueMigrationFromCacheDirectory() {
public void testGetDataQueue_DataQueueMigrationFromCacheDirectory_DatabasesDirectoryAbsent() {
assertFalse(context.getDatabasePath(TEST_DATABASE_NAME).exists());
File cacheDatabaseFile = new File(context.getCacheDir(), TEST_DATABASE_NAME);
DataQueue dataQueue = new SQLiteDataQueue(cacheDatabaseFile.getPath());
DataQueue dataQueue = new SQLiteDataQueue(TEST_DATABASE_NAME, cacheDatabaseFile.getPath());
dataQueue.add(new DataEntity("test_data_1"));
DataQueue dataQueueExisting = new DataQueueService().getDataQueue(TEST_DATABASE_NAME);
Assert.assertEquals("test_data_1", dataQueueExisting.peek().getData());
Expand All @@ -112,7 +112,7 @@ public void testGetDataQueue_DataQueueMigrationFromCacheDirectory_DatabasesDirec
@Test
public void testGetDataQueue_DataQueueExistsInDatabaseDirectory() {
File databaseFile = context.getDatabasePath(TEST_DATABASE_NAME);
DataQueue dataQueue = new SQLiteDataQueue(databaseFile.getPath());
DataQueue dataQueue = new SQLiteDataQueue(TEST_DATABASE_NAME, databaseFile.getPath());
dataQueue.add(new DataEntity("test_data_1"));
DataQueue dataQueueExisting = new DataQueueService().getDataQueue(TEST_DATABASE_NAME);
Assert.assertEquals("test_data_1", dataQueueExisting.peek().getData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class SqliteDataQueueTests {
@Before
public void setUp() {
Context context = ApplicationProvider.getApplicationContext();
dataQueue = new SQLiteDataQueue(context.getDatabasePath(QUEUE_NAME).getPath());
dataQueue = new SQLiteDataQueue(QUEUE_NAME, context.getDatabasePath(QUEUE_NAME).getPath());
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package com.adobe.marketing.mobile.internal

internal object CoreConstants {
const val LOG_TAG = "MobileCore"
const val VERSION = "3.0.1"
const val VERSION = "3.0.2"

object EventDataKeys {
/**
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
CoreConstants.LOG_TAG,
LOG_PREFIX,
String.format(
"createTableIfNotExists - Error in creating/accessing table. Error:"
+ " (%s)",
e.getMessage()));
"createTableIfNotExists - Error in creating/accessing database (%s)."
+ "Error: (%s)",
dbPath, e.getMessage()));

Check warning on line 49 in code/core/src/main/java/com/adobe/marketing/mobile/internal/util/SQLiteDatabaseHelper.java

View check run for this annotation

Codecov / codecov/patch

code/core/src/main/java/com/adobe/marketing/mobile/internal/util/SQLiteDatabaseHelper.java#L49

Added line #L49 was not covered by tests
return false;
} finally {
closeDatabase(database);
Expand All @@ -73,9 +73,9 @@
CoreConstants.LOG_TAG,
LOG_PREFIX,
String.format(
"getTableSize - Error in querying table(%s) size. Returning 0. Error:"
+ " (%s)",
tableName, e.getMessage()));
"getTableSize - Error in querying table(%s) size from database(%s)."
+ "Returning 0. Error: (%s)",
tableName, dbPath, e.getMessage()));
return 0;
} finally {
closeDatabase(database);
Expand All @@ -101,9 +101,9 @@
CoreConstants.LOG_TAG,
LOG_PREFIX,
String.format(
"clearTable - Error in clearing table(%s). Returning false. Error:"
+ " (%s)",
tableName, e.getMessage()));
"clearTable - Error in clearing table(%s) from database(%s)."
+ "Returning false. Error: (%s)",
tableName, dbPath, e.getMessage()));
return false;
} finally {
closeDatabase(database);
Expand Down Expand Up @@ -208,7 +208,9 @@
Log.warning(
CoreConstants.LOG_TAG,
LOG_PREFIX,
"Failed to open database -" + e.getLocalizedMessage());
"Failed to open database (%s). Error: %s",
filePath,
e.getLocalizedMessage());
return false;
} finally {
if (database != null) {
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 @@
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(

Check warning on line 137 in code/core/src/phone/java/com/adobe/marketing/mobile/MobileCore.java

View check run for this annotation

Codecov / codecov/patch

code/core/src/phone/java/com/adobe/marketing/mobile/MobileCore.java#L136-L137

Added lines #L136 - L137 were not covered by tests
CoreConstants.LOG_TAG,
LOG_TAG,
"Migration from V4 SDK failed with error - "
+ e.getLocalizedMessage());

Check warning on line 141 in code/core/src/phone/java/com/adobe/marketing/mobile/MobileCore.java

View check run for this annotation

Codecov / codecov/patch

code/core/src/phone/java/com/adobe/marketing/mobile/MobileCore.java#L141

Added line #L141 was not covered by tests
}

// Initialize event history
EventHub.Companion.getShared().initializeEventHistory();
return null;
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public DataQueue getDataQueue(final String databaseName) {
databaseName);
return null;
}
dataQueue = new SQLiteDataQueue(databaseDirDataQueue.getPath());
dataQueue = new SQLiteDataQueue(databaseName, databaseDirDataQueue.getPath());
dataQueueCache.put(databaseName, dataQueue);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
private static final String TB_KEY_UNIQUE_IDENTIFIER = "uniqueIdentifier";
private static final String TB_KEY_TIMESTAMP = "timestamp";
private static final String TB_KEY_DATA = "data";
private static final String LOG_PREFIX = "SQLiteDataQueue";
private final String LOG_PREFIX;
private final String databasePath;
private boolean isClose = false;
private final Object dbMutex = new Object();

SQLiteDataQueue(final String databasePath) {
SQLiteDataQueue(final String databaseName, final String databasePath) {
this.LOG_PREFIX = "SQLiteDataQueue-" + databaseName;
this.databasePath = databasePath;
createTableIfNotExists();
}
Expand Down Expand Up @@ -121,26 +122,22 @@
ServiceConstants.LOG_TAG,
LOG_PREFIX,
String.format(
"query - Successfully read %d rows from table(%s)",
rows.size(), TABLE_NAME));
"query - Successfully read %d rows from table.",
rows.size()));
return true;
} catch (final SQLiteException e) {
Log.warning(
ServiceConstants.LOG_TAG,
LOG_PREFIX,
String.format(
"query - Error in querying database table (%s). Error:"
"query - Error in querying database table. Error:"
+ " (%s)",
TABLE_NAME, e.getLocalizedMessage()));
e.getLocalizedMessage()));

Check warning on line 135 in code/core/src/phone/java/com/adobe/marketing/mobile/services/SQLiteDataQueue.java

View check run for this annotation

Codecov / codecov/patch

code/core/src/phone/java/com/adobe/marketing/mobile/services/SQLiteDataQueue.java#L135

Added line #L135 was not covered by tests
return false;
}
});
}

if (rows.isEmpty()) {
return new ArrayList<>();
}

final List<DataEntity> dataEntitiesList = new ArrayList<>(rows.size());

for (ContentValues row : rows) {
Expand Down Expand Up @@ -238,8 +235,8 @@
LOG_PREFIX,
String.format(
"removeRows - Error in deleting rows from"
+ " table(%s). Returning 0. Error: (%s)",
TABLE_NAME, e.getMessage()));
+ " table. Returning 0. Error: (%s)",
e.getMessage()));
return false;
}
});
Expand Down Expand Up @@ -273,8 +270,7 @@
ServiceConstants.LOG_TAG,
LOG_PREFIX,
String.format(
"clear - %s in clearing Table %s",
(result ? "Successful" : "Failed"), TABLE_NAME));
"clear - %s in clearing table", (result ? "Successful" : "Failed")));

if (!result) {
resetDatabase();
Expand Down Expand Up @@ -323,20 +319,16 @@
Log.trace(
ServiceConstants.LOG_TAG,
LOG_PREFIX,
String.format(
"createTableIfNotExists - Successfully created/already existed"
+ " table (%s) ",
TABLE_NAME));
"createTableIfNotExists - Successfully created/already existed"
+ " table.");
return;
}
}

Log.warning(
ServiceConstants.LOG_TAG,
LOG_PREFIX,
String.format(
"createTableIfNotExists - Error creating/accessing table (%s) ",
TABLE_NAME));
"createTableIfNotExists - Error creating/accessing table.");
}

/**
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
Loading
Loading