-
-
Notifications
You must be signed in to change notification settings - Fork 475
tests for Android profiling #1949
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
907bdf0
9a30566
afda1be
de74c6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package io.sentry | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertNull | ||
|
|
||
| class NoOpTransactionProfilerTest { | ||
| private var profiler: NoOpTransactionProfiler = NoOpTransactionProfiler.getInstance() | ||
|
|
||
| @Test | ||
| fun `onTransactionStart is no op`() = profiler.onTransactionStart(NoOpTransaction.getInstance()) | ||
|
marandaneto marked this conversation as resolved.
Outdated
|
||
|
|
||
| @Test | ||
| fun `onTransactionFinish returns null`() = | ||
| assertNull(profiler.onTransactionFinish(NoOpTransaction.getInstance())) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package io.sentry.util | ||
|
|
||
| import org.junit.Test | ||
|
stefanosiano marked this conversation as resolved.
Outdated
|
||
| import java.io.File | ||
| import java.nio.file.Files | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertNull | ||
|
|
||
| class FileUtilsTest { | ||
|
stefanosiano marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| fun `deleteRecursively returns true on non-existing file or null`() { | ||
| assert(FileUtils.deleteRecursively(null)) | ||
| assert(FileUtils.deleteRecursively(File(""))) | ||
|
stefanosiano marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| @Test | ||
| fun `deleteRecursively deletes a simple file`() { | ||
| val f = Files.createTempFile("here", "test").toFile() | ||
| assert(f.exists()) | ||
| assert(FileUtils.deleteRecursively(f)) | ||
| assertFalse(f.exists()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `deleteRecursively deletes a folder`() { | ||
| val d = Files.createTempDirectory("here").toFile() | ||
| val f = File(d, "test") | ||
| val d2 = File(d, "dir2") | ||
| val f2 = File(d2, "test") | ||
|
stefanosiano marked this conversation as resolved.
Outdated
|
||
| f.createNewFile() | ||
| d2.mkdir() | ||
| f2.createNewFile() | ||
| assert(d.exists() && d.isDirectory && f.exists() && d2.exists() && d2.isDirectory) | ||
| assert(f2.exists()) | ||
| assert(FileUtils.deleteRecursively(d)) | ||
| assertFalse(f.exists() || d.exists() || f2.exists() || d2.exists()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `readText returns null on null, non existing or unreadable file`() { | ||
| val f = File("here", "test") | ||
| val unreadableFile = Files.createTempFile("here", "test").toFile() | ||
| unreadableFile.setReadable(false) | ||
| assertNull(FileUtils.readText(null)) | ||
| assertNull(FileUtils.readText(f)) | ||
| assertNull(FileUtils.readText(unreadableFile)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `readText returns the content of a file`() { | ||
| val f = Files.createTempFile("here", "test").toFile() | ||
| val text = "Lorem ipsum dolor sit amet\nLorem ipsum dolor sit amet" | ||
| f.writeText(text) | ||
| assertEquals(text, FileUtils.readText(f)) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,8 @@ | |
| import java.util.concurrent.Future; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.jetbrains.annotations.TestOnly; | ||
| import org.jetbrains.annotations.VisibleForTesting; | ||
|
|
||
| final class AndroidTransactionProfiler implements ITransactionProfiler { | ||
|
|
||
|
|
@@ -41,7 +43,7 @@ final class AndroidTransactionProfiler implements ITransactionProfiler { | |
| private @Nullable File traceFile = null; | ||
| private @Nullable File traceFilesDir = null; | ||
| private @Nullable Future<?> scheduledFinish = null; | ||
| private volatile @Nullable ITransaction activeTransaction = null; | ||
| @VisibleForTesting volatile @Nullable ITransaction activeTransaction = null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we test the class without
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoiding it would make it harder/less readable to test the code without increasing the visibility of the field.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is that we could not use You can configure AS to warn in such cases well. You can also test the behavior of the method without using Usually |
||
| private volatile @Nullable ProfilingTraceData timedOutProfilingData = null; | ||
| private final @NotNull Context context; | ||
| private final @NotNull SentryAndroidOptions options; | ||
|
|
@@ -59,6 +61,10 @@ public AndroidTransactionProfiler( | |
| Objects.requireNonNull(buildInfoProvider, "The BuildInfoProvider is required."); | ||
| this.packageInfo = ContextUtils.getPackageInfo(context, options.getLogger()); | ||
| final String tracesFilesDirPath = options.getProfilingTracesDirPath(); | ||
| if (!options.isProfilingEnabled()) { | ||
| options.getLogger().log(SentryLevel.INFO, "Profiling is disabled in options."); | ||
| return; | ||
| } | ||
| if (tracesFilesDirPath == null || tracesFilesDirPath.isEmpty()) { | ||
| options | ||
| .getLogger() | ||
|
|
@@ -90,7 +96,7 @@ public synchronized void onTransactionStart(@NotNull ITransaction transaction) { | |
|
|
||
| // traceFilesDir is null or intervalUs is 0 only if there was a problem in the constructor, but | ||
| // we already logged that | ||
| if (traceFilesDir == null || intervalUs == 0) { | ||
| if (traceFilesDir == null || intervalUs == 0 || !traceFilesDir.exists()) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -225,7 +231,7 @@ public synchronized void onTransactionStart(@NotNull ITransaction transaction) { | |
| buildInfoProvider.getModel(), | ||
| buildInfoProvider.getVersionRelease(), | ||
| buildInfoProvider.isEmulator(), | ||
| CpuInfoUtils.readMaxFrequencies(), | ||
| CpuInfoUtils.getInstance().readMaxFrequencies(), | ||
| totalMem, | ||
| options.getProguardUuid(), | ||
| versionName, | ||
|
|
@@ -253,4 +259,9 @@ public synchronized void onTransactionStart(@NotNull ITransaction transaction) { | |
| return null; | ||
| } | ||
| } | ||
|
|
||
| @TestOnly | ||
| void setTimedOutProfilingData(@Nullable ProfilingTraceData data) { | ||
| this.timedOutProfilingData = data; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.