|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. |
| 3 | + * This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | + * Copyright 2016-Present Datadog, Inc. |
| 5 | + */ |
| 6 | + |
| 7 | +package com.datadog.android.rum.internal.domain.scope |
| 8 | + |
| 9 | +import com.datadog.android.api.InternalLogger |
| 10 | +import com.datadog.android.api.context.DatadogContext |
| 11 | +import com.datadog.android.api.context.NetworkInfo |
| 12 | +import com.datadog.android.api.feature.Feature |
| 13 | +import com.datadog.android.api.feature.FeatureScope |
| 14 | +import com.datadog.android.api.storage.DataWriter |
| 15 | +import com.datadog.android.api.storage.EventBatchWriter |
| 16 | +import com.datadog.android.api.storage.EventType |
| 17 | +import com.datadog.android.core.InternalSdkCore |
| 18 | +import com.datadog.android.core.internal.net.FirstPartyHostHeaderTypeResolver |
| 19 | +import com.datadog.android.rum.RumActionType |
| 20 | +import com.datadog.android.rum.assertj.ActionEventAssert.Companion.assertThat |
| 21 | +import com.datadog.android.rum.internal.FeaturesContextResolver |
| 22 | +import com.datadog.android.rum.internal.domain.RumContext |
| 23 | +import com.datadog.android.rum.internal.domain.Time |
| 24 | +import com.datadog.android.rum.internal.vitals.VitalMonitor |
| 25 | +import com.datadog.android.rum.model.ActionEvent |
| 26 | +import com.datadog.android.rum.utils.config.GlobalRumMonitorTestConfiguration |
| 27 | +import com.datadog.android.rum.utils.forge.Configurator |
| 28 | +import com.datadog.tools.unit.annotations.TestConfigurationsProvider |
| 29 | +import com.datadog.tools.unit.extensions.TestConfigurationExtension |
| 30 | +import com.datadog.tools.unit.extensions.config.TestConfiguration |
| 31 | +import com.datadog.tools.unit.forge.exhaustiveAttributes |
| 32 | +import fr.xgouchet.elmyr.Forge |
| 33 | +import fr.xgouchet.elmyr.annotation.BoolForgery |
| 34 | +import fr.xgouchet.elmyr.annotation.FloatForgery |
| 35 | +import fr.xgouchet.elmyr.annotation.Forgery |
| 36 | +import fr.xgouchet.elmyr.annotation.LongForgery |
| 37 | +import fr.xgouchet.elmyr.annotation.StringForgery |
| 38 | +import fr.xgouchet.elmyr.junit5.ForgeConfiguration |
| 39 | +import fr.xgouchet.elmyr.junit5.ForgeExtension |
| 40 | +import org.assertj.core.api.Assertions.assertThat |
| 41 | +import org.junit.jupiter.api.BeforeEach |
| 42 | +import org.junit.jupiter.api.Test |
| 43 | +import org.junit.jupiter.api.extension.ExtendWith |
| 44 | +import org.junit.jupiter.api.extension.Extensions |
| 45 | +import org.mockito.Mock |
| 46 | +import org.mockito.junit.jupiter.MockitoExtension |
| 47 | +import org.mockito.junit.jupiter.MockitoSettings |
| 48 | +import org.mockito.kotlin.any |
| 49 | +import org.mockito.kotlin.argumentCaptor |
| 50 | +import org.mockito.kotlin.doAnswer |
| 51 | +import org.mockito.kotlin.doReturn |
| 52 | +import org.mockito.kotlin.eq |
| 53 | +import org.mockito.kotlin.mock |
| 54 | +import org.mockito.kotlin.verify |
| 55 | +import org.mockito.kotlin.whenever |
| 56 | +import org.mockito.quality.Strictness |
| 57 | + |
| 58 | +@Extensions( |
| 59 | + ExtendWith(MockitoExtension::class), |
| 60 | + ExtendWith(ForgeExtension::class), |
| 61 | + ExtendWith(TestConfigurationExtension::class) |
| 62 | +) |
| 63 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 64 | +@ForgeConfiguration(Configurator::class) |
| 65 | +internal class RumActionScopeAttributePropagationTest { |
| 66 | + |
| 67 | + lateinit var testedScope: RumActionScope |
| 68 | + |
| 69 | + @Mock |
| 70 | + lateinit var mockSdkCore: InternalSdkCore |
| 71 | + |
| 72 | + @Mock |
| 73 | + lateinit var mockParentScope: RumScope |
| 74 | + |
| 75 | + @Mock |
| 76 | + lateinit var mockWriter: DataWriter<Any> |
| 77 | + |
| 78 | + @Mock |
| 79 | + lateinit var mockResolver: FirstPartyHostHeaderTypeResolver |
| 80 | + |
| 81 | + @Mock |
| 82 | + lateinit var mockCpuVitalMonitor: VitalMonitor |
| 83 | + |
| 84 | + @Mock |
| 85 | + lateinit var mockMemoryVitalMonitor: VitalMonitor |
| 86 | + |
| 87 | + @Mock |
| 88 | + lateinit var mockFrameRateVitalMonitor: VitalMonitor |
| 89 | + |
| 90 | + @Mock |
| 91 | + lateinit var mockInternalLogger: InternalLogger |
| 92 | + |
| 93 | + @Mock |
| 94 | + lateinit var mockRumFeatureScope: FeatureScope |
| 95 | + |
| 96 | + @Mock |
| 97 | + lateinit var mockEventBatchWriter: EventBatchWriter |
| 98 | + |
| 99 | + @Mock |
| 100 | + lateinit var mockFeaturesContextResolver: FeaturesContextResolver |
| 101 | + |
| 102 | + lateinit var fakeParentAttributes: Map<String, Any?> |
| 103 | + |
| 104 | + lateinit var fakeActionAttributes: Map<String, Any?> |
| 105 | + |
| 106 | + @Forgery |
| 107 | + lateinit var fakeParentContext: RumContext |
| 108 | + |
| 109 | + lateinit var fakeEventTime: Time |
| 110 | + |
| 111 | + lateinit var fakeEvent: RumRawEvent |
| 112 | + |
| 113 | + @Forgery |
| 114 | + lateinit var fakeType: RumActionType |
| 115 | + |
| 116 | + @StringForgery |
| 117 | + lateinit var fakeName: String |
| 118 | + |
| 119 | + @Forgery |
| 120 | + lateinit var fakeNetworkInfoAtScopeStart: NetworkInfo |
| 121 | + |
| 122 | + @Forgery |
| 123 | + lateinit var fakeDatadogContext: DatadogContext |
| 124 | + |
| 125 | + @BoolForgery |
| 126 | + var fakeHasReplay: Boolean = false |
| 127 | + |
| 128 | + @FloatForgery(min = 0f, max = 100f) |
| 129 | + var fakeSampleRate: Float = 0f |
| 130 | + |
| 131 | + @BoolForgery |
| 132 | + var fakeTrackFrustrations: Boolean = true |
| 133 | + |
| 134 | + @LongForgery(-1000L, 1000L) |
| 135 | + var fakeServerOffset: Long = 0L |
| 136 | + |
| 137 | + @BeforeEach |
| 138 | + fun `set up`(forge: Forge) { |
| 139 | + fakeEventTime = Time() |
| 140 | + |
| 141 | + fakeParentAttributes = forge.exhaustiveAttributes() |
| 142 | + fakeActionAttributes = forge.exhaustiveAttributes() |
| 143 | + whenever(mockParentScope.getCustomAttributes()) doReturn fakeParentAttributes.toMutableMap() |
| 144 | + whenever(mockParentScope.getRumContext()) doReturn fakeParentContext |
| 145 | + |
| 146 | + whenever(rumMonitor.mockSdkCore.internalLogger) doReturn mock() |
| 147 | + whenever(rumMonitor.mockSdkCore.networkInfo) doReturn fakeNetworkInfoAtScopeStart |
| 148 | + whenever(rumMonitor.mockSdkCore.getFeature(Feature.RUM_FEATURE_NAME)) doReturn mockRumFeatureScope |
| 149 | + whenever(mockRumFeatureScope.withWriteContext(any(), any())) doAnswer { |
| 150 | + val callback = it.getArgument<(DatadogContext, EventBatchWriter) -> Unit>(1) |
| 151 | + callback.invoke(fakeDatadogContext, mockEventBatchWriter) |
| 152 | + } |
| 153 | + whenever(mockWriter.write(eq(mockEventBatchWriter), any(), eq(EventType.DEFAULT))) doReturn true |
| 154 | + |
| 155 | + testedScope = RumActionScope( |
| 156 | + parentScope = mockParentScope, |
| 157 | + sdkCore = rumMonitor.mockSdkCore, |
| 158 | + waitForStop = false, |
| 159 | + eventTime = fakeEventTime, |
| 160 | + initialType = fakeType, |
| 161 | + initialName = fakeName, |
| 162 | + initialAttributes = fakeActionAttributes, |
| 163 | + serverTimeOffsetInMs = fakeServerOffset, |
| 164 | + inactivityThresholdMs = TEST_INACTIVITY_MS, |
| 165 | + maxDurationMs = TEST_MAX_DURATION_MS, |
| 166 | + featuresContextResolver = mockFeaturesContextResolver, |
| 167 | + trackFrustrations = true, |
| 168 | + sampleRate = fakeSampleRate |
| 169 | + ) |
| 170 | + } |
| 171 | + |
| 172 | + // region Propagate parent attributes |
| 173 | + |
| 174 | + @Test |
| 175 | + fun `M return parent and action attributes W getCustomAttributes()`() { |
| 176 | + // Given |
| 177 | + val expectedAttributes = mutableMapOf<String, Any?>() |
| 178 | + expectedAttributes.putAll(fakeParentAttributes) |
| 179 | + expectedAttributes.putAll(fakeActionAttributes) |
| 180 | + |
| 181 | + // When |
| 182 | + val customAttributes = testedScope.getCustomAttributes() |
| 183 | + |
| 184 | + // Then |
| 185 | + assertThat(customAttributes) |
| 186 | + .containsExactlyInAnyOrderEntriesOf(expectedAttributes) |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + fun `M send Action with parent attributes W handleEvent(any)`() { |
| 191 | + // Given |
| 192 | + val expectedAttributes = mutableMapOf<String, Any?>() |
| 193 | + expectedAttributes.putAll(fakeParentAttributes) |
| 194 | + expectedAttributes.putAll(fakeActionAttributes) |
| 195 | + |
| 196 | + // When |
| 197 | + Thread.sleep(TEST_INACTIVITY_MS * 2) |
| 198 | + testedScope.handleEvent(mockEvent(), mockWriter) |
| 199 | + |
| 200 | + // Then |
| 201 | + argumentCaptor<ActionEvent> { |
| 202 | + verify(mockWriter).write(eq(mockEventBatchWriter), capture(), eq(EventType.DEFAULT)) |
| 203 | + assertThat(lastValue) |
| 204 | + .containsExactlyContextAttributes(expectedAttributes) |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + // endregion |
| 209 | + |
| 210 | + // region Internal |
| 211 | + |
| 212 | + private fun mockEvent(): RumRawEvent { |
| 213 | + val event: RumRawEvent = mock() |
| 214 | + whenever(event.eventTime) doReturn Time() |
| 215 | + return event |
| 216 | + } |
| 217 | + |
| 218 | + // endregion |
| 219 | + |
| 220 | + companion object { |
| 221 | + |
| 222 | + val rumMonitor = GlobalRumMonitorTestConfiguration() |
| 223 | + |
| 224 | + @TestConfigurationsProvider |
| 225 | + @JvmStatic |
| 226 | + fun getTestConfigurations(): List<TestConfiguration> { |
| 227 | + return listOf(rumMonitor) |
| 228 | + } |
| 229 | + |
| 230 | + private const val TEST_SLEEP_MS = 50L |
| 231 | + private const val TEST_INACTIVITY_MS = TEST_SLEEP_MS * 3 |
| 232 | + private const val TEST_MAX_DURATION_MS = TEST_SLEEP_MS * 10 |
| 233 | + } |
| 234 | +} |
0 commit comments