Skip to content

Commit

Permalink
Merge pull request #2363 from DataDog/nogorodnikov/lazy-rum-event-cre…
Browse files Browse the repository at this point in the history
…ation-in-event-generators

Lazy RUM raw event creation in event generator methods
  • Loading branch information
0xnm authored Oct 31, 2024
2 parents a1b97be + 0b9e2ac commit 51ab074
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ internal class RumApplicationScopeTest {
val fakeEvents = forge.aList {
forge.anyRumEvent(
excluding = listOf(
RumRawEvent.ApplicationStarted::class.java,
RumRawEvent.SdkInit::class.java
RumRawEvent.ApplicationStarted::class,
RumRawEvent.SdkInit::class
)
)
}
Expand Down Expand Up @@ -428,8 +428,8 @@ internal class RumApplicationScopeTest {
val fakeEvents = forge.aList {
forge.anyRumEvent(
excluding = listOf(
RumRawEvent.ApplicationStarted::class.java,
RumRawEvent.SdkInit::class.java
RumRawEvent.ApplicationStarted::class,
RumRawEvent.SdkInit::class
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import com.datadog.android.rum.internal.domain.Time
import com.datadog.tools.unit.forge.aThrowable
import com.datadog.tools.unit.forge.exhaustiveAttributes
import fr.xgouchet.elmyr.Forge
import java.lang.reflect.Type
import java.net.URL
import java.util.UUID
import kotlin.reflect.KClass

internal fun Forge.interactiveRumRawEvent(): RumRawEvent {
return anElementFrom(
Expand Down Expand Up @@ -168,48 +168,50 @@ internal fun Forge.addCustomTimingEvent(): RumRawEvent.AddCustomTiming {
internal fun Forge.validBackgroundEvent(): RumRawEvent {
return this.anElementFrom(
listOf(
startActionEvent(),
addErrorEvent(),
startResourceEvent()
{ startActionEvent() },
{ addErrorEvent() },
{ startResourceEvent() }
)
)
).invoke()
}

internal fun Forge.invalidBackgroundEvent(): RumRawEvent {
return this.anElementFrom(
listOf(
addLongTaskEvent(),
stopActionEvent(),
stopResourceEvent(),
stopResourceWithErrorEvent(),
stopResourceWithStacktraceEvent(),
addViewLoadingTimeEvent()
{ addLongTaskEvent() },
{ stopActionEvent() },
{ stopResourceEvent() },
{ stopResourceWithErrorEvent() },
{ stopResourceWithStacktraceEvent() },
{ addViewLoadingTimeEvent() }
)
)
}

internal fun Forge.anyRumEvent(excluding: List<Type> = listOf()): RumRawEvent {
val allEvents = listOf(
startViewEvent(),
stopViewEvent(),
startActionEvent(),
stopActionEvent(),
startResourceEvent(),
stopResourceEvent(),
stopResourceWithErrorEvent(),
stopResourceWithStacktraceEvent(),
addErrorEvent(),
addLongTaskEvent(),
addFeatureFlagEvaluationEvent(),
addCustomTimingEvent(),
updatePerformanceMetricEvent(),
addViewLoadingTimeEvent()
).invoke()
}

internal fun Forge.anyRumEvent(excluding: List<KClass<out RumRawEvent>> = listOf()): RumRawEvent {
fun <T : RumRawEvent> strictSameTypePair(key: KClass<T>, value: () -> T) = key to value
val allEventsFactories = mapOf<KClass<out RumRawEvent>, () -> RumRawEvent>(
strictSameTypePair(RumRawEvent.StartView::class, { startViewEvent() }),
strictSameTypePair(RumRawEvent.StopView::class, { stopViewEvent() }),
strictSameTypePair(RumRawEvent.StartAction::class, { startActionEvent() }),
strictSameTypePair(RumRawEvent.StopAction::class, { stopActionEvent() }),
strictSameTypePair(RumRawEvent.StartResource::class, { startResourceEvent() }),
strictSameTypePair(RumRawEvent.StopResource::class, { stopResourceEvent() }),
strictSameTypePair(RumRawEvent.StopResourceWithError::class, { stopResourceWithErrorEvent() }),
strictSameTypePair(RumRawEvent.StopResourceWithStackTrace::class, { stopResourceWithStacktraceEvent() }),
strictSameTypePair(RumRawEvent.AddError::class, { addErrorEvent() }),
strictSameTypePair(RumRawEvent.AddLongTask::class, { addLongTaskEvent() }),
strictSameTypePair(RumRawEvent.AddFeatureFlagEvaluation::class, { addFeatureFlagEvaluationEvent() }),
strictSameTypePair(RumRawEvent.AddCustomTiming::class, { addCustomTimingEvent() }),
strictSameTypePair(RumRawEvent.UpdatePerformanceMetric::class, { updatePerformanceMetricEvent() }),
strictSameTypePair(RumRawEvent.AddViewLoadingTime::class, { addViewLoadingTimeEvent() })
)
return this.anElementFrom(
allEvents.filter {
it.javaClass !in excluding
}
)
allEventsFactories
.filter { !excluding.contains(it.key) }
.values
.toList()
).invoke()
}

internal fun Forge.invalidAppLaunchEvent(): RumRawEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -962,14 +962,14 @@ internal class RumSessionScopeTest {
initializeTestedScope(backgroundTrackingEnabled = false)
val fakeNonInteractionEvent1 = forge.anyRumEvent(
excluding = listOf(
RumRawEvent.StartView::class.java,
RumRawEvent.StartAction::class.java
RumRawEvent.StartView::class,
RumRawEvent.StartAction::class
)
)
val fakeNonInteractionEvent2 = forge.anyRumEvent(
excluding = listOf(
RumRawEvent.StartView::class.java,
RumRawEvent.StartAction::class.java
RumRawEvent.StartView::class,
RumRawEvent.StartAction::class
)
)
testedScope.handleEvent(fakeNonInteractionEvent1, mockWriter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9349,14 +9349,14 @@ internal class RumViewScopeTest {
testedScope.handleEvent(
forge.anyRumEvent(
excluding = listOf(
RumRawEvent.StartView::class.java,
RumRawEvent.StopView::class.java,
RumRawEvent.StartAction::class.java,
RumRawEvent.StopAction::class.java,
RumRawEvent.StartResource::class.java,
RumRawEvent.StopResource::class.java,
RumRawEvent.StopResourceWithError::class.java,
RumRawEvent.StopResourceWithStackTrace::class.java
RumRawEvent.StartView::class,
RumRawEvent.StopView::class,
RumRawEvent.StartAction::class,
RumRawEvent.StopAction::class,
RumRawEvent.StartResource::class,
RumRawEvent.StopResource::class,
RumRawEvent.StopResourceWithError::class,
RumRawEvent.StopResourceWithStackTrace::class
)
),
mockWriter
Expand Down

0 comments on commit 51ab074

Please sign in to comment.