{"id":13414108,"url":"https://github.com/jonboulle/clockwork","last_synced_at":"2025-03-14T20:31:07.616Z","repository":{"id":42996066,"uuid":"23845253","full_name":"jonboulle/clockwork","owner":"jonboulle","description":"a fake clock for golang","archived":false,"fork":false,"pushed_at":"2025-01-02T13:06:52.000Z","size":100,"stargazers_count":683,"open_issues_count":5,"forks_count":59,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-03-06T18:52:44.710Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonboulle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-09-09T18:24:00.000Z","updated_at":"2025-02-21T12:00:01.000Z","dependencies_parsed_at":"2023-11-30T05:26:41.213Z","dependency_job_id":"8e9a8b52-8905-4559-8baf-81adf6dfdd70","html_url":"https://github.com/jonboulle/clockwork","commit_stats":{"total_commits":79,"total_committers":23,"mean_commits":"3.4347826086956523","dds":0.7468354430379747,"last_synced_commit":"7e524bd2b23863a60332249b53a095fc9370603a"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonboulle%2Fclockwork","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonboulle%2Fclockwork/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonboulle%2Fclockwork/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonboulle%2Fclockwork/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonboulle","download_url":"https://codeload.github.com/jonboulle/clockwork/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243642088,"owners_count":20323954,"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-07-30T20:01:57.898Z","updated_at":"2025-03-14T20:31:07.279Z","avatar_url":"https://github.com/jonboulle.png","language":"Go","readme":"# clockwork\n\n[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge-flat.svg)](https://github.com/avelino/awesome-go#utilities)\n\n[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/jonboulle/clockwork/ci.yaml?style=flat-square)](https://github.com/jonboulle/clockwork/actions?query=workflow%3ACI)\n[![Go Report Card](https://goreportcard.com/badge/github.com/jonboulle/clockwork?style=flat-square)](https://goreportcard.com/report/github.com/jonboulle/clockwork)\n![Go Version](https://img.shields.io/badge/go%20version-%3E=1.15-61CFDD.svg?style=flat-square)\n[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white\u0026style=flat-square)](https://pkg.go.dev/mod/github.com/jonboulle/clockwork)\n\n**A simple fake clock for Go.**\n\n\n## Usage\n\nReplace uses of the `time` package with the `clockwork.Clock` interface instead.\n\nFor example, instead of using `time.Sleep` directly:\n\n```go\nfunc myFunc() {\n\ttime.Sleep(3 * time.Second)\n\tdoSomething()\n}\n```\n\nInject a clock and use its `Sleep` method instead:\n\n```go\nfunc myFunc(clock clockwork.Clock) {\n\tclock.Sleep(3 * time.Second)\n\tdoSomething()\n}\n```\n\nNow you can easily test `myFunc` with a `FakeClock`:\n\n```go\nfunc TestMyFunc(t *testing.T) {\n\tctx := context.Background()\n\tc := clockwork.NewFakeClock()\n\n\t// Start our sleepy function\n\tvar wg sync.WaitGroup\n\twg.Add(1)\n\tgo func() {\n\t\tmyFunc(c)\n\t\twg.Done()\n\t}()\n\n\t// Ensure we wait until myFunc is waiting on the clock.\n\t// Use a context to avoid blocking forever if something\n\t// goes wrong.\n\tctx, cancel := context.WithTimeout(ctx, 10*time.Second)\n\tdefer cancel()\n\tc.BlockUntilContext(ctx, 1)\n\n\tassertState()\n\n\t// Advance the FakeClock forward in time\n\tc.Advance(3 * time.Second)\n\n\t// Wait until the function completes\n\twg.Wait()\n\n\tassertState()\n}\n```\n\nand in production builds, simply inject the real clock instead:\n\n```go\nmyFunc(clockwork.NewRealClock())\n```\n\nSee [example_test.go](example_test.go) for a full example.\n\n\n# Credits\n\nclockwork is inspired by @wickman's [threaded fake clock](https://gist.github.com/wickman/3840816), and the [Golang playground](https://blog.golang.org/playground#TOC_3.1.)\n\n\n## License\n\nApache License, Version 2.0. Please see [License File](LICENSE) for more information.\n","funding_links":[],"categories":["Go","Utilities","公用事业公司","工具库`可以提升效率的通用代码库和工具`","Utility","工具库"],"sub_categories":["HTTP Clients","Utility/Miscellaneous","实用程序/Miscellaneous","查询语","Fail injection"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonboulle%2Fclockwork","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonboulle%2Fclockwork","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonboulle%2Fclockwork/lists"}