我正在使用scalatest测试套接字连接并打开连接是一个异步操作,以防止在异步操作完成之前完成测试我试图包装在Future中:
import io.backchat.hookup._
it should "test connection" in {
def doAsyncAction: Promise[T] = {
val p = Promise[T]
p success {
val hookupClient = new DefaultHookupClient(HookupClientConfig(URI.create("ws://localhost:9000/get/info"))) {
val messages = ListBuffer[String]()
def receive = {
case Connected =>
println("Connected")
case Disconnected(_) =>
println("Disconnected")
case JsonMessage(json) =>
println("Json message = " + json)
case TextMessage(text) =>
messages += text
println("Text message = " + text)
}
connect() onSuccess {
case Success => {
send("test")
}
}
}
}
p
}
val async = doAsyncAction;
async.future onSuccess {
println("finished");
};
}
我收到编译错误:
[error] found : Unit
[error] required: T
[error] }
[error] ^
[error] one error found
[error] (test:compileIncremental) Compilation failed
异步代码基于:我基于How do I wait for asynchronous tasks to complete in scala?下面的
如何在Future中包含上面的测试,以便在测试完成之前运行异步操作?
更新: 我不能使用来自ScalaTest的whenReady作为使用Play框架的早期版本。试图在测试中包装多个未来的调用:
for( a <- 1 until 10){
val fut = Future {
val hookupClient : io.backchat.hookup.DefaultHookupClient = new DefaultHookupClient(HookupClientConfig(URI.create("ws://localhost:9000/get/info"))) {
val messages = ListBuffer[String]()
def receive = {
case Connected =>
println("Connected")
case Disconnected(_) =>
println("Disconnected")
case JsonMessage(json) =>
println("Json message = " + json)
case TextMessage(text) =>
messages += text
println("Text message = " + text)
}
connect() onSuccess {
case Success => {
send("test")
}
}
}
}
Await.result(fut, 600.seconds)
}
但即使使用Await.result