-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1757 from embrace-io/disable-sdk
Add ability to disable data export
- Loading branch information
Showing
16 changed files
with
236 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...tionTest/kotlin/io/embrace/android/embracesdk/testcases/features/DisableSdkFeatureTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package io.embrace.android.embracesdk.testcases.features | ||
|
||
import android.content.Context | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import io.embrace.android.embracesdk.assertions.returnIfConditionMet | ||
import io.embrace.android.embracesdk.fakes.FakeEmbLogger | ||
import io.embrace.android.embracesdk.internal.delivery.storage.StorageLocation | ||
import io.embrace.android.embracesdk.testframework.IntegrationTestRule | ||
import java.io.File | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
internal class DisableSdkFeatureTest { | ||
|
||
private companion object { | ||
private const val TEST_PREFIX = "emb_test_" | ||
private const val SPAN_1 = "${TEST_PREFIX}1" | ||
private const val SPAN_2 = "${TEST_PREFIX}2" | ||
private const val SPAN_3 = "${TEST_PREFIX}3" | ||
private const val LOG_1 = "${TEST_PREFIX}1" | ||
private const val LOG_2 = "${TEST_PREFIX}2" | ||
private const val LOG_3 = "${TEST_PREFIX}3" | ||
private const val TEST_FILE_NAME = "test_file" | ||
private const val DUMMY_CONTENT = "Hello, world!" | ||
} | ||
|
||
@Rule | ||
@JvmField | ||
val testRule: IntegrationTestRule = IntegrationTestRule() | ||
|
||
private lateinit var embraceDirs: List<File> | ||
|
||
@Before | ||
fun setUp() { | ||
val ctx = ApplicationProvider.getApplicationContext<Context>() | ||
embraceDirs = StorageLocation.values().map { it.asFile(ctx, FakeEmbLogger()).value } | ||
} | ||
|
||
@Test | ||
fun `disabling SDK stops data export`() { | ||
testRule.runTest( | ||
setupAction = { | ||
// create some dummy values in embrace directories to see if they get deleted | ||
embraceDirs.forEach { | ||
File(it, TEST_FILE_NAME).writeText(DUMMY_CONTENT) | ||
} | ||
}, | ||
testCaseAction = { | ||
embraceDirs.forEach { | ||
assertEquals(DUMMY_CONTENT, File(it, TEST_FILE_NAME).readText()) | ||
} | ||
recordSession { | ||
embrace.startSpan(SPAN_1)?.stop() | ||
embrace.logInfo(LOG_1) | ||
embrace.startSpan(SPAN_2)?.stop() | ||
embrace.logInfo(LOG_2) | ||
|
||
// disable SDK at this point | ||
embrace.disable() | ||
|
||
// log some more data | ||
embrace.startSpan(SPAN_3)?.stop() | ||
embrace.logInfo(LOG_3) | ||
} | ||
}, | ||
assertAction = { | ||
// ensure that the files were deleted by waiting for the background thread | ||
returnIfConditionMet( | ||
desiredValueSupplier = { true }, | ||
dataProvider = { | ||
embraceDirs.all { | ||
!File(it, TEST_FILE_NAME).exists() | ||
} | ||
}, | ||
condition = { true }, | ||
) | ||
|
||
assertEquals(0, getLogEnvelopes(0).size) | ||
assertEquals(0, getSessionEnvelopes(0).size) | ||
}, | ||
otelExportAssertion = { | ||
val spanData = awaitSpans(2) { spanData -> | ||
spanData.name.startsWith(TEST_PREFIX) | ||
} | ||
val spans = spanData.map { it.name } | ||
assertEquals(listOf(SPAN_1, SPAN_2), spans) | ||
|
||
val logData = awaitLogs(2) { logData -> | ||
val msg = logData.bodyValue?.value.toString() | ||
msg.startsWith(TEST_PREFIX) | ||
} | ||
assertEquals(listOf(LOG_1, LOG_2), logData.map { it.bodyValue?.value }) | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...tionTest/kotlin/io/embrace/android/embracesdk/testframework/export/FilteredLogExporter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.embrace.android.embracesdk.testframework.export | ||
|
||
import io.embrace.android.embracesdk.assertions.returnIfConditionMet | ||
import io.opentelemetry.sdk.common.CompletableResultCode | ||
import io.opentelemetry.sdk.logs.data.LogRecordData | ||
import io.opentelemetry.sdk.logs.export.LogRecordExporter | ||
|
||
internal class FilteredLogExporter: LogRecordExporter { | ||
|
||
private val logData = mutableListOf<LogRecordData>() | ||
|
||
override fun export(logs: MutableCollection<LogRecordData>): CompletableResultCode { | ||
logData.addAll(logs) | ||
return CompletableResultCode.ofSuccess() | ||
} | ||
|
||
override fun flush(): CompletableResultCode { | ||
return CompletableResultCode.ofSuccess() | ||
} | ||
|
||
override fun shutdown(): CompletableResultCode { | ||
return CompletableResultCode.ofSuccess() | ||
} | ||
|
||
fun awaitLogs(expectedCount: Int, filter: (LogRecordData) -> Boolean): List<LogRecordData> { | ||
val supplier = { logData.filter(filter) } | ||
return returnIfConditionMet( | ||
desiredValueSupplier = supplier, | ||
dataProvider = supplier, | ||
condition = { data -> | ||
data.size == expectedCount | ||
}, | ||
errorMessageSupplier = { | ||
"Timeout. Expected $expectedCount logs, but got ${supplier().size}." | ||
} | ||
) | ||
} | ||
} |
Oops, something went wrong.