Skip to content

Commit 04af4e9

Browse files
markvdouweinsteinx2
andauthored
fix: SQDSDKS-5287 unable to drop batches nullability (#379)
* 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 <bbaron@mparticle.com>
1 parent c24d38d commit 04af4e9

3 files changed

Lines changed: 68 additions & 7 deletions

File tree

android-core/src/androidTest/kotlin/com.mparticle/BatchCreationCallbackTests.kt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,70 @@ class BatchCreationCallbackTests : BaseCleanInstallEachTest() {
5151
)
5252
}
5353

54+
@Test
55+
fun testNullBatchCreationSENDwithoutModify() {
56+
val targetEventName = "should send without modified"
57+
58+
val options = MParticleOptions.builder(mContext)
59+
.batchCreationListener(null)
60+
startMParticle(options)
61+
62+
MParticle.getInstance()?.apply {
63+
logEvent(MPEvent.Builder(targetEventName).build())
64+
upload()
65+
}
66+
67+
mServer.waitForVerify(
68+
Matcher(mServer.Endpoints().eventsUrl).bodyMatch {
69+
it.optJSONArray("msgs")
70+
?.toList()
71+
?.filterIsInstance<JSONObject>()
72+
?.any { it.optString("n") == targetEventName && it.optString("mb").isNullOrEmpty() } ?: false
73+
}
74+
)
75+
76+
mServer.Requests().events.any {
77+
it.bodyJson.optJSONArray("msgs")
78+
?.toList()
79+
?.filterIsInstance<JSONObject>()
80+
?.any { it.optString("n") == targetEventName && it.optString("mb").isNullOrEmpty() } ?: false
81+
}.let {
82+
assertTrue { it }
83+
}
84+
}
85+
86+
@Test
87+
fun testNullOnBatchCreatedShouldNOTsend() {
88+
val targetEventName = "should not send"
89+
90+
val options = MParticleOptions.builder(mContext)
91+
.batchCreationListener { null }
92+
startMParticle(options)
93+
94+
MParticle.getInstance()?.apply {
95+
logEvent(MPEvent.Builder(targetEventName).build())
96+
upload()
97+
}
98+
99+
mServer.waitForVerify(
100+
Matcher(mServer.Endpoints().eventsUrl).bodyMatch {
101+
it.optJSONArray("msgs")
102+
?.toList()
103+
?.filterIsInstance<JSONObject>()
104+
?.any { it.optString("n") == targetEventName } ?: false
105+
}
106+
)
107+
108+
mServer.Requests().events.any {
109+
it.bodyJson.optJSONArray("msgs")
110+
?.toList()
111+
?.filterIsInstance<JSONObject>()
112+
?.any { it.optString("n") == targetEventName } ?: false
113+
}.let {
114+
assertFalse { it }
115+
}
116+
}
117+
54118
@Test
55119
fun testListenerModified() {
56120
var newBatch = JSONObject().put("the whole", "batch")

android-core/src/main/java/com/mparticle/MParticleOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,13 +966,13 @@ public DataplanOptions build() {
966966

967967
/**
968968
* Custom handler to modify or block batch data before upload.
969-
* 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.
969+
* 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.
970970
* 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.
971971
* 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.
972972
* 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.
973973
*/
974974
public interface BatchCreationListener {
975-
@NonNull
975+
@Nullable
976976
JSONObject onBatchCreated(@NonNull JSONObject batch);
977977
}
978978
}

testutils/src/main/java/com/mparticle/testutils/MPLatch.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ public void await() throws InterruptedException {
5757
return;
5858
}
5959
}
60-
mHandler.postDelayed(timeoutRunnable, timeoutTimeMs - 100L);
61-
this.await(timeoutTimeMs, TimeUnit.MILLISECONDS);
62-
if (timedOut.value) {
63-
fail("timed out");
64-
}
60+
this.await(timeoutTimeMs, TimeUnit.MILLISECONDS);
61+
mHandler.postDelayed(timeoutRunnable, timeoutTimeMs - 100L);
6562
}
6663
}

0 commit comments

Comments
 (0)