Skip to content

Commit

Permalink
Rely on true time instead of the system clock
Browse files Browse the repository at this point in the history
  • Loading branch information
paulinaaniola committed Jun 11, 2024
1 parent ed8b134 commit 289f5a2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions eventproducer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
implementation(libs.tickaroo.annotation)
implementation(libs.tickaroo.core)
implementation(libs.tickaroo.retrofitConverter)
implementation(libs.truetime)

ksp(libs.dagger.compiler)
kapt(libs.room.compiler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package com.tidal.sdk.eventproducer.di
import com.tidal.sdk.auth.CredentialsProvider
import com.tidal.sdk.eventproducer.model.EventsConfigProvider
import com.tidal.sdk.eventproducer.utils.HeadersUtils
import com.tidal.sdk.eventproducer.utils.TrueTimeWrapper
import dagger.Module
import dagger.Provides
import dagger.Reusable

@Module
internal class UtilsModule {
Expand All @@ -13,5 +15,10 @@ internal class UtilsModule {
fun provideHeadersUtils(
configProvider: EventsConfigProvider,
credentialsProvider: CredentialsProvider,
) = HeadersUtils(configProvider.config.appVersion, credentialsProvider)
trueTimeWrapper: TrueTimeWrapper,
) = HeadersUtils(configProvider.config.appVersion, credentialsProvider, trueTimeWrapper)

@Provides
@Reusable
fun provideTrueTimeWrapper() = TrueTimeWrapper()
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal const val DEVICE_VENDOR_KEY = "device-vendor"
internal class HeadersUtils @Inject constructor(
private val appVersion: String,
private val credentialsProvider: CredentialsProvider,
private val trueTimeWrapper: TrueTimeWrapper,
) {
fun getEventHeaders(
defaultHeaders: Map<String, String>,
Expand All @@ -31,7 +32,7 @@ internal class HeadersUtils @Inject constructor(
): Map<String, String> {
val deviceModel = Build.MODEL
val deviceVendor = Build.MANUFACTURER
val sentTimestamp = System.currentTimeMillis().toString()
val sentTimestamp = trueTimeWrapper.currentTimeMillis.toString()
val osName = "Android"
val osVersion = Build.VERSION.SDK_INT.toString()
val headers = mutableMapOf<String, String>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.tidal.sdk.eventproducer.utils

import com.instacart.library.truetime.TrueTime

class TrueTimeWrapper {

val currentTimeMillis: Long
get() = try {
TrueTime.now().time
} catch (_: Throwable) {
System.currentTimeMillis()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.tidal.sdk.eventproducer.utils.APP_VERSION_KEY
import com.tidal.sdk.eventproducer.utils.CLIENT_ID_KEY
import com.tidal.sdk.eventproducer.utils.HeadersUtils
import com.tidal.sdk.eventproducer.utils.OS_NAME_KEY
import com.tidal.sdk.eventproducer.utils.TrueTimeWrapper
import io.mockk.mockk
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

Expand All @@ -15,6 +17,7 @@ class HeadersUtilsTest {
private val headerUtils = HeadersUtils(
"",
FakeCredentialsProvider(),
mockk<TrueTimeWrapper>(),
)

@Test
Expand Down

0 comments on commit 289f5a2

Please sign in to comment.