Added test for parallel access of Index

This commit is contained in:
Kjell Winblad 2011-10-13 13:27:07 +02:00
parent bff7d10bb4
commit c877b69520

View file

@ -5,6 +5,8 @@ package akka.util
import org.scalatest.WordSpec
import org.scalatest.matchers.MustMatchers
import scala.actors.Actor._
import java.util.concurrent.CountDownLatch
class IndexSpec extends WordSpec with MustMatchers {
@ -27,17 +29,17 @@ class IndexSpec extends WordSpec with MustMatchers {
"take and return a value" in {
val index = emptyIndex
index.put("s1", 1)
assert(index.valueIterator("s1").toSet === Set(1))
index.valueIterator("s1").toSet must be === Set(1)
}
"take and return several values" in {
val index = emptyIndex
assert(index.put("s1", 1))
assert(!index.put("s1", 1))
index.put("s1", 1) must be === true
index.put("s1", 1) must be === false
index.put("s1", 2)
index.put("s1", 3)
index.put("s2", 4)
assert(index.valueIterator("s1").toSet === Set(1, 2, 3))
assert(index.valueIterator("s2").toSet === Set(4))
index.valueIterator("s1").toSet must be === Set(1, 2, 3)
index.valueIterator("s2").toSet must be === Set(4)
}
"remove values" in {
val index = emptyIndex
@ -46,16 +48,16 @@ class IndexSpec extends WordSpec with MustMatchers {
index.put("s2", 1)
index.put("s2", 2)
//Remove value
assert(index.remove("s1", 1))
assert(index.remove("s1", 1) === false)
assert(index.valueIterator("s1").toSet === Set(2))
index.remove("s1", 1) must be === true
index.remove("s1", 1) must be === false
index.valueIterator("s1").toSet must be === Set(2)
//Remove key
index.remove("s2") match {
case Some(iter) assert(iter.toSet === Set(1, 2))
case Some(iter) iter.toSet must be === Set(1, 2)
case None fail()
}
assert(index.remove("s2") === None)
assert(index.valueIterator("s2").toSet === Set())
index.remove("s2") must be === None
index.valueIterator("s2").toSet must be === Set()
}
"remove the specified value" in {
val index = emptyIndex
@ -67,9 +69,9 @@ class IndexSpec extends WordSpec with MustMatchers {
index.put("s3", 2)
index.removeValue(1)
assert(index.valueIterator("s1").toSet === Set(2, 3))
assert(index.valueIterator("s2").toSet === Set(2))
assert(index.valueIterator("s3").toSet === Set(2))
index.valueIterator("s1").toSet must be === Set(2, 3)
index.valueIterator("s2").toSet must be === Set(2)
index.valueIterator("s3").toSet must be === Set(2)
}
"apply a function for all key-value pairs and find every value" in {
val index = indexWithValues
@ -77,14 +79,58 @@ class IndexSpec extends WordSpec with MustMatchers {
var valueCount = 0
index.foreach((key, value) {
valueCount = valueCount + 1
assert(index.findValue(key)(_ == value) === Some(value))
index.findValue(key)(_ == value) must be === Some(value)
})
assert(valueCount === 6)
valueCount must be === 6
}
"be cleared" in {
val index = indexWithValues
index.isEmpty must be === false
index.clear()
assert(index.isEmpty)
index.isEmpty must be === true
}
"be able to be accessed in parallel" in {
val index = new Index[Int, Int](100, (a, b) a.compareTo(b))
val iterations = 500
val latch = new CountDownLatch(List(100, 50, 2, 100).map(_ * iterations).fold(0)(_ + _))
//Fill
for (key 1 to 10; value 1 to 10)
index.put(key, value)
//Perform operations in parallell
for (_ 1 to iterations) {
//Put actors
actor {
for (key 1 to 10; value 1 to 10)
actor {
index.put(key, value)
latch.countDown() //100
}
}
//Remove actors
actor {
for (key 1 to 10; value 1 to 5)
actor {
index.remove(key, value)
latch.countDown() //50
}
for (key 9 to 10)
actor {
index.remove(key)
latch.countDown() //2
}
}
//Read actors
actor {
for (key 1 to 10; value 1 to 10)
actor {
val values = index.valueIterator(key)
if (key < 9 && value > 5)
(values contains value) must be === true
latch.countDown() //100
}
}
}
latch.await()
}
}
}