Skip to content

Commit

Permalink
Merge pull request #677 from adobe/staging
Browse files Browse the repository at this point in the history
staging -> main (Core 3.0.2)
  • Loading branch information
praveek authored Jun 4, 2024
2 parents 55d1790 + 79487fa commit d297aa5
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 62 deletions.
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 @@ public static boolean createTableIfNotExist(final String dbPath, final String qu
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()));
return false;
} finally {
closeDatabase(database);
Expand All @@ -73,9 +73,9 @@ public static int getTableSize(final String dbPath, final String tableName) {
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 @@ public static boolean clearTable(final String dbPath, final String tableName) {
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 @@ public static boolean process(
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 @@ 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 @@ -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 @@ final class SQLiteDataQueue implements DataQueue {
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 @@ public List<DataEntity> peek(final int n) {
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()));
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 @@ public boolean remove(final int n) {
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 @@ public boolean clear() {
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 @@ private void createTableIfNotExists() {
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

0 comments on commit d297aa5

Please sign in to comment.