aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClombrong <clombrong@egregore.fun>2025-04-24 16:14:27 +0200
committerClombrong <cromblong@egregore.fun>2025-04-24 16:14:27 +0200
commit9c36f4089d4636f37853d0c13d6a6ca2f4fd6b05 (patch)
tree508ba4331ce314612e9de25fdd5189e4a1b11095
parentbde5d745550496727d7977156b8141d8fe85f404 (diff)
fix: custom lwt runner for js tests
-rw-r--r--test/js/websockets_hello.ml37
1 files changed, 26 insertions, 11 deletions
diff --git a/test/js/websockets_hello.ml b/test/js/websockets_hello.ml
index 6b74d96..fa47965 100644
--- a/test/js/websockets_hello.ml
+++ b/test/js/websockets_hello.ml
@@ -1,20 +1,35 @@
open Lwt.Syntax
+open Js_of_ocaml
+
+(* https://stackoverflow.com/questions/34929382/what-are-the-differences-between-lwt-async-and-lwt-main-run-on-ocaml-node-js *)
+let rec run t =
+ let next_tick (_callback : unit -> unit) =
+ Js.Unsafe.(fun_call
+ (js_expr "process.nextTick")
+ [| inject (Js.wrap_callback _callback) |])
+ in Lwt.wakeup_paused ();
+ match Lwt.poll t with
+ | Some x -> x
+ | None ->
+ if Lwt.paused_count () > 0
+ then next_tick (fun () -> run t)
+ else ()
let () =
- (* Echo is a websocket that... echoes you stuff. *)
- let stream, push = Portal_ws.ws_stream "wss://echo.websocket.org" in
- push (Some "great text");
- push (Some "other text");
- push (Some "yet another text");
- push (Some "BYE");
- Lwt.async @@
- fun () ->
- let* server = Portal_ws.ws_endpoint "egregore.fun"
- in let+ _ = Lwt_stream.iter
+ run @@
+ let* server = Portal_ws.ws_endpoint "telepath.im" in
+ let stream, push =
+ (* Echo is a websocket that... echoes you stuff. *)
+ Portal_ws.ws_stream "wss://echo.websocket.org" in
+ push (Some "great text");
+ push (Some "other text");
+ push (Some "yet another text");
+ push (Some "BYE");
+ let+ _ = Lwt_stream.iter
(fun greetings ->
match greetings with
(* When the websocket sends "BYE", we close. *)
| "BYE" -> print_endline "CLOSING BYE"; push None
| hello -> print_endline ("> " ^ hello))
stream
- in print_endline server
+ in print_endline server