{"id":18015316,"url":"https://github.com/talex5/mirage-clock-test","last_synced_at":"2025-04-04T15:16:47.285Z","repository":{"id":151668341,"uuid":"67691106","full_name":"talex5/mirage-clock-test","owner":"talex5","description":"An implementation of Mirage's CLOCK and TIME types for unit-tests","archived":false,"fork":false,"pushed_at":"2016-09-08T11:35:42.000Z","size":6,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-10T00:47:18.746Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"OCaml","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/talex5.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-08T09:56:52.000Z","updated_at":"2016-09-08T10:30:21.000Z","dependencies_parsed_at":"2023-07-24T15:45:19.838Z","dependency_job_id":null,"html_url":"https://github.com/talex5/mirage-clock-test","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/talex5%2Fmirage-clock-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/talex5%2Fmirage-clock-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/talex5%2Fmirage-clock-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/talex5%2Fmirage-clock-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/talex5","download_url":"https://codeload.github.com/talex5/mirage-clock-test/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247198466,"owners_count":20900081,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-30T04:13:41.565Z","updated_at":"2025-04-04T15:16:47.263Z","avatar_url":"https://github.com/talex5.png","language":"OCaml","readme":"# mirage-clock-test\n\nAn implementation of Mirage's CLOCK and TIME types for unit-tests.\n\nIt is often useful to test code that uses `sleep` to implement timeouts or delays.\nRunning with the real clock makes the tests run unnecessarily slow.\n`Mirage_clock_test` provides a clock that is under the control of the unit-tests and is independent of any real clock.\n\nThe `test/test.ml` file contains a simple example.\nThe `App` module represents the application being tested.\nIt runs two Lwt loops in parallel.\nOne logs \"fizz\" every three seconds, the other logs \"buzz\" every five seconds.\n\n```\nmodule App(C : V1.CLOCK)(T : V1_LWT.TIME) = struct\n  let rec loop msg delay =\n    T.sleep delay \u003e\u003e= fun () -\u003e\n    Log.info \"[%.2f] %s\" (C.time ()) msg;\n    loop msg delay\n\n  let main () =\n    Lwt.choose [\n      loop \"fizz\" 3.0;\n      loop \"buzz\" 5.0;\n    ]\nend\n```\n\nThe test code applies this to `Mirage_clock_test`, then uses the control functions `reset` and `run_to` to control the virtual clock:\n\n```\nmodule A = App(Mirage_clock_test)(Mirage_clock_test)\n\nlet () =\n  Lwt_main.run begin\n    Mirage_clock_test.reset ();\n    Mirage_clock_test.run_to 100.0;   (* Start at time t=100 *)\n    (* Start the main thread running. *)\n    Lwt.async A.main;\n    (* Nothing happens in the first second. *)\n    Mirage_clock_test.run_to 101.0;\n    Log.expect [];\n    (* Lots happens in the first 14 seconds. *)\n    Mirage_clock_test.run_to 114.0;\n    Log.expect [\n      \"[103.00] fizz\";\n      \"[105.00] buzz\";\n      \"[106.00] fizz\";\n      \"[109.00] fizz\";\n      \"[110.00] buzz\";\n      \"[112.00] fizz\";\n    ];\n    print_endline \"Tests passed!\";\n    Lwt.return ()\nend\n```\n\nNote that we stop before 15s.\nAt that point both events fire at once and so the log messages could appear in either order.\nIt's helpful to arrange your test events to happen at different times to avoid having to handle multiple cases.\n\nThis code is based on the code used for [CueKeeper's unit tests](https://github.com/talex5/cuekeeper/blob/ce81f4e3c40b79d99ac4063d22cfba4cd568e7e5/tests/test.ml#L27).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftalex5%2Fmirage-clock-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftalex5%2Fmirage-clock-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftalex5%2Fmirage-clock-test/lists"}