Skip to content

Commit

Permalink
Add getSdkCurrentTime to internal interface (#8)
Browse files Browse the repository at this point in the history
## Goal

Add the method to the internal interface so the OkHttp module or code that can only access the static `Embrace` instance can use the same time as the core SDK.

## Testing

<!-- Describe how this change has been tested -->

## Release Notes

Add unit and integration tests
  • Loading branch information
bidetofevil authored Oct 25, 2023
2 parents cac62fc + 924a37b commit 1e103be
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 3 deletions.
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

0 comments on commit 1e103be

Please sign in to comment.