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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import me.bush.illnamethislater.*
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.core.config.Configurator
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import kotlin.random.Random
/**
* I don't know how to do these....
*
* @author bush
* @since 1.0.0
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class KotlinTest {
private lateinit var eventBus: EventBus
private val logger = LogManager.getLogger("Kotlin Test")
@BeforeAll
fun `setup logger and initialize eventbus`() {
// Log level defaults to only error
Configurator.setRootLevel(Level.ALL)
eventBus = EventBus(
// Defaults
Config(
logger = LogManager.getLogger("Eventbus"),
parallelScope = CoroutineScope(Dispatchers.Default),
thirdPartyCompatibility = true,
annotationRequired = false
)
)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Test
fun `test listener priority and ability to cancel events or receive cancelled events`() {
eventBus.subscribe(this)
val event = SimpleEvent()
eventBus.post(event)
Assertions.assertEquals(3, event.count)
eventBus.unsubscribe(this)
}
// Last to be called; does not have receiveCancelled, but the last listener un-cancelled the event.
fun listener4() = listener<SimpleEvent>(priority = Int.MIN_VALUE) {
Assertions.assertEquals(2, it.count)
it.count++
}
// Will not be called; second-highest priority, no receiveCancelled.
val listener2
get() = listener<SimpleEvent>(priority = 0) {
Assertions.fail("This should not be called")
}
// First to be called; highest priority.
private val listener1 = listener<SimpleEvent>(priority = 10) {
Assertions.assertEquals(0, it.count)
it.count++
// Cancel, so next listener shouldn't receive it.
it.cancel()
}
// Second to be called; has receiveCancelled and can un-cancel the event.
fun listener3() = listener<SimpleEvent>(priority = Int.MIN_VALUE + 100, receiveCancelled = true) {
Assertions.assertEquals(1, it.count)
it.count++
it.cancelled = false
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Test
fun `test subscribing on a kotlin singleton object`() {
eventBus.subscribe(ObjectTest)
val event = SimpleEvent()
eventBus.post(event)
Assertions.assertEquals(3, event.count)
eventBus.unsubscribe(ObjectTest)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Test
fun `test primitive types and listeners which don't belong to a class`() {
val random = Random.nextInt()
var changed = 0
val listener = listener<Int> {
changed = it
}
eventBus.register(listener)
eventBus.post(random)
Assertions.assertEquals(random, changed)
eventBus.unregister(listener)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Test
fun `test that we can detect if an external event is cancelled`() {
eventBus.subscribe(this)
val event = ExternalEvent()
eventBus.post(event)
eventBus.unsubscribe(this)
}
@EventListener
fun externalListener1() = listener<ExternalEvent>(priority = 1) {
it.canceled = true
}
// Should not be called
fun externalListener2() = listener<ExternalEvent>(priority = -1) {
Assertions.fail("This should not be called")
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Test
fun `test parallel event posting`() {
runBlocking {
sus()
}
}
suspend fun sus() {
println()
}
fun sussy() {
println()
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Test
fun `call every method on multiple threads concurrently to ensure no CME is thrown`() {
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Test
fun `test that inheritance doesn't affect events or listeners`() {
val superTest = listener<SimpleEvent> {
Assertions.fail("This should not be called")
}
eventBus.register(superTest)
// No listeners should be called when a superclass is posted.
eventBus.post(Any())
eventBus.unregister(superTest)
val subTest = listener<Any> {
Assertions.fail("This should not be called")
}
eventBus.register(subTest)
// No listeners should be called when a subclass is posted.
eventBus.post(SimpleEvent())
eventBus.unregister(subTest)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Test
fun `test that require annotation mode works`() {
val eventBus = EventBus(Config(annotationRequired = true))
eventBus.subscribe(this)
eventBus.post(Unit)
Assertions.assertTrue(called)
}
var called = false
@EventListener
val annotation = listener<Unit> {
called = true
}
val noAnnotation = listener<Unit> {
Assertions.fail("This should not be called")
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Test
fun `test subscribing on a companion object`() {
eventBus.subscribe(KotlinTest)
val string = "i love bush's eventbus <3"
eventBus.post(string)
Assertions.assertEquals(string, value)
eventBus.unsubscribe(KotlinTest)
}
companion object {
var value = ""
@EventListener
private val listener = listener<String> {
value = it
}
}
}
object ObjectTest {
private val listener1 = listener<SimpleEvent> {
it.count++
}
@EventListener
private val listener2
get() = listener<SimpleEvent> {
it.count++
}
private fun listener3() = listener<SimpleEvent> {
it.count++
}
}
class SimpleEvent : Event() {
override val cancellable = true
var count = 0
}
class ExternalEvent {
var canceled = false
}
|