-
-
Notifications
You must be signed in to change notification settings - Fork 475
Expand file tree
/
Copy pathMocks.kt
More file actions
116 lines (93 loc) · 3.5 KB
/
Copy pathMocks.kt
File metadata and controls
116 lines (93 loc) · 3.5 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
package io.sentry.test
import io.sentry.IScope
import io.sentry.ISentryClient
import io.sentry.ISentryExecutorService
import io.sentry.Scope
import io.sentry.Scopes
import io.sentry.SentryOptions
import io.sentry.backpressure.IBackpressureMonitor
import java.util.concurrent.Callable
import java.util.concurrent.Future
import java.util.concurrent.FutureTask
import java.util.concurrent.atomic.AtomicBoolean
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
class ImmediateExecutorService : ISentryExecutorService {
override fun submit(runnable: Runnable): Future<*> {
if (runnable !is IBackpressureMonitor) {
runnable.run()
}
return mock()
}
override fun <T> submit(callable: Callable<T>): Future<T> = mock()
override fun schedule(runnable: Runnable, delayMillis: Long): Future<*> {
if (runnable !is IBackpressureMonitor) {
runnable.run()
}
return mock<Future<Runnable>>()
}
override fun close(timeoutMillis: Long) {}
override fun isClosed(): Boolean = false
}
class DeferredExecutorService : ISentryExecutorService {
private var runnables = ArrayList<Runnable>()
private var scheduledRunnables = ArrayList<Runnable>()
fun runAll() {
// take a snapshot of the runnable list in case
// executing the runnable itself schedules more runnables
val currentRunnableList = runnables
val currentScheduledRunnableList = scheduledRunnables
synchronized(this) {
runnables = ArrayList()
scheduledRunnables = ArrayList()
}
currentRunnableList.forEach { it.run() }
currentScheduledRunnableList.forEach { it.run() }
}
override fun submit(runnable: Runnable): Future<*> {
synchronized(this) { runnables.add(runnable) }
return FutureTask {}
}
override fun <T> submit(callable: Callable<T>): Future<T> = mock()
override fun schedule(runnable: Runnable, delayMillis: Long): Future<*> {
synchronized(this) { scheduledRunnables.add(runnable) }
return FutureTask {}
}
override fun close(timeoutMillis: Long) {}
override fun isClosed(): Boolean = false
fun hasScheduledRunnables(): Boolean = scheduledRunnables.isNotEmpty()
}
class NonOverridableNoOpSentryExecutorService : ISentryExecutorService {
override fun submit(runnable: Runnable): Future<*> = FutureTask<Void> { null }
override fun <T> submit(callable: Callable<T>): Future<T> = FutureTask<T> { null }
override fun schedule(runnable: Runnable, delayMillis: Long): Future<*> =
FutureTask<Void> { null }
override fun close(timeoutMillis: Long) {}
override fun isClosed(): Boolean = false
}
fun createSentryClientMock(enabled: Boolean = true) =
mock<ISentryClient>().also {
val isEnabled = AtomicBoolean(enabled)
whenever(it.isEnabled).then { isEnabled.get() }
whenever(it.close()).then { isEnabled.set(false) }
whenever(it.close(any())).then { isEnabled.set(false) }
}
fun createTestScopes(
options: SentryOptions? = null,
enabled: Boolean = true,
scope: IScope? = null,
isolationScope: IScope? = null,
globalScope: IScope? = null,
): Scopes {
val optionsToUse = options ?: SentryOptions().also { it.dsn = "https://key@sentry.io/proj" }
initForTest(optionsToUse)
val scopeToUse = scope ?: Scope(optionsToUse)
val isolationScopeToUse = isolationScope ?: Scope(optionsToUse)
val globalScopeToUse = globalScope ?: Scope(optionsToUse)
return Scopes(scopeToUse, isolationScopeToUse, globalScopeToUse, "test").also {
if (enabled) {
it.bindClient(createSentryClientMock())
}
}
}