text
stringlengths
0
601k
let update_conn t = match t . socket with | Some s -> ( U . set_nonblock s ; let connected = try U . read s t . dummy_one_byte 0 1 != 0 with | U . Unix_error ( U . EAGAIN , _ , _ ) -> true | _ -> false in ( U . clear_nonblock s ; if not connected then close t ) ) | None -> ( )
let write t buf start offset = update_conn t ; connect_if_needed t ; match t . socket with | Some s -> ( try Some ( U . single_write s buf start offset ) with U . Unix_error ( e , fn , p ) -> ( print_unix_error " write error " e fn p ; close t ; None ) ) | None -> ( prerr_endline " write error : not connected " ; close...
let setup_test_server ( ) = let socket = U . socket U . PF_INET U . SOCK_STREAM 0 in U . setsockopt socket U . TCP_NODELAY true ; U . set_close_on_exec socket ; U . bind socket ( U . ADDR_INET ( U . inet_addr_any , 0 ) ) ; U . listen socket 50 ; let recv_buf = Buffer . create 512 in let current_accept_socket = ref None...
let test_inet_sender_write ( ) = let ( socket , port , get_accept_socket , get_recv_data ) = setup_test_server ( ) in let sender = Stream_sender . create ( Stream_sender . INET ( " localhost " , port ) ) 3 in let msg1 = " helloworld " in let msg_len = String . length msg1 in assert_equal ( Some msg_len ) ( Stream_sende...
let suite = " Fluent_logger tests " >::: [ " test_inet_sender_write " >:: test_inet_sender_write ]
let ( ) = Printexc . record_backtrace true ; ignore ( run_test_tt_main suite )
let subject_try a = Alcotest . testable ( Try . pp ( Alcotest . pp a ) ) ( Try . equal ( Alcotest . equal a ) ) ; ;
let rec numbers n = stream n ( lazy ( numbers ( n + 1 ) ) )
let naturals = numbers 0
let should_extract ( ) = let expected = 0 and computed = extract naturals in Alcotest . ( check int ) " should_extract " expected computed ; ;
let should_extend ( ) = let increment s = 1 + extract s in let expected = 1 and computed = extract @@ extend increment naturals in Alcotest . ( check int ) " should_extend " expected computed ; ;
let should_infix_extend ( ) = let increment s = 1 + extract s in let expected = 1 and computed = extract ( increment <<= naturals ) in Alcotest . ( check int ) " should_extend " expected computed ; ;
let should_flipped_infix_extend ( ) = let increment s = 1 + extract s in let expected = 1 and computed = extract ( naturals =>> increment ) in Alcotest . ( check int ) " should_extend " expected computed ; ;
let should_compose_right_to_left ( ) = let increment s = 1 + extract s in let to_string s = string_of_int @@ extract s in let expected = " 1 " and computed = compose_right_to_left to_string increment naturals in Alcotest . ( check string ) " should_compose_right_to_left " expected computed ; ;
let should_lift ( ) = let increment s = 1 + s in let expected = 1 and computed = extract @@ lift increment naturals in Alcotest . ( check int ) " should_lift " expected computed ; ;
let should_lift2 ( ) = let adder a b = a + b in let expected = 42 and computed = extract @@ lift2 adder ( numbers 2 ) ( numbers 40 ) in Alcotest . ( check int ) " should_lift2 " expected computed ; ;
let should_lift3 ( ) = let adder a b c = a + b + c in let expected = 42 and computed = extract @@ lift3 adder ( numbers 2 ) ( numbers 10 ) ( numbers 30 ) in Alcotest . ( check int ) " should_lift3 " expected computed ; ;
let should_syntax_extend ( ) = let increment s = 1 + extract s in let expected = 1 and computed = let @ x = naturals in increment x in Alcotest . ( check int ) " should_syntax_extend " expected @@ extract computed ; ;
let should_compose_left_to_right ( ) = let increment s = 1 + extract s in let to_string s = string_of_int @@ extract s in let expected = " 1 " and computed = compose_left_to_right increment to_string naturals in Alcotest . ( check string ) " should_compose_left_to_right " expected computed ; ;
let should_infix_compose_right_to_left ( ) = let increment s = 1 + extract s in let to_string s = string_of_int @@ extract s in let expected = " 1 " and computed = ( to_string =<= increment ) naturals in Alcotest . ( check string ) " should_infix_compose_right_to_left " expected computed ; ;
let should_infix_compose_left_to_right ( ) = let increment s = 1 + extract s in let to_string s = string_of_int @@ extract s in let expected = " 1 " and computed = ( increment =>= to_string ) naturals in Alcotest . ( check string ) " should_infix_compose_left_to_right " expected computed ; ;
let should_apply ( ) = let rec increments n = stream ( fun x -> x + n ) ( lazy ( increments ( n + 1 ) ) ) in let expected = 42 and computed = extract ( numbers 1 <@@> increments 41 ) in Alcotest . ( check int ) " should_apply " expected computed ; ;
let should_inverse_apply ( ) = let rec increments n = stream ( fun x -> x + n ) ( lazy ( increments ( n + 1 ) ) ) in let expected = 42 and computed = extract ( increments 41 <@> numbers 1 ) in Alcotest . ( check int ) " should_inverse_apply " expected computed ; ;
let should_discard_first ( ) = let expected = 42 and computed = extract ( pure ( ) @> numbers 42 ) in Alcotest . ( check int ) " should_discard_first " expected computed ; ;
let should_discard_second ( ) = let expected = 42 and computed = extract ( numbers 42 <@ pure ( ) ) in Alcotest . ( check int ) " should_discard_second " expected computed ; ;
let hd_get_a_value ( ) = let expected = 42 and computed = hd ( numbers 42 ) in Alcotest . ( check int ) " should_extract_head " expected computed ; ;
let tl_get_a_stream ( ) = let expected = 43 and computed = hd ( tl ( numbers 42 ) ) in Alcotest . ( check int ) " should_extract_head_of_tail " expected computed ; ;
let cons_test ( ) = let expected = 68 and expected2 = 0 and computed = hd ( 68 <:> naturals ) and computed2 = hd ( tl ( 68 <:> naturals ) ) in Alcotest . ( check int ) " should_extract_head_of_cons " expected computed ; Alcotest . ( check int ) " should_extract_head_of_cons " expected2 computed2 ; ;
let access_test ( ) = let expected = Try . ok 1000 and computed = naturals . [ % 1000 ] in Alcotest . ( check ( subject_try int ) ) " should_access " expected computed ; ;
let access_test_2 ( ) = let expected = Try . error ( Preface_stdlib . Exn . Negative_position ( - 10 ) ) and computed = naturals . [ %- 10 ] in Alcotest . ( check ( subject_try int ) ) " access_should_fail " expected computed ; ;
let fibonacci ( ) = let rec fb a b = stream ( a + b ) ( lazy ( fb b ( a + b ) ) ) in let fib = 0 <:> stream 1 ( lazy ( fb 0 1 ) ) in Alcotest . ( check ( subject_try int ) ) " fibonacci 0 " ( Try . ok 0 ) fib . [ % 0 ] ; Alcotest . ( check ( subject_try int ) ) " fibonacci 2 " ( Try . ok 1 ) fib . [ % 2 ] ; Alcotest . ...
let drop_take_test ( ) = let open Try . Monad . Infix in let expected = Try . ok [ 4 ; 5 ; 6 ; 7 ] and computed = drop 4 naturals >>= take 4 in Alcotest . ( check ( subject_try ( list int ) ) ) " access_drop_and_take " expected computed ; ;
let take_while_test ( ) = let expected = [ 0 ; 1 ; 2 ; 3 ; 4 ] and computed = take_while ( fun x -> x < 5 ) naturals in Alcotest . ( check ( list int ) ) " should_take_while " expected computed ; ;
let drop_while_test ( ) = let expected = Try . ok [ 5 ; 6 ; 7 ; 8 ; 9 ; 10 ] and computed = drop_while ( fun x -> x < 5 ) naturals |> take 6 in Alcotest . ( check ( subject_try ( list int ) ) ) " should_drop_while " expected computed ; ;
let cases = let open Alcotest in [ ( " Stream " , [ test_case " Extract " ` Quick should_extract ; test_case " Extend " ` Quick should_extend ; test_case " Infix Extend " ` Quick should_infix_extend ; test_case " Flipped Infix Extend " ` Quick should_flipped_infix_extend ; test_case " Compose right to left " ` Quick sh...
let ( ) = let verbose = true in let t = T . test T . int ~ verbose in T . group " Stream . empty " [ t " empty length " ~ actual ( : S . len S . empty ) ~ expected : 0 ; ] ; let t = T . test T . ( list int ) ~ verbose in T . group " Stream . single " [ t " same value " ~ actual ( : S . to_list ( S . single 42 ) ) ~ exp...
module Configuration = struct type t = { name : string ; parameters : ( string * string ) list } let name t = t . name let to_json { name ; parameters } : Yojson . Basic . json = ` Assoc [ " name " , ` String name ; " parameters " , ` Assoc ( List . map parameters ~ f ( : fun ( a , b ) -> a , ` String b ) ) ; ] let gen...
let run ~ run_with ~ normal ~ tumor ~ result_prefix ( ? more_edges = [ ] ) ( ? configuration = Configuration . default ) ( ) = let open KEDSL in let open Configuration in let name = Filename . basename result_prefix in let result_file suffix = result_prefix ^ suffix in let output_dir = result_file " strelka_output " in...
let rec signature : Cpath . module_ -> ? canonical : Cpath . module_ -> Signature . t -> Signature . t = fun prefix ? canonical sg -> let sg ' , strengthened_modules = sig_items prefix ? canonical sg in let substs = List . fold_left ( fun s mid -> Subst . path_invalidate_module ( mid :> Ident . path_module ) s ) Subst ...
let wait_for_n_injections ( ? log = false ) n node = if log then Log . info " Waiting for % d injections . " n ; let seen = ref 0 in let filter json = match JSON . ( json |-> " view " |-> " request " |> as_string_opt ) with | Some s when s = " inject " -> if log then Log . info " Injection % d witnessed . " ! seen ; in...
let test_stresstest_sources_format = Protocol . register_test ~ __FILE__ ~ title " : stresstest sources format " ~ tags [ " : client " ; " stresstest " ; " sources " ] @@ fun protocol -> let transfers = 30 in let * ( node , client ) = Client . init_with_protocol ~ nodes_args : [ Synchronisation_threshold 0 ; Connection...
let check_n_applied_operations ( ? log = false ) ~ expected client = let * mempool_ops = RPC . get_mempool_pending_operations client in let n_applied = JSON . ( mempool_ops |-> " applied " |> as_list |> List . length ) in if not ( Int . equal n_applied expected ) then ( let name = Client . name client in Log . warn " M...
let wait_or_timeout ( ? timeout = 30 . ) ? sleep_at_resolution ( waiter : unit Lwt . t ) = Lwt . pick [ ( let * ( ) = waiter in match sleep_at_resolution with | None -> unit | Some delay -> Lwt_unix . sleep delay ) ; Lwt_unix . sleep timeout ; ]
let test_stresstest_applied = Protocol . register_test ~ __FILE__ ~ title " : stresstest applied " ~ tags [ " : client " ; " stresstest " ; " applied " ] @@ fun protocol -> let transfers = 30 in let * ( node , client ) = Client . init_with_protocol ~ nodes_args : [ Synchronisation_threshold 0 ; Connections 0 ; Disable_...
let test_stresstest_applied_new_bootstraps = Protocol . register_test ~ __FILE__ ~ title " : stresstest applied new bootstraps " ~ tags [ " : client " ; " stresstest " ; " applied " ] @@ fun protocol -> let transfers = 30 in let n_new_accounts = 10 in let * ( node , client ) = Client . init_with_protocol ~ nodes_args :...
let non_terminating_process ( _process : Process . t ) = ( )
let test_stresstest_applied_1op = Protocol . register_test ~ __FILE__ ~ title " : stresstest applied 1op " ~ tags [ " : client " ; " stresstest " ; " stresstest_1op " ; " applied " ] @@ fun protocol -> let target_transfers = 10 in let n_accounts = target_transfers in let * ( node , client ) = Client . init_with_protoco...
let init_nodes_star ~ protocol ~ n_nodes ~ accounts_per_node ( ? additional_node_args = [ ] ) ( ) = let * ( first_node , first_client ) = Client . init_with_protocol ~ nodes_args : ( Node . [ Synchronisation_threshold 0 ; Connections ( n_nodes - 1 ) ] @ additional_node_args ) ~ additional_bootstrap_account_count : ( ( ...
let test_stresstest_applied_multiple_nodes = Protocol . register_test ~ __FILE__ ~ title " : stresstest applied multiple nodes " ~ tags [ " : client " ; " stresstest " ; " applied " ] @@ fun protocol -> let n_nodes = 3 in let transfers_per_node = 10 in let accounts_per_node = 5 in let * ( ( ( first_node , first_client ...
let test_stresstest_applied_multiple_nodes_1op = Protocol . register_test ~ __FILE__ ~ title " : stresstest applied multiple nodes 1op " ~ tags [ " : client " ; " stresstest " ; " stresstest_1op " ; " applied " ] @@ fun protocol -> let n_nodes = 3 in let target_transfers_per_node = 4 in let accounts_per_node = target_t...
let register ~ protocols = test_stresstest_sources_format ~ protocols ; test_stresstest_applied ~ protocols ; test_stresstest_applied_new_bootstraps ~ protocols ; test_stresstest_applied_1op ~ protocols ; test_stresstest_applied_multiple_nodes ~ protocols ; test_stresstest_applied_multiple_nodes_1op ~ protocols
let show_heap_size ( ) = Gc . ( ( quick_stat ( ) ) . heap_words ) * 8 |> float_of_int |> fun bytes -> bytes . / 1024 . . / 1024 . |> Dream . log " Heap size : . % 0f MB "
let stress ( ? megabytes = 1024 ) ( ? chunk = 64 ) stream = let limit = megabytes * 1024 * 1024 in let chunk = chunk * 1024 in let chunk_a = String . make chunk ' a ' in let chunk_b = String . make chunk ' b ' in let start = Unix . gettimeofday ( ) in let rec loop sent = if sent >= limit then let % lwt ( ) = Dream . fl...
let query_int request name = Dream . query request name |> Option . map int_of_string
let ( ) = show_heap_size ( ) ; Dream . run @@ Dream . logger @@ Dream . router [ Dream . get " " / ( fun request -> Dream . stream ~ headers [ " : Content - Type " , " application / octet - stream " ] ( stress ? megabytes ( : query_int request " mb " ) ? chunk ( : query_int request " chunk " ) ) ) ; ]
let home = < html > < body > < script > function socket ( ) { var websocket = new WebSocket ( " ws :// localhost : 8080 / websocket " ) ; websocket . onmessage = function ( ) { console . log ( " message " ) ; } websocket . onclose = function ( ) { console . log ( " closed " ) ; } ; } socket ( ) ; socket ( ) ; socket ( ...
let show_heap_size ( ) = Gc . ( ( quick_stat ( ) ) . heap_words ) * 8 |> float_of_int |> fun bytes -> bytes . / 1024 . . / 1024 . |> Dream . log " Heap size : . % 0f MB "
let frame = 64 * 1024
let frame_a = String . make frame ' a '
let frame_b = String . make frame ' b '
let stress websocket = let limit = 1024 * 1024 * 1024 in let start = Unix . gettimeofday ( ) in let rec loop sent = if sent >= limit then let % lwt ( ) = Dream . close_websocket websocket in Lwt . return ( Unix . gettimeofday ( ) . - start ) else let % lwt ( ) = Dream . send websocket frame_a ~ text_or_binary ` : Binar...
let ( ) = show_heap_size ( ) ; Dream . run @@ Dream . logger @@ Dream . router [ Dream . get " " / ( fun _ -> Dream . html home ) ; Dream . get " / websocket " ( fun _ -> Dream . websocket stress ) ; ]
type ( _ , _ , _ ) _ overflow_behavior = | Crash : ( ' a , crash , unit ) unit overflow_behavior | Drop_head : ( ' a -> unit ) unit -> ( ' a , drop_head , unit ) unit overflow_behavior | Call : ( ' a -> ' r ) ' r -> ( ' a , call , ' r option ) option overflow_behavior
type _ buffered = Type_buffered
type ( _ , _ , _ ) _ type_ = | Synchronous : ( ' a , synchronous , unit Deferred . t ) t type_ | Buffered : [ ` Capacity of int ] * [ ` Overflow of ( ' a , ' b , ' r ) ' r overflow_behavior ] -> ( ' a , ' b buffered , ' r ) ' r type_
let value_or_empty = Option . value ~ default " :< unnamed " >
module Reader0 = struct type ' t t = { reader : ' t Pipe . Reader . t ; mutable has_reader : bool ; mutable downstreams : downstreams ; name : string option } and downstreams = | [ ] : downstreams | ( :: ) : ' a t * downstreams -> downstreams let rec downstreams_from_list : ' a t list -> downstreams = function | [ ] ->...
module Writer = struct type ( ' t , ' type_ , ' write_return ) ' write_return t = { type_ : ( ' t , ' type_ , ' write_return ) ' write_return type_ ; strict_reader : ' t Reader0 . t ; writer : ' t Pipe . Writer . t ; warn_on_drop : bool ; name : string option } let to_linear_pipe { writer = pipe ; _ } = pipe let handle...
let create ? name ( ? warn_on_drop = true ) true type_ = let reader , writer = Pipe . create ( ) in let strict_reader = Reader0 { . reader ; has_reader = false ; downstreams = [ ] ; name } in let strict_writer = Writer { . type_ ; strict_reader ; warn_on_drop ; writer ; name } in ( strict_reader , strict_writer ) stric...
let transfer reader Writer { . strict_reader ; writer ; _ } ~ f = Reader0 ( . reader . downstreams <- [ strict_reader ] ) ; Reader0 . enforce_single_reader reader ( Pipe . transfer reader . reader writer ~ f ) f
let rec transfer_while_writer_alive reader writer ~ f = if Pipe . is_closed writer . Writer . writer then Deferred . unit else match % bind Pipe . read reader . Reader0 . reader with | ` Ok x -> let % bind ( ) = Pipe . write_if_open writer . Writer . writer ( f x ) x in transfer_while_writer_alive reader writer ~ f | `...
module Reader = struct include Reader0 let partition_map3 reader ~ f = let ( reader_a , writer_a ) writer_a , ( reader_b , writer_b ) writer_b , ( reader_c , writer_c ) writer_c = ( create Synchronous , create Synchronous , create Synchronous ) Synchronous in don ' t_wait_for ( Reader0 . iter reader ~ f ( : fun x -> ma...
= " caml_blit_string " [ @@ noalloc ] = " caml_fill_string " [ @@ noalloc ]
let make n c = B . make n c |> bts
let init n f = B . init n f |> bts
let copy s = B . copy ( bos s ) |> bts
let sub s ofs len = B . sub ( bos s ) ofs len |> bts
let ensure_ge ( x : int ) y = if x >= y then x else invalid_arg " String . concat "
let rec sum_lengths acc seplen = function | [ ] -> acc | hd :: [ ] -> length hd + acc | hd :: tl -> sum_lengths ( ensure_ge ( length hd + seplen + acc ) acc ) seplen tl
let rec unsafe_blits dst pos sep seplen = function [ ] -> dst | hd :: [ ] -> unsafe_blit hd 0 dst pos ( length hd ) ; dst | hd :: tl -> unsafe_blit hd 0 dst pos ( length hd ) ; unsafe_blit sep 0 dst ( pos + length hd ) seplen ; unsafe_blits dst ( pos + length hd + seplen ) sep seplen tl
let concat sep = function [ ] -> " " | l -> let seplen = length sep in bts @@ unsafe_blits ( B . create ( sum_lengths 0 seplen l ) ) 0 sep seplen l
let iter f s = for i = 0 to length s - 1 do f ( unsafe_get s i ) done
let iteri f s = for i = 0 to length s - 1 do f i ( unsafe_get s i ) done
let map f s = B . map f ( bos s ) |> bts
let mapi f s = B . mapi f ( bos s ) |> bts
let is_space = function | ' ' | ' \ 012 ' | ' \ n ' | ' \ r ' | ' \ t ' -> true | _ -> false
let trim s = if s = " " then s else if is_space ( unsafe_get s 0 ) || is_space ( unsafe_get s ( length s - 1 ) ) then bts ( B . trim ( bos s ) ) else s
let escaped s = let rec escape_if_needed s n i = if i >= n then s else match unsafe_get s i with | ' " ' \ | ' ' \\ | ' \ 000 ' . . ' \ 031 ' | ' \ 127 ' . . ' \ 255 ' -> bts ( B . escaped ( bos s ) ) | _ -> escape_if_needed s n ( i + 1 ) in escape_if_needed s ( length s ) 0
let rec index_rec s lim i c = if i >= lim then raise Not_found else if unsafe_get s i = c then i else index_rec s lim ( i + 1 ) c
let index s c = index_rec s ( length s ) 0 c
let rec index_rec_opt s lim i c = if i >= lim then None else if unsafe_get s i = c then Some i else index_rec_opt s lim ( i + 1 ) c
let index_opt s c = index_rec_opt s ( length s ) 0 c
let index_from s i c = let l = length s in if i < 0 || i > l then invalid_arg " String . index_from / Bytes . index_from " else index_rec s l i c
let index_from_opt s i c = let l = length s in if i < 0 || i > l then invalid_arg " String . index_from_opt / Bytes . index_from_opt " else index_rec_opt s l i c
let rec rindex_rec s i c = if i < 0 then raise Not_found else if unsafe_get s i = c then i else rindex_rec s ( i - 1 ) c
let rindex s c = rindex_rec s ( length s - 1 ) c
let rindex_from s i c = if i < - 1 || i >= length s then invalid_arg " String . rindex_from / Bytes . rindex_from " else rindex_rec s i c
let rec rindex_rec_opt s i c = if i < 0 then None else if unsafe_get s i = c then Some i else rindex_rec_opt s ( i - 1 ) c
let rindex_opt s c = rindex_rec_opt s ( length s - 1 ) c
let rindex_from_opt s i c = if i < - 1 || i >= length s then invalid_arg " String . rindex_from_opt / Bytes . rindex_from_opt " else rindex_rec_opt s i c