|
| 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.sdk.integration.rum |
| 8 | + |
| 9 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 10 | +import androidx.test.filters.LargeTest |
| 11 | +import androidx.test.platform.app.InstrumentationRegistry |
| 12 | +import com.datadog.android.privacy.TrackingConsent |
| 13 | +import com.datadog.android.rum.assertj.TimeseriesCpuEventAssert |
| 14 | +import com.datadog.android.rum.assertj.TimeseriesMemoryEventAssert |
| 15 | +import com.datadog.android.rum.model.TimeseriesCpuEvent |
| 16 | +import com.datadog.android.rum.model.TimeseriesMemoryEvent |
| 17 | +import com.datadog.android.sdk.integration.RuntimeConfig |
| 18 | +import com.datadog.android.sdk.integration.rum.TimeseriesTrackingPlaygroundActivity.Companion.SAMPLE_INTERVAL_MS |
| 19 | +import com.datadog.android.sdk.rules.HandledRequest |
| 20 | +import com.datadog.android.sdk.rules.RumMockServerActivityTestRule |
| 21 | +import com.datadog.tools.unit.ConditionWatcher |
| 22 | +import com.google.gson.JsonObject |
| 23 | +import org.assertj.core.api.Assertions.assertThat |
| 24 | +import org.junit.Assert.assertEquals |
| 25 | +import org.junit.Assert.assertTrue |
| 26 | +import org.junit.Rule |
| 27 | +import org.junit.Test |
| 28 | +import org.junit.runner.RunWith |
| 29 | + |
| 30 | +@RunWith(AndroidJUnit4::class) |
| 31 | +@LargeTest |
| 32 | +internal class TimeseriesCollectionTest { |
| 33 | + |
| 34 | + @get:Rule |
| 35 | + val mockServerRule = RumMockServerActivityTestRule( |
| 36 | + TimeseriesTrackingPlaygroundActivity::class.java, |
| 37 | + keepRequests = true, |
| 38 | + trackingConsent = TrackingConsent.GRANTED |
| 39 | + ) |
| 40 | + |
| 41 | + @Test |
| 42 | + fun verifyCpuAndMemoryTimeseriesAreCollected() { |
| 43 | + // Given |
| 44 | + Thread.sleep(FOREGROUND_COLLECTION_DURATION_MS) |
| 45 | + |
| 46 | + // When |
| 47 | + moveActivityToBackground() |
| 48 | + val events: List<JsonObject> = waitForTimeseriesEvents(expectedCount = TIMESERIES_PIPELINE_COUNT) |
| 49 | + |
| 50 | + // Then |
| 51 | + assertTrue(events.size >= TIMESERIES_PIPELINE_COUNT) |
| 52 | + |
| 53 | + TimeseriesCpuEventAssert.assertThat(events.cpuEvents().single()) |
| 54 | + .hasDataPointsCount(EXPECTED_FOREGROUND_CPU_DATA_POINTS) |
| 55 | + |
| 56 | + TimeseriesMemoryEventAssert.assertThat(events.memoryEvents().single()) |
| 57 | + .hasDataPointsCount(EXPECTED_FOREGROUND_MEMORY_DATA_POINTS) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun verifyCollectionFlushesPausesAndResumesAcrossBackgroundTransition() { |
| 62 | + // Given |
| 63 | + Thread.sleep(FOREGROUND_COLLECTION_DURATION_MS) |
| 64 | + |
| 65 | + // When |
| 66 | + moveActivityToBackground() |
| 67 | + val firstForegroundEvents = waitForTimeseriesEvents(expectedCount = TIMESERIES_PIPELINE_COUNT) |
| 68 | + Thread.sleep(BACKGROUND_OBSERVATION_DURATION_MS) |
| 69 | + val backgroundEvents = timeseriesEvents() |
| 70 | + moveActivityToForeground() |
| 71 | + Thread.sleep(FOREGROUND_COLLECTION_DURATION_MS) |
| 72 | + moveActivityToBackground() |
| 73 | + val secondForegroundEvents = waitForTimeseriesEvents(expectedCount = TIMESERIES_PIPELINE_COUNT * 2) |
| 74 | + |
| 75 | + // Then |
| 76 | + assertEquals(TIMESERIES_PIPELINE_COUNT, firstForegroundEvents.size) |
| 77 | + TimeseriesCpuEventAssert.assertThat(firstForegroundEvents.cpuEvents().single()) |
| 78 | + .hasDataPointsCount(EXPECTED_FOREGROUND_CPU_DATA_POINTS) |
| 79 | + TimeseriesMemoryEventAssert.assertThat(firstForegroundEvents.memoryEvents().single()) |
| 80 | + .hasDataPointsCount(EXPECTED_FOREGROUND_MEMORY_DATA_POINTS) |
| 81 | + assertThat(firstForegroundEvents.size).isEqualTo(backgroundEvents.size) |
| 82 | + assertThat(secondForegroundEvents.size).isEqualTo(TIMESERIES_PIPELINE_COUNT * 2) |
| 83 | + secondForegroundEvents.memoryEvents().forEach { |
| 84 | + TimeseriesMemoryEventAssert.assertThat(it) |
| 85 | + .hasDataPointsCount(EXPECTED_FOREGROUND_MEMORY_DATA_POINTS) |
| 86 | + } |
| 87 | + secondForegroundEvents.cpuEvents().forEach { |
| 88 | + TimeseriesCpuEventAssert.assertThat(it) |
| 89 | + .hasDataPointsCount(EXPECTED_FOREGROUND_CPU_DATA_POINTS) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private fun moveActivityToBackground() { |
| 94 | + val instrumentation = InstrumentationRegistry.getInstrumentation() |
| 95 | + instrumentation.runOnMainSync { |
| 96 | + instrumentation.callActivityOnPause(mockServerRule.activity) |
| 97 | + instrumentation.callActivityOnStop(mockServerRule.activity) |
| 98 | + } |
| 99 | + instrumentation.waitForIdleSync() |
| 100 | + } |
| 101 | + |
| 102 | + private fun moveActivityToForeground() { |
| 103 | + val instrumentation = InstrumentationRegistry.getInstrumentation() |
| 104 | + instrumentation.runOnMainSync { |
| 105 | + instrumentation.callActivityOnStart(mockServerRule.activity) |
| 106 | + instrumentation.callActivityOnResume(mockServerRule.activity) |
| 107 | + } |
| 108 | + instrumentation.waitForIdleSync() |
| 109 | + } |
| 110 | + |
| 111 | + private fun waitForTimeseriesEvents(expectedCount: Int): List<JsonObject> { |
| 112 | + var events = emptyList<JsonObject>() |
| 113 | + ConditionWatcher { |
| 114 | + events = timeseriesEvents() |
| 115 | + assertTrue(events.size >= expectedCount) |
| 116 | + true |
| 117 | + }.doWait(timeoutMs = RumTest.FINAL_WAIT_MS) |
| 118 | + return events |
| 119 | + } |
| 120 | + |
| 121 | + private fun timeseriesEvents(): List<JsonObject> = mockServerRule |
| 122 | + .getRequests(RuntimeConfig.rumEndpointUrl) |
| 123 | + .rumEvents() |
| 124 | + .filter { it.get(TYPE_KEY)?.asString == TIMESERIES_TYPE } |
| 125 | + |
| 126 | + private fun List<HandledRequest>.rumEvents(): List<JsonObject> = flatMap { request -> |
| 127 | + request.textBody?.let(::rumPayloadToJsonList).orEmpty() |
| 128 | + } |
| 129 | + |
| 130 | + companion object { |
| 131 | + private const val CPU_TIMESERIES_NAME = "cpu" |
| 132 | + private const val MEMORY_TIMESERIES_NAME = "memory" |
| 133 | + private const val TIMESERIES_TYPE = "timeseries" |
| 134 | + private const val TYPE_KEY = "type" |
| 135 | + private const val TIMESERIES_KEY = "timeseries" |
| 136 | + private const val NAME_KEY = "name" |
| 137 | + |
| 138 | + // One independent pipeline for each timeseries source: CPU and memory. |
| 139 | + private const val TIMESERIES_PIPELINE_COUNT = 2 |
| 140 | + private const val ACTIVITY_STOP_DELAY_MS = 200L |
| 141 | + private const val FOREGROUND_COLLECTION_DURATION_MS = SAMPLE_INTERVAL_MS * 2 + SAMPLE_INTERVAL_MS / 4 |
| 142 | + private const val UPLOAD_GRACE_PERIOD_MS = 500L |
| 143 | + private const val BACKGROUND_OBSERVATION_DURATION_MS = |
| 144 | + SAMPLE_INTERVAL_MS + UPLOAD_GRACE_PERIOD_MS |
| 145 | + |
| 146 | + private const val EXPECTED_FOREGROUND_MEMORY_DATA_POINTS = |
| 147 | + ((FOREGROUND_COLLECTION_DURATION_MS + ACTIVITY_STOP_DELAY_MS) / SAMPLE_INTERVAL_MS).toInt() |
| 148 | + private const val EXPECTED_FOREGROUND_CPU_DATA_POINTS = |
| 149 | + EXPECTED_FOREGROUND_MEMORY_DATA_POINTS - 1 |
| 150 | + |
| 151 | + private val JsonObject.timeseriesName: String? |
| 152 | + get() = getAsJsonObject(TIMESERIES_KEY).get(NAME_KEY).asString |
| 153 | + |
| 154 | + private fun List<JsonObject>.filterByName(name: String): List<JsonObject> = filter { it.timeseriesName == name } |
| 155 | + |
| 156 | + private fun List<JsonObject>.cpuEvents(): List<TimeseriesCpuEvent> = |
| 157 | + filterByName(CPU_TIMESERIES_NAME).map(TimeseriesCpuEvent::fromJsonObject) |
| 158 | + |
| 159 | + private fun List<JsonObject>.memoryEvents(): List<TimeseriesMemoryEvent> = |
| 160 | + filterByName(MEMORY_TIMESERIES_NAME).map(TimeseriesMemoryEvent::fromJsonObject) |
| 161 | + } |
| 162 | +} |
0 commit comments