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

RUM-891 Unit test to confirm SR records order is kept when having same timestamps #1659

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.google.gson.JsonPrimitive
import fr.xgouchet.elmyr.Forge
import fr.xgouchet.elmyr.junit5.ForgeConfiguration
import fr.xgouchet.elmyr.junit5.ForgeExtension
import fr.xgouchet.elmyr.jvm.ext.aTimestamp
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand All @@ -34,7 +35,7 @@ import org.mockito.quality.Strictness
)
@MockitoSettings(strictness = Strictness.LENIENT)
@ForgeConfiguration(ForgeConfigurator::class)
internal class BatchesToSegmentMapperTest {
internal class BatchesToSegmentsMapperTest {

@Mock
lateinit var mockInternalLogger: InternalLogger
Expand Down Expand Up @@ -87,6 +88,48 @@ internal class BatchesToSegmentMapperTest {
assertThat(serializedSegment).isEqualTo(expectedSerializedSegment)
}

@Test
fun `M keep the same order W map { records have same timestamps }`(forge: Forge) {
// Given
val rumContext: SessionReplayRumContext = forge.getForgery()
val fakeRecordsSize = forge.anInt(min = 10, max = 20)
val fakeTimestamp = forge.aTimestamp()
val fakeRecords: List<MobileSegment.MobileRecord> = forge.aList(fakeRecordsSize) {
forge.getForgery<MobileSegment.MobileRecord>().copy(fakeTimestamp)
}
val fakeEnrichedRecords = fakeRecords
.chunked(forge.anInt(min = 1, max = fakeRecordsSize))
.map {
EnrichedRecord(
rumContext.applicationId,
rumContext.sessionId,
rumContext.sessionId,
it
)
}
val fakeExpectedEnrichedRecord = EnrichedRecord(
rumContext.applicationId,
rumContext.sessionId,
rumContext.sessionId,
fakeRecords
)

val expectedEmptySegment =
fakeExpectedEnrichedRecord.toSegment().copy(records = emptyList())
val expectedSerializedSegment = fakeExpectedEnrichedRecord.toSegment()
.toJson().asJsonObject.toString()
val fakeBatchData = fakeEnrichedRecords.map { it.toJson().toByteArray() }

// When
val mappedSegment = testedMapper.map(fakeBatchData)

// Then
val segment = mappedSegment?.first
val serializedSegment = mappedSegment?.second.toString()
assertThat(segment).isEqualTo(expectedEmptySegment)
assertThat(serializedSegment).isEqualTo(expectedSerializedSegment)
}

@Test
fun `M use the first record in the list as start timestamp W map`(
forge: Forge
Expand Down Expand Up @@ -657,5 +700,18 @@ internal class BatchesToSegmentMapperTest {
}
}

private fun MobileSegment.MobileRecord.copy(timestamp: Long): MobileSegment.MobileRecord {
return when (this) {
is MobileSegment.MobileRecord.MetaRecord -> this.copy(timestamp = timestamp)
is MobileSegment.MobileRecord.FocusRecord -> this.copy(timestamp = timestamp)
is MobileSegment.MobileRecord.ViewEndRecord -> this.copy(timestamp = timestamp)
is MobileSegment.MobileRecord.MobileFullSnapshotRecord ->
this.copy(timestamp = timestamp)
is MobileSegment.MobileRecord.MobileIncrementalSnapshotRecord ->
this.copy(timestamp = timestamp)
is MobileSegment.MobileRecord.VisualViewportRecord -> this.copy(timestamp = timestamp)
}
}

// endregion
}