I have a method which connects to a websocket and gets stream messages from some really outside system.
The simplified version is:
def watchOrders(): Var[Option[Order]] = {
val value = Var[Option[Order]](None)
// onMessage( order => value.update(Some(order))
value
}
When I test it (with scalatest), I want to make it connect to the real outside system, and only check the first 4 orders:
test("watchOrders") {
var result = List.empty[Order]
val stream = client.watchOrders()
stream.foreach {
case Some(order) =>
result = depth :: result
if (result.size == 4) { // 1.
assert(orders should ...) // 2.
stream.kill() // 3.
}
case _ =>
}
Thread.sleep(10000) // 4.
}
I have 4 questions:
- Is it the right way to check the first 4 orders? there is no
take(4)
method found in scala.rx - If the
assert
fails, the test still passes, how to fix it? - Is it the right way to stop the stream?
- If the thread doesn't sleep here, the test will pass the code in
case Some(order)
never runs. Is there a better way to wait?