Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventProducer - rely on true time instead of the system clock #14

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading