diff options
author | Jakob Odersky <jakob@odersky.com> | 2017-01-08 21:16:25 +0100 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2017-01-21 17:22:10 -0800 |
commit | 23959966760174477a6b0fcbf9dd1e8ef37c643b (patch) | |
tree | 9a0ee44eb43a8c13af57b0d06313f3aabf9e4555 /sync/src/test/scala/akka/serial/sync/SerialConnectionSpec.scala | |
parent | 6c371ba6d69c891c1f0d6df00bb643e1d543cc9d (diff) | |
download | akka-serial-23959966760174477a6b0fcbf9dd1e8ef37c643b.tar.gz akka-serial-23959966760174477a6b0fcbf9dd1e8ef37c643b.tar.bz2 akka-serial-23959966760174477a6b0fcbf9dd1e8ef37c643b.zip |
Rename project to akka-serial
Diffstat (limited to 'sync/src/test/scala/akka/serial/sync/SerialConnectionSpec.scala')
-rw-r--r-- | sync/src/test/scala/akka/serial/sync/SerialConnectionSpec.scala | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/sync/src/test/scala/akka/serial/sync/SerialConnectionSpec.scala b/sync/src/test/scala/akka/serial/sync/SerialConnectionSpec.scala new file mode 100644 index 0000000..24069d7 --- /dev/null +++ b/sync/src/test/scala/akka/serial/sync/SerialConnectionSpec.scala @@ -0,0 +1,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) + } + } + } + + } + +} |