Repository navigation
Expand file tree
/
Copy pathSentryTimberIntegrationTest.kt
More file actions
216 lines (181 loc) · 6.1 KB
/
Copy pathSentryTimberIntegrationTest.kt
File metadata and controls
216 lines (181 loc) · 6.1 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package io.sentry.android.timber
import io.sentry.Breadcrumb
import io.sentry.IScopes
import io.sentry.ITransportFactory
import io.sentry.ScopesAdapter
import io.sentry.Sentry
import io.sentry.SentryLevel
import io.sentry.SentryLogLevel
import io.sentry.SentryOptions
import io.sentry.logger.ILoggerApi
import io.sentry.logger.SentryLogParameters
import io.sentry.protocol.SdkVersion
import io.sentry.transport.ITransport
import io.sentry.util.LazyEvaluator.Evaluator
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoInteractions
import org.mockito.kotlin.whenever
import timber.log.Timber
class SentryTimberIntegrationTest {
private class Fixture {
val scopes = mock<IScopes>()
val logs = mock<ILoggerApi>()
val options = SentryOptions().apply { sdkVersion = SdkVersion("test", "1.2.3") }
init {
whenever(scopes.logger()).thenReturn(logs)
}
fun getSut(
minEventLevel: SentryLevel = SentryLevel.ERROR,
minBreadcrumbLevel: SentryLevel = SentryLevel.INFO,
minLogsLevel: SentryLogLevel = SentryLogLevel.INFO,
enableLogs: Boolean? = null,
): SentryTimberIntegration =
if (enableLogs == null) {
SentryTimberIntegration(
minEventLevel = minEventLevel,
minBreadcrumbLevel = minBreadcrumbLevel,
minLogsLevel = minLogsLevel,
)
} else {
SentryTimberIntegration(
minEventLevel = minEventLevel,
minBreadcrumbLevel = minBreadcrumbLevel,
minLogsLevel = minLogsLevel,
enableLogs = enableLogs,
)
}
}
private val fixture = Fixture()
@BeforeTest
fun beforeTest() {
Timber.uprootAll()
}
@Test
fun `Integrations plants a tree into Timber on register`() {
val sut = fixture.getSut()
sut.register(fixture.scopes, fixture.options)
assertEquals(1, Timber.treeCount())
val trees = Timber.forest()
val first = trees.first()
assertTrue(first is SentryTimberTree)
}
@Test
fun `Integrations plants the SentryTimberTree tree`() {
val sut = fixture.getSut()
sut.register(fixture.scopes, fixture.options)
Timber.e(Throwable())
verify(fixture.scopes).captureEvent(any())
}
@Test
fun `Manual integration defaults logs to disabled while capturing events and breadcrumbs`() {
val sut = fixture.getSut()
sut.register(fixture.scopes, fixture.options)
assertFalse(sut.enableLogs)
Timber.e("message")
verify(fixture.scopes).captureEvent(any())
verify(fixture.scopes).addBreadcrumb(any<Breadcrumb>())
verifyNoInteractions(fixture.logs)
}
@Test
fun `Manual integration captures logs when enabled`() {
val sut = fixture.getSut(enableLogs = true)
sut.register(fixture.scopes, fixture.options)
assertTrue(sut.enableLogs)
Timber.i("message")
verify(fixture.logs).log(any(), any<SentryLogParameters>(), any<String>())
}
@Test
fun `Integration evaluates Logs provider when registered`() {
var enableLogs = false
val sut = SentryTimberIntegration(Evaluator { enableLogs })
enableLogs = true
sut.register(fixture.scopes, fixture.options)
Timber.i("message")
verify(fixture.logs).log(any(), any<SentryLogParameters>(), any<String>())
}
@Test
fun `Integrations removes a tree from Timber on close integration`() {
val sut = fixture.getSut()
sut.register(fixture.scopes, fixture.options)
assertEquals(1, Timber.treeCount())
sut.close()
assertEquals(0, Timber.treeCount())
}
@Test
fun `Integrations do not throw if close is called before register`() {
val sut = fixture.getSut()
sut.close()
assertEquals(0, Timber.treeCount())
}
@Test
fun `Integrations pass the right min levels`() {
val sut =
fixture.getSut(
minEventLevel = SentryLevel.INFO,
minBreadcrumbLevel = SentryLevel.DEBUG,
minLogsLevel = SentryLogLevel.TRACE,
)
sut.register(fixture.scopes, fixture.options)
assertEquals(sut.minEventLevel, SentryLevel.INFO)
assertEquals(sut.minBreadcrumbLevel, SentryLevel.DEBUG)
assertEquals(sut.minLogsLevel, SentryLogLevel.TRACE)
}
@Test
fun `Integration adds itself to the package list`() {
val sut = fixture.getSut()
sut.register(fixture.scopes, fixture.options)
assertTrue(
fixture.options.sdkVersion!!.packageSet.any {
it.name == "maven:io.sentry:sentry-android-timber" && it.version == BuildConfig.VERSION_NAME
}
)
}
@Test
fun `Integration adds itself to the integration list`() {
val sut = fixture.getSut()
sut.register(fixture.scopes, fixture.options)
assertTrue(fixture.options.sdkVersion!!.integrationSet.contains("Timber"))
}
@Test
fun `a beforeSend callback that logs via Timber does not recurse`() {
// End-to-end guard against SDK-CRASHES-JAVA-3T3H style recursion: with a real Sentry instance,
// a beforeSend callback that logs through the planted SentryTimberTree must not loop back into
// capture forever.
val transport = mock<ITransport>()
val transportFactory = mock<ITransportFactory>()
whenever(transportFactory.create(any(), any())).thenReturn(transport)
var beforeSendInvocations = 0
Sentry.init { options ->
options.dsn = "https://key@sentry.io/123"
options.setTransportFactory(transportFactory)
options.beforeSend = SentryOptions.BeforeSendCallback { event, _ ->
beforeSendInvocations++
Timber.e("logging from beforeSend")
event
}
}
Timber.plant(
SentryTimberTree(
ScopesAdapter.getInstance(),
SentryLevel.ERROR,
SentryLevel.INFO,
SentryLogLevel.INFO,
)
)
try {
Timber.e("outer error")
// Without the core re-entrancy guard this recurses until a StackOverflowError. The nested
// Timber.e is dropped before its own beforeSend, so the callback runs exactly once.
assertEquals(1, beforeSendInvocations)
} finally {
Sentry.close()
}
}
}