t_job.ml 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. open Seppo_lib
  2. type t_f = Csexp.t -> (unit, string) result Lwt.t option
  3. let test_do_wait () =
  4. let now = Ptime.epoch
  5. and jitter = 0.0 in
  6. Job.do_wait ~now ~jitter 1
  7. |> Job.rfc3339
  8. |> Assrt.equals_string __LOC__ "1970-01-01T000100Z";
  9. Job.do_wait ~now ~jitter 10
  10. |> Job.rfc3339
  11. |> Assrt.equals_string __LOC__ "1970-01-01T170300Z";
  12. Job.do_wait ~now ~jitter 13 (* maximum wait interval *)
  13. |> Job.rfc3339
  14. |> Assrt.equals_string __LOC__ "1970-01-06T163100Z";
  15. Job.do_wait ~now ~jitter 14 (* maximum wait all interval summed up *)
  16. |> Job.rfc3339
  17. |> Assrt.equals_string __LOC__ "1970-01-12T090300Z"
  18. let test_compute_fn () =
  19. let now = Ptime.epoch
  20. and jitter = 0. in
  21. let now = Job.do_wait ~now ~jitter 10 |> Job.rfc3339
  22. and nonce = "foo bar baz" |> Bytes.of_string |> Job.compute_nonce in
  23. Job.compute_fn now 10 nonce
  24. |> Assrt.equals_string __LOC__ "1970-01-01T170300Z.10.776976811.s"
  25. let test_job () =
  26. Logr.debug (fun m -> m "%s" "job_test.job");
  27. let run_job_yes : t_f = function
  28. | Csexp.Atom s ->
  29. Logr.debug (fun m -> m "%s run_job_yes: %s" "job_test.job" s);
  30. Some (Lwt.return (Ok ()))
  31. | _ -> None in
  32. let run_job_no _ =
  33. Logr.debug (fun m -> m "%s run_job_no" "job_test.job");
  34. None in
  35. (* call functions from fkts with argument v until first return Some _ *)
  36. let run_until_some (fkts : t_f list) (v : Csexp.t) =
  37. fkts |> List.fold_left
  38. (fun r fkt ->
  39. match r with
  40. | Some _ as r -> r (* we have a result, don't call more workers *)
  41. | None -> fkt v
  42. )
  43. None
  44. in
  45. match "fil" ,
  46. (Csexp.Atom "xyz")
  47. |> run_until_some [run_job_no; run_job_no; run_job_yes; run_job_yes; run_job_no]
  48. with
  49. | _,None -> Lwt.return ()
  50. | fn,Some x ->
  51. let%lwt x = x in
  52. (match x with
  53. | Ok () ->
  54. fn |> Assrt.equals_string __LOC__ "fil" |> Lwt.return
  55. | Error x ->
  56. x |> Assrt.equals_string __LOC__ "" |> Lwt.return
  57. )
  58. let test_jitter () =
  59. let now = Ptime.epoch in
  60. let jitter = 0.0 in
  61. Job.do_wait ~now ~jitter 3 |> Job.rfc3339
  62. |> Assrt.equals_string __LOC__ "1970-01-01T000700Z";
  63. Job.do_wait ~now ~jitter 13 |> Job.rfc3339
  64. |> Assrt.equals_string __LOC__ "1970-01-06T163100Z";
  65. let jitter = -0.05 in
  66. Job.do_wait ~now ~jitter 3 |> Job.rfc3339
  67. |> Assrt.equals_string __LOC__ "1970-01-01T000639Z";
  68. Job.do_wait ~now ~jitter 13 |> Job.rfc3339
  69. |> Assrt.equals_string __LOC__ "1970-01-06T094127Z";
  70. ()
  71. let () =
  72. test_do_wait ();
  73. test_compute_fn ();
  74. test_jitter ();
  75. Lwt_main.run (test_job ())