Skip to content
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

Make ServiceProvider.getService thread safe #2012

Merged
merged 3 commits into from
Mar 1, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import com.onesignal.debug.internal.logging.Logging
class ServiceProvider(
registrations: List<ServiceRegistration<*>>,
) : IServiceProvider {
private var serviceMap: Map<Class<*>, List<ServiceRegistration<*>>>
private val serviceMap = mutableMapOf<Class<*>, MutableList<ServiceRegistration<*>>>()

init {
val serviceMap = mutableMapOf<Class<*>, MutableList<ServiceRegistration<*>>>()

// go through the registrations to create the service map for easier lookup post-build
for (reg in registrations) {
for (service in reg.services) {
Expand All @@ -23,8 +21,6 @@ class ServiceProvider(
}
}
}

this.serviceMap = serviceMap
}

internal inline fun <reified T : Any> hasService(): Boolean {
Expand All @@ -44,23 +40,27 @@ class ServiceProvider(
}

override fun <T> hasService(c: Class<T>): Boolean {
return serviceMap.containsKey(c)
synchronized(serviceMap) {
return serviceMap.containsKey(c)
}
}

override fun <T> getAllServices(c: Class<T>): List<T> {
val listOfServices: MutableList<T> = mutableListOf()
synchronized(serviceMap) {
val listOfServices: MutableList<T> = mutableListOf()

if (serviceMap.containsKey(c)) {
for (serviceReg in serviceMap!![c]!!) {
val service =
serviceReg.resolve(this) as T?
?: throw Exception("Could not instantiate service: $serviceReg")
if (serviceMap.containsKey(c)) {
for (serviceReg in serviceMap!![c]!!) {
val service =
serviceReg.resolve(this) as T?
?: throw Exception("Could not instantiate service: $serviceReg")

listOfServices.add(service)
listOfServices.add(service)
}
}
}

return listOfServices
return listOfServices
}
}

override fun <T> getService(c: Class<T>): T {
Expand All @@ -74,11 +74,10 @@ class ServiceProvider(
}

override fun <T> getServiceOrNull(c: Class<T>): T? {
Logging.debug("${indent}Retrieving service $c")
// indent += " "
val service = serviceMap[c]?.last()?.resolve(this) as T?
// indent = indent.substring(0, indent.length-2)
return service
synchronized(serviceMap) {
Logging.debug("${indent}Retrieving service $c")
return serviceMap[c]?.last()?.resolve(this) as T?
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.onesignal.common

import com.onesignal.common.services.ServiceBuilder
import com.onesignal.common.services.ServiceProvider
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.types.shouldBeSameInstanceAs
import java.util.concurrent.LinkedBlockingQueue

internal interface IMyTestInterface

internal class MySlowConstructorClass : IMyTestInterface {
init {
// NOTE: Keep these println calls, otherwise Kotlin optimizes
// something which cases the test not fail when it should.
println("MySlowConstructorClass BEFORE")
Thread.sleep(10)
println("MySlowConstructorClass AFTER")
}
}

class ServiceProviderTest : FunSpec({

fun setupServiceProviderWithSlowInitClass(): ServiceProvider {
val serviceBuilder = ServiceBuilder()
serviceBuilder.register<MySlowConstructorClass>().provides<IMyTestInterface>()
return serviceBuilder.build()
}

test("getService is thread safe") {
val services = setupServiceProviderWithSlowInitClass()

val queue = LinkedBlockingQueue<IMyTestInterface>()
Thread {
queue.add(services.getService<IMyTestInterface>())
}.start()
Thread {
queue.add(services.getService<IMyTestInterface>())
}.start()

val firstReference = queue.take()
val secondReference = queue.take()
firstReference shouldBeSameInstanceAs secondReference
}
})
Loading