Label `from_root` suffix argument
[hiphop-php.git] / hphp / hack / test / unit / procs / worker_test.ml
blob4554ff3f6f1009f51ed7cc5bede9753fa14c6493
1 open Core_kernel
3 let entry =
4 WorkerController.register_entry_point ~restore:(fun () ~(worker_id : int) ->
5 Hh_logger.set_id (Printf.sprintf "worker_test %d" worker_id))
7 let num_workers = 2
9 let make_worker ?call_wrapper heap_handle =
10 WorkerController.make
11 ?call_wrapper
12 ~saved_state:()
13 ~entry
14 ~nbr_procs:num_workers
15 ~gc_control:(Gc.get ())
16 ~heap_handle
18 let rec wait_until_ready handle =
19 let { WorkerController.readys; waiters = _; ready_fds = _ } =
20 WorkerController.select [handle] []
22 match readys with
23 | [] -> wait_until_ready handle
24 | ready :: _ -> ready
26 (** If "f x" throws, we exit the program with a custom exit code. *)
27 let catch_exception_and_custom_exit_wrapper : 'x 'b. ('x -> 'b) -> 'x -> 'b =
28 (fun f x -> (try f x with _ -> exit 17))
30 let call_and_verify_result worker f x expected =
31 let result =
32 WorkerController.call worker f x
33 |> wait_until_ready
34 |> WorkerController.get_result
36 result = expected
38 (** This is just like the test_worker_uncaught_exception_exits_with_2 test
39 * except we add a call_wapper to the worker. It catches all exceptions and
40 * makes the worker exit with code 17. *)
41 let test_wrapped_worker_with_custom_exit heap_handle () =
42 let workers =
43 make_worker
44 ~call_wrapper:
45 { WorkerController.wrap = catch_exception_and_custom_exit_wrapper }
46 heap_handle
48 match workers with
49 | [] ->
50 Printf.eprintf "Failed to create workers";
51 false
52 | worker :: _ ->
53 (try
54 call_and_verify_result
55 worker
56 (fun () -> raise (Failure "oops"))
58 "dummy"
59 with
60 | WorkerController.Worker_failed
61 (_, WorkerController.Worker_quit (Unix.WEXITED i))
63 i = 17)
65 let test_worker_uncaught_exception_exits_with_2 heap_handle () =
66 let workers = make_worker heap_handle in
67 match workers with
68 | [] ->
69 Printf.eprintf "Failed to create workers";
70 false
71 | worker :: _ ->
72 (try
73 call_and_verify_result
74 worker
75 (fun () -> raise (Failure "oops"))
77 "dummy"
78 with
79 | WorkerController.Worker_failed
80 (_, WorkerController.Worker_quit (Unix.WEXITED i))
82 i = 2)
84 let test_simple_worker_spawn heap_handle () =
85 let workers = make_worker heap_handle in
86 match workers with
87 | [] ->
88 Printf.eprintf "Failed to create workers";
89 false
90 | worker :: _ -> call_and_verify_result worker (fun () -> "hello") () "hello"
92 let make_tests handle =
94 ("simple_worker_spawn_test", test_simple_worker_spawn handle);
95 ( "worker_uncaught_exception_exits_with_2",
96 test_worker_uncaught_exception_exits_with_2 handle );
97 ( "wrapped_worker_with_custom_exit",
98 test_wrapped_worker_with_custom_exit handle );
101 let () =
102 Daemon.check_entry_point ();
104 (* this call might not return *)
105 let heap_handle =
106 SharedMem.init ~num_workers GlobalConfig.default_sharedmem_config
108 let tests = make_tests heap_handle in
109 Unit_test.run_all tests