aboutsummaryrefslogtreecommitdiff
path: root/sync/src/test/scala/akka/serial/sync/SerialConnectionSpec.scala
blob: 24069d7264966391d1bde42e5f8da601af08226f (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package akka.serial
package sync

import java.nio.ByteBuffer
import org.scalatest._

class SerialConnectionSpec extends WordSpec with PseudoTerminal {

  def withEchoConnection[A](action: SerialConnection => A): A = {
    withEcho { (port, settings) =>
      val connection = SerialConnection.open(port, settings)
      try {
        action(connection)
      } finally {
        connection.close()
      }
    }
  }

  "A SerialConnection" should {

    "open a valid port" in {
      withEcho { (port, settings) =>
        SerialConnection.open(port, settings)
      }
    }

    "throw an exception on an invalid port" in {
      val settings = SerialSettings(baud = 115200)
      intercept[NoSuchPortException] {
        SerialConnection.open("/dev/nonexistant", settings)
      }
    }

    "read the same data it writes to an echo pty" in {
      withEchoConnection { conn =>
        /* Note: this test assumes that all data will be written and read
         * within single write and read calls. This in turn assumes that
         * internal operating system buffers have enough capacity to
         * store all data. */
        val bufferSize = 64

        val outString = "hello world"
        val outBuffer = ByteBuffer.allocateDirect(bufferSize)
        val outData = outString.getBytes
        outBuffer.put(outData)
        conn.write(outBuffer)

        val inBuffer = ByteBuffer.allocateDirect(bufferSize)
        conn.read(inBuffer)
        val inData = new Array[Byte](inBuffer.remaining())
        inBuffer.get(inData)
        val inString = new String(inData)

        assert(inString == outString)
      }
    }

    "interrupt a read when closing a port" in {
      withEchoConnection { conn =>
        val buffer = ByteBuffer.allocateDirect(64)

        val closer = new Thread {
          override def run(): Unit = {
            Thread.sleep(100)
            conn.close()
          }
        }
        closer.start()
        intercept[PortInterruptedException]{
          conn.read(buffer)
        }
        closer.join()
      }
    }

    "throw an exception when reading from a closed port" in {
      withEchoConnection { conn =>
        val buffer = ByteBuffer.allocateDirect(64)
        conn.close()

        intercept[PortClosedException]{
          conn.read(buffer)
        }
      }
    }

    "throw an exception when writing to a closed port" in {
      withEchoConnection { conn =>
        val buffer = ByteBuffer.allocateDirect(64)
        conn.close()

        intercept[PortClosedException]{
          conn.write(buffer)
        }
      }
    }
        
  }

}