Skip to content

Commit 3c3068c

Browse files
committed
Add test for SentryFileOutputStream
1 parent 0e7b432 commit 3c3068c

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4146,6 +4146,7 @@ public final class io/sentry/instrumentation/file/SentryFileOutputStream : java/
41464146
public final class io/sentry/instrumentation/file/SentryFileOutputStream$Factory {
41474147
public fun <init> ()V
41484148
public static fun create (Ljava/io/FileOutputStream;Ljava/io/File;)Ljava/io/FileOutputStream;
4149+
public static fun create (Ljava/io/FileOutputStream;Ljava/io/File;Lio/sentry/IScopes;)Ljava/io/FileOutputStream;
41494150
public static fun create (Ljava/io/FileOutputStream;Ljava/io/File;Z)Ljava/io/FileOutputStream;
41504151
public static fun create (Ljava/io/FileOutputStream;Ljava/io/FileDescriptor;)Ljava/io/FileOutputStream;
41514152
public static fun create (Ljava/io/FileOutputStream;Ljava/lang/String;)Ljava/io/FileOutputStream;

sentry/src/main/java/io/sentry/instrumentation/file/SentryFileOutputStream.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ public static FileOutputStream create(
178178
: delegate;
179179
}
180180

181+
public static FileOutputStream create(
182+
final @NotNull FileOutputStream delegate,
183+
final @Nullable File file,
184+
final @NotNull IScopes scopes)
185+
throws FileNotFoundException {
186+
return isTracingEnabled(scopes)
187+
? new SentryFileOutputStream(init(file, false, delegate, scopes))
188+
: delegate;
189+
}
190+
181191
private static boolean isTracingEnabled(final @NotNull IScopes scopes) {
182192
final @NotNull SentryOptions options = scopes.getOptions();
183193
return options.isTracingEnabled();

sentry/src/test/java/io/sentry/instrumentation/file/SentryFileOutputStreamTest.kt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import org.junit.rules.TemporaryFolder
1414
import org.mockito.kotlin.mock
1515
import org.mockito.kotlin.whenever
1616
import java.io.File
17+
import java.io.FileOutputStream
18+
import java.io.IOException
1719
import java.util.concurrent.atomic.AtomicBoolean
1820
import kotlin.concurrent.thread
1921
import kotlin.test.Test
@@ -26,6 +28,7 @@ import kotlin.test.assertTrue
2628
class SentryFileOutputStreamTest {
2729
class Fixture {
2830
val scopes = mock<IScopes>()
31+
val options = SentryOptions()
2932
lateinit var sentryTracer: SentryTracer
3033

3134
internal fun getSut(
@@ -34,7 +37,7 @@ class SentryFileOutputStreamTest {
3437
append: Boolean = false,
3538
optionsConfiguration: (SentryOptions) -> Unit = {}
3639
): SentryFileOutputStream {
37-
val options = SentryOptions().apply {
40+
options.run {
3841
threadChecker = ThreadChecker.getInstance()
3942
addInAppInclude("org.junit")
4043
optionsConfiguration(this)
@@ -46,6 +49,22 @@ class SentryFileOutputStreamTest {
4649
}
4750
return SentryFileOutputStream(tmpFile, append, scopes)
4851
}
52+
53+
internal fun getSut(
54+
tmpFile: File? = null,
55+
delegate: FileOutputStream,
56+
tracesSampleRate: Double? = 1.0
57+
): FileOutputStream {
58+
options.tracesSampleRate = tracesSampleRate
59+
whenever(scopes.options).thenReturn(options)
60+
sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes)
61+
whenever(scopes.span).thenReturn(sentryTracer)
62+
return SentryFileOutputStream.Factory.create(
63+
delegate,
64+
tmpFile,
65+
scopes
66+
)
67+
}
4968
}
5069

5170
@get:Rule
@@ -197,4 +216,25 @@ class SentryFileOutputStreamTest {
197216
assertEquals(false, fileIOSpan.data[SpanDataConvention.BLOCKED_MAIN_THREAD_KEY])
198217
assertNull(fileIOSpan.data[SpanDataConvention.CALL_STACK_KEY])
199218
}
219+
220+
@Test
221+
fun `when tracing is disabled does not instrument the stream`() {
222+
val file = tmpFile
223+
val delegate = ThrowingFileOutputStream(file)
224+
val stream = fixture.getSut(file, delegate = delegate, tracesSampleRate = null)
225+
226+
assertTrue { stream is ThrowingFileOutputStream }
227+
}
228+
}
229+
230+
class ThrowingFileOutputStream(file: File) : FileOutputStream(file) {
231+
val throwable = IOException("Oops!")
232+
233+
override fun write(b: Int) {
234+
throw throwable
235+
}
236+
237+
override fun close() {
238+
throw throwable
239+
}
200240
}

0 commit comments

Comments
 (0)