aboutsummaryrefslogtreecommitdiff
path: root/flow-core/src/test/scala/ch/jodersky/flow/SerialOperatorSpec.scala
blob: 4a6cf1e508bb059b6bc6a9ce7967f0978a59a422 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package ch.jodersky.flow

import scala.concurrent.duration._

import akka.actor.{ActorRef, ActorSystem}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.ByteString
import org.scalatest._

case class Ack(n: Int) extends Serial.Event

class SerialOperatorSpec
    extends TestKit(ActorSystem("serial-operator"))
    with ImplicitSender
    with WordSpecLike
    with Matchers
    with BeforeAndAfterAll
    with SequentialNestedSuiteExecution
    with PseudoTerminal {

  override def afterAll {
    TestKit.shutdownActorSystem(system)
  }

  def withEchoOp[A](action: ActorRef => A): A = {
    withEcho { case (port, settings) =>
      val connection = SerialConnection.open(port, settings)
      val operator = system.actorOf(SerialOperator.apply(connection, 1024, testActor))
      action(operator)
    }
  }

  "Serial operator" should {

    "follow the correct protocol" in withEchoOp { op =>
      expectMsgType[Serial.Opened]

      val data = ByteString("hello world".getBytes("utf-8"))
      op ! Serial.Write(data)
      expectMsg(Serial.Received(data))

      op ! Serial.Write(data, n => Ack(n))
      expectMsg(Serial.Received(data))
      expectMsg(Ack(data.length))

      op ! Serial.Close
      expectMsg(Serial.Closed)

    }

  }

}