Richard Searle's Blog

Thoughts about software

Testing akka system IO

Posted by eggsearle on February 25, 2012

All code needs tests and the contents of the previous post are no exception

The tests are surprisingly easy to define and certainly help to clarify how the implementation incrementally consumes the input as it becomes available.

import org.junit._
import Assert._
import akka.util.{ ByteString, ByteStringBuilder }
class LengthBoundedTest {
 import IO._
 def one {
 val i1 = LengthBoundedServer.readMessage(Chunk(ByteString("0001A")))
 assertEquals((Done("A"), Chunk(ByteString())), i1)
 assertEquals("A", i1._1.get)
 def two {
 val i1 = LengthBoundedServer.readMessage(Chunk(ByteString("0002AB")))
 assertEquals((Done("AB"), Chunk(ByteString())), i1)
 assertEquals("AB", i1._1.get)
 def oneTwo {
 val i1 = LengthBoundedServer.readMessage(Chunk(ByteString("0001A0002AB")))
 assertEquals((Done("A"), Chunk(ByteString(48, 48, 48, 50, 65, 66))), i1)
 assertEquals("A", i1._1.get)
 def onePartial {
 val i1 = LengthBoundedServer.readMessage(Chunk(ByteString("0001")))
 assertEquals("(Cont(<function1>,None),Chunk(ByteString()))", i1.toString)
 assertEquals("", i1._1.get)
 val i2 = i1._1(Chunk(ByteString("A")))
 assertEquals((Done("A"), Chunk(ByteString())), i2)
 assertEquals("A", i2._1.get)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: