pekko/akka-stream-tests/src/test/scala/akka/stream/scaladsl/QueueSinkSpec.scala

117 lines
3.5 KiB
Scala
Raw Normal View History

/**
* Copyright (C) 2015-2016 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.stream.scaladsl
import akka.actor.Status
2015-12-04 09:37:32 -05:00
import akka.pattern.pipe
2016-01-16 12:17:19 -05:00
import akka.stream.{ OverflowStrategy, ActorMaterializer }
import akka.stream.testkit.Utils._
import akka.stream.testkit.{ AkkaSpec, _ }
2015-12-04 09:37:32 -05:00
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.util.control.NoStackTrace
2015-12-04 09:37:32 -05:00
class QueueSinkSpec extends AkkaSpec {
implicit val ec = system.dispatcher
implicit val materializer = ActorMaterializer()
val ex = new RuntimeException("ex") with NoStackTrace
val noMsgTimeout = 300.millis
2015-12-04 09:37:32 -05:00
"An QueueSinkSpec" must {
"send the elements as result of future" in assertAllStagesStopped {
val expected = List(Some(1), Some(2), Some(3), None)
2015-12-04 09:37:32 -05:00
val queue = Source(expected.flatten).runWith(Sink.queue())
expected foreach { v
queue.pull() pipeTo testActor
expectMsg(v)
}
}
"allow to have only one future waiting for result in each point of time" in assertAllStagesStopped {
val probe = TestPublisher.manualProbe[Int]()
val queue = Source.fromPublisher(probe).runWith(Sink.queue())
val sub = probe.expectSubscription()
val future = queue.pull()
val future2 = queue.pull()
an[IllegalStateException] shouldBe thrownBy { Await.result(future2, 300.millis) }
sub.sendNext(1)
future.pipeTo(testActor)
expectMsg(Some(1))
sub.sendComplete()
2015-12-04 09:37:32 -05:00
queue.pull()
}
"wait for next element from upstream" in assertAllStagesStopped {
val probe = TestPublisher.manualProbe[Int]()
val queue = Source.fromPublisher(probe).runWith(Sink.queue())
val sub = probe.expectSubscription()
queue.pull().pipeTo(testActor)
expectNoMsg(noMsgTimeout)
sub.sendNext(1)
expectMsg(Some(1))
sub.sendComplete()
2015-12-04 09:37:32 -05:00
queue.pull()
}
"fail future on stream failure" in assertAllStagesStopped {
val probe = TestPublisher.manualProbe[Int]()
val queue = Source.fromPublisher(probe).runWith(Sink.queue())
val sub = probe.expectSubscription()
queue.pull().pipeTo(testActor)
expectNoMsg(noMsgTimeout)
sub.sendError(ex)
expectMsg(Status.Failure(ex))
}
"fail future when stream failed" in assertAllStagesStopped {
val probe = TestPublisher.manualProbe[Int]()
val queue = Source.fromPublisher(probe).runWith(Sink.queue())
val sub = probe.expectSubscription()
2015-12-04 09:37:32 -05:00
sub.sendError(ex)
2015-12-04 09:37:32 -05:00
the[Exception] thrownBy { Await.result(queue.pull(), 300.millis) } should be(ex)
}
"timeout future when stream cannot provide data" in assertAllStagesStopped {
val probe = TestPublisher.manualProbe[Int]()
val queue = Source.fromPublisher(probe).runWith(Sink.queue())
val sub = probe.expectSubscription()
queue.pull().pipeTo(testActor)
expectNoMsg(noMsgTimeout)
sub.sendNext(1)
expectMsg(Some(1))
sub.sendComplete()
2015-12-04 09:37:32 -05:00
queue.pull()
}
2016-01-16 12:17:19 -05:00
"fail pull future when stream is completed" in assertAllStagesStopped {
val probe = TestPublisher.manualProbe[Int]()
val queue = Source.fromPublisher(probe).runWith(Sink.queue())
val sub = probe.expectSubscription()
queue.pull().pipeTo(testActor)
sub.sendNext(1)
expectMsg(Some(1))
sub.sendComplete()
Await.result(queue.pull(), noMsgTimeout) should be(None)
queue.pull().onFailure { case e e.isInstanceOf[IllegalStateException] should ===(true) }
}
}
}