File: pool_test.ml

package info (click to toggle)
liquidsoap 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,372 kB
  • sloc: ml: 71,806; javascript: 27,320; ansic: 398; xml: 114; sh: 99; lisp: 96; makefile: 26
file content (46 lines) | stat: -rw-r--r-- 1,171 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
type req = { id : int; destroyed : bool }

module Pool = Pool.Make (struct
  type t = req

  let id { id; _ } = id
  let destroyed id = { id; destroyed = true }
  let is_destroyed { destroyed; _ } = destroyed
end)

let m = Mutex.create ()
let _done = Condition.create ()
let started = Condition.create ()

let fill () =
  (* Create a bunch of requests. *)
  let l =
    List.init 100 (fun _ -> Pool.add (fun id -> { id; destroyed = false }))
  in
  List.iter (fun { id; _ } -> ignore (Printf.sprintf "id: %d\n%!" id)) l;
  (* Delete 15th one. *)
  Pool.remove 15;
  Gc.full_major ();
  List.iter (fun { id; _ } -> ignore (Printf.sprintf "id: %d\n%!" id)) l;
  assert (Pool.size () = 99);
  let r = Pool.add (fun id -> { id; destroyed = false }) in
  assert (Pool.size () = 100);
  assert (r.id = 100);
  assert (List.length l = 100);
  Condition.signal _done

let check () =
  Mutex.lock m;
  Condition.signal started;
  Condition.wait _done m;
  Gc.full_major ();
  assert (Pool.size () = 0);
  Mutex.unlock m

let () =
  Mutex.lock m;
  let th = Thread.create check () in
  Condition.wait started m;
  Mutex.unlock m;
  ignore (Thread.create fill ());
  Thread.join th