Skip to content
This repository

Adding more tests to MailboxSpec so that we know they follow the contrac... #1303

Merged
merged 1 commit into from about 1 year ago

3 participants

Viktor Klang (√) akka-ci Patrik Nordwall
Viktor Klang (√)
Owner

...t ;)

akka-ci
Collaborator

Started jenkins job akka-pr-validator at https://jenkins.akka.io/job/akka-pr-validator/807/

akka-ci
Collaborator

jenkins job akka-pr-validator: Success - https://jenkins.akka.io/job/akka-pr-validator/807/

Patrik Nordwall patriknw commented on the diff April 05, 2013
akka-actor-tests/src/test/scala/akka/dispatch/MailboxConfigSpec.scala
@@ -23,15 +23,22 @@ abstract class MailboxSpec extends AkkaSpec with BeforeAndAfterAll with BeforeAn
23 23
 
24 24
   def maxConsumers = 4
25 25
 
  26
+  private val exampleMessage = createMessageInvocation("test")
  27
+
26 28
   name should {
1
Patrik Nordwall Collaborator
patriknw added a note April 05, 2013

should? must!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Patrik Nordwall
Collaborator

LGTM

Viktor Klang (√) viktorklang merged commit 785c7d2 into from April 06, 2013
Viktor Klang (√) viktorklang closed this April 06, 2013
Viktor Klang (√) viktorklang deleted the branch April 06, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 05, 2013
Viktor Klang (√) Adding more tests to MailboxSpec so that we know they follow the cont…
…ract ;)
51d3149
This page is out of date. Refresh to see the latest.
58  akka-actor-tests/src/test/scala/akka/dispatch/MailboxConfigSpec.scala
@@ -10,7 +10,7 @@ import org.scalatest.{ BeforeAndAfterEach, BeforeAndAfterAll }
10 10
 import com.typesafe.config.Config
11 11
 import akka.actor.{ RepointableRef, Props, DeadLetter, ActorSystem, ActorRefWithCell, ActorRef, ActorCell }
12 12
 import akka.testkit.AkkaSpec
13  
-import scala.concurrent.{ Future, Promise, Await }
  13
+import scala.concurrent.{ Future, Promise, Await, ExecutionContext }
14 14
 import scala.concurrent.duration._
15 15
 
16 16
 @org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
@@ -23,15 +23,22 @@ abstract class MailboxSpec extends AkkaSpec with BeforeAndAfterAll with BeforeAn
23 23
 
24 24
   def maxConsumers = 4
25 25
 
  26
+  private val exampleMessage = createMessageInvocation("test")
  27
+
26 28
   name should {
  29
+
27 30
     "create an unbounded mailbox" in {
28 31
       val config = UnboundedMailbox()
29 32
       val q = factory(config)
30 33
       ensureInitialMailboxState(config, q)
  34
+    }
31 35
 
32  
-      val f = spawn { q.dequeue }
  36
+    "UnboundedMailbox.numberOfMessages must be consistent with queue size" in {
  37
+      ensureSingleConsumerEnqueueDequeue(UnboundedMailbox())
  38
+    }
33 39
 
34  
-      Await.result(f, 1 second) must be(null)
  40
+    "BoundedMailbox.numberOfMessages must be consistent with queue size" in {
  41
+      ensureSingleConsumerEnqueueDequeue(BoundedMailbox(1000, 10 milliseconds))
35 42
     }
36 43
 
37 44
     "create a bounded mailbox with 10 capacity and with push timeout" in {
@@ -39,9 +46,7 @@ abstract class MailboxSpec extends AkkaSpec with BeforeAndAfterAll with BeforeAn
39 46
       val q = factory(config)
40 47
       ensureInitialMailboxState(config, q)
41 48
 
42  
-      val exampleMessage = createMessageInvocation("test")
43  
-
44  
-      for (i ← 1 to config.capacity) q.enqueue(null, exampleMessage)
  49
+      for (i ← 1 to config.capacity) q.enqueue(testActor, exampleMessage)
45 50
 
46 51
       q.numberOfMessages must be === config.capacity
47 52
       q.hasMessages must be === true
@@ -70,21 +75,38 @@ abstract class MailboxSpec extends AkkaSpec with BeforeAndAfterAll with BeforeAn
70 75
   }
71 76
 
72 77
   //CANDIDATE FOR TESTKIT
73  
-  def spawn[T <: AnyRef](fun: ⇒ T): Future[T] = {
74  
-    val result = Promise[T]()
75  
-    val t = new Thread(new Runnable {
76  
-      def run = try {
77  
-        result.success(fun)
78  
-      } catch {
79  
-        case e: Throwable ⇒ result.failure(e)
80  
-      }
81  
-    })
82  
-    t.start
83  
-    result.future
84  
-  }
  78
+  def spawn[T <: AnyRef](fun: ⇒ T): Future[T] = Future(fun)(ExecutionContext.global)
85 79
 
86 80
   def createMessageInvocation(msg: Any): Envelope = Envelope(msg, system.deadLetters, system)
87 81
 
  82
+  def ensureMailboxSize(q: MessageQueue, expected: Int): Unit = q.numberOfMessages match {
  83
+    case -1 | `expected` ⇒
  84
+      q.hasMessages must be === (expected != 0)
  85
+    case other ⇒
  86
+      other must be === expected
  87
+      q.hasMessages must be === (expected != 0)
  88
+  }
  89
+
  90
+  def ensureSingleConsumerEnqueueDequeue(config: MailboxType) {
  91
+    val q = factory(config)
  92
+    ensureMailboxSize(q, 0)
  93
+    q.dequeue must be === null
  94
+    for (i ← 1 to 100) {
  95
+      q.enqueue(testActor, exampleMessage)
  96
+      ensureMailboxSize(q, i)
  97
+    }
  98
+
  99
+    ensureMailboxSize(q, 100)
  100
+
  101
+    for (i ← 99 to 0 by -1) {
  102
+      q.dequeue() must be === exampleMessage
  103
+      ensureMailboxSize(q, i)
  104
+    }
  105
+
  106
+    q.dequeue must be === null
  107
+    ensureMailboxSize(q, 0)
  108
+  }
  109
+
88 110
   def ensureInitialMailboxState(config: MailboxType, q: MessageQueue) {
89 111
     q must not be null
90 112
     q match {
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.