Skip to content

Commit 51ab074

Browse files
authored
Merge pull request #2363 from DataDog/nogorodnikov/lazy-rum-event-creation-in-event-generators
Lazy RUM raw event creation in event generator methods
2 parents a1b97be + 0b9e2ac commit 51ab074

File tree

4 files changed

+52
-50
lines changed

4 files changed

+52
-50
lines changed

features/dd-sdk-android-rum/src/test/kotlin/com/datadog/android/rum/internal/domain/scope/RumApplicationScopeTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ internal class RumApplicationScopeTest {
379379
val fakeEvents = forge.aList {
380380
forge.anyRumEvent(
381381
excluding = listOf(
382-
RumRawEvent.ApplicationStarted::class.java,
383-
RumRawEvent.SdkInit::class.java
382+
RumRawEvent.ApplicationStarted::class,
383+
RumRawEvent.SdkInit::class
384384
)
385385
)
386386
}
@@ -428,8 +428,8 @@ internal class RumApplicationScopeTest {
428428
val fakeEvents = forge.aList {
429429
forge.anyRumEvent(
430430
excluding = listOf(
431-
RumRawEvent.ApplicationStarted::class.java,
432-
RumRawEvent.SdkInit::class.java
431+
RumRawEvent.ApplicationStarted::class,
432+
RumRawEvent.SdkInit::class
433433
)
434434
)
435435
}

features/dd-sdk-android-rum/src/test/kotlin/com/datadog/android/rum/internal/domain/scope/RumRawEventExt.kt

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import com.datadog.android.rum.internal.domain.Time
1414
import com.datadog.tools.unit.forge.aThrowable
1515
import com.datadog.tools.unit.forge.exhaustiveAttributes
1616
import fr.xgouchet.elmyr.Forge
17-
import java.lang.reflect.Type
1817
import java.net.URL
1918
import java.util.UUID
19+
import kotlin.reflect.KClass
2020

2121
internal fun Forge.interactiveRumRawEvent(): RumRawEvent {
2222
return anElementFrom(
@@ -168,48 +168,50 @@ internal fun Forge.addCustomTimingEvent(): RumRawEvent.AddCustomTiming {
168168
internal fun Forge.validBackgroundEvent(): RumRawEvent {
169169
return this.anElementFrom(
170170
listOf(
171-
startActionEvent(),
172-
addErrorEvent(),
173-
startResourceEvent()
171+
{ startActionEvent() },
172+
{ addErrorEvent() },
173+
{ startResourceEvent() }
174174
)
175-
)
175+
).invoke()
176176
}
177177

178178
internal fun Forge.invalidBackgroundEvent(): RumRawEvent {
179179
return this.anElementFrom(
180180
listOf(
181-
addLongTaskEvent(),
182-
stopActionEvent(),
183-
stopResourceEvent(),
184-
stopResourceWithErrorEvent(),
185-
stopResourceWithStacktraceEvent(),
186-
addViewLoadingTimeEvent()
181+
{ addLongTaskEvent() },
182+
{ stopActionEvent() },
183+
{ stopResourceEvent() },
184+
{ stopResourceWithErrorEvent() },
185+
{ stopResourceWithStacktraceEvent() },
186+
{ addViewLoadingTimeEvent() }
187187
)
188-
)
189-
}
190-
191-
internal fun Forge.anyRumEvent(excluding: List<Type> = listOf()): RumRawEvent {
192-
val allEvents = listOf(
193-
startViewEvent(),
194-
stopViewEvent(),
195-
startActionEvent(),
196-
stopActionEvent(),
197-
startResourceEvent(),
198-
stopResourceEvent(),
199-
stopResourceWithErrorEvent(),
200-
stopResourceWithStacktraceEvent(),
201-
addErrorEvent(),
202-
addLongTaskEvent(),
203-
addFeatureFlagEvaluationEvent(),
204-
addCustomTimingEvent(),
205-
updatePerformanceMetricEvent(),
206-
addViewLoadingTimeEvent()
188+
).invoke()
189+
}
190+
191+
internal fun Forge.anyRumEvent(excluding: List<KClass<out RumRawEvent>> = listOf()): RumRawEvent {
192+
fun <T : RumRawEvent> strictSameTypePair(key: KClass<T>, value: () -> T) = key to value
193+
val allEventsFactories = mapOf<KClass<out RumRawEvent>, () -> RumRawEvent>(
194+
strictSameTypePair(RumRawEvent.StartView::class, { startViewEvent() }),
195+
strictSameTypePair(RumRawEvent.StopView::class, { stopViewEvent() }),
196+
strictSameTypePair(RumRawEvent.StartAction::class, { startActionEvent() }),
197+
strictSameTypePair(RumRawEvent.StopAction::class, { stopActionEvent() }),
198+
strictSameTypePair(RumRawEvent.StartResource::class, { startResourceEvent() }),
199+
strictSameTypePair(RumRawEvent.StopResource::class, { stopResourceEvent() }),
200+
strictSameTypePair(RumRawEvent.StopResourceWithError::class, { stopResourceWithErrorEvent() }),
201+
strictSameTypePair(RumRawEvent.StopResourceWithStackTrace::class, { stopResourceWithStacktraceEvent() }),
202+
strictSameTypePair(RumRawEvent.AddError::class, { addErrorEvent() }),
203+
strictSameTypePair(RumRawEvent.AddLongTask::class, { addLongTaskEvent() }),
204+
strictSameTypePair(RumRawEvent.AddFeatureFlagEvaluation::class, { addFeatureFlagEvaluationEvent() }),
205+
strictSameTypePair(RumRawEvent.AddCustomTiming::class, { addCustomTimingEvent() }),
206+
strictSameTypePair(RumRawEvent.UpdatePerformanceMetric::class, { updatePerformanceMetricEvent() }),
207+
strictSameTypePair(RumRawEvent.AddViewLoadingTime::class, { addViewLoadingTimeEvent() })
207208
)
208209
return this.anElementFrom(
209-
allEvents.filter {
210-
it.javaClass !in excluding
211-
}
212-
)
210+
allEventsFactories
211+
.filter { !excluding.contains(it.key) }
212+
.values
213+
.toList()
214+
).invoke()
213215
}
214216

215217
internal fun Forge.invalidAppLaunchEvent(): RumRawEvent {

features/dd-sdk-android-rum/src/test/kotlin/com/datadog/android/rum/internal/domain/scope/RumSessionScopeTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -962,14 +962,14 @@ internal class RumSessionScopeTest {
962962
initializeTestedScope(backgroundTrackingEnabled = false)
963963
val fakeNonInteractionEvent1 = forge.anyRumEvent(
964964
excluding = listOf(
965-
RumRawEvent.StartView::class.java,
966-
RumRawEvent.StartAction::class.java
965+
RumRawEvent.StartView::class,
966+
RumRawEvent.StartAction::class
967967
)
968968
)
969969
val fakeNonInteractionEvent2 = forge.anyRumEvent(
970970
excluding = listOf(
971-
RumRawEvent.StartView::class.java,
972-
RumRawEvent.StartAction::class.java
971+
RumRawEvent.StartView::class,
972+
RumRawEvent.StartAction::class
973973
)
974974
)
975975
testedScope.handleEvent(fakeNonInteractionEvent1, mockWriter)

features/dd-sdk-android-rum/src/test/kotlin/com/datadog/android/rum/internal/domain/scope/RumViewScopeTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9349,14 +9349,14 @@ internal class RumViewScopeTest {
93499349
testedScope.handleEvent(
93509350
forge.anyRumEvent(
93519351
excluding = listOf(
9352-
RumRawEvent.StartView::class.java,
9353-
RumRawEvent.StopView::class.java,
9354-
RumRawEvent.StartAction::class.java,
9355-
RumRawEvent.StopAction::class.java,
9356-
RumRawEvent.StartResource::class.java,
9357-
RumRawEvent.StopResource::class.java,
9358-
RumRawEvent.StopResourceWithError::class.java,
9359-
RumRawEvent.StopResourceWithStackTrace::class.java
9352+
RumRawEvent.StartView::class,
9353+
RumRawEvent.StopView::class,
9354+
RumRawEvent.StartAction::class,
9355+
RumRawEvent.StopAction::class,
9356+
RumRawEvent.StartResource::class,
9357+
RumRawEvent.StopResource::class,
9358+
RumRawEvent.StopResourceWithError::class,
9359+
RumRawEvent.StopResourceWithStackTrace::class
93609360
)
93619361
),
93629362
mockWriter

0 commit comments

Comments
 (0)