@@ -28,6 +28,7 @@ import okhttp3.mockwebserver.MockResponse
28
28
import okhttp3.mockwebserver.MockWebServer
29
29
import okhttp3.mockwebserver.RecordedRequest
30
30
import org.assertj.core.api.Assertions.assertThat
31
+ import org.assertj.core.api.Assertions.within
31
32
import org.junit.Test
32
33
import org.junit.runner.RunWith
33
34
@@ -190,6 +191,96 @@ class FunctionalTests {
190
191
}
191
192
}
192
193
194
+ /* *
195
+ * In this scenario consumer app starts an engagement session and after 27150 ms,
196
+ * it stops the session.
197
+ *
198
+ * Intervals:
199
+ * With current implementation of `HeartbeatIntervalCalculator`, the next intervals are:
200
+ * - 10500ms for the first interval
201
+ * - 13650ms for the second interval
202
+ *
203
+ * So after ~27,2s we should observe
204
+ * - 2 `heartbeat` events from `startEngagement` + 1 `heartbeat` event caused by `stopEngagement` which is triggered during engagement interval
205
+ *
206
+ * Time off-differences in assertions are acceptable, because it's a time-sensitive test
207
+ */
208
+ @Test
209
+ fun engagementManagerTest () {
210
+ val engagementUrl = " engagementUrl"
211
+ var startTimestamp = Duration .ZERO
212
+ val firstInterval = 10500 .milliseconds
213
+ val secondInterval = 13650 .milliseconds
214
+ val pauseInterval = 3 .seconds
215
+ ActivityScenario .launch(SampleActivity ::class .java).use { scenario ->
216
+ // given
217
+ scenario.onActivity { activity: Activity ->
218
+ beforeEach(activity)
219
+ server.enqueue(MockResponse ().setResponseCode(200 ))
220
+ parselyTracker = initializeTracker(activity, flushInterval = 30 .seconds)
221
+
222
+ // when
223
+ startTimestamp = System .currentTimeMillis().milliseconds
224
+ parselyTracker.startEngagement(engagementUrl, null )
225
+ }
226
+
227
+ Thread .sleep((firstInterval + secondInterval + pauseInterval).inWholeMilliseconds)
228
+ parselyTracker.stopEngagement()
229
+
230
+ // then
231
+ val request = server.takeRequest(35 , TimeUnit .SECONDS )!! .toMap()[" events" ]!!
232
+
233
+ assertThat(
234
+ request.sortedBy { it.data.timestamp }
235
+ .filter { it.action == " heartbeat" }
236
+ ).hasSize(3 )
237
+ .satisfies({
238
+ val firstEvent = it[0 ]
239
+ val secondEvent = it[1 ]
240
+ val thirdEvent = it[2 ]
241
+
242
+ assertThat(firstEvent.data.timestamp).isCloseTo(
243
+ (startTimestamp + firstInterval).inWholeMilliseconds,
244
+ within(1 .seconds.inWholeMilliseconds)
245
+ )
246
+ assertThat(firstEvent.totalTime).isCloseTo(
247
+ firstInterval.inWholeMilliseconds,
248
+ within(100L )
249
+ )
250
+ assertThat(firstEvent.incremental).isCloseTo(
251
+ firstInterval.inWholeSeconds,
252
+ within(1L )
253
+ )
254
+
255
+ assertThat(secondEvent.data.timestamp).isCloseTo(
256
+ (startTimestamp + firstInterval + secondInterval).inWholeMilliseconds,
257
+ within(1 .seconds.inWholeMilliseconds)
258
+ )
259
+ assertThat(secondEvent.totalTime).isCloseTo(
260
+ (firstInterval + secondInterval).inWholeMilliseconds,
261
+ within(100L )
262
+ )
263
+ assertThat(secondEvent.incremental).isCloseTo(
264
+ secondInterval.inWholeSeconds,
265
+ within(1L )
266
+ )
267
+
268
+ assertThat(thirdEvent.data.timestamp).isCloseTo(
269
+ (startTimestamp + firstInterval + secondInterval + pauseInterval).inWholeMilliseconds,
270
+ within(1 .seconds.inWholeMilliseconds)
271
+ )
272
+ assertThat(thirdEvent.totalTime).isCloseTo(
273
+ (firstInterval + secondInterval + pauseInterval).inWholeMilliseconds,
274
+ within(100L )
275
+ )
276
+ assertThat(thirdEvent.incremental).isCloseTo(
277
+ (pauseInterval).inWholeSeconds,
278
+ within(1L )
279
+ )
280
+ })
281
+ }
282
+ }
283
+
193
284
private fun RecordedRequest.toMap (): Map <String , List <Event >> {
194
285
val listType: TypeReference <Map <String , List <Event >>> =
195
286
object : TypeReference <Map <String , List <Event >>>() {}
@@ -200,6 +291,15 @@ class FunctionalTests {
200
291
@JsonIgnoreProperties(ignoreUnknown = true )
201
292
data class Event (
202
293
@JsonProperty(" idsite" ) var idsite : String ,
294
+ @JsonProperty(" action" ) var action : String ,
295
+ @JsonProperty(" data" ) var data : ExtraData ,
296
+ @JsonProperty(" tt" ) var totalTime : Long ,
297
+ @JsonProperty(" inc" ) var incremental : Long ,
298
+ )
299
+
300
+ @JsonIgnoreProperties(ignoreUnknown = true )
301
+ data class ExtraData (
302
+ @JsonProperty(" ts" ) var timestamp : Long ,
203
303
)
204
304
205
305
private val locallyStoredEvents
0 commit comments