本文整理汇总了Scala中akka.testkit.ImplicitSender类的典型用法代码示例。如果您正苦于以下问题:Scala ImplicitSender类的具体用法?Scala ImplicitSender怎么用?Scala ImplicitSender使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ImplicitSender类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: AbstractActorSpec
//设置package包名称以及导入依赖的类
package name.mikulskibartosz.demo.akka.actors
import akka.actor.ActorSystem
import akka.testkit.{ImplicitSender, DefaultTimeout, TestKit}
import name.mikulskibartosz.demo.akka.service.Service
import org.mockito.Mockito._
import org.scalatest.mock.MockitoSugar
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}
import scala.util.Try
abstract class AbstractActorSpec extends TestKit(ActorSystem()) with DefaultTimeout with ImplicitSender
with WordSpecLike with Matchers with BeforeAndAfterAll with MockitoSugar {
override def afterAll() = {
super.afterAll()
shutdown()
}
def createServiceReturning(value: Try[Int]) = {
val service = mock[Service]
when(service.generate()).thenReturn(value)
service
}
}
示例2: SnapshotDirectoryFailureSpec
//设置package包名称以及导入依赖的类
package akka.persistence
import akka.testkit.{ ImplicitSender, EventFilter, TestEvent, AkkaSpec }
import java.io.{ IOException, File }
import akka.actor.{ ActorInitializationException, Props, ActorRef }
object SnapshotDirectoryFailureSpec {
val inUseSnapshotPath = "target/inUseSnapshotPath"
class TestProcessor(name: String, probe: ActorRef) extends Processor {
override def persistenceId: String = name
override def preStart(): Unit = ()
def receive = {
case s: String ? saveSnapshot(s)
case SaveSnapshotSuccess(md) ? probe ! md.sequenceNr
case other ? probe ! other
}
}
}
class SnapshotDirectoryFailureSpec extends AkkaSpec(PersistenceSpec.config("leveldb", "SnapshotDirectoryFailureSpec", extraConfig = Some(
s"""
|akka.persistence.snapshot-store.local.dir = "${SnapshotDirectoryFailureSpec.inUseSnapshotPath}"
""".stripMargin))) with ImplicitSender {
import SnapshotDirectoryFailureSpec._
val file = new File(inUseSnapshotPath)
override protected def atStartup() {
if (!file.createNewFile()) throw new IOException(s"Failed to create test file [${file.getCanonicalFile}]")
}
override protected def afterTermination() {
if (!file.delete()) throw new IOException(s"Failed to delete test file [${file.getCanonicalFile}]")
}
"A local snapshot store configured with an failing directory name " must {
"throw an exception at startup" in {
EventFilter[ActorInitializationException](occurrences = 1).intercept {
val processor = system.actorOf(Props(classOf[TestProcessor], "SnapshotDirectoryFailureSpec-1", testActor))
processor ! "blahonga"
}
}
}
}
示例3: MySpec
//设置package包名称以及导入依赖的类
package docs.testkit
//#plain-spec
import akka.actor.ActorSystem
import akka.actor.Actor
import akka.actor.Props
import akka.testkit.{ TestActors, TestKit, ImplicitSender }
import org.scalatest.WordSpecLike
import org.scalatest.Matchers
import org.scalatest.BeforeAndAfterAll
//#implicit-sender
class MySpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender
with WordSpecLike with Matchers with BeforeAndAfterAll {
//#implicit-sender
def this() = this(ActorSystem("MySpec"))
override def afterAll {
TestKit.shutdownActorSystem(system)
}
"An Echo actor" must {
"send back messages unchanged" in {
val echo = system.actorOf(TestActors.echoActorProps)
echo ! "hello world"
expectMsg("hello world")
}
}
}
//#plain-spec
示例4: ControllerSpec
//设置package包名称以及导入依赖的类
package akka.remote.testconductor
import akka.testkit.AkkaSpec
import akka.actor.{ PoisonPill, Props, AddressFromURIString }
import akka.testkit.ImplicitSender
import akka.remote.testconductor.Controller.NodeInfo
import java.net.InetSocketAddress
import java.net.InetAddress
object ControllerSpec {
val config = """
akka.testconductor.barrier-timeout = 5s
akka.actor.provider = akka.remote.RemoteActorRefProvider
akka.actor.debug.fsm = on
akka.actor.debug.lifecycle = on
"""
}
class ControllerSpec extends AkkaSpec(ControllerSpec.config) with ImplicitSender {
val A = RoleName("a")
val B = RoleName("b")
"A Controller" must {
"publish its nodes" in {
val c = system.actorOf(Props(classOf[Controller], 1, new InetSocketAddress(InetAddress.getLocalHost, 0)))
c ! NodeInfo(A, AddressFromURIString("akka://sys"), testActor)
expectMsg(ToClient(Done))
c ! NodeInfo(B, AddressFromURIString("akka://sys"), testActor)
expectMsg(ToClient(Done))
c ! Controller.GetNodes
expectMsgType[Iterable[RoleName]].toSet should be(Set(A, B))
c ! PoisonPill // clean up so network connections don't accumulate during test run
}
}
}
示例5: ARouterTestSuite
//设置package包名称以及导入依赖的类
package service.a.core.actors
import akka.testkit.TestKit
import org.scalatest.SpecLike
import akka.testkit.ImplicitSender
import akka.actor.Props
import akka.actor.ActorSystem
class ARouterTestSuite extends TestKit(ActorSystem()) with SpecLike with ImplicitSender {
import ARouter._
object `An ARouter` {
object `when being send a random message` {
def `replies with a "Not yet implemented" message` {
val aRouter = system.actorOf(Props[ARouter])
aRouter ! "Find the answer to everything"
expectMsg("Not yet implemented")
}
}
}
}
示例6: WireTransferSpec
//设置package包名称以及导入依赖的类
package akka
import BankAccount.{BankAccount, WireTransfer}
import akka.actor.{ActorSystem, Props}
import akka.testkit.{ImplicitSender, TestKit, TestProbe}
import org.scalatest._
class WireTransferSpec(_system: ActorSystem) extends TestKit(_system)
with FlatSpecLike
with ImplicitSender
with Matchers
with BeforeAndAfterAll {
def this() = this(ActorSystem("WireTransferSpec"))
override def afterAll {
TestKit.shutdownActorSystem(system)
}
it should "transfer money from A to B in case of sufficient funds on A" in {
val accountA = TestProbe()
val accountB = TestProbe()
val transfer = system.actorOf(Props[WireTransfer])
transfer ! WireTransfer.Transfer(accountA.ref, accountB.ref, 20)
accountA.expectMsg(BankAccount.Withdraw(20))
accountA.reply(BankAccount.Done)
accountB.expectMsg(BankAccount.Deposit(20))
accountB.reply(BankAccount.Done)
expectMsg(WireTransfer.Done)
}
it should "not transfer money from A to B in case of insufficient funds on A" in {
val accountA = TestProbe()
val accountB = TestProbe()
val transfer = system.actorOf(Props[WireTransfer])
transfer ! WireTransfer.Transfer(accountA.ref, accountB.ref, 20)
accountA.expectMsg(BankAccount.Withdraw(20))
accountA.reply(BankAccount.Failed)
expectMsg(WireTransfer.Failed)
}
}
示例7: BankAccountSpec
//设置package包名称以及导入依赖的类
package akka
import BankAccount.BankAccount
import akka.actor.{ActorRef, ActorSystem, Props}
import akka.testkit.{ImplicitSender, TestKit}
import org.scalatest._
class BankAccountSpec(_system: ActorSystem) extends TestKit(_system)
with FlatSpecLike
with ImplicitSender
with Matchers
with BeforeAndAfterAll {
def this() = this(ActorSystem("BankAccountSpec"))
override def afterAll {
TestKit.shutdownActorSystem(system)
}
it should "has 0 balance by default" in {
val bankAccount = system.actorOf(Props[BankAccount])
bankAccount ! BankAccount.GetBalance
expectMsg(BankAccount.Balance(0))
}
it should "has deposit amount as balance" in {
val bankAccount = system.actorOf(Props[BankAccount])
deposit(bankAccount, 10)
bankAccount ! BankAccount.GetBalance
expectMsg(BankAccount.Balance(10))
}
it should "has reduced balance after withdraw in case of sufficient funds" in {
val bankAccount = system.actorOf(Props[BankAccount])
deposit(bankAccount, 10)
bankAccount ! BankAccount.Withdraw(5)
expectMsg(BankAccount.Done)
bankAccount ! BankAccount.GetBalance
expectMsg(BankAccount.Balance(5))
}
it should "should fail withdraw in case of insufficient funds" in {
val bankAccount = system.actorOf(Props[BankAccount])
bankAccount ! BankAccount.Withdraw(5)
expectMsg(BankAccount.Failed)
}
def deposit(bankAccount: ActorRef, amount: Int) = {
bankAccount ! BankAccount.Deposit(amount)
expectMsg(BankAccount.Done)
}
}
示例8: aultPatience
//设置package包名称以及导入依赖的类
import akka.actor.ActorSystem
import akka.stream.{ActorMaterializer, ActorMaterializerSettings, Supervision}
import akka.testkit.{ImplicitSender, TestKit, TestKitBase}
import com.amazonaws.services.sqs.AmazonSQSAsync
import com.taxis99.amazon.sqs.SqsClientFactory
import com.typesafe.config.ConfigFactory
import org.scalatest._
import org.scalatest.concurrent.PatienceConfiguration
import org.scalatest.time._
import scala.concurrent.{ExecutionContext, Future}
package object test {
trait BaseSpec extends FlatSpec with Matchers with OptionValues with PatienceConfiguration with RecoverMethods {
implicit val defaultPatience =
PatienceConfig(timeout = Span(3, Seconds), interval = Span(5, Millis))
}
trait StreamSpec extends AsyncFlatSpec with Matchers with OptionValues with PatienceConfiguration
with TestKitBase with ImplicitSender with BeforeAndAfterAll {
implicit lazy val system: ActorSystem = ActorSystem("test", ConfigFactory.parseString("""
akka.actor.deployment.default.dispatcher = "akka.test.calling-thread-dispatcher"
"""))
override implicit def executionContext: ExecutionContext = system.dispatcher
override implicit def patienceConfig = PatienceConfig(timeout = Span(1, Minute), interval = Span(5, Millis))
val decider: Supervision.Decider = {
case _ => Supervision.Stop
}
val settings = ActorMaterializerSettings(system).withSupervisionStrategy(decider)
implicit lazy val materializer = ActorMaterializer(settings)
def withInMemoryQueue(testCode: (AmazonSQSAsync) => Future[Assertion]): Future[Assertion] = {
val (server, aws) = SqsClientFactory.inMemory(Some(system))
// "loan" the fixture to the test
testCode(aws) andThen {
case _ => server.stopAndWait()
}
}
override def afterAll {
TestKit.shutdownActorSystem(system)
}
}
}
示例9: ReaperUTest
//设置package包名称以及导入依赖的类
package com.example.util
import akka.actor.{ActorSystem, Props}
import akka.testkit.{ImplicitSender, TestKit, TestProbe}
import com.example.StopSystemAfterAll
import org.scalatest.{FlatSpecLike, ShouldMatchers}
class ReaperUTest
extends TestKit(ActorSystem("testsystem"))
with FlatSpecLike
with ShouldMatchers
with StopSystemAfterAll
with ImplicitSender {
import Reaper._
override def afterAll(): Unit = {
system.shutdown()
super.afterAll()
}
trait ReaperFixture {
val reaper = system.actorOf(Props(new TestReaper(testActor)))
val actor1 = TestProbe()
val actor2 = TestProbe()
val actor3 = TestProbe()
val actor4 = TestProbe()
}
"The Reaper" should "call the allSoulsReaped method after all watched actors are dead" in new ReaperFixture {
reaper ! WatchMe(actor3.ref)
reaper ! WatchMe(actor1.ref)
system.stop(actor1.ref)
system.stop(actor3.ref)
expectMsg("Dead")
}
it should "fail to call the allSoulsReaped method if not all watched actors are dead" in new ReaperFixture {
reaper ! WatchMe(actor3.ref)
reaper ! WatchMe(actor1.ref)
system.stop(actor1.ref)
expectNoMsg()
}
}
示例10: UnhandledMessageWatcherSpec
//设置package包名称以及导入依赖的类
package com.tpalanga.newsletter.utils
import akka.actor.{Actor, ActorSystem, Props}
import akka.event.Logging.LogEvent
import akka.testkit.{ImplicitSender, TestKit, TestProbe}
import com.tpalanga.newsletter.util.UnhandledMessageWatcher
import org.scalatest.{FlatSpecLike, Matchers}
object UnhandledMessageWatcherSpec {
abstract class Test(implicit system: ActorSystem) {
val watcher = system.actorOf(UnhandledMessageWatcher.props())
val logProbe = TestProbe()
system.eventStream.subscribe(logProbe.ref, classOf[LogEvent])
val destination = system.actorOf(Props(new Actor {
override def receive: Receive = {
case 'Handled =>
}
}))
}
}
class UnhandledMessageWatcherSpec extends TestKit(ActorSystem("UnhandledMessageWatcherSpec")) with FlatSpecLike with Matchers with ImplicitSender {
import UnhandledMessageWatcherSpec._
"UnhandledMessageWatcher" should "log unhandled messages" in new Test {
destination ! 'Unhandled
val event = logProbe.fishForMessage() {
case akka.event.Logging.Error(_, _, _, msg) if msg.toString startsWith "UnhandledMessage:" =>
true
case _ =>
false
}
}
it should "log DeadLetters" in new Test {
system.stop(destination)
Thread.sleep(100)
destination ! 'Handled
val event = logProbe.fishForMessage() {
case akka.event.Logging.Warning(_, _, msg) if msg.toString startsWith "DeadLetter:" =>
true
case _ =>
false
}
}
}
示例11: AmazonFineFoodManagerSpec
//设置package包名称以及导入依赖的类
package com.taintech.aff.actor
import akka.actor.ActorSystem
import akka.testkit.{ImplicitSender, TestKit}
import com.taintech.aff.actor.AmazonFineFoodManager.{Review, ReviewParser}
import org.scalatest.{BeforeAndAfterAll, FlatSpecLike, MustMatchers}
class AmazonFineFoodManagerSpec extends TestKit(ActorSystem("test-system"))
with FlatSpecLike
with ImplicitSender
with BeforeAndAfterAll
with MustMatchers {
import AmazonFineFoodManagerSpec._
"ReviewParser" should "parse test line with values test" in {
val rp = new ReviewParser()
rp.parse(testLine) must equal(Review("test3", "test2", "test10"))
}
}
object AmazonFineFoodManagerSpec {
val testLine = "test1,test2,test3,test4,test5,test6,test7,test8,test9,test10"
val mockReview: String => Review = _ => Review("test user", "test product", "test text")
}
示例12: GGG
//设置package包名称以及导入依赖的类
package dgl
import akka.actor.ActorSystem
import akka.actor.Props
import akka.testkit.{ TestKit, ImplicitSender }
import dcp.business.actors.HbaseRouter
import org.specs2.mutable.SpecificationLike
case class GGG(msg: String)
case class TestMsg(msggg: GGG)
class TestDcpActor extends TestKit(ActorSystem()) with ImplicitSender with SpecificationLike {
"An Echo actor" should {
"send back messages unchanged" in {
val echo = system.actorOf(Props[HbaseRouter],"echo-actor")
val ggg = GGG("lllllll")
val msg = TestMsg(ggg)
echo ! msg
println("waiting for echo reply from echo actor")
expectMsg(msg)
success
}
}
}
示例13: UserRepositorySpec
//设置package包名称以及导入依赖的类
package org.cristal.repository
import akka.actor.ActorSystem
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import com.github.t3hnar.bcrypt._
import org.cristal.model.{NewUser, User}
import org.cristal.repository.UserRepository.UserCreated
import org.cristal.repository.dao.UserDAO
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito.when
import org.scalatest.concurrent.Eventually
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}
import scala.concurrent.Future
import scala.concurrent.duration._
class UserRepositorySpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender
with WordSpecLike with Matchers with BeforeAndAfterAll with Eventually with MockitoSugar {
implicit val duration: Timeout = 10 seconds
def this() = this(ActorSystem("UserRepositorySpecSystem"))
override def afterAll {
TestKit.shutdownActorSystem(system)
}
"An UserRepository Actor" should {
"Create a new user" in {
val userDAO = mock[UserDAO]
val passowod = "my_password"
val encryptedPassword = passowod.bcrypt
when(userDAO.insert(any())).thenReturn(Future.successful(()))
val userRepository = system.actorOf(UserRepository.props(userDAO))
val newUser = NewUser("name", passowod, "[email protected]", "John", "Doe")
userRepository ! UserRepository.CreateUser(newUser, self)
expectMsgPF() {
case UserCreated(User("name", encryptedPassword, "[email protected]", "John", "Doe")) => ()
}
}
}
}
示例14: ReviewerActorSpec
//设置package包名称以及导入依赖的类
package io.github.jlprat.akka.http.workshop.bookstore.actor
import akka.actor.ActorSystem
import akka.testkit.{ImplicitSender, TestKit}
import io.github.jlprat.akka.http.workshop.bookstore.model.{Author, Book, Review}
import Review._
import io.github.jlprat.akka.http.workshop.bookstore.actor.ReviewerActor._
import org.scalatest.{BeforeAndAfterAll, FlatSpecLike, Matchers}
class ReviewerActorSpec extends TestKit(ActorSystem("ReviewerActorSpec"))
with ImplicitSender with FlatSpecLike with Matchers with BeforeAndAfterAll {
override def afterAll {
TestKit.shutdownActorSystem(system)
}
"ReviewerActor" should "accept new reviews" in {
val reviewerActor = system.actorOf(ReviewerActor.props)
val book = Book("1234567", "The art of Doe", 321, Author("Jane Doe"))
val review = Review(author = Author("John Doe"), comment = "I liked it", stars = `*****`)
reviewerActor ! AddReview(review, book)
expectMsg(Success)
}
it should "return the reviews for a given book" in {
val reviewerActor = system.actorOf(ReviewerActor.props)
val book = Book("1234567", "The art of Doe", 321, Author("Jane Doe"))
val review = Review(author = Author("John Doe"), comment = "I liked it", stars = `*****`)
reviewerActor ! AddReview(review, book)
expectMsg(Success)
reviewerActor ! ListReviews(book)
expectMsg(Reviews(Seq(review)))
}
it should "return empty reviews if the book has none" in {
val reviewerActor = system.actorOf(ReviewerActor.props)
val book = Book("1234567", "The art of Doe", 321, Author("Jane Doe"))
reviewerActor ! ListReviews(book)
expectMsg(Reviews(Seq.empty))
}
it should "accept and return more than one review per book" in {
val reviewerActor = system.actorOf(ReviewerActor.props)
val book = Book("1234567", "The art of Doe", 321, Author("Jane Doe"))
val review1 = Review(author = Author("John Doe"), comment = "I liked it", stars = `*****`)
val review2 = Review(author = Author("Alice"), comment = "I liked it not", stars = `*`)
reviewerActor ! AddReview(review1, book)
expectMsg(Success)
reviewerActor ! AddReview(review2, book)
expectMsg(Success)
reviewerActor ! ListReviews(book)
expectMsg(Reviews(Seq(review2, review1)))
}
}
示例15: SellerTest
//设置package包名称以及导入依赖的类
package auction_system
import akka.actor.{Props, ActorSystem}
import akka.testkit.{TestProbe, TestFSMRef, ImplicitSender, TestKit}
import auction_system.auction.Auction
import auction_system.auction.Auction.{AuctionEnded, Setup}
import auction_system.auction_search.{MyAuctions, AuctionSearch}
import auction_system.notifier.Notifier
import auction_system.seller.Seller.Initialize
import auction_system.seller.{Active, Uninitialized, Seller}
import org.scalatest.{BeforeAndAfterAll, MustMatchers, WordSpecLike}
class SellerTest (_system: ActorSystem) extends TestKit(_system) with ImplicitSender
with WordSpecLike
with MustMatchers
with BeforeAndAfterAll {
def this() = this(ActorSystem("AuctionSearchTest"))
override def afterAll: Unit = {
system.terminate()
}
"A Seller actor" must {
val seller = TestFSMRef(new Seller)
"start in Uninitialized state" in {
assert(seller.stateName == Uninitialized)
}
val auctionSearcher = TestFSMRef(new AuctionSearch, "auction_search")
val notifier = TestFSMRef(new Notifier, "notifier")
"create and register an auction and switch to Active state" in {
seller ! Initialize(List("a1"), notifier)
assert(seller.stateName == Active)
assert(auctionSearcher.stateData.asInstanceOf[MyAuctions].auctions.head.path.name == "a1")
}
}
"An Auction actor" must {
val seller = TestProbe()
val notifier = TestFSMRef(new Notifier, "notifier")
val auction = seller.childActorOf(Props[Auction])
seller.send(auction, Setup(seller.ref, notifier))
"report the end of an auction afret tieout" in {
seller.expectMsg(AuctionEnded(auction))
}
}
}