0

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

Community
  • 1
  • 1
blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • http://doc.scalatest.org/2.0/index.html#org.scalatest.concurrent.Futures – Kolmar May 20 '16 at 14:45
  • @Kolmar please see update – blue-sky May 20 '16 at 18:43
  • If you have some `Seq[Future[...]]` or `Iterable[Future[...]]`, you can convert it to a single `Future` with `Future.sequence`, and then `Await` on that. But in your example, it seems you wait only for creation of hookup clients but **not** wait for connecting to the remote or receiving the reply. – Kolmar May 21 '16 at 08:17
  • I don't know the details of the library you are using, but I think you don't have to wrap creating a `hookupClient` in the `Future`. On the other hand it seems that `connect()` is the method that returns a `Future`, so you may want to `Await` specifically on `connect()`. – Kolmar May 21 '16 at 08:19

0 Answers0