Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #33

Merged
merged 2 commits into from
Nov 2, 2024
Merged

Dev #33

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Currently only a development version is available. You can [pin][opam-pin]
it using the [OPAM] package manager.
```console
$ opam nats-client-lwt.pin https://github.com/romanchechyotkin/nats.ocaml.git
$ opam pin nats-client-lwt.dev https://github.com/romanchechyotkin/nats.ocaml.git
```

### Simple echo example
Expand All @@ -28,12 +28,17 @@ Take it from [`examples/simple.ml`](./examples/simple.ml).
```ocaml
let () =
Lwt_main.run @@
(* Make connection with NATS server. *)
let%lwt client = Nats_client_lwt.make { port = 4222; host = "127.0.0.1" } in

(* Initialize connection with parameters. Required. *)
client#init
{ echo = true; tls_required = false; pedantic = false; verbose = false };%lwt
(* Connect to a NATS server. *)
Nats_client_lwt.with_connect_to ~port:4222 ~host:"127.0.0.1"
~init:
{
echo = true;
tls_required = false;
pedantic = false;
verbose = false;
}
@@ fun client ->

(* Subscribe to HELLO subject. *)
let%lwt hello_subject = client#sub ~subject:"HELLO" () in
Expand All @@ -45,8 +50,7 @@ let () =
(* Send "Hello World" message to HELLO subject. *)
client#pub ~subject:"HELLO" "Hello World";%lwt

Lwt_unix.sleep 0.1;%lwt
client#close
Lwt_unix.sleep 0.1
```

```console
Expand Down
4 changes: 3 additions & 1 deletion examples/demo.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
let main () =
let%lwt client = Nats_client_lwt.make { port = 4222; host = "127.0.0.1" } in
let%lwt client =
Nats_client_lwt.connect_to { port = 4222; host = "127.0.0.1" }
in

Format.printf "info %a\n" Yojson.Safe.pp client#info;

Expand Down
21 changes: 11 additions & 10 deletions examples/simple.ml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
let () =
Lwt_main.run
@@
(* Make connection with NATS server. *)
let%lwt client = Nats_client_lwt.make { port = 4222; host = "127.0.0.1" } in

(* Initialize connection with parameters. Required. *)
client#init
{ echo = true; tls_required = false; pedantic = false; verbose = false };%lwt

(* Connect to a NATS server. *)
@@ Nats_client_lwt.with_connect_to ~port:4222 ~host:"127.0.0.1"
~init:
{
echo = true;
tls_required = false;
pedantic = false;
verbose = false;
}
@@ fun client ->
(* Subscribe to HELLO subject. *)
let%lwt hello_subject = client#sub ~subject:"HELLO" () in

Expand All @@ -18,5 +20,4 @@ let () =
(* Send "Hello World" message to HELLO subject. *)
client#pub ~subject:"HELLO" "Hello World";%lwt

Lwt_unix.sleep 0.1;%lwt
client#close
Lwt_unix.sleep 0.1
9 changes: 8 additions & 1 deletion lwt/nats_client_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,18 @@ class client ~(info : Yojson.Safe.t) ~(connection : Connection.t) =
end

(** @raises Connection.Connection_refused *)
let make settings =
let connect_to settings =
let%lwt connection = Connection.create settings in
let%lwt info =
match%lwt Connection.receive connection with
| Message.Incoming.Info info -> Lwt.return info
| _ -> raise @@ Invalid_response "INFO message"
in
Lwt.return @@ new client ~info ~connection

(** @raises Connection.Connection_refused *)
let with_connect_to ~port ~host ~(init : Message.Initial.t) f =
let%lwt client = connect_to { port; host } in
client#init init;%lwt
f client;%lwt
client#close
14 changes: 10 additions & 4 deletions tests/e2e/test_lwt_client.ml
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
open Alcotest

let init_test_correct_address _ () =
let%lwt _ = Nats_client_lwt.make { port = 4222; host = "127.0.0.1" } in
let%lwt _ = Nats_client_lwt.connect_to { port = 4222; host = "127.0.0.1" } in
Lwt.return_unit

let init_test_wrong_address _ () =
try%lwt
let%lwt _ = Nats_client_lwt.make { port = 42222; host = "127.0.0.1" } in
let%lwt _ =
Nats_client_lwt.connect_to { port = 42222; host = "127.0.0.1" }
in
fail "Expected Connection_refused exception, but none was raised"
with Nats_client_lwt.Connection.Connection_refused -> Lwt.return_unit

let connect_test _ () =
let%lwt client = Nats_client_lwt.make { port = 4222; host = "127.0.0.1" } in
let%lwt client =
Nats_client_lwt.connect_to { port = 4222; host = "127.0.0.1" }
in
client#init
{ echo = true; tls_required = false; pedantic = false; verbose = true };%lwt

Lwt.return ()

let connect_test_with_verbose_false _ () =
let%lwt client = Nats_client_lwt.make { port = 4222; host = "127.0.0.1" } in
let%lwt client =
Nats_client_lwt.connect_to { port = 4222; host = "127.0.0.1" }
in
client#init
{ echo = true; tls_required = false; pedantic = false; verbose = false };%lwt

Expand Down