Skip to content

Commit 97d02c3

Browse files
committed
add a new test with add plugin in between
1 parent 44b4ff7 commit 97d02c3

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

core/src/test/kotlin/com/amplitude/core/platform/MediatorTest.kt

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.amplitude.core.platform
22

3+
import okhttp3.internal.wait
34
import org.junit.jupiter.api.Assertions.assertEquals
45
import org.junit.jupiter.api.Test
56
import org.junit.jupiter.api.Timeout
67
import java.util.concurrent.CopyOnWriteArrayList
8+
import java.util.concurrent.CountDownLatch
79
import java.util.concurrent.TimeUnit
810
import java.util.concurrent.atomic.AtomicInteger
911
import kotlin.concurrent.thread
@@ -26,7 +28,7 @@ class MediatorTest {
2628

2729
@Test
2830
@Timeout(3, unit = TimeUnit.SECONDS)
29-
fun `two threads that call flush twice on two destination plugins`() {
31+
fun `call flush twice on two destination plugins`() {
3032
val fakeDestinationPlugins = List(2) { FakeDestinationPlugin() }
3133
fakeDestinationPlugins.forEach {
3234
mediator.add(it)
@@ -51,4 +53,43 @@ class MediatorTest {
5153
assertEquals(2, it.amountOfWorkDone.get())
5254
}
5355
}
56+
57+
@Test
58+
@Timeout(5, unit = TimeUnit.SECONDS)
59+
fun `flush, add a new plugin, and flush again on two destination plugins`() {
60+
val fakeDestinationPlugin1 = FakeDestinationPlugin()
61+
val fakeDestinationPlugin2 = FakeDestinationPlugin()
62+
63+
mediator.add(fakeDestinationPlugin1)
64+
65+
val work = {
66+
mediator.applyClosure {
67+
(it as EventPlugin).flush()
68+
}
69+
}
70+
71+
// flush and add
72+
val latch = CountDownLatch(2)
73+
val t1 = thread {
74+
work()
75+
latch.countDown()
76+
}
77+
val t2 = thread {
78+
// add plugin 2, work() should catch up with the newly added plugin
79+
mediator.add(fakeDestinationPlugin2)
80+
latch.countDown()
81+
}
82+
latch.await()
83+
84+
// flush again
85+
val t3 = thread {
86+
work()
87+
}
88+
t1.join()
89+
t2.join()
90+
t3.join()
91+
92+
assertEquals(2, fakeDestinationPlugin1.amountOfWorkDone.get())
93+
assertEquals(2, fakeDestinationPlugin2.amountOfWorkDone.get())
94+
}
5495
}

0 commit comments

Comments
 (0)