本文整理汇总了Scala中akka.testkit.TestKit类的典型用法代码示例。如果您正苦于以下问题:Scala TestKit类的具体用法?Scala TestKit怎么用?Scala TestKit使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TestKit类的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: NodeTest
//设置package包名称以及导入依赖的类
package communication
import java.io.File
import akka.actor.{ActorRef, ActorSystem}
import akka.testkit.{ImplicitSender, TestKit}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}
import index.{DirectoryIndex, FileIndex}
class NodeTest() extends TestKit(ActorSystem("NodeTest")) with ImplicitSender
with WordSpecLike with Matchers with BeforeAndAfterAll {
implicit val homeDirPath = "/home/mike/Programming/scala/Cloudia"
override def afterAll {
TestKit.shutdownActorSystem(system)
}
"Node" must {
"send back directory index if pinged" in {
val node: ActorRef = system.actorOf(Node.props(homeDirPath))
node ! Ping()
expectMsgPF() {
case DirectoryIndex(_) => ()
}
}
}
it must {
"send back file manifest if one was requested" in {
val node: ActorRef = system.actorOf(Node.props(homeDirPath))
val fileIndex = new FileIndex(new File("src/test/resources/chunkifierTest"))
node ! Request(fileIndex)
expectMsgPF() {
case FileManifest(_, _) => ()
}
}
}
}
示例3: PersistenceSerializerDocSpec
//设置package包名称以及导入依赖的类
package docs.persistence
import com.typesafe.config._
import scala.concurrent.duration._
import org.scalatest.WordSpec
import akka.actor.ActorSystem
import akka.serialization.{ Serializer, SerializationExtension }
import akka.testkit.TestKit
class PersistenceSerializerDocSpec extends WordSpec {
val customSerializerConfig =
"""
//#custom-serializer-config
akka.actor {
serializers {
my-payload = "docs.persistence.MyPayloadSerializer"
my-snapshot = "docs.persistence.MySnapshotSerializer"
}
serialization-bindings {
"docs.persistence.MyPayload" = my-payload
"docs.persistence.MySnapshot" = my-snapshot
}
}
//#custom-serializer-config
""".stripMargin
val system = ActorSystem("PersistenceSerializerDocSpec", ConfigFactory.parseString(customSerializerConfig))
try {
SerializationExtension(system)
} finally {
TestKit.shutdownActorSystem(system, 10.seconds, false)
}
}
class MyPayload
class MySnapshot
class MyPayloadSerializer extends Serializer {
def identifier: Int = 77124
def includeManifest: Boolean = false
def toBinary(o: AnyRef): Array[Byte] = ???
def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef = ???
}
class MySnapshotSerializer extends Serializer {
def identifier: Int = 77125
def includeManifest: Boolean = false
def toBinary(o: AnyRef): Array[Byte] = ???
def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef = ???
}
示例4: CamelConfigSpec
//设置package包名称以及导入依赖的类
package akka.camel
import org.scalatest.Matchers
import org.scalatest.WordSpec
import akka.actor.ActorSystem
import scala.concurrent.duration.Duration
import java.util.concurrent.TimeUnit._
import akka.testkit.TestKit
import akka.util.Helpers.ConfigOps
class CamelConfigSpec extends WordSpec with Matchers {
val (settings, config) = {
val system = ActorSystem("CamelConfigSpec")
val result = (CamelExtension(system).settings, system.settings.config)
TestKit.shutdownActorSystem(system)
result
}
"CamelConfigSpec" must {
"have correct activationTimeout config" in {
settings.ActivationTimeout should be(config.getMillisDuration("akka.camel.consumer.activation-timeout"))
}
"have correct autoAck config" in {
settings.AutoAck should be(config.getBoolean("akka.camel.consumer.auto-ack"))
}
"have correct replyTimeout config" in {
settings.ReplyTimeout should be(config.getMillisDuration("akka.camel.consumer.reply-timeout"))
}
"have correct streamingCache config" in {
settings.StreamingCache should be(config.getBoolean("akka.camel.streamingCache"))
}
"have correct jmxStatistics config" in {
settings.JmxStatistics should be(config.getBoolean("akka.camel.jmx"))
}
"have correct body conversions config" in {
val conversions = config.getConfig("akka.camel.conversions")
conversions.getString("file") should be("java.io.InputStream")
conversions.entrySet.size should be(1)
}
"have correct Context Provider" in {
settings.ContextProvider.isInstanceOf[DefaultContextProvider] should be(true)
}
}
}
示例5: AConfigTestSuite
//设置package包名称以及导入依赖的类
package service.a.app
import org.scalatest.SpecLike
import akka.actor.ActorSystem
import akka.testkit.TestKit
class AConfigTestSuite extends TestKit(ActorSystem()) with SpecLike with AConfig {
object `A Config` {
object `when initialized` {
def `should have attribute "akka.loglevel" set` {
assert(logLevel.nonEmpty)
}
def `should have attribute "akka.loglevel" set to "INFO" per default` {
assert(logLevel.equals("INFO"))
}
def `should have attribute "akka.stdout-loglevel" set` {
assert(stdOutLogLevel.nonEmpty)
}
def `should have attribute "akka.stdout-loglevel" set to "OFF" per default` {
assert(stdOutLogLevel.equals("OFF"))
}
def `should have attribute "akka.log-config-on-start" set` {
assert(logConfigOnStart.nonEmpty)
}
def `should have attribute "akka.log-config-on-start" set to "OFF" per default` {
assert(logConfigOnStart.equals("off"))
}
def `should have attribute "akka.interface" set` {
assert(interface.nonEmpty)
}
def `should have attribute "http.interface" set to "0.0.0.0" per default` {
assert(interface.equals("0.0.0.0"))
}
def `should have attribute "akka.port" set` {
assert(port.nonEmpty)
}
def `should have attribute "http.port" set to "11011" per default` {
assert(port.equals("11011"))
}
}
}
}
示例6: HelloEntitySpec
//设置package包名称以及导入依赖的类
package se.hultner.hello.impl
import akka.actor.ActorSystem
import akka.testkit.TestKit
import com.lightbend.lagom.scaladsl.testkit.PersistentEntityTestDriver
import com.lightbend.lagom.scaladsl.playjson.JsonSerializerRegistry
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpec}
class HelloEntitySpec extends WordSpec with Matchers with BeforeAndAfterAll {
private val system = ActorSystem("HelloEntitySpec",
JsonSerializerRegistry.actorSystemSetupFor(HelloSerializerRegistry))
override protected def afterAll(): Unit = {
TestKit.shutdownActorSystem(system)
}
private def withTestDriver(block: PersistentEntityTestDriver[HelloCommand[_], HelloEvent, HelloState] => Unit): Unit = {
val driver = new PersistentEntityTestDriver(system, new HelloEntity, "hello-1")
block(driver)
driver.getAllIssues should have size 0
}
"Hello entity" should {
"say hello by default" in withTestDriver { driver =>
val outcome = driver.run(Hello("Alice", None))
outcome.replies should contain only "Hello, Alice!"
}
"allow updating the greeting message" in withTestDriver { driver =>
val outcome1 = driver.run(UseGreetingMessage("Hi"))
outcome1.events should contain only GreetingMessageChanged("Hi")
val outcome2 = driver.run(Hello("Alice", None))
outcome2.replies should contain only "Hi, Alice!"
}
}
}
示例7: JmsSpec
//设置package包名称以及导入依赖的类
package akka.stream.alpakka.jms
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.testkit.TestKit
import org.apache.activemq.broker.BrokerService
import org.scalatest._
import org.scalatest.concurrent.ScalaFutures
import akka.testkit.SocketUtil
abstract class JmsSpec
extends WordSpec
with Matchers
with BeforeAndAfterAll
with BeforeAndAfterEach
with ScalaFutures {
implicit val system = ActorSystem(this.getClass.getSimpleName)
implicit val materializer = ActorMaterializer()
override protected def afterAll(): Unit =
TestKit.shutdownActorSystem(system)
def withServer(network: Boolean = true)(test: Context => Unit): Unit = {
val broker = new BrokerService()
val host: String = "localhost"
val url = if (network) {
val port = SocketUtil.temporaryServerAddress(host).getPort
val serverUrl = s"tcp://$host:$port"
broker.addConnector(serverUrl)
serverUrl
} else {
s"vm://$host"
}
broker.setPersistent(false)
broker.setBrokerName(host)
broker.setUseJmx(false)
broker.start()
try {
test(Context(url, broker))
} finally {
if (broker.isStarted) {
broker.stop()
}
}
}
case class Context(url: String, broker: BrokerService)
}
示例8: CsvSpec
//设置package包名称以及导入依赖的类
package akka.stream.alpakka.csv.scaladsl
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.testkit.TestKit
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Matchers, WordSpec}
abstract class CsvSpec
extends WordSpec
with Matchers
with BeforeAndAfterAll
with BeforeAndAfterEach
with ScalaFutures {
implicit val system = ActorSystem(this.getClass.getSimpleName)
implicit val materializer = ActorMaterializer()
override protected def afterAll(): Unit =
TestKit.shutdownActorSystem(system)
}
示例9: TableSpec
//设置package包名称以及导入依赖的类
package akka.stream.alpakka.dynamodb
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.alpakka.dynamodb.impl.DynamoSettings
import akka.stream.alpakka.dynamodb.scaladsl.DynamoClient
import akka.testkit.TestKit
import org.scalatest.{AsyncWordSpecLike, BeforeAndAfterAll, Matchers}
import scala.collection.JavaConverters._
class TableSpec extends TestKit(ActorSystem("TableSpec")) with AsyncWordSpecLike with Matchers with BeforeAndAfterAll {
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
val settings = DynamoSettings(system)
val client = DynamoClient(settings)
override def beforeAll() = {
System.setProperty("aws.accessKeyId", "someKeyId")
System.setProperty("aws.secretKey", "someSecretKey")
}
"DynamoDB Client" should {
import TableSpecOps._
import akka.stream.alpakka.dynamodb.scaladsl.DynamoImplicits._
"1) create table" in {
client.single(createTableRequest).map(_.getTableDescription.getTableName shouldEqual tableName)
}
"2) list tables" in {
client.single(listTablesRequest).map(_.getTableNames.asScala.count(_ == tableName) shouldEqual 1)
}
"3) describe table" in {
client.single(describeTableRequest).map(_.getTable.getTableName shouldEqual tableName)
}
"4) update table" in {
client
.single(describeTableRequest)
.map(_.getTable.getProvisionedThroughput.getWriteCapacityUnits shouldEqual 10L)
.flatMap(_ => client.single(updateTableRequest))
.map(_.getTableDescription.getProvisionedThroughput.getWriteCapacityUnits shouldEqual newMaxLimit)
}
"5) delete table" in {
client
.single(deleteTableRequest)
.flatMap(_ => client.single(listTablesRequest))
.map(_.getTableNames.asScala.count(_ == tableName) shouldEqual 0)
}
}
}
示例10: AbstractSpec
//设置package包名称以及导入依赖的类
package core
import actors.Receptionist
import akka.actor.ActorSystem
import akka.testkit.{ImplicitSender, TestKit}
import org.scalatest.{BeforeAndAfterAll, FlatSpecLike, MustMatchers}
class AbstractSpec extends TestKit(ActorSystem("test-system"))
with FlatSpecLike
with ImplicitSender
with BeforeAndAfterAll
with MustMatchers {
val receptionist = system.actorOf(Receptionist.props(), "receptionist")
override def afterAll = {
TestKit.shutdownActorSystem(system)
}
}
示例11: InboundFSConnectionSpec
//设置package包名称以及导入依赖的类
import akka.actor.ActorSystem
import akka.testkit.TestKit
import esl.InboundFSConnection
class InboundFSConnectionSpec extends TestKit(ActorSystem("outbound-connection"))
with EslTestKit {
"connect function" should {
"enqueue Auth command" in {
val inbound = new InboundFSConnection()
whenReady(inbound.connect("ClueCon")) {
commandResponse =>
commandResponse.command.toString shouldBe "auth ClueCon\n\n"
}
}
}
}
示例12: 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)
}
}
示例13: SettingsSpec
//设置package包名称以及导入依赖的类
package com.github.btesila.weather.monitor
import akka.actor.ActorSystem
import akka.testkit.TestKit
import org.scalatest.{Matchers, WordSpecLike}
class SettingsSpec extends TestKit(ActorSystem()) with WordSpecLike with Matchers {
"The `Settings` extension" should {
"provide the appropriate Accuweather settings" in {
val settings = Settings(system).Accuweather
settings.ApiKey shouldBe "WTUr8dnrpCGYNnwDMDyv42qU9bxsAxjv"
settings.LocationUri shouldBe "https://dataservice.accuweather.com/locations/v1/search"
settings.CurrentConditionUri shouldBe "https://dataservice.accuweather.com/currentconditions/v1"
settings.DailyForecastUri shouldBe "https://dataservice.accuweather.com/forecasts/v1/daily/1day"
settings.ExtendedForecastUri shouldBe "https://dataservice.accuweather.com/forecasts/v1/daily/5day"
}
"provide the appropriate Http settings" in {
val settings = Settings(system).WeatherMonitor.Acceptor
settings.Host shouldBe "localhost"
settings.Port shouldBe 8140
}
}
}
示例14: SsEntitySpec
//设置package包名称以及导入依赖的类
package com.ss.ss.impl
import akka.actor.ActorSystem
import akka.testkit.TestKit
import com.lightbend.lagom.scaladsl.testkit.PersistentEntityTestDriver
import com.lightbend.lagom.scaladsl.playjson.JsonSerializerRegistry
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpec}
class SsEntitySpec extends WordSpec with Matchers with BeforeAndAfterAll {
private val system = ActorSystem("SsEntitySpec",
JsonSerializerRegistry.actorSystemSetupFor(SsSerializerRegistry))
override protected def afterAll(): Unit = {
TestKit.shutdownActorSystem(system)
}
private def withTestDriver(block: PersistentEntityTestDriver[SsCommand[_], SsEvent, SsState] => Unit): Unit = {
val driver = new PersistentEntityTestDriver(system, new SsEntity, "ss-1")
block(driver)
driver.getAllIssues should have size 0
}
"ss entity" should {
"say hello by default" in withTestDriver { driver =>
val outcome = driver.run(Hello("Alice", None))
outcome.replies should contain only "Hello, Alice!"
}
"allow updating the greeting message" in withTestDriver { driver =>
val outcome1 = driver.run(UseGreetingMessage("Hi"))
outcome1.events should contain only GreetingMessageChanged("Hi")
val outcome2 = driver.run(Hello("Alice", None))
outcome2.replies should contain only "Hi, Alice!"
}
}
}
示例15: PingPongActorUTest
//设置package包名称以及导入依赖的类
package com.example
import akka.actor.{Props, ActorSystem}
import akka.testkit.{ImplicitSender, TestKit}
import com.example.util.ReaperImpl
import org.scalatest.{FlatSpecLike, BeforeAndAfterAll, Matchers, WordSpecLike}
class PingPongActorUTest(_system: ActorSystem)
extends TestKit(_system)
with ImplicitSender
with FlatSpecLike
with Matchers
with BeforeAndAfterAll {
def this() = this(ActorSystem("MySpec"))
override def afterAll {
TestKit.shutdownActorSystem(system)
}
val reaper = system.actorOf(Props[ReaperImpl], "reaper")
val pongActor = system.actorOf(Props(classOf[PongActor], reaper), "pongActor")
val pingActor = system.actorOf(Props(classOf[PingActor], reaper, pongActor), "pingActor")
"A Ping actor" should "send back a ping on a pong" in {
pingActor ! PongActor.PongMessage("pong")
expectMsg(PingActor.PingMessage("ping"))
}
"A Pong actor" should "send back a pong on a ping" in {
pongActor ! PingActor.PingMessage("ping")
expectMsg(PongActor.PongMessage("pong"))
}
}