Skip to content

Commit

Permalink
Reformat and tweak two tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
paurkedal committed Oct 26, 2017
1 parent f365f17 commit 5ae66ec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
35 changes: 20 additions & 15 deletions tests/test_parallel_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,22 @@ let random_int () = Random.int (1 + Random.int 16)

let do_query =
Caqti_lwt.Pool.use @@ fun (module C : Caqti_lwt.CONNECTION) ->
match Random.int 4 with
| 0 -> C.exec insert_q C.Param.[|int (random_int ()); int (random_int ())|] >>
Lwt.return 0
| 1 -> C.exec delete_q C.Param.[|int (random_int ())|] >>
Lwt.return 0
| 2 -> C.fold select_1_q C.Tuple.(fun t -> (+) (int 0 t))
C.Param.[|int (random_int ())|] 0
| 3 -> C.find select_2_q C.Tuple.(option int 0)
C.Param.[|int (random_int ())|] >|=
(function None -> 0 | Some i -> i)
| _ -> assert false
(match Random.int 4 with
| 0 ->
C.exec insert_q C.Param.[|int (random_int ()); int (random_int ())|] >>
Lwt.return 0
| 1 ->
C.exec delete_q C.Param.[|int (random_int ())|] >>
Lwt.return 0
| 2 ->
C.fold select_1_q C.Tuple.(fun t -> (+) (int 0 t))
C.Param.[|int (random_int ())|] 0
| 3 ->
C.find select_2_q C.Tuple.(option int 0)
C.Param.[|int (random_int ())|]
>|= (function None -> 0 | Some i -> i)
| _ ->
assert false)

let rec list_diff f = function
| x0 :: x1 :: xs -> f x1 x0 :: list_diff f (x1 :: xs)
Expand All @@ -60,10 +65,10 @@ let rec test2 pool n =
if n = 1 then do_query pool else
let ns = Array.init (Random.int n * (Random.int n + 1) / n + 1)
(fun _ -> Random.int n)
|> Array.to_list |> (fun xs -> n :: xs)
|> List.sort compare
|> list_diff (-)
|> List.filter ((<>) 0) in
|> Array.to_list |> (fun xs -> n :: xs)
|> List.sort compare
|> list_diff (-)
|> List.filter ((<>) 0) in
let xs = List.map (test2 pool) ns in
merge (+) xs 0

Expand Down
33 changes: 16 additions & 17 deletions tests/test_pool_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*)

let (>>=) = Lwt.(>>=)
let (>|=) = Lwt.(>|=)
open Lwt.Infix

module Pool = Caqti_pool.Make (Caqti_lwt.System)

Expand All @@ -34,23 +33,23 @@ let test n =
for _ = 0 to n - 1 do
let j = Random.int n in
assert (Pool.size pool = Hashtbl.length Resource.ht);
match a.(j) with
| None ->
let waiter, waker = Lwt.wait () in
wait_count := succ !wait_count;
Lwt.async
(fun () ->
Pool.use
(fun _ -> waiter >>= fun () ->
wait_count := pred !wait_count; Lwt.return_unit)
pool);
a.(j) <- Some waker
| Some u -> wake j u
(match a.(j) with
| None ->
let waiter, waker = Lwt.wait () in
wait_count := succ !wait_count;
Lwt.async
(fun () ->
Pool.use
(fun _ -> waiter >>= fun () ->
wait_count := pred !wait_count; Lwt.return_unit)
pool);
a.(j) <- Some waker
| Some u -> wake j u)
done;
for j = 0 to n - 1 do
match a.(j) with
| None -> ()
| Some u -> wake j u
(match a.(j) with
| None -> ()
| Some u -> wake j u)
done;
assert (!wait_count = 0);
Pool.drain pool >|= fun () ->
Expand Down

0 comments on commit 5ae66ec

Please sign in to comment.