-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from Parsely/engagment_interval_calculator_tests
- Loading branch information
Showing
7 changed files
with
125 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.parsely.parselyandroid | ||
|
||
import java.util.Calendar | ||
import java.util.TimeZone | ||
import kotlin.time.Duration.Companion.milliseconds | ||
|
||
open class Clock { | ||
open val now | ||
get() = Calendar.getInstance(TimeZone.getTimeZone("UTC")).timeInMillis.milliseconds | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
parsely/src/main/java/com/parsely/parselyandroid/HeartbeatIntervalCalculator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.parsely.parselyandroid | ||
|
||
import java.util.Calendar | ||
import kotlin.time.Duration.Companion.hours | ||
import kotlin.time.Duration.Companion.milliseconds | ||
import kotlin.time.Duration.Companion.minutes | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
internal open class HeartbeatIntervalCalculator(private val clock: Clock) { | ||
|
||
open fun calculate(startTime: Calendar): Long { | ||
val startTimeDuration = startTime.time.time.milliseconds | ||
val nowDuration = clock.now | ||
|
||
val totalTrackedTime = nowDuration - startTimeDuration | ||
val totalWithOffset = totalTrackedTime + OFFSET_MATCHING_BASE_INTERVAL | ||
val newInterval = totalWithOffset * BACKOFF_PROPORTION | ||
val clampedNewInterval = minOf(MAX_TIME_BETWEEN_HEARTBEATS, newInterval) | ||
return clampedNewInterval.inWholeMilliseconds | ||
} | ||
|
||
companion object { | ||
const val BACKOFF_PROPORTION = 0.3 | ||
val OFFSET_MATCHING_BASE_INTERVAL = 35.seconds | ||
val MAX_TIME_BETWEEN_HEARTBEATS = 15.minutes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
parsely/src/main/java/com/parsely/parselyandroid/UpdateEngagementIntervalCalculator.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
parsely/src/test/java/com/parsely/parselyandroid/HeartbeatIntervalCalculatorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.parsely.parselyandroid | ||
|
||
import com.parsely.parselyandroid.HeartbeatIntervalCalculator.Companion.MAX_TIME_BETWEEN_HEARTBEATS | ||
import java.util.Calendar | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
internal class HeartbeatIntervalCalculatorTest { | ||
|
||
private lateinit var sut: HeartbeatIntervalCalculator | ||
private val fakeClock = FakeClock() | ||
|
||
@Before | ||
fun setUp() { | ||
sut = HeartbeatIntervalCalculator(fakeClock) | ||
} | ||
|
||
@Test | ||
fun `given the same time of start and current time, when calculating interval, return offset times backoff proportion`() { | ||
// given | ||
fakeClock.fakeNow = Duration.ZERO | ||
val startTime = Calendar.getInstance().apply { | ||
timeInMillis = 0 | ||
} | ||
|
||
// when | ||
val result = sut.calculate(startTime) | ||
|
||
// then | ||
// ((currentTime + offset) * backoff) and then in milliseconds | ||
// (0 + 35) * 0.3 * 1000 = 10500 | ||
assertThat(result).isEqualTo(10500) | ||
} | ||
|
||
@Test | ||
fun `given a time that will cause the interval to surpass the MAX_TIME_BETWEEN_HEARTBEATS, when calculating interval, then return the MAX_TIME_BETWEEN_HEARTBEATS`() { | ||
// given | ||
// "excessiveTime" is a calculated point in time where the resulting interval would | ||
// surpass MAX_TIME_BETWEEN_HEARTBEATS | ||
// (currentTime + offset) * backoff = max | ||
// currentTime = (max / backoff) - offset, so | ||
// (15 minutes / 0.3) - 35 seconds = 2965 seconds. Add 1 second to be over the limit | ||
val excessiveTime = 2965.seconds + 1.seconds | ||
fakeClock.fakeNow = excessiveTime | ||
val startTime = Calendar.getInstance().apply { | ||
timeInMillis = 0 | ||
} | ||
|
||
// when | ||
val result = sut.calculate(startTime) | ||
|
||
// then | ||
assertThat(result).isEqualTo(MAX_TIME_BETWEEN_HEARTBEATS.inWholeMilliseconds) | ||
} | ||
|
||
@Test | ||
fun `given a specific time point, when updating latest interval, it correctly calculates the interval`() { | ||
// given | ||
val startTime = Calendar.getInstance().apply { | ||
timeInMillis = 0 | ||
} | ||
fakeClock.fakeNow = 2.seconds | ||
|
||
// when | ||
val result = sut.calculate(startTime) | ||
|
||
// then | ||
// ((currentTime + offset) * backoff) and then in milliseconds | ||
// (2 + 35) * 0.3 * 1000 = 11100 | ||
assertThat(result).isEqualTo(11100) | ||
} | ||
|
||
class FakeClock : Clock() { | ||
var fakeNow = Duration.ZERO | ||
|
||
override val now: Duration | ||
get() = fakeNow | ||
} | ||
} |