Commits

Oliver Gu committed 4c5bd70

Delayed the construction of randomly generated responses

  • Participants
  • Parent commits cafc3cc

Comments (0)

Files changed (4)

File lib_test/client_test.ml

   "server-time" >:: (fun () ->
     with_tws_client (fun tws ->
       let module R = Response.Server_time in
+      let gen_server_time = Lazy.force Gen.server_time in
       Tws.server_time_exn tws
       >>| fun time ->
       let server_time = R.create ~time in
       assert_response_equal
         (module R : Response_intf.S with type t = R.t)
-        ~expected:Gen.server_time
+        ~expected:gen_server_time
         ~actual:server_time;
       Log.Global.sexp ~level:`Debug server_time R.sexp_of_t)
   );
   "market-data" >:: (fun () ->
     with_tws_client (fun tws ->
       let module W = Tws.Market_data in
+      let gen_market_data = Lazy.force Gen.market_data in
       Tws.market_data_exn tws ~contract:(Rg.contract_g ())
       >>= fun (reader, id) ->
-      Pipe.read_exactly reader ~num_values:(List.length Gen.market_data)
+      Pipe.read_exactly reader ~num_values:(List.length gen_market_data)
       >>| fun read_result ->
       Exn.protectx read_result ~f:(function
       | `Eof
       | `Fewer _ -> assert false
       | `Exactly result ->
-        List.iter2_exn Gen.market_data (Queue.to_list result)
+        List.iter2_exn gen_market_data (Queue.to_list result)
           ~f:(fun gen_tick tick ->
             assert_wrapper_equal
               (module W : Response_intf.Wrapper.S with type t = W.t)
   "option-price" >:: (fun () ->
     with_tws_client (fun tws ->
       let module R = Response.Tick_option in
+      let gen_tick_option = Lazy.force Gen.tick_option in
       Tws.option_price_exn tws
         ~contract:(Rg.option_g ())
         ~volatility:(Rg.pfg ())
         ~underlying_price:(Rg.price_g ())
       >>| fun opt_price ->
-      let gen_opt_price = Option.value_exn (R.option_price Gen.tick_option) in
+      let gen_opt_price = Option.value_exn (R.option_price gen_tick_option) in
       assert (Price.(=.) gen_opt_price opt_price);
       Log.Global.sexp ~level:`Debug opt_price Price.sexp_of_t)
   );
   "implied-volatility" >:: (fun () ->
     with_tws_client (fun tws ->
       let module R = Response.Tick_option in
+      let gen_tick_option = Lazy.force Gen.tick_option in
       Tws.implied_volatility_exn tws
         ~contract:(Rg.option_g ())
         ~option_price:(Rg.price_g ())
         ~underlying_price:(Rg.price_g ())
       >>| fun implied_vol ->
-      let gen_implied_vol = Option.value_exn (R.implied_volatility Gen.tick_option) in
+      let gen_implied_vol = Option.value_exn (R.implied_volatility gen_tick_option) in
       assert (Float.(=.) gen_implied_vol implied_vol);
       Log.Global.sexp ~level:`Debug implied_vol <:sexp_of< float >>)
   );
   "submit-orders" >:: (fun () ->
     with_tws_client (fun tws ->
       let module R = Response.Order_status in
+      let gen_order_states = Lazy.force Gen.order_states in
       Tws.submit_order_exn tws ~contract:(Rg.contract_g ()) ~order:(Rg.order_g ())
       >>= fun (reader, oid) ->
-      Pipe.read_exactly reader ~num_values:(List.length Gen.order_states)
+      Pipe.read_exactly reader ~num_values:(List.length gen_order_states)
       >>| fun read_result ->
       Exn.protectx read_result ~f:(function
       | `Eof
       | `Fewer _ -> assert false
       | `Exactly result ->
-        List.iter2_exn Gen.order_states (Queue.to_list result)
+        List.iter2_exn gen_order_states (Queue.to_list result)
           ~f:(fun gen_order_state order_state ->
             assert_response_equal
               (module R : Response_intf.S with type t = R.t)
   "filter-executions" >:: (fun () ->
     with_tws_client (fun tws ->
       let module R = Response.Execution_report in
+      let gen_execution_reports = Lazy.force Gen.execution_reports in
       Tws.filter_executions_exn tws ~contract:(Rg.contract_g ())
         ~order_action:(Rg.order_action_g ())
       >>= fun reader ->
       Pipe.read_all reader
       >>| fun result ->
-      List.iter2_exn Gen.execution_reports (Queue.to_list result)
+      List.iter2_exn gen_execution_reports (Queue.to_list result)
         ~f:(fun gen_execution_report execution_report ->
           assert_response_equal
             (module R : Response_intf.S with type t = R.t)
   "contract-specs" >:: (fun () ->
     with_tws_client (fun tws ->
       let module R = Response.Contract_specs in
+      let gen_contract_specs = Lazy.force Gen.contract_specs in
       Tws.contract_specs_exn tws ~contract:(Rg.contract_g ())
       >>| fun contract_specs ->
       assert_response_equal
         (module R : Response_intf.S with type t = R.t)
-        ~expected:Gen.contract_specs
+        ~expected:gen_contract_specs
         ~actual:contract_specs;
       Log.Global.sexp ~level:`Debug contract_specs R.sexp_of_t)
   );
   "market-depth" >:: (fun () ->
     with_tws_client (fun tws ->
       let module R = Response.Book_update in
+      let gen_book_updates = Lazy.force Gen.book_updates in
       Tws.market_depth_exn tws ~contract:(Rg.contract_g ())
       >>= fun (reader, id) ->
-      Pipe.read_exactly reader ~num_values:(List.length Gen.book_updates)
+      Pipe.read_exactly reader ~num_values:(List.length gen_book_updates)
       >>| fun read_result ->
       Exn.protectx read_result ~f:(function
       | `Eof
       | `Fewer _ -> assert false
       | `Exactly result ->
-        List.iter2_exn Gen.book_updates (Queue.to_list result)
+        List.iter2_exn gen_book_updates (Queue.to_list result)
           ~f:(fun gen_book_update book_update ->
             assert_response_equal
               (module R : Response_intf.S with type t = R.t)
   "historical-data" >:: (fun () ->
     with_tws_client (fun tws ->
       let module R = Response.Historical_data in
+      let gen_historical_data = Lazy.force Gen.historical_data in
       Tws.historical_data_exn tws ~contract:(Rg.contract_g ())
       >>| fun historical_data ->
       assert_response_equal
         (module R : Response_intf.S with type t = R.t)
-        ~expected:Gen.historical_data
+        ~expected:gen_historical_data
         ~actual:historical_data;
       Log.Global.sexp ~level:`Debug historical_data R.sexp_of_t)
   );
   "realtime-bars" >:: (fun () ->
     with_tws_client (fun tws ->
       let module R = Response.Realtime_bar in
+      let gen_realtime_bars = Lazy.force Gen.realtime_bars in
       Tws.realtime_bars_exn tws ~contract:(Rg.contract_g ())
       >>= fun (reader, id) ->
-      Pipe.read_exactly reader ~num_values:(List.length Gen.realtime_bars)
+      Pipe.read_exactly reader ~num_values:(List.length gen_realtime_bars)
       >>| fun read_result ->
       Exn.protectx read_result ~f:(function
       | `Eof
       | `Fewer _ -> assert false
       | `Exactly result ->
-        List.iter2_exn Gen.realtime_bars (Queue.to_list result)
+        List.iter2_exn gen_realtime_bars (Queue.to_list result)
           ~f:(fun gen_realtime_bar realtime_bar ->
             assert_response_equal
               (module R : Response_intf.S with type t = R.t)

File lib_test/gen.ml

 
 open Rg.R
 
-let server_time       = server_time_g ()
-let market_data       = market_data_g ()
-let tick_option       = tick_option_g ()
-let order_states      = order_states_g ()
-let execution_reports = execution_reports_g ()
-let contract_specs    = contract_specs_g ()
-let book_updates      = book_updates_g ()
-let historical_data   = historical_data_g ()
-let realtime_bars     = realtime_bars_g ()
+let server_time       = lazy (server_time_g ())
+let market_data       = lazy (market_data_g ())
+let tick_option       = lazy (tick_option_g ())
+let order_states      = lazy (order_states_g ())
+let execution_reports = lazy (execution_reports_g ())
+let contract_specs    = lazy (contract_specs_g ())
+let book_updates      = lazy (book_updates_g ())
+let historical_data   = lazy (historical_data_g ())
+let realtime_bars     = lazy (realtime_bars_g ())

File lib_test/gen.mli

 
 open Ibx.Std
 
-val server_time       : Server_time.t
-val market_data       : Market_data.t list
-val tick_option       : Tick_option.t
-val order_states      : Order_status.t list
-val execution_reports : Execution_report.t list
-val contract_specs    : Contract_specs.t
-val book_updates      : Book_update.t list
-val historical_data   : Historical_data.t
-val realtime_bars     : Realtime_bar.t list
+val server_time       : Server_time.t Lazy.t
+val market_data       : Market_data.t list Lazy.t
+val tick_option       : Tick_option.t Lazy.t
+val order_states      : Order_status.t list Lazy.t
+val execution_reports : Execution_report.t list Lazy.t
+val contract_specs    : Contract_specs.t Lazy.t
+val book_updates      : Book_update.t list Lazy.t
+val historical_data   : Historical_data.t Lazy.t
+val realtime_bars     : Realtime_bar.t list Lazy.t

File lib_test/simulation_server.ml

             tag      = V.Server_time;
             version  = 1;
             query_id = None;
-            data     = to_tws pickler Gen.server_time }
+            data     = to_tws pickler (Lazy.force Gen.server_time) }
           ]
 
         | S.Set_server_log_level -> []
         (* ======================== Market data ========================== *)
 
         | S.Market_data ->
-          let market_data =  List.map Gen.market_data ~f:(function
+          let market_data =  List.map (Lazy.force Gen.market_data) ~f:(function
             | `Tick_price x ->
               let pickler = Only_in_test.force R.Tick_price.pickler in
               E.Server_response {
             tag      = V.Tick_option;
             version  = 6;
             query_id = query.Query.id;
-            data     = to_tws pickler Gen.tick_option }
+            data     = to_tws pickler (Lazy.force Gen.tick_option) }
           ]
 
         | S.Cancel_option_price
 
         | S.Submit_order ->
           let pickler = Only_in_test.force R.Order_status.pickler in
-          List.map Gen.order_states ~f:(fun x ->
+          List.map (Lazy.force Gen.order_states) ~f:(fun x ->
             E.Server_response {
               Response.
               tag      = V.Order_status;
         (* ======================== Executions =========================== *)
 
         | S.Execution_reports ->
-          List.map Gen.execution_reports ~f:(fun x ->
+          List.map (Lazy.force Gen.execution_reports) ~f:(fun x ->
             let pickler = Only_in_test.force R.Execution_report.pickler in
             E.Server_response {
               Response.
             tag      = V.Contract_data;
             version  = 8;
             query_id = query.Query.id;
-            data     = to_tws pickler Gen.contract_specs }
+            data     = to_tws pickler (Lazy.force Gen.contract_specs) }
           ; E.Server_response {
             Response.
             tag      = V.Contract_data_end;
         (* ========================= Market depth ======================== *)
 
         | S.Market_depth ->
-          List.map Gen.book_updates ~f:(fun x ->
+          List.map (Lazy.force Gen.book_updates) ~f:(fun x ->
             let pickler = Only_in_test.force R.Book_update.pickler in
             E.Server_response {
               Response.
             tag      = V.Historical_data;
             version  = 3;
             query_id = query.Query.id;
-            data     = to_tws pickler Gen.historical_data }
+            data     = to_tws pickler (Lazy.force Gen.historical_data) }
           ]
 
         | S.Cancel_historical_data -> []
         (* ======================= Realtime data ========================= *)
 
         | S.Realtime_bars ->
-          List.map Gen.realtime_bars ~f:(fun x ->
+          List.map (Lazy.force Gen.realtime_bars) ~f:(fun x ->
             let pickler = Only_in_test.force R.Realtime_bar.pickler in
             E.Server_response {
               Response.