I have a test (ObjectOfMapSpec) that throws a NullPointerException when run a 24xcore machine while it works fine on a 2-core machine. Question is whether the problem is related to Thread Visibility, and if so, why does it occur.
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
import org.scalatest.FlatSpec
import org.scalatest.Matchers
trait Resource {
val key: String = UUID.randomUUID().toString()
}
trait SubResource extends Resource {
val name: String
//In a multi-core machine, removing the comment creates a NullPointerException in the map
override val key: String = name
}
object ObjectOfResource extends SubResource {
val name = “ObjectOfResource”
}
trait SomeMap {
private val hashMap = new ConcurrentHashMap[String, Resource]
def addResource(someResource: Resource): Resource = {
//In a multi-core machine, key becomes null
println("…" + someResource.key)
hashMap.putIfAbsent(someResource.key, someResource)
}
}
object ObjectOfMap extends SomeMap {
addResource(ObjectOfResource)
def dummyMethod() = true
}
class ObjectOfMapSpec extends FlatSpec with Matchers {
“ObjectOfMap” should “work just fine…” in {
val ok = ObjectOfMap.dummyMethod()
ok should be(true)
}
}