2015-09-17 12:57:07 +02:00
|
|
|
/**
|
2016-01-25 10:16:14 +01:00
|
|
|
* Copyright (C) 2015-2016 Typesafe Inc. <http://www.typesafe.com>
|
2015-09-17 12:57:07 +02:00
|
|
|
*/
|
|
|
|
|
package akka.stream.testkit
|
|
|
|
|
|
|
|
|
|
import akka.stream._
|
|
|
|
|
import akka.stream.scaladsl.Source
|
|
|
|
|
import akka.stream.testkit.scaladsl.TestSink
|
|
|
|
|
|
|
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
|
|
|
|
|
class StreamTestKitSpec extends AkkaSpec {
|
|
|
|
|
|
2015-12-11 14:45:24 +01:00
|
|
|
implicit val materializer = ActorMaterializer()
|
2015-09-17 12:57:07 +02:00
|
|
|
|
|
|
|
|
val ex = new Exception("Boom!")
|
|
|
|
|
|
|
|
|
|
"A TestSink Probe" must {
|
|
|
|
|
"#toStrict" in {
|
2015-09-17 13:10:09 +02:00
|
|
|
Source(1 to 4).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
.toStrict(300.millis) should ===(List(1, 2, 3, 4))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"#toStrict with failing source" in {
|
2016-01-16 17:54:39 +01:00
|
|
|
val error = intercept[AssertionError] {
|
2015-12-17 11:48:30 +02:00
|
|
|
Source.fromIterator(() ⇒ new Iterator[Int] {
|
2015-09-17 12:57:07 +02:00
|
|
|
var i = 0
|
|
|
|
|
override def hasNext: Boolean = true
|
|
|
|
|
override def next(): Int = {
|
|
|
|
|
i += 1
|
|
|
|
|
i match {
|
|
|
|
|
case 3 ⇒ throw ex
|
|
|
|
|
case n ⇒ n
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-17 13:10:09 +02:00
|
|
|
}).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
.toStrict(300.millis)
|
2016-01-16 17:54:39 +01:00
|
|
|
}
|
2015-09-17 12:57:07 +02:00
|
|
|
|
2016-01-16 17:54:39 +01:00
|
|
|
error.getCause.getMessage should include("Boom!")
|
|
|
|
|
error.getMessage should include("List(1, 2)")
|
2015-09-17 12:57:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"#toStrict when subscription was already obtained" in {
|
2015-09-17 13:10:09 +02:00
|
|
|
val p = Source(1 to 4).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
p.expectSubscription()
|
|
|
|
|
p.toStrict(300.millis) should ===(List(1, 2, 3, 4))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"#expectNextOrError with right element" in {
|
2015-09-17 13:10:09 +02:00
|
|
|
Source(1 to 4).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
.request(4)
|
|
|
|
|
.expectNextOrError(1, ex)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"#expectNextOrError with right exception" in {
|
2015-09-17 13:10:09 +02:00
|
|
|
Source.failed[Int](ex).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
.request(4)
|
|
|
|
|
.expectNextOrError(1, ex)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"#expectNextOrError fail if the next element is not the expected one" in {
|
|
|
|
|
intercept[AssertionError] {
|
2015-09-17 13:10:09 +02:00
|
|
|
Source(1 to 4).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
.request(4)
|
|
|
|
|
.expectNextOrError(100, ex)
|
|
|
|
|
}.getMessage should include("OnNext(1)")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"#expectError" in {
|
2015-09-17 13:10:09 +02:00
|
|
|
Source.failed[Int](ex).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
.request(1)
|
|
|
|
|
.expectError() should ===(ex)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"#expectError fail if no error signalled" in {
|
|
|
|
|
intercept[AssertionError] {
|
2015-09-17 13:10:09 +02:00
|
|
|
Source(1 to 4).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
.request(1)
|
|
|
|
|
.expectError()
|
|
|
|
|
}.getMessage should include("OnNext")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"#expectComplete should fail if error signalled" in {
|
|
|
|
|
intercept[AssertionError] {
|
2015-09-17 13:10:09 +02:00
|
|
|
Source.failed[Int](ex).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
.request(1)
|
|
|
|
|
.expectComplete()
|
|
|
|
|
}.getMessage should include("OnError")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"#expectComplete should fail if next element signalled" in {
|
|
|
|
|
intercept[AssertionError] {
|
2015-09-17 13:10:09 +02:00
|
|
|
Source(1 to 4).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
.request(1)
|
|
|
|
|
.expectComplete()
|
|
|
|
|
}.getMessage should include("OnNext")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"#expectNextOrComplete with right element" in {
|
2015-09-17 13:10:09 +02:00
|
|
|
Source(1 to 4).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
.request(4)
|
|
|
|
|
.expectNextOrComplete(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"#expectNextOrComplete with completion" in {
|
2015-09-17 13:10:09 +02:00
|
|
|
Source.single(1).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
.request(4)
|
|
|
|
|
.expectNextOrComplete(1)
|
|
|
|
|
.expectNextOrComplete(1337)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"#expectNextN given a number of elements" in {
|
2015-09-17 13:10:09 +02:00
|
|
|
Source(1 to 4).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
.request(4)
|
|
|
|
|
.expectNextN(4) should ===(List(1, 2, 3, 4))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"#expectNextN given specific elements" in {
|
2015-09-17 13:10:09 +02:00
|
|
|
Source(1 to 4).runWith(TestSink.probe)
|
2015-09-17 12:57:07 +02:00
|
|
|
.request(4)
|
|
|
|
|
.expectNextN(4) should ===(List(1, 2, 3, 4))
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-17 13:10:09 +02:00
|
|
|
}
|