I'm testing a socket connection using scalatest and opening the connection is an asynchronous operation, to prevent the test completing before the async operating completes I'm attempting to wrap in a 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");
};
}
I receive compilation error :
[error] found : Unit
[error] required: T
[error] }
[error] ^
[error] one error found
[error] (test:compileIncremental) Compilation failed
The async code is based on : I'm basing below on How do I wait for asynchronous tasks to complete in scala?
How to wrap above test in a Future so can run async operation before test completes ?
Update : I cannot use whenReady from ScalaTest as using earlier version with play framework. Attempting to wrap multiple future calls in the test :
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)
}
But the test still completes before the future has time to run even with using Await.result