You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

61 lines
1.8 KiB

import org.scalatest.funsuite.AnyFunSuite
import sttp.client._
import sttp.client.httpclient.monix.HttpClientMonixBackend
import monix.execution.Scheduler
import sttp.client.httpclient.monix.MonixWebSocketHandler
import org.scalatest.BeforeAndAfterAll
import monix.eval.Task
import sttp.model.ws.WebSocketFrame
import scala.concurrent.duration._
import nova.monadic_sfx.implicits._
import monix.catnap.ConcurrentQueue
class WebSocketTest extends AnyFunSuite with BeforeAndAfterAll {
implicit val sched = Scheduler.global
implicit val backend = HttpClientMonixBackend().runSyncUnsafe()
val ws = basicRequest
.get(uri"ws://localhost:6789")
.openWebsocketF(MonixWebSocketHandler())
.map(_.result)
.runSyncUnsafe()
test("open websocket") {
(for {
isOpen <- ws.isOpen
_ <- Task(assert(isOpen == true))
_ <- Task(assert(isOpen != false))
} yield ()).runSyncUnsafe(10.seconds)
}
test("send message") {
(for {
_ <- ws.send(WebSocketFrame.text("Test Message"))
// _ <- Task.sleep(1.second)
// _ <- Task(assert(isOpen == true))
} yield ()).runSyncUnsafe(10.seconds)
}
test("receive messages observable") {
(for {
queue <- ConcurrentQueue.bounded[Task, Int](10)
_ <-
ws.observableSource
.filter(_.isRight)
.map(_.right.get)
.doOnNext(s =>
Task(println(s"Received item: $s")) >>
s.toIntOption.fold(Task.unit)(queue.offer)
)
.take(5)
.completedL
.timeout(5.seconds)
items <- queue.drain(5, 5).timeout(5.seconds)
_ <- Task(assert(items == Seq(1, 2, 3, 4, 5)))
_ <- Task(assert(items != Seq(1, 2, 3, 4, 5, 6)))
} yield ()).runSyncUnsafe(10.seconds)
}
override def afterAll() = {
ws.close.runSyncUnsafe()
backend.close()
}
}