-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
SentryFileOutputStreamTest.kt
116 lines (95 loc) · 3.69 KB
/
SentryFileOutputStreamTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package io.sentry.instrumentation.file
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.IHub
import io.sentry.SentryOptions
import io.sentry.SentryTracer
import io.sentry.SpanStatus
import io.sentry.TransactionContext
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import java.io.File
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
class SentryFileOutputStreamTest {
class Fixture {
val hub = mock<IHub>()
lateinit var sentryTracer: SentryTracer
internal fun getSut(
tmpFile: File? = null,
activeTransaction: Boolean = true,
append: Boolean = false
): SentryFileOutputStream {
whenever(hub.options).thenReturn(SentryOptions())
sentryTracer = SentryTracer(TransactionContext("name", "op"), hub)
if (activeTransaction) {
whenever(hub.span).thenReturn(sentryTracer)
}
return SentryFileOutputStream(tmpFile, append, hub)
}
}
@get:Rule
val tmpDir = TemporaryFolder()
private val fixture = Fixture()
private val tmpFile: File get() = tmpDir.newFile("test.txt")
@Test
fun `when no active transaction does not capture a span`() {
fixture.getSut(tmpFile, activeTransaction = false)
.use { it.write("Text".toByteArray()) }
assertEquals(fixture.sentryTracer.children.size, 0)
}
@Test
fun `when stream is not closed does not finish a span`() {
fixture.getSut(tmpFile)
assertEquals(fixture.sentryTracer.children.size, 1)
val fileIOSpan = fixture.sentryTracer.children.first()
assertEquals(fileIOSpan.isFinished, false)
}
@Test
fun `when stream is closed captures a span`() {
val fos = fixture.getSut(tmpFile)
fos.close()
assertEquals(fixture.sentryTracer.children.size, 1)
val fileIOSpan = fixture.sentryTracer.children.first()
assertEquals(fileIOSpan.spanContext.description, "test.txt (0 B)")
assertEquals(fileIOSpan.data["file.size"], 0L)
assertEquals(fileIOSpan.throwable, null)
assertEquals(fileIOSpan.isFinished, true)
assertEquals(fileIOSpan.status, SpanStatus.OK)
}
@Test
fun `when stream is closed file descriptor is also closed`() {
val fos = fixture.getSut(tmpFile)
fos.use { it.write("hello".toByteArray()) }
assertFalse(fos.fd.valid())
}
@Test
fun `write one byte`() {
fixture.getSut(tmpFile).use { it.write(29) }
val fileIOSpan = fixture.sentryTracer.children.first()
assertEquals(fileIOSpan.spanContext.description, "test.txt (1 B)")
assertEquals(fileIOSpan.data["file.size"], 1L)
}
@Test
fun `write array of bytes`() {
fixture.getSut(tmpFile).use { it.write(ByteArray(10)) }
val fileIOSpan = fixture.sentryTracer.children.first()
assertEquals(fileIOSpan.spanContext.description, "test.txt (10 B)")
assertEquals(fileIOSpan.data["file.size"], 10L)
}
@Test
fun `write array range of bytes`() {
fixture.getSut(tmpFile).use { it.write(ByteArray(10), 1, 3) }
val fileIOSpan = fixture.sentryTracer.children.first()
assertEquals(fileIOSpan.spanContext.description, "test.txt (3 B)")
assertEquals(fileIOSpan.data["file.size"], 3L)
}
@Test
fun `write all bytes`() {
fixture.getSut(tmpFile).use { it.write("Text".toByteArray()) }
val fileIOSpan = fixture.sentryTracer.children.first()
assertEquals(fileIOSpan.spanContext.description, "test.txt (4 B)")
assertEquals(fileIOSpan.data["file.size"], 4L)
}
}