fix typo HAVE_DECL_CURLINFO_HTTP_CONNECTCODE
[ocurl.git] / examples / ocurl_test_threads.ml
blob01064eeee19631f8b9abf59637f58c20add92508
1 (**
2 Test threads and GC interaction in bindings
4 ocamlopt -thread -c -I .. ocurl_test_threads.ml -o ocurl_test_threads.cmx
5 ocamlopt -thread unix.cmxa threads.cmxa -I .. -ccopt -L.. curl.cmxa ocurl_test_threads.cmx -o ocurl_test_threads
6 *)
8 let read_file connection path =
9 let id = (Thread.id (Thread.self ())) in
10 Printf.printf "%u read_file %s...\n%!" id path;
11 let received_size = ref 0L in
12 let writefunction buffer =
13 let buffer_size = String.length buffer in
14 Printf.printf "W(%d)%!" buffer_size;
15 received_size := Int64.add !received_size (Int64.of_int buffer_size);
16 buffer_size
18 Curl.set_httpget connection true;
19 Curl.set_url connection path;
20 Curl.set_writefunction connection writefunction;
21 Curl.perform connection;
22 Printf.printf "%u done \n%!" id;
23 match Curl.get_responsecode connection with
24 | 200 ->
25 !received_size
26 | _ as code ->
27 failwith (Printf.sprintf "read_file %d" code)
29 let () =
30 Curl.global_init Curl.CURLINIT_GLOBALALL;
31 let f () =
32 let connection = Curl.init () in
33 let path = if Array.length Sys.argv > 1 then Sys.argv.(1) else "http://localhost:8080/files/test.tar.gz" in
34 while true do
35 ignore (read_file connection path)
36 done
38 let a = Array.init 20 (fun _ -> Thread.create f ()) in
39 Array.iter Thread.join a