Skip to content

Commit

Permalink
fix: serialisation happening on a separate thread executor service (#403
Browse files Browse the repository at this point in the history
)

* fix: adds executor service for event processing

* fix: updates robolectric to version 4.10.3

* fix: make executor as a class property

* refactor: executor to messageExecutor

* chore: make messageExecutor variable private and final

---------

Co-authored-by: Abhishek Pandey <[email protected]>
  • Loading branch information
ChryssaAliferi and 1abhishekpandey authored Mar 19, 2024
1 parent 71b001e commit c3b0d84
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
12 changes: 6 additions & 6 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
buildConfigField("String", "VERSION_CODE", "\"${VERSION_CODE}\"")
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildFeatures{
buildFeatures {
buildConfig true
}
buildTypes {
Expand Down Expand Up @@ -58,25 +58,25 @@ dependencies {
compileOnly "androidx.sqlite:sqlite:2.3.1"

//test
testImplementation ('com.android.support.test:rules:1.0.2')
testImplementation('com.android.support.test:rules:1.0.2')
testImplementation 'com.android.support.test:runner:1.0.2'
testImplementation 'org.robolectric:robolectric:4.3'
testImplementation 'org.robolectric:robolectric:4.10.3'
testImplementation 'androidx.test:core-ktx:1.5.0'
testImplementation "org.hamcrest:hamcrest:2.2"
testImplementation "org.mockito:mockito-core:3.11.2"
testImplementation 'androidx.lifecycle:lifecycle-process:2.6.1'
testImplementation 'androidx.lifecycle:lifecycle-common:2.6.1'

testImplementation "org.powermock:powermock-core:2.0.9"
testImplementation ("org.powermock:powermock-module-junit4:2.0.9"){
testImplementation("org.powermock:powermock-module-junit4:2.0.9") {
exclude group: 'org.hamcrest', module: 'hamcrest'
}
testImplementation "org.skyscreamer:jsonassert:1.5.1"

testImplementation "org.powermock:powermock-api-mockito2:2.0.9"
testImplementation 'org.awaitility:awaitility:4.2.0'
androidTestImplementation ('androidx.test.ext:junit:1.1.5')
androidTestImplementation ('androidx.test.espresso:espresso-core:3.5.1')
androidTestImplementation('androidx.test.ext:junit:1.1.5')
androidTestImplementation('androidx.test.espresso:espresso-core:3.5.1')
compileOnly "androidx.work:work-runtime:2.7.1"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*
* utility class for event processing
Expand Down Expand Up @@ -65,6 +67,8 @@ class EventRepository {

private static final String CHARSET_UTF_8 = "UTF-8";

private final ExecutorService messageExecutor = Executors.newSingleThreadExecutor();

// Handler instance associated with the main thread
static final Handler HANDLER =
new Handler(Looper.getMainLooper()) {
Expand Down Expand Up @@ -324,33 +328,39 @@ private void saveFlushConfig() {
rudderFlushWorkManager.saveRudderFlushConfig(rudderFlushConfig);
}


/*
* generic method for processing all the events
* */
void processMessage(@NonNull RudderMessage message) {
if (!isSDKEnabled) {
incrementDiscardedCounter(1, Collections.singletonMap(LABEL_TYPE, ReportManager.LABEL_TYPE_SDK_DISABLED));
return;
}
RudderLogger.logDebug(String.format(Locale.US, "EventRepository: processMessage: eventName: %s", message.getEventName()));

applyRudderOptionsToMessageIntegrations(message);
RudderMessage updatedMessage = updateMessageWithConsentedDestinations(message);
userSessionManager.applySessionTracking(updatedMessage);

String eventJson = getEventJsonString(updatedMessage);
if (eventJson == null) {
RudderLogger.logError("EventRepository: processMessage: eventJson is null after serialization");
return;
}
if (isMessageJsonExceedingMaxSize(eventJson)) {
incrementDiscardedCounter(1, Collections.singletonMap(LABEL_TYPE, ReportManager.LABEL_TYPE_MSG_SIZE_INVALID));
RudderLogger.logError(String.format(Locale.US, "EventRepository: processMessage: Event size exceeds the maximum permitted event size(%d)", Utils.MAX_EVENT_SIZE));
return;
}
RudderLogger.logVerbose(String.format(Locale.US, "EventRepository: processMessage: message: %s", eventJson));
dbManager.saveEvent(eventJson, new EventInsertionCallback(message, deviceModeManager));
Runnable runnable = () -> {
try {
if (!isSDKEnabled) {
incrementDiscardedCounter(1, Collections.singletonMap(LABEL_TYPE, ReportManager.LABEL_TYPE_SDK_DISABLED));
return;
}
RudderLogger.logDebug(String.format(Locale.US, "EventRepository: processMessage: eventName: %s", message.getEventName()));
applyRudderOptionsToMessageIntegrations(message);
RudderMessage updatedMessage = updateMessageWithConsentedDestinations(message);
userSessionManager.applySessionTracking(updatedMessage);

String eventJson = getEventJsonString(updatedMessage);
if (eventJson == null) {
RudderLogger.logError("EventRepository: processMessage: eventJson is null after serialization");
return;
}
if (isMessageJsonExceedingMaxSize(eventJson)) {
incrementDiscardedCounter(1, Collections.singletonMap(LABEL_TYPE, ReportManager.LABEL_TYPE_MSG_SIZE_INVALID));
RudderLogger.logError(String.format(Locale.US, "EventRepository: processMessage: Event size exceeds the maximum permitted event size(%d)", Utils.MAX_EVENT_SIZE));
return;
}
RudderLogger.logVerbose(String.format(Locale.US, "EventRepository: processMessage: message: %s", eventJson));
dbManager.saveEvent(eventJson, new EventInsertionCallback(message, deviceModeManager));
} catch (Exception e) {
RudderLogger.logError(e);
ReportManager.reportError(e);
}
};
messageExecutor.execute(runnable);
}

String getEventJsonString(RudderMessage message) {
Expand Down

0 comments on commit c3b0d84

Please sign in to comment.