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

Add getSdkCurrentTime to internal interface #8

Merged
merged 3 commits into from
Oct 25, 2023
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 embrace-android-sdk/api/embrace-android-sdk.api
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public abstract interface annotation class io/embrace/android/embracesdk/annotat
}

public abstract interface class io/embrace/android/embracesdk/internal/EmbraceInternalInterface {
public abstract fun getSdkCurrentTime ()J
public abstract fun isNetworkSpanForwardingEnabled ()Z
public abstract fun logComposeTap (Landroid/util/Pair;Ljava/lang/String;)V
public abstract fun logError (Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Z)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ internal class EmbraceInternalInterfaceTest {
assertFalse(shouldCaptureNetworkBody("", ""))
setProcessStartedByNotification()
assertFalse(isNetworkSpanForwardingEnabled())
getSdkCurrentTime()
}
}

Expand Down Expand Up @@ -241,6 +242,16 @@ internal class EmbraceInternalInterfaceTest {
}
}

@Test
fun `test sdk time`() {
with(testRule) {
embrace.start(harness.fakeCoreModule.context)
assertEquals(harness.fakeClock.now(), embrace.internalInterface.getSdkCurrentTime())
harness.fakeClock.tick()
assertEquals(harness.fakeClock.now(), embrace.internalInterface.getSdkCurrentTime())
}
}

companion object {
private const val URL = "https://embrace.io"
private const val START_TIME = 1692201601L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ private void startImpl(@NonNull Context context,

// initialize internal interfaces
InternalInterfaceModuleImpl internalInterfaceModule = new InternalInterfaceModuleImpl(
initModule,
coreModule,
androidServicesModule,
essentialServiceModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.embrace.android.embracesdk

import android.util.Pair
import io.embrace.android.embracesdk.injection.InitModule
import io.embrace.android.embracesdk.internal.EmbraceInternalInterface
import io.embrace.android.embracesdk.network.EmbraceNetworkRequest
import io.embrace.android.embracesdk.network.http.HttpMethod
import io.embrace.android.embracesdk.network.http.NetworkCaptureData
import io.embrace.android.embracesdk.payload.TapBreadcrumb

internal class EmbraceInternalInterfaceImpl(
private val embraceImpl: EmbraceImpl
private val embraceImpl: EmbraceImpl,
private val initModule: InitModule
) : EmbraceInternalInterface {

override fun logInfo(message: String, properties: Map<String, Any>?) {
Expand Down Expand Up @@ -173,4 +175,6 @@ internal class EmbraceInternalInterfaceImpl(
override fun isNetworkSpanForwardingEnabled(): Boolean {
return embraceImpl.configService?.networkSpanForwardingBehavior?.isNetworkSpanForwardingEnabled() ?: false
}

override fun getSdkCurrentTime(): Long = initModule.clock.now()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.embrace.android.embracesdk.injection.AndroidServicesModule
import io.embrace.android.embracesdk.injection.CoreModule
import io.embrace.android.embracesdk.injection.CrashModule
import io.embrace.android.embracesdk.injection.EssentialServiceModule
import io.embrace.android.embracesdk.injection.InitModule
import io.embrace.android.embracesdk.injection.singleton
import io.embrace.android.embracesdk.internal.EmbraceInternalInterface

Expand All @@ -15,6 +16,7 @@ internal interface InternalInterfaceModule {
}

internal class InternalInterfaceModuleImpl(
initModule: InitModule,
coreModule: CoreModule,
androidServicesModule: AndroidServicesModule,
essentialServiceModule: EssentialServiceModule,
Expand All @@ -23,7 +25,7 @@ internal class InternalInterfaceModuleImpl(
) : InternalInterfaceModule {

override val embraceInternalInterface: EmbraceInternalInterface by singleton {
EmbraceInternalInterfaceImpl(embrace)
EmbraceInternalInterfaceImpl(embrace, initModule)
}

override val reactNativeInternalInterface: ReactNativeInternalInterface by singleton {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ public interface EmbraceInternalInterface {
* Whether the Network Span Forwarding feature is enabled
*/
public fun isNetworkSpanForwardingEnabled(): Boolean

/**
* Return internal time the SDK is using in milliseconds. It is equivalent to [System.currentTimeMillis] assuming the system clock did
* not change after the SDK has started.
*/
public fun getSdkCurrentTime(): Long
}

internal val defaultImpl = object : EmbraceInternalInterface {
Expand Down Expand Up @@ -185,4 +191,6 @@ internal val defaultImpl = object : EmbraceInternalInterface {
override fun setProcessStartedByNotification() { }

override fun isNetworkSpanForwardingEnabled(): Boolean = false

override fun getSdkCurrentTime(): Long = System.currentTimeMillis()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package io.embrace.android.embracesdk

import android.net.Uri
import android.webkit.URLUtil
import io.embrace.android.embracesdk.fakes.FakeClock
import io.embrace.android.embracesdk.fakes.injection.FakeInitModule
import io.embrace.android.embracesdk.injection.InitModule
import io.embrace.android.embracesdk.internal.defaultImpl
import io.embrace.android.embracesdk.network.EmbraceNetworkRequest
import io.embrace.android.embracesdk.network.http.HttpMethod
import io.mockk.every
Expand All @@ -11,18 +15,23 @@ import io.mockk.slot
import io.mockk.verify
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test

internal class EmbraceInternalInterfaceImplTest {

private lateinit var impl: EmbraceInternalInterfaceImpl
private lateinit var embrace: EmbraceImpl
private lateinit var fakeClock: FakeClock
private lateinit var initModule: InitModule

@Before
fun setUp() {
embrace = mockk(relaxed = true)
impl = EmbraceInternalInterfaceImpl(embrace)
fakeClock = FakeClock(currentTime = beforeObjectInitTime)
initModule = FakeInitModule(clock = fakeClock)
impl = EmbraceInternalInterfaceImpl(embrace, initModule)
}

@Test
Expand Down Expand Up @@ -176,4 +185,22 @@ internal class EmbraceInternalInterfaceImplTest {

assertEquals(url, captor.captured.url)
}

@Test
fun `check usage of SDK time`() {
assertEquals(beforeObjectInitTime, impl.getSdkCurrentTime())
assertTrue(impl.getSdkCurrentTime() < System.currentTimeMillis())
fakeClock.tick(10L)
assertEquals(fakeClock.now(), impl.getSdkCurrentTime())
}

@Test
fun `check default implementation`() {
assertTrue(beforeObjectInitTime < defaultImpl.getSdkCurrentTime())
assertTrue(defaultImpl.getSdkCurrentTime() <= System.currentTimeMillis())
}

companion object {
val beforeObjectInitTime = System.currentTimeMillis() - 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.embrace.android.embracesdk.fakes.injection.FakeAndroidServicesModule
import io.embrace.android.embracesdk.fakes.injection.FakeCoreModule
import io.embrace.android.embracesdk.fakes.injection.FakeCrashModule
import io.embrace.android.embracesdk.fakes.injection.FakeEssentialServiceModule
import io.embrace.android.embracesdk.fakes.injection.FakeInitModule
import org.junit.Assert.assertNotNull
import org.junit.Test

Expand All @@ -12,6 +13,7 @@ internal class InternalInterfaceModuleImplTest {
@Test
fun testModule() {
val module: InternalInterfaceModule = InternalInterfaceModuleImpl(
FakeInitModule(),
FakeCoreModule(),
FakeAndroidServicesModule(),
FakeEssentialServiceModule(),
Expand Down
Loading