Skip to content

Commit 12704a9

Browse files
authored
Open Improve ui and unit tests (#2992)
removed ui test EnvelopeTests.checkProfileNotSentIfEmpty. Added unit test with same goal: SentryEnvelopeItemTest.fromProfilingTrace with empty file throws added description messages to eventually failing benchmark tests added small sleeps in SentryWrapperTest to avoid race conditions
1 parent 0bf143e commit 12704a9

File tree

6 files changed

+46
-64
lines changed

6 files changed

+46
-64
lines changed

sentry-android-integration-tests/sentry-uitest-android-benchmark/src/androidTest/java/io/sentry/uitest/android/benchmark/SdkBenchmarkTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class SdkBenchmarkTest : BaseBenchmarkTest() {
4040
val perfProfilingSdkResult = perfProfilingSdkResults.getSummaryResult()
4141
perfProfilingSdkResult.printResults()
4242

43-
assertTrue(simpleSdkResult.cpuTimeIncreaseNanos in 0..TimeUnit.MILLISECONDS.toNanos(100))
44-
assertTrue(perfProfilingSdkResult.cpuTimeIncreaseNanos in 0..TimeUnit.MILLISECONDS.toNanos(100))
43+
assertTrue(simpleSdkResult.cpuTimeIncreaseNanos in 0..TimeUnit.MILLISECONDS.toNanos(100), "Expected ${simpleSdkResult.cpuTimeIncreaseNanos} to be in range 0 < x < 100000000")
44+
assertTrue(perfProfilingSdkResult.cpuTimeIncreaseNanos in 0..TimeUnit.MILLISECONDS.toNanos(100), "Expected ${perfProfilingSdkResult.cpuTimeIncreaseNanos} to be in range 0 < x < 100000000")
4545
}
4646

4747
private fun getOperation(init: (() -> Unit)? = null) = BenchmarkOperation(

sentry-android-integration-tests/sentry-uitest-android-benchmark/src/androidTest/java/io/sentry/uitest/android/benchmark/SentryBenchmarkTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class SentryBenchmarkTest : BaseBenchmarkTest() {
4646
comparisonResult.printResults()
4747

4848
// Currently we just want to assert the cpu overhead
49-
assertTrue(comparisonResult.cpuTimeIncreasePercentage in -2F..2F)
49+
assertTrue(comparisonResult.cpuTimeIncreasePercentage in -2F..2F, "Expected ${comparisonResult.cpuTimeIncreasePercentage} to be in range -2 < x < 2")
5050
// The fps decrease comparison is skipped, due to approximation: 59.51 and 59.49 fps are considered 60 and 59,
5151
// respectively. Also, if the average fps is 20 or 60, a difference of 1 fps becomes 5% or 1.66% respectively.
5252
}
@@ -90,7 +90,7 @@ class SentryBenchmarkTest : BaseBenchmarkTest() {
9090
comparisonResult.printResults()
9191

9292
// Currently we just want to assert the cpu overhead
93-
assertTrue(comparisonResult.cpuTimeIncreasePercentage in 0F..5F)
93+
assertTrue(comparisonResult.cpuTimeIncreasePercentage in 0F..5F, "Expected ${comparisonResult.cpuTimeIncreasePercentage} to be in range 0 < x < 5")
9494
}
9595

9696
/**

sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/AutomaticSpansTest.kt

+15-19
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,47 @@ import androidx.test.core.app.launchActivity
55
import androidx.test.ext.junit.runners.AndroidJUnit4
66
import io.sentry.Sentry
77
import io.sentry.SentryLevel
8-
import io.sentry.SentryOptions
98
import io.sentry.android.core.SentryAndroidOptions
109
import io.sentry.protocol.SentryTransaction
1110
import org.junit.runner.RunWith
1211
import kotlin.test.Test
13-
import kotlin.test.assertEquals
1412
import kotlin.test.assertTrue
1513

1614
@RunWith(AndroidJUnit4::class)
1715
class AutomaticSpansTest : BaseUiTest() {
1816

1917
@Test
2018
fun ttidTtfdSpans() {
21-
val transactions = mutableListOf<SentryTransaction>()
22-
23-
initSentry(false) { options: SentryAndroidOptions ->
19+
initSentry(true) { options: SentryAndroidOptions ->
2420
options.isDebug = true
2521
options.setDiagnosticLevel(SentryLevel.DEBUG)
2622
options.tracesSampleRate = 1.0
2723
options.profilesSampleRate = 1.0
2824
options.isEnableAutoActivityLifecycleTracing = true
2925
options.isEnableTimeToFullDisplayTracing = true
30-
options.beforeSendTransaction =
31-
SentryOptions.BeforeSendTransactionCallback { transaction, _ ->
32-
transactions.add(transaction)
33-
transaction
34-
}
3526
}
3627

28+
relayIdlingResource.increment()
3729
val activity = launchActivity<ComposeActivity>()
3830
activity.moveToState(Lifecycle.State.RESUMED)
3931
activity.onActivity {
4032
Sentry.reportFullyDisplayed()
4133
}
4234
activity.moveToState(Lifecycle.State.DESTROYED)
4335

44-
assertEquals(1, transactions.size)
45-
assertTrue("TTID span missing") {
46-
transactions.first().spans.any {
47-
it.op == "ui.load.initial_display"
48-
}
49-
}
50-
assertTrue("TTFD span missing") {
51-
transactions.first().spans.any {
52-
it.op == "ui.load.full_display"
36+
relay.assert {
37+
assertFirstEnvelope {
38+
val transactionItem: SentryTransaction = it.assertItem()
39+
assertTrue("TTID span missing") {
40+
transactionItem.spans.any { span ->
41+
span.op == "ui.load.initial_display"
42+
}
43+
}
44+
assertTrue("TTFD span missing") {
45+
transactionItem.spans.any { span ->
46+
span.op == "ui.load.full_display"
47+
}
48+
}
5349
}
5450
}
5551
}

sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/EnvelopeTests.kt

+1-40
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import io.sentry.profilemeasurements.ProfileMeasurement
1717
import io.sentry.protocol.SentryTransaction
1818
import org.junit.Assume.assumeNotNull
1919
import org.junit.runner.RunWith
20-
import java.io.File
2120
import java.util.concurrent.TimeUnit
2221
import kotlin.test.Test
2322
import kotlin.test.assertEquals
@@ -171,44 +170,6 @@ class EnvelopeTests : BaseUiTest() {
171170
}
172171
}
173172

174-
@Test
175-
fun checkProfileNotSentIfEmpty() {
176-
initSentry(true) { options: SentryAndroidOptions ->
177-
options.tracesSampleRate = 1.0
178-
options.profilesSampleRate = 1.0
179-
}
180-
relayIdlingResource.increment()
181-
val profilesDirPath = Sentry.getCurrentHub().options.profilingTracesDirPath
182-
val transaction = Sentry.startTransaction("emptyProfileTransaction", "test empty")
183-
184-
var finished = false
185-
Thread {
186-
while (!finished) {
187-
// Let's modify the trace file to be empty, so that the profile will actually be empty.
188-
val origProfileFile = File(profilesDirPath!!).listFiles()?.maxByOrNull { f -> f.lastModified() }
189-
origProfileFile?.writeBytes(ByteArray(0))
190-
}
191-
}.start()
192-
transaction.finish()
193-
// The profiler is stopped in background on the executor service, so we can stop deleting the trace file
194-
// only after the profiler is stopped. This means we have to stop the deletion in the executorService
195-
Sentry.getCurrentHub().options.executorService.submit {
196-
finished = true
197-
}
198-
199-
relay.assert {
200-
findEnvelope {
201-
assertEnvelopeItem<SentryTransaction>(it.items.toList()).transaction == "emptyProfileTransaction"
202-
}.assert {
203-
val transactionItem: SentryTransaction = it.assertItem()
204-
it.assertNoOtherItems()
205-
assertEquals("emptyProfileTransaction", transactionItem.transaction)
206-
}
207-
assertNoOtherEnvelopes()
208-
assertNoOtherRequests()
209-
}
210-
}
211-
212173
@Test
213174
fun checkTimedOutProfile() {
214175
// We increase the IdlingResources timeout to exceed the profiling timeout
@@ -235,7 +196,7 @@ class EnvelopeTests : BaseUiTest() {
235196
assertEquals("timedOutProfile", transactionItem.transaction)
236197
assertEquals("timedOutProfile", profilingTraceData.transactionName)
237198
// The profile should timeout after 30 seconds
238-
assertTrue(profilingTraceData.durationNs.toLong() < TimeUnit.SECONDS.toNanos(31))
199+
assertTrue(profilingTraceData.durationNs.toLong() < TimeUnit.SECONDS.toNanos(31), "Profile duration expected to be less than 31 seconds. It was ${profilingTraceData.durationNs.toLong()} ns")
239200
assertEquals(ProfilingTraceData.TRUNCATION_REASON_TIMEOUT, profilingTraceData.truncationReason)
240201
}
241202
assertNoOtherEnvelopes()

sentry/src/test/java/io/sentry/SentryEnvelopeItemTest.kt

+22-1
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,35 @@ class SentryEnvelopeItemTest {
307307
assertFailsWith<SentryEnvelopeException>("Dropping profiling trace data, because the file ${file.path} doesn't exists") {
308308
SentryEnvelopeItem.fromProfilingTrace(profilingTraceData, fixture.maxAttachmentSize, mock()).data
309309
}
310+
}
311+
312+
@Test
313+
fun `fromProfilingTrace with unreadable file throws`() {
314+
val file = File(fixture.pathname)
315+
val profilingTraceData = mock<ProfilingTraceData> {
316+
whenever(it.traceFile).thenReturn(file)
317+
}
310318
file.writeBytes(fixture.bytes)
311-
assertNotNull(SentryEnvelopeItem.fromProfilingTrace(profilingTraceData, fixture.maxAttachmentSize, mock()).data)
312319
file.setReadable(false)
313320
assertFailsWith<SentryEnvelopeException>("Dropping profiling trace data, because the file ${file.path} doesn't exists") {
314321
SentryEnvelopeItem.fromProfilingTrace(profilingTraceData, fixture.maxAttachmentSize, mock()).data
315322
}
316323
}
317324

325+
@Test
326+
fun `fromProfilingTrace with empty file throws`() {
327+
val file = File(fixture.pathname)
328+
file.writeBytes(ByteArray(0))
329+
val profilingTraceData = mock<ProfilingTraceData> {
330+
whenever(it.traceFile).thenReturn(file)
331+
}
332+
333+
val traceData = SentryEnvelopeItem.fromProfilingTrace(profilingTraceData, fixture.maxAttachmentSize, mock())
334+
assertFailsWith<SentryEnvelopeException>("Profiling trace file is empty") {
335+
traceData.data
336+
}
337+
}
338+
318339
@Test
319340
fun `fromProfilingTrace with file too big`() {
320341
val file = File(fixture.pathname)

sentry/src/test/java/io/sentry/SentryWrapperTest.kt

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class SentryWrapperTest {
3737
val callableFuture =
3838
CompletableFuture.supplyAsync(
3939
SentryWrapper.wrapSupplier {
40+
Thread.sleep(20)
4041
Sentry.addBreadcrumb("MyClonedBreadcrumb")
4142
Sentry.captureMessage("ClonedMessage")
4243
"Result 1"
@@ -47,6 +48,7 @@ class SentryWrapperTest {
4748
val callableFuture2 =
4849
CompletableFuture.supplyAsync(
4950
SentryWrapper.wrapSupplier {
51+
Thread.sleep(10)
5052
Sentry.addBreadcrumb("MyClonedBreadcrumb2")
5153
Sentry.captureMessage("ClonedMessage2")
5254
"Result 2"
@@ -87,6 +89,7 @@ class SentryWrapperTest {
8789

8890
val future1 = executor.submit(
8991
SentryWrapper.wrapCallable {
92+
Thread.sleep(20)
9093
Sentry.addBreadcrumb("MyClonedBreadcrumb")
9194
Sentry.captureMessage("ClonedMessage")
9295
"Result 1"
@@ -95,6 +98,7 @@ class SentryWrapperTest {
9598

9699
val future2 = executor.submit(
97100
SentryWrapper.wrapCallable {
101+
Thread.sleep(10)
98102
Sentry.addBreadcrumb("MyClonedBreadcrumb2")
99103
Sentry.captureMessage("ClonedMessage2")
100104
"Result 2"

0 commit comments

Comments
 (0)