Skip to content

Commit

Permalink
fix: SQDSDKS-5287 unable to drop batches nullability (#379)
Browse files Browse the repository at this point in the history
* fix: Unable to Drop Batches with Client-side Rules

* Reviewing and adding test to support Nullable BarchCreationListener

* Ktlint change

* Changes

* Suggestion

---------

Co-authored-by: Ben Baron <[email protected]>
  • Loading branch information
markvdouw and einsteinx2 authored May 18, 2023
1 parent c24d38d commit 04af4e9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,70 @@ class BatchCreationCallbackTests : BaseCleanInstallEachTest() {
)
}

@Test
fun testNullBatchCreationSENDwithoutModify() {
val targetEventName = "should send without modified"

val options = MParticleOptions.builder(mContext)
.batchCreationListener(null)
startMParticle(options)

MParticle.getInstance()?.apply {
logEvent(MPEvent.Builder(targetEventName).build())
upload()
}

mServer.waitForVerify(
Matcher(mServer.Endpoints().eventsUrl).bodyMatch {
it.optJSONArray("msgs")
?.toList()
?.filterIsInstance<JSONObject>()
?.any { it.optString("n") == targetEventName && it.optString("mb").isNullOrEmpty() } ?: false
}
)

mServer.Requests().events.any {
it.bodyJson.optJSONArray("msgs")
?.toList()
?.filterIsInstance<JSONObject>()
?.any { it.optString("n") == targetEventName && it.optString("mb").isNullOrEmpty() } ?: false
}.let {
assertTrue { it }
}
}

@Test
fun testNullOnBatchCreatedShouldNOTsend() {
val targetEventName = "should not send"

val options = MParticleOptions.builder(mContext)
.batchCreationListener { null }
startMParticle(options)

MParticle.getInstance()?.apply {
logEvent(MPEvent.Builder(targetEventName).build())
upload()
}

mServer.waitForVerify(
Matcher(mServer.Endpoints().eventsUrl).bodyMatch {
it.optJSONArray("msgs")
?.toList()
?.filterIsInstance<JSONObject>()
?.any { it.optString("n") == targetEventName } ?: false
}
)

mServer.Requests().events.any {
it.bodyJson.optJSONArray("msgs")
?.toList()
?.filterIsInstance<JSONObject>()
?.any { it.optString("n") == targetEventName } ?: false
}.let {
assertFalse { it }
}
}

@Test
fun testListenerModified() {
var newBatch = JSONObject().put("the whole", "batch")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -966,13 +966,13 @@ public DataplanOptions build() {

/**
* Custom handler to modify or block batch data before upload.
* If set, this will be called when a new batch of data is created. By returning a different value, you can change the batch contents, or by returning 'nil' you can block the batch from being uploaded.
* If set, this will be called when a new batch of data is created. By returning a different value, you can change the batch contents, or by returning 'null' you can block the batch from being uploaded.
* Use with care. This feature was initially added to allow the value of existing fields to be modified. If you add new data in a format that the platform is not expecting, it may be dropped or not parsed correctly.
* Note: Use of this handler will also cause the field 'mb' (modified batch) to appear in the batch so we can distinguish for troubleshooting purposes whether data was changed.
* Also note: Unlike other callbacks, this block will be called on the SDK queue to prevent batches from being processed out of order. Please avoid excessively blocking in this handler as this will prevent the SDK from doing other tasks.
*/
public interface BatchCreationListener {
@NonNull
@Nullable
JSONObject onBatchCreated(@NonNull JSONObject batch);
}
}
7 changes: 2 additions & 5 deletions testutils/src/main/java/com/mparticle/testutils/MPLatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ public void await() throws InterruptedException {
return;
}
}
mHandler.postDelayed(timeoutRunnable, timeoutTimeMs - 100L);
this.await(timeoutTimeMs, TimeUnit.MILLISECONDS);
if (timedOut.value) {
fail("timed out");
}
this.await(timeoutTimeMs, TimeUnit.MILLISECONDS);
mHandler.postDelayed(timeoutRunnable, timeoutTimeMs - 100L);
}
}

0 comments on commit 04af4e9

Please sign in to comment.