Skip to content

Commit

Permalink
[OCaml/Dream] Add initial tests for Dream (#9278)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReallySnazzy authored Oct 1, 2024
1 parent b03e526 commit d96bcdc
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 0 deletions.
35 changes: 35 additions & 0 deletions frameworks/OCaml/dream/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Dream

## Overview

Most of all of the code is inside of `test_dream/bin/main.ml` file.

## Implemented tests

| Test Name | Endpoint |
|------------|-------------------------------|
| Plain text | http://0.0.0.0:8080/plaintext |
| Json | http://0.0.0.0:8080/json |

## Headers

A simple middleware was added that adds the required headers for the Techempower Benchmarks.
The date header is refreshed only once per second as allowed by the rules for performance.

## Dependencies

The `test_dream/dune-project` and `test_dream/bin/dune` are where dependencies are managed
for this project. If you add a dependency to those locations and then run the following:

```
cd test_dream
dune build test_dream.opam
opam install --yes --deps-only .
```

You will update the opam package list and install your changes locally.

## Running tests

$ tfb --mode verify --test dream

26 changes: 26 additions & 0 deletions frameworks/OCaml/dream/benchmark_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"framework": "dream",
"tests": [
{
"default": {
"json_url": "/json",
"plaintext_url": "/plaintext",
"port": 8080,
"approach": "Realistic",
"classification": "Fullstack",
"database": "postgres",
"framework": "Dream",
"language": "OCaml",
"flavor": "None",
"orm": "Micro",
"platform": "http/af",
"webserver": "None",
"os": "Linux",
"database_os": "Linux",
"display_name": "Dream",
"notes": "",
"versus": "httpaf"
}
}
]
}
12 changes: 12 additions & 0 deletions frameworks/OCaml/dream/dream.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM ocaml/opam:debian-ocaml-5.1
COPY ./dream_test/ /app
WORKDIR /app
USER root
RUN apt install -y libgmp-dev libev-dev pkg-config libssl-dev
RUN opam install --yes --deps-only .
RUN opam install dune
RUN eval $(opam env)
RUN opam exec -- dune build --profile release
CMD [ "./_build/default/bin/main.exe" ]
EXPOSE 8080

5 changes: 5 additions & 0 deletions frameworks/OCaml/dream/dream_test/bin/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(executable
(public_name dream_test)
(name main)
(preprocess (pps lwt_ppx ppx_yojson_conv))
(libraries dream_test dream ppx_yojson_conv calendar))
53 changes: 53 additions & 0 deletions frameworks/OCaml/dream/dream_test/bin/main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
open Ppx_yojson_conv_lib.Yojson_conv.Primitives

type message_object = {
message : string;
} [@@deriving yojson]

let time_cache = Atomic.make false;;
let time_ref = ref("None");;

let time () =
if not @@ Atomic.get time_cache
then begin
time_ref := CalendarLib.Printer.Calendar.sprint "%a, %d %b %Y %H:%M:%S UTC" @@ CalendarLib.Calendar.now ();
Atomic.set time_cache true;
(!time_ref)
end
else
(!time_ref);;

let tech_empower_headers (inner_handler: Dream.handler) =
(fun (req) ->
let%lwt res = inner_handler req in
Dream.set_header res "Server" "dream";
Dream.set_header res "Date" @@ time ();
Lwt.return res
);;

let rec timer () =
Unix.sleepf 0.9;
Atomic.set time_cache false;
timer();;

let () =
let time_invalidator = Domain.spawn(fun () -> timer ()) in
Dream.run ~interface: "0.0.0.0"
@@ tech_empower_headers
@@ Dream.router [
Dream.get "/" (fun _ ->
Dream.html "Hello, world!"
);
Dream.get "/plaintext" (fun _ ->
Dream.response ~headers: [("Content-Type", "text/plain")]
"Hello, world!"
|> Lwt.return
);
Dream.get "/json" (fun _ ->
{ message = "Hello, world!" }
|> yojson_of_message_object
|> Yojson.Safe.to_string
|> Dream.json
);
];
Domain.join time_invalidator;;
35 changes: 35 additions & 0 deletions frameworks/OCaml/dream/dream_test/dream_test.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "A short synopsis"
description: "A longer description"
maintainer: ["Maintainer Name"]
authors: ["Author Name"]
license: "LICENSE"
tags: ["topics" "to describe" "your" "project"]
homepage: "https://github.com/username/reponame"
doc: "https://url/to/documentation"
bug-reports: "https://github.com/username/reponame/issues"
depends: [
"ocaml"
"dune" {>= "3.14"}
"dream"
"lwt"
"ppx_yojson_conv"
"calendar"
"odoc" {with-doc}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
dev-repo: "git+https://github.com/username/reponame.git"
26 changes: 26 additions & 0 deletions frameworks/OCaml/dream/dream_test/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(lang dune 3.14)

(name dream_test)

(generate_opam_files true)

(source
(github username/reponame))

(authors "Author Name")

(maintainers "Maintainer Name")

(license LICENSE)

(documentation https://url/to/documentation)

(package
(name dream_test)
(synopsis "A short synopsis")
(description "A longer description")
(depends ocaml dune dream lwt ppx_yojson_conv calendar)
(tags
(topics "to describe" your project)))

; See the complete stanza docs at https://dune.readthedocs.io/en/stable/dune-files.html#dune-project
2 changes: 2 additions & 0 deletions frameworks/OCaml/dream/dream_test/lib/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(library
(name dream_test))
2 changes: 2 additions & 0 deletions frameworks/OCaml/dream/dream_test/test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(test
(name test_dream_test))
Empty file.

0 comments on commit d96bcdc

Please sign in to comment.