Add flag shm_use_sharded_hashtbl placeholder
[hiphop-php.git] / hphp / hack / test / unit / deptable / dependency_table_tests.ml
blobc55b18c9ad9c11d52d7afaed5776d10eb9dbe8f8
1 (*
2 * Copyright (c) 2015, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
9 *)
11 (* These are basically the same tests as in
12 * test/integration_ml/saved_state/test_naming_table_sqlite_fallback.ml, but
13 * as stripped down to just the basics as possible to make finding the root
14 * cause of test failures easier. *)
16 open Hh_prelude
18 module Types_pos_asserter = Asserter.Make_asserter (struct
19 type t = FileInfo.pos * Naming_types.kind_of_type
21 let to_string (pos, type_of_type) =
22 Printf.sprintf
23 "(%s, %d)"
24 (FileInfo.show_pos pos)
25 (Naming_types.kind_of_type_to_enum type_of_type)
27 let is_equal = Poly.( = )
28 end)
30 module Pos_asserter = Asserter.Make_asserter (struct
31 type t = FileInfo.pos
33 let to_string pos = Printf.sprintf "(%s)" (FileInfo.show_pos pos)
35 let is_equal = FileInfo.equal_pos
36 end)
38 let files =
40 ("foo.php", {|<?hh
41 class Foo {}
42 |});
43 ("bar.php", {|<?hh
44 function bar(): void {}
45 |});
46 ("baz.php", {|<?hh
47 type Baz = Foo;
48 |});
49 ("qux.php", {|<?hh
50 const int Qux = 5;
51 |});
54 let write_and_parse_test_files ctx =
55 let files =
56 List.map files ~f:(fun (fn, contents) ->
57 (Relative_path.from_root ~suffix:fn, contents))
59 List.iter files ~f:(fun (fn, contents) ->
60 let fn = Path.make (Relative_path.to_absolute fn) in
61 let dir = Path.dirname fn in
62 Disk.mkdir_p (Path.to_string dir);
63 Disk.write_file ~file:(Path.to_string fn) ~contents);
64 let (file_infos, errors, failed_parsing) =
65 Parsing_service.go
66 ctx
67 None
68 Relative_path.Set.empty
69 ~get_next:(MultiWorker.next None (List.map files ~f:fst))
70 ParserOptions.default
71 ~trace:true
73 if not (Errors.is_empty errors) then (
74 Errors.iter_error_list
75 (fun e ->
76 List.iter (Errors.to_list_ e) ~f:(fun (pos, msg) ->
77 eprintf
78 "%s: %s\n"
79 (Pos.string
80 (Pos.to_absolute @@ Pos_or_decl.unsafe_to_raw_pos pos))
81 msg))
82 errors;
83 failwith "Expected no errors from parsing."
85 if not (Relative_path.Set.is_empty failed_parsing) then
86 failwith "Expected all files to pass parsing.";
87 Naming_table.create file_infos
89 let run_test f =
90 Tempfile.with_real_tempdir (fun path ->
91 Relative_path.set_path_prefix
92 Relative_path.Root
93 (Path.concat path "root/");
94 let config =
95 SharedMem.
97 global_size = 1024;
98 heap_size = 1024 * 1024;
99 dep_table_pow = 16;
100 hash_table_pow = 10;
101 shm_dirs = [];
102 shm_use_sharded_hashtbl = false;
103 shm_min_avail = 0;
104 log_level = 0;
105 sample_rate = 0.0;
106 compression = 0;
109 let (_ : SharedMem.handle) = SharedMem.init config ~num_workers:0 in
110 let ctx =
111 Provider_context.empty_for_test
112 ~popt:ParserOptions.default
113 ~tcopt:TypecheckerOptions.default
114 ~deps_mode:Typing_deps_mode.SQLiteMode
117 let unbacked_naming_table = write_and_parse_test_files ctx in
118 let db_name = Path.to_string (Path.concat path "naming_table.sqlite") in
119 let save_results = Naming_table.save unbacked_naming_table db_name in
120 Asserter.Int_asserter.assert_equals
122 Naming_sqlite.(save_results.files_added + save_results.symbols_added)
123 "Expected to add eight rows (four files and four symbols)";
124 let (_backed_naming_table : Naming_table.t) =
125 Naming_table.load_from_sqlite ctx db_name
127 f ();
128 true)
130 let test_dep_graph_blob () =
131 run_test (fun () ->
132 Tempfile.with_tempdir @@ fun dir ->
133 let workers = None in
134 let delegate_state = Typing_service_delegate.default in
135 let opts = TypecheckerOptions.default in
136 let dynamic_view_files = Relative_path.Set.empty in
137 let memory_cap = None in
138 let check_info =
139 Typing_service_types.
141 init_id = "123";
142 recheck_id = None;
143 profile_log = false;
144 profile_type_check_duration_threshold = 0.0;
145 profile_type_check_memory_threshold_mb = 0;
146 profile_type_check_twice = false;
147 profile_decling = Typing_service_types.DeclingOff;
150 let ctx =
151 Provider_context.empty_for_test
152 ~popt:ParserOptions.default
153 ~tcopt:opts
154 ~deps_mode:Typing_deps_mode.SQLiteMode
157 (* Check reentrancy *)
158 for i = 0 to 2 do
159 let { Typing_check_service.errors; _ } =
160 Typing_check_service.go
162 workers
163 delegate_state
164 (Telemetry.create ())
165 dynamic_view_files
166 [Relative_path.from_root ~suffix:"baz.php"]
167 ~memory_cap
168 ~longlived_workers:false
169 ~remote_execution:None
170 ~check_info
173 Asserter.Bool_asserter.assert_equals
174 true
175 (Errors.is_empty errors)
176 "Unexpected type errors";
178 Asserter.Int_asserter.assert_equals
180 (SharedMem.get_in_memory_dep_table_entry_count ())
181 "Expected the correct # of edges in memory after saving dep table blob";
183 let filename = Path.(to_string (concat dir "deptable.bin")) in
184 let edges =
185 SharedMem.save_dep_table_blob
186 filename
187 "build_revision"
188 ~reset_state_after_saving:true
191 Asserter.Int_asserter.assert_equals
193 edges
194 "Expected # of edges to be correct";
196 Asserter.Int_asserter.assert_equals
198 (SharedMem.get_in_memory_dep_table_entry_count ())
199 "Expected 0 edges in memory after saving dep table blob"
200 done)
202 let () = Unit_test.run_all [("test_dep_graph_blob", test_dep_graph_blob)]