Skip to content

Commit 44b4ff7

Browse files
committed
update to test to actually use mediator
1 parent 7abecac commit 44b4ff7

File tree

1 file changed

+15
-35
lines changed

1 file changed

+15
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.amplitude.core.platform
22

3-
import org.junit.jupiter.api.Assertions.assertTrue
3+
import org.junit.jupiter.api.Assertions.assertEquals
44
import org.junit.jupiter.api.Test
55
import org.junit.jupiter.api.Timeout
66
import java.util.concurrent.CopyOnWriteArrayList
7-
import java.util.concurrent.Executors
87
import java.util.concurrent.TimeUnit
8+
import java.util.concurrent.atomic.AtomicInteger
99
import kotlin.concurrent.thread
1010

1111
class MediatorTest {
@@ -15,60 +15,40 @@ class MediatorTest {
1515
* Fake [DestinationPlugin] that does work on [flush] for 1 second
1616
*/
1717
private class FakeDestinationPlugin : DestinationPlugin() {
18-
var workDone = false
18+
var amountOfWorkDone = AtomicInteger()
1919

2020
override fun flush() {
2121
super.flush()
22-
println("$this start work ${System.currentTimeMillis()} ${Thread.currentThread().name}")
2322
Thread.sleep(1_000)
24-
println("$this end work ${System.currentTimeMillis()} ${Thread.currentThread().name}")
25-
workDone = true
23+
amountOfWorkDone.incrementAndGet()
2624
}
2725
}
2826

2927
@Test
30-
@Timeout(2, unit = TimeUnit.SECONDS)
31-
fun `multiple threads that call flush on destination plugin`() {
32-
val fakeDestinationPlugins = List(10) { FakeDestinationPlugin() }
33-
fakeDestinationPlugins.forEach {
34-
mediator.add(it)
35-
}
36-
37-
// simulate 10 threads executing flush on 10 different [DestinationPlugin]s
38-
val executor = Executors.newFixedThreadPool(10)
39-
fakeDestinationPlugins.forEach {
40-
executor.submit {
41-
it.flush()
42-
}
43-
}
44-
executor.shutdown()
45-
executor.awaitTermination(2, TimeUnit.SECONDS)
46-
47-
assertTrue {
48-
fakeDestinationPlugins.all { it.workDone }
49-
}
50-
}
51-
52-
@Test
53-
@Timeout(2, unit = TimeUnit.SECONDS)
54-
fun `two threads that call flush on destination plugin`() {
28+
@Timeout(3, unit = TimeUnit.SECONDS)
29+
fun `two threads that call flush twice on two destination plugins`() {
5530
val fakeDestinationPlugins = List(2) { FakeDestinationPlugin() }
5631
fakeDestinationPlugins.forEach {
5732
mediator.add(it)
5833
}
5934

6035
// simulate 2 threads executing flush on 2 different DestinationPlugins
36+
val work = {
37+
mediator.applyClosure {
38+
(it as EventPlugin).flush()
39+
}
40+
}
6141
val t1 = thread {
62-
fakeDestinationPlugins[0].flush()
42+
work()
6343
}
6444
val t2 = thread {
65-
fakeDestinationPlugins[1].flush()
45+
work()
6646
}
6747
t1.join()
6848
t2.join()
6949

70-
assertTrue {
71-
fakeDestinationPlugins.all { it.workDone }
50+
fakeDestinationPlugins.forEach {
51+
assertEquals(2, it.amountOfWorkDone.get())
7252
}
7353
}
7454
}

0 commit comments

Comments
 (0)