@@ -14,6 +14,8 @@ import org.junit.rules.TemporaryFolder
1414import org.mockito.kotlin.mock
1515import org.mockito.kotlin.whenever
1616import java.io.File
17+ import java.io.FileOutputStream
18+ import java.io.IOException
1719import java.util.concurrent.atomic.AtomicBoolean
1820import kotlin.concurrent.thread
1921import kotlin.test.Test
@@ -26,6 +28,7 @@ import kotlin.test.assertTrue
2628class 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