Skip to content

Commit d96bcdc

Browse files
authored
[OCaml/Dream] Add initial tests for Dream (#9278)
1 parent b03e526 commit d96bcdc

File tree

10 files changed

+196
-0
lines changed

10 files changed

+196
-0
lines changed

frameworks/OCaml/dream/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Dream
2+
3+
## Overview
4+
5+
Most of all of the code is inside of `test_dream/bin/main.ml` file.
6+
7+
## Implemented tests
8+
9+
| Test Name | Endpoint |
10+
|------------|-------------------------------|
11+
| Plain text | http://0.0.0.0:8080/plaintext |
12+
| Json | http://0.0.0.0:8080/json |
13+
14+
## Headers
15+
16+
A simple middleware was added that adds the required headers for the Techempower Benchmarks.
17+
The date header is refreshed only once per second as allowed by the rules for performance.
18+
19+
## Dependencies
20+
21+
The `test_dream/dune-project` and `test_dream/bin/dune` are where dependencies are managed
22+
for this project. If you add a dependency to those locations and then run the following:
23+
24+
```
25+
cd test_dream
26+
dune build test_dream.opam
27+
opam install --yes --deps-only .
28+
```
29+
30+
You will update the opam package list and install your changes locally.
31+
32+
## Running tests
33+
34+
$ tfb --mode verify --test dream
35+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"framework": "dream",
3+
"tests": [
4+
{
5+
"default": {
6+
"json_url": "/json",
7+
"plaintext_url": "/plaintext",
8+
"port": 8080,
9+
"approach": "Realistic",
10+
"classification": "Fullstack",
11+
"database": "postgres",
12+
"framework": "Dream",
13+
"language": "OCaml",
14+
"flavor": "None",
15+
"orm": "Micro",
16+
"platform": "http/af",
17+
"webserver": "None",
18+
"os": "Linux",
19+
"database_os": "Linux",
20+
"display_name": "Dream",
21+
"notes": "",
22+
"versus": "httpaf"
23+
}
24+
}
25+
]
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM ocaml/opam:debian-ocaml-5.1
2+
COPY ./dream_test/ /app
3+
WORKDIR /app
4+
USER root
5+
RUN apt install -y libgmp-dev libev-dev pkg-config libssl-dev
6+
RUN opam install --yes --deps-only .
7+
RUN opam install dune
8+
RUN eval $(opam env)
9+
RUN opam exec -- dune build --profile release
10+
CMD [ "./_build/default/bin/main.exe" ]
11+
EXPOSE 8080
12+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(executable
2+
(public_name dream_test)
3+
(name main)
4+
(preprocess (pps lwt_ppx ppx_yojson_conv))
5+
(libraries dream_test dream ppx_yojson_conv calendar))
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
open Ppx_yojson_conv_lib.Yojson_conv.Primitives
2+
3+
type message_object = {
4+
message : string;
5+
} [@@deriving yojson]
6+
7+
let time_cache = Atomic.make false;;
8+
let time_ref = ref("None");;
9+
10+
let time () =
11+
if not @@ Atomic.get time_cache
12+
then begin
13+
time_ref := CalendarLib.Printer.Calendar.sprint "%a, %d %b %Y %H:%M:%S UTC" @@ CalendarLib.Calendar.now ();
14+
Atomic.set time_cache true;
15+
(!time_ref)
16+
end
17+
else
18+
(!time_ref);;
19+
20+
let tech_empower_headers (inner_handler: Dream.handler) =
21+
(fun (req) ->
22+
let%lwt res = inner_handler req in
23+
Dream.set_header res "Server" "dream";
24+
Dream.set_header res "Date" @@ time ();
25+
Lwt.return res
26+
);;
27+
28+
let rec timer () =
29+
Unix.sleepf 0.9;
30+
Atomic.set time_cache false;
31+
timer();;
32+
33+
let () =
34+
let time_invalidator = Domain.spawn(fun () -> timer ()) in
35+
Dream.run ~interface: "0.0.0.0"
36+
@@ tech_empower_headers
37+
@@ Dream.router [
38+
Dream.get "/" (fun _ ->
39+
Dream.html "Hello, world!"
40+
);
41+
Dream.get "/plaintext" (fun _ ->
42+
Dream.response ~headers: [("Content-Type", "text/plain")]
43+
"Hello, world!"
44+
|> Lwt.return
45+
);
46+
Dream.get "/json" (fun _ ->
47+
{ message = "Hello, world!" }
48+
|> yojson_of_message_object
49+
|> Yojson.Safe.to_string
50+
|> Dream.json
51+
);
52+
];
53+
Domain.join time_invalidator;;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This file is generated by dune, edit dune-project instead
2+
opam-version: "2.0"
3+
synopsis: "A short synopsis"
4+
description: "A longer description"
5+
maintainer: ["Maintainer Name"]
6+
authors: ["Author Name"]
7+
license: "LICENSE"
8+
tags: ["topics" "to describe" "your" "project"]
9+
homepage: "https://github.com/username/reponame"
10+
doc: "https://url/to/documentation"
11+
bug-reports: "https://github.com/username/reponame/issues"
12+
depends: [
13+
"ocaml"
14+
"dune" {>= "3.14"}
15+
"dream"
16+
"lwt"
17+
"ppx_yojson_conv"
18+
"calendar"
19+
"odoc" {with-doc}
20+
]
21+
build: [
22+
["dune" "subst"] {dev}
23+
[
24+
"dune"
25+
"build"
26+
"-p"
27+
name
28+
"-j"
29+
jobs
30+
"@install"
31+
"@runtest" {with-test}
32+
"@doc" {with-doc}
33+
]
34+
]
35+
dev-repo: "git+https://github.com/username/reponame.git"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(lang dune 3.14)
2+
3+
(name dream_test)
4+
5+
(generate_opam_files true)
6+
7+
(source
8+
(github username/reponame))
9+
10+
(authors "Author Name")
11+
12+
(maintainers "Maintainer Name")
13+
14+
(license LICENSE)
15+
16+
(documentation https://url/to/documentation)
17+
18+
(package
19+
(name dream_test)
20+
(synopsis "A short synopsis")
21+
(description "A longer description")
22+
(depends ocaml dune dream lwt ppx_yojson_conv calendar)
23+
(tags
24+
(topics "to describe" your project)))
25+
26+
; See the complete stanza docs at https://dune.readthedocs.io/en/stable/dune-files.html#dune-project
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(library
2+
(name dream_test))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(test
2+
(name test_dream_test))

frameworks/OCaml/dream/dream_test/test/test_dream_test.ml

Whitespace-only changes.

0 commit comments

Comments
 (0)