{"id":17047121,"url":"https://github.com/jhalterman/concurrentunit","last_synced_at":"2025-04-12T18:45:21.374Z","repository":{"id":1137844,"uuid":"1016715","full_name":"jhalterman/concurrentunit","owner":"jhalterman","description":"Toolkit for testing multi-threaded and asynchronous applications","archived":false,"fork":false,"pushed_at":"2024-04-05T14:47:39.000Z","size":356,"stargazers_count":423,"open_issues_count":13,"forks_count":46,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-04-03T20:13:02.716Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","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/jhalterman.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","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":"2010-10-23T00:21:24.000Z","updated_at":"2025-03-07T04:07:18.000Z","dependencies_parsed_at":"2024-10-29T20:24:50.729Z","dependency_job_id":null,"html_url":"https://github.com/jhalterman/concurrentunit","commit_stats":{"total_commits":139,"total_committers":7,"mean_commits":"19.857142857142858","dds":0.07913669064748197,"last_synced_commit":"df7c650260c8ecc8f86e753ce09209e61982ba46"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhalterman%2Fconcurrentunit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhalterman%2Fconcurrentunit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhalterman%2Fconcurrentunit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhalterman%2Fconcurrentunit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jhalterman","download_url":"https://codeload.github.com/jhalterman/concurrentunit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248617343,"owners_count":21134190,"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-14T09:48:30.559Z","updated_at":"2025-04-12T18:45:21.353Z","avatar_url":"https://github.com/jhalterman.png","language":"Java","readme":"# ConcurrentUnit\n\n[![Build Status](https://github.com/jhalterman/concurrentunit/workflows/build/badge.svg)](https://github.com/jhalterman/concurrentunit/actions)\n[![Maven Central](https://img.shields.io/maven-central/v/net.jodah/concurrentunit.svg?maxAge=60\u0026colorB=53C92E)](https://maven-badges.herokuapp.com/maven-central/net.jodah/concurrentunit)\n[![License](http://img.shields.io/:license-apache-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)\n[![JavaDoc](https://img.shields.io/maven-central/v/net.jodah/concurrentunit.svg?maxAge=60\u0026label=javadoc\u0026color=blue)](https://jodah.net/concurrentunit/javadoc/)\r\n\r\nA simple, zero-dependency toolkit for testing multi-threaded code. Supports Java 1.6+.\r\n\r\n## Introduction\r\n\r\nConcurrentUnit was created to help developers test multi-threaded or asynchronous code. It allows you to perform assertions and wait for operations in any thread, with failures being properly reported back to the main test thread. If an assertion fails, your test fails, regardless of which thread the assertion came from.\r\n\r\n## Usage\r\n\r\n1. Create a `Waiter`\r\n2. Use `Waiter.await` to block the main test thread.\r\n3. Use the `Waiter.assert` calls from any thread to perform assertions. \r\n4. Once expected assertions are completed, use `Waiter.resume` call to unblock the `await`ing thread.\r\n\nWhen your test runs, assertion failures will result in the main thread being interrupted and the failure thrown. If an `await` call times out before all expected `resume` calls occur, the test will fail with a `TimeoutException`.\r\n\r\n## Examples\n\nConsider a test for a message bus that delivers messages asynchronously:\r\n\n```java\r\n@Test\npublic void shouldDeliverMessage() throws Throwable {\n  final Waiter waiter = new Waiter();\n\n  messageBus.registerHandler(message -\u003e {\n    // Called on separate thread\n    waiter.assertEquals(message, \"foo\");\n    waiter.resume();\n  };\n  \n  messageBus.send(\"foo\");\n  \n  // Wait for resume() to be called\n  waiter.await(1000);\n}\r\n```\n\nWe can also handle wait for multiple `resume` calls:\r\n\n```java\r\n@Test\npublic void shouldDeliverMessages() throws Throwable {\n  final Waiter waiter = new Waiter();\n\n  messageBus.registerHandler(message -\u003e {\n    waiter.assertEquals(message, \"foo\");\n    waiter.resume();\n  };\n  \n  messageBus.send(\"foo\");\n  messageBus.send(\"foo\");\n  messageBus.send(\"foo\");\n  \n  // Wait for resume() to be called 3 times\n  waiter.await(1000, 3);\n}\r\n```\n\r\nIf an assertion fails in any thread, the test will fail as expected:\r\n\n```java\r\n@Test(expected = AssertionError.class)\npublic void shouldFail() throws Throwable {\n  final Waiter waiter = new Waiter();\n\n  new Thread(() -\u003e {\n    waiter.assertTrue(false);\n  }).start();\n  \n  waiter.await();\n}\n```\r\n\r\nTimeoutException is thrown if `resume` is not called before the `await` time is exceeded:\r\n\n```java\r\n@Test(expected = TimeoutException.class)\npublic void shouldTimeout() throws Throwable {\n  new Waiter().await(1);\n}\n```\n\n#### Alternatively\n\nAs a more concise alternative to using the `Waiter` class, you can extend the `ConcurrentTestCase`:\n\n```java\nclass SomeTest extends ConcurrentTestCase {\n  @Test\n  public void shouldSucceed() throws Throwable {\n    new Thread(() -\u003e {\n      doSomeWork();\n      threadAssertTrue(true);\n      resume();\n    }).start();\n\n    await(1000);\n  }\n}\n```\n\n#### Assertions\n\nConcurrentUnit's `Waiter` supports the standard assertions along with [Hamcrest Matcher](http://hamcrest.org/JavaHamcrest/javadoc/) assertions:\n\n```java\nwaiter.assertEquals(expected, result);\nwaiter.assertThat(result, is(equalTo(expected)));\n```\n\nSince Hamcrest is an optional dependency, users need to explicitly add it to their classpath (via Maven/Gradle/etc).\n\n#### Other Examples\n\nMore example usages can be found in the [WaiterTest](https://github.com/jhalterman/concurrentunit/blob/master/src/test/java/net/jodah/concurrentunit/WaiterTest.java).\n\n## Additional Notes\n\n#### On `await` / `resume` Timing\n\nSince it is not always possible to ensure that `resume` is called after `await` in multi-threaded tests, ConcurrentUnit allows them to be called in either order. If `resume` is called before `await`, the resume calls are recorded and `await` will return immediately if the expected number of resumes have already occurred. This ability comes with a caveat though: it is not possible to detect when additional unexpected `resume` calls are made since ConcurrentUnit allows an `await` call to follow.\n\n## Additional Resources\n\n- [Javadocs](https://jodah.net/concurrentunit/javadoc)\n- [An article](https://jodah.net/testing-multi-threaded-code) describing the motivation for ConcurrentUnit\n\r\n## License\r\n\r\nCopyright 2011-2021 Jonathan Halterman - Released under the [Apache 2.0 license](http://www.apache.org/licenses/LICENSE-2.0.html).\r\n","funding_links":[],"categories":["Projects","项目","测试","Testing"],"sub_categories":["Testing","测试"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhalterman%2Fconcurrentunit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjhalterman%2Fconcurrentunit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhalterman%2Fconcurrentunit/lists"}