Skip to content

Commit

Permalink
feat: use Clock for getting time in EventsBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
wzieba committed Dec 14, 2023
1 parent 3dc2e2b commit d154b8a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import java.util.TimeZone

internal class EventsBuilder(
private val deviceInfoRepository: DeviceInfoRepository,
private val siteId: String
private val siteId: String,
private val clock: Clock,
) {
/**
* Create an event Map
Expand All @@ -26,7 +27,6 @@ internal class EventsBuilder(
uuid: String
): Map<String, Any> {
log("buildEvent called for %s/%s", action, url)
val now = Calendar.getInstance(TimeZone.getTimeZone("UTC"))

// Main event info
val event: MutableMap<String, Any> = HashMap()
Expand All @@ -41,7 +41,7 @@ internal class EventsBuilder(
data.putAll(extraData)
}
val deviceInfo = deviceInfoRepository.collectDeviceInfo()
data["ts"] = now.timeInMillis
data["ts"] = clock.now.inWholeMilliseconds
data.putAll(deviceInfo)
event["data"] = data
metadata?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ public class ParselyTracker {
*/
protected ParselyTracker(String siteId, int flushInterval, Context c) {
Context context = c.getApplicationContext();
clock = new Clock();
eventsBuilder = new EventsBuilder(
new AndroidDeviceInfoRepository(
new AdvertisementIdProvider(context, ParselyCoroutineScopeKt.getSdkScope()),
new AndroidIdProvider(context)
), siteId);
), siteId, clock);
LocalStorageRepository localStorageRepository = new LocalStorageRepository(context);
flushManager = new ParselyFlushManager(new Function0<Unit>() {
@Override
Expand All @@ -88,7 +89,6 @@ public Unit invoke() {
return Unit.INSTANCE;
});
flushQueue = new FlushQueue(flushManager, localStorageRepository, new ParselyAPIConnection(ROOT_URL + "mobileproxy"), ParselyCoroutineScopeKt.getSdkScope(), new AndroidConnectivityStatusProvider(context));
clock = new Clock();
intervalCalculator = new HeartbeatIntervalCalculator(clock);

// get the adkey straight away on instantiation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.parsely.parselyandroid

import kotlin.time.Duration
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.test.TestCoroutineScheduler
import kotlinx.coroutines.test.runTest
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.MapAssert
import org.junit.Before
import org.junit.Test

internal class EventsBuilderTest {
private lateinit var sut: EventsBuilder
private val clock = FakeClock()

@Before
fun setUp() {
sut = EventsBuilder(
FakeDeviceInfoRepository(),
TEST_SITE_ID,
clock
)
}

@Test
fun `when building pageview event, then build the correct one`() {
fun `when building pageview event, then build the correct one`() = runTest {
// when
val event: Map<String, Any> = sut.buildEvent(
TEST_URL,
Expand Down Expand Up @@ -187,20 +194,22 @@ internal class EventsBuilderTest {
assertThat(it)
.hasSize(2)
.containsAllEntriesOf(FAKE_DEVICE_INFO)
.hasEntrySatisfying("ts") { timestamp ->
assertThat(timestamp as Long).isBetween(1111111111111, 9999999999999)
}
}

companion object {
const val TEST_SITE_ID = "Example"
const val TEST_URL = "http://example.com/some-old/article.html"
const val TEST_UUID = "123e4567-e89b-12d3-a456-426614174000"

val FAKE_DEVICE_INFO = mapOf("device" to "info")
val FAKE_NOW = 15.hours
val FAKE_DEVICE_INFO = mapOf("device" to "info", "ts" to FAKE_NOW.inWholeMilliseconds.toString())
}

class FakeDeviceInfoRepository: DeviceInfoRepository {
override fun collectDeviceInfo(): Map<String, String> = FAKE_DEVICE_INFO
}

class FakeClock() : Clock() {
override val now: Duration = FAKE_NOW
}
}

0 comments on commit d154b8a

Please sign in to comment.