Skip to content

Commit c93865b

Browse files
committed
fix: simulate multiple work by t1 and fix flakiness
1 parent d71d59b commit c93865b

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

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

+13-10
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class MediatorTest {
2727

2828
@Test
2929
@Timeout(3, unit = TimeUnit.SECONDS)
30-
fun `call flush twice on two destination plugins`() {
30+
fun `does work twice on two destination plugins`() {
3131
val fakeDestinationPlugins = List(2) { FakeDestinationPlugin() }
3232
fakeDestinationPlugins.forEach {
3333
mediator.add(it)
3434
}
3535

36-
// simulate 2 threads executing flush on 2 different DestinationPlugins
36+
// simulate 2 threads executing work on 2 different DestinationPlugins
3737
val work = {
3838
mediator.applyClosure {
3939
(it as EventPlugin).flush()
@@ -54,8 +54,8 @@ class MediatorTest {
5454
}
5555

5656
@Test
57-
@Timeout(5, unit = TimeUnit.SECONDS)
58-
fun `flush, add a new plugin, and flush again on two destination plugins`() {
57+
@Timeout(7, unit = TimeUnit.SECONDS)
58+
fun `work, add a new plugin and work, and work again on two destination plugins`() {
5959
val fakeDestinationPlugin1 = FakeDestinationPlugin()
6060
val fakeDestinationPlugin2 = FakeDestinationPlugin()
6161

@@ -67,28 +67,31 @@ class MediatorTest {
6767
}
6868
}
6969

70-
// flush and add
70+
// work and add, work again
7171
val latch = CountDownLatch(2)
7272
val t1 = thread {
73+
work()
7374
work()
7475
latch.countDown()
7576
}
7677
val t2 = thread {
77-
// add plugin 2, work() should catch up with the newly added plugin
78+
// give time for the first work() to start
79+
Thread.sleep(100)
80+
// add plugin 2, 2nd work() should catch up with the newly added plugin
7881
mediator.add(fakeDestinationPlugin2)
7982
latch.countDown()
8083
}
84+
t1.join()
85+
t2.join()
8186
latch.await()
8287

83-
// flush again
88+
// work again
8489
val t3 = thread {
8590
work()
8691
}
87-
t1.join()
88-
t2.join()
8992
t3.join()
9093

91-
assertEquals(2, fakeDestinationPlugin1.amountOfWorkDone.get())
94+
assertEquals(3, fakeDestinationPlugin1.amountOfWorkDone.get())
9295
assertEquals(2, fakeDestinationPlugin2.amountOfWorkDone.get())
9396
}
9497
}

0 commit comments

Comments
 (0)