Skip to content

fix: remove synchronized on mediator #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions core/src/main/java/com/amplitude/core/platform/Mediator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import com.amplitude.core.events.BaseEvent
import com.amplitude.core.events.GroupIdentifyEvent
import com.amplitude.core.events.IdentifyEvent
import com.amplitude.core.events.RevenueEvent
import java.util.concurrent.CopyOnWriteArrayList

internal class Mediator(internal val plugins: MutableList<Plugin>) {
fun add(plugin: Plugin) = synchronized(plugins) {
internal class Mediator(
private val plugins: CopyOnWriteArrayList<Plugin> = CopyOnWriteArrayList()
) {
fun add(plugin: Plugin) {
plugins.add(plugin)
}

fun remove(plugin: Plugin) = synchronized(plugins) {
plugins.removeAll { it === plugin }
}
fun remove(plugin: Plugin) = plugins.removeAll { it === plugin }

fun execute(event: BaseEvent): BaseEvent? = synchronized(plugins) {
fun execute(event: BaseEvent): BaseEvent? {
var result: BaseEvent? = event

plugins.forEach { plugin ->
Expand Down Expand Up @@ -53,9 +54,14 @@ internal class Mediator(internal val plugins: MutableList<Plugin>) {
return result
}

fun applyClosure(closure: (Plugin) -> Unit) = synchronized(plugins) {
fun applyClosure(closure: (Plugin) -> Unit) {
plugins.forEach {
closure(it)
}
}

/**
* Only visible for testing
*/
internal fun size() = plugins.size
}
8 changes: 4 additions & 4 deletions core/src/main/java/com/amplitude/core/platform/Timeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import com.amplitude.core.events.BaseEvent

open class Timeline {
internal val plugins: Map<Plugin.Type, Mediator> = mapOf(
Plugin.Type.Before to Mediator(mutableListOf()),
Plugin.Type.Enrichment to Mediator(mutableListOf()),
Plugin.Type.Destination to Mediator(mutableListOf()),
Plugin.Type.Utility to Mediator(mutableListOf())
Plugin.Type.Before to Mediator(),
Plugin.Type.Enrichment to Mediator(),
Plugin.Type.Destination to Mediator(),
Plugin.Type.Utility to Mediator()
)
lateinit var amplitude: Amplitude

Expand Down
8 changes: 4 additions & 4 deletions core/src/test/kotlin/com/amplitude/core/AmplitudeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ internal class AmplitudeTest {
override lateinit var amplitude: Amplitude
}
amplitude.add(middleware)
amplitude.timeline.plugins[Plugin.Type.Enrichment]?.plugins?.let {
amplitude.timeline.plugins[Plugin.Type.Enrichment]?.size()?.let {
assertEquals(
2,
it.size
it
)
} ?: fail()
}
Expand All @@ -332,10 +332,10 @@ internal class AmplitudeTest {
}
amplitude.add(middleware)
amplitude.remove(middleware)
amplitude.timeline.plugins[Plugin.Type.Enrichment]?.plugins?.let {
amplitude.timeline.plugins[Plugin.Type.Enrichment]?.size()?.let {
assertEquals(
1, // SegmentLog is the other added at startup
it.size
it
)
} ?: fail()
}
Expand Down
94 changes: 94 additions & 0 deletions core/src/test/kotlin/com/amplitude/core/platform/MediatorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.amplitude.core.platform

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Timeout
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import kotlin.concurrent.thread

class MediatorTest {
private val mediator = Mediator(CopyOnWriteArrayList())

/**
* Fake [DestinationPlugin] that does work on [flush] for 1 second
*/
private class FakeDestinationPlugin : DestinationPlugin() {
var amountOfWorkDone = AtomicInteger()

override fun flush() {
super.flush()
Thread.sleep(1_000)
amountOfWorkDone.incrementAndGet()
}
}

@Test
@Timeout(3, unit = TimeUnit.SECONDS)
fun `call flush twice on two destination plugins`() {
val fakeDestinationPlugins = List(2) { FakeDestinationPlugin() }
fakeDestinationPlugins.forEach {
mediator.add(it)
}

// simulate 2 threads executing flush on 2 different DestinationPlugins
val work = {
mediator.applyClosure {
(it as EventPlugin).flush()
}
}
val t1 = thread {
work()
}
val t2 = thread {
work()
}
t1.join()
t2.join()

fakeDestinationPlugins.forEach {
assertEquals(2, it.amountOfWorkDone.get())
}
}

@Test
@Timeout(5, unit = TimeUnit.SECONDS)
fun `flush, add a new plugin, and flush again on two destination plugins`() {
val fakeDestinationPlugin1 = FakeDestinationPlugin()
val fakeDestinationPlugin2 = FakeDestinationPlugin()

mediator.add(fakeDestinationPlugin1)

val work = {
mediator.applyClosure {
(it as EventPlugin).flush()
}
}

// flush and add
val latch = CountDownLatch(2)
val t1 = thread {
work()
latch.countDown()
}
val t2 = thread {
// add plugin 2, work() should catch up with the newly added plugin
mediator.add(fakeDestinationPlugin2)
latch.countDown()
}
latch.await()

// flush again
val t3 = thread {
work()
}
t1.join()
t2.join()
t3.join()

assertEquals(2, fakeDestinationPlugin1.amountOfWorkDone.get())
assertEquals(2, fakeDestinationPlugin2.amountOfWorkDone.get())
}
}
Loading