Richard Searle

home

Testing akka system IO

25 Feb 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.actor._
import akka.util.{ ByteString, ByteStringBuilder }
class LengthBoundedTest {
 import IO._
@Test
 def one {
 val i1 = LengthBoundedServer.readMessage(Chunk(ByteString("0001A")))
 assertEquals((Done("A"), Chunk(ByteString())), i1)
 assertEquals("A", i1._1.get)
 }
@Test
 def two {
 val i1 = LengthBoundedServer.readMessage(Chunk(ByteString("0002AB")))
 assertEquals((Done("AB"), Chunk(ByteString())), i1)
 assertEquals("AB", i1._1.get)
 }
@Test
 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)
 }
@Test
 def onePartial {
 val i1 = LengthBoundedServer.readMessage(Chunk(ByteString("0001")))
 assertEquals("(Cont(,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)
 }
}