ScaleChain / scalechain

A customizable blockchain for altcoins.
236 stars 65 forks source link

Add test cases for classes and actors in Net layer. #27

Open Kangmo opened 8 years ago

Kangmo commented 8 years ago

What

Add test cases for classes and actors in Net layer.

Why

Need to make sure each class and actor works well before we start integration tests. (Without unit tests, we will take much time for debugging each class and actor in the net layer.)

How

Use Akka Actor testkit and scalatest. See http://doc.akka.io/docs/akka/snapshot/scala/testing.html

Also use Akka Streams testkit. http://doc.akka.io/docs/akka/2.4.2/scala/stream/stream-testkit.html

Kangmo commented 8 years ago

Will use probe actors instead of creating mockups.

import scala.concurrent.duration._
import scala.concurrent.Future
import akka.actor._
import akka.testkit.TestProbe

class MyDoubleEcho extends Actor {
  var dest1: ActorRef = _
  var dest2: ActorRef = _
  def receive = {
    case (d1: ActorRef, d2: ActorRef) =>
      dest1 = d1
      dest2 = d2
    case x =>
      dest1 ! x
      dest2 ! x
  }
}

val probe1 = TestProbe()
val probe2 = TestProbe()
val actor = system.actorOf(Props[MyDoubleEcho])
actor ! ((probe1.ref, probe2.ref))
actor ! "hello"
probe1.expectMsg(500 millis, "hello")
probe2.expectMsg(500 millis, "hello")
Kangmo commented 1 year ago

Actors are not used anymore, but still needs to test classes in the net layer.