{"id":179,"date":"2012-02-05T23:36:24","date_gmt":"2012-02-05T23:36:24","guid":{"rendered":"http:\/\/www.lexicalscope.com\/blog\/?p=179"},"modified":"2012-02-05T23:56:24","modified_gmt":"2012-02-05T23:56:24","slug":"contest-concurrent-testing-using-junit","status":"publish","type":"post","link":"https:\/\/www.lexicalscope.com\/blog\/2012\/02\/05\/contest-concurrent-testing-using-junit\/","title":{"rendered":"Contest &#8211; concurrent testing using junit"},"content":{"rendered":"<p>Inspired by <a href=\"http:\/\/mir.cs.illinois.edu\/imunit\/\" title=\"imunit\">imunit<\/a> I have put up a very early version of a concurrent testing library for junit which allows you to test your thread-safe objects with multiple execution schedules.<\/p>\n<p>You can specify constraints on each schedule which contest will enforce &#8211; if the test progresses the execution schedule will meet the constraint (at the moment contest doesn&#8217;t detect impossible schedules or runtime deadlocks).<\/p>\n<p>Tests look like this:<\/p>\n<pre lang=\"java\">\r\n@Test @Schedules({\r\n        @Schedule(\r\n                when = AddAddRemove.class,\r\n                then = SizeIsOne.class),\r\n        @Schedule(\r\n                when = AddRemoveAdd.class,\r\n                then = SizeIsOne.class),\r\n        @Schedule(\r\n                when = RemoveAddAdd.class,\r\n                then = SizeIsTwo.class)\r\n}) public void twoAddsOneRemove()\r\n{\r\n    context.checking(new TestRun() {\r\n        {\r\n            inThread(Producer).action(FirstAdd).is(multiset).add(42);\r\n            inThread(Producer).action(SecondAdd).is(multiset).add(42);\r\n            inThread(Consumer).action(Remove).is(multiset).remove(42);\r\n        }\r\n    });\r\n}\r\n<\/pre>\n<p>The following is a schedule constraint read as &#8220;Action <em>FirstAdd<\/em> happens before action <em>Remove<\/em>, and action <em>SecondAdd<\/em> also happens before action <em>Remove<\/em>&#8220;.<\/p>\n<pre lang=\"java\">\r\nclass AddAddRemove extends BaseSchedule\r\n{\r\n    {\r\n        action(FirstAdd).isBefore(Remove);\r\n        action(SecondAdd).isBefore(Remove);\r\n    }\r\n}\r\n<\/pre>\n<p>Theories are defined like this (using hamcrest matchers):<\/p>\n<pre lang=\"java\">\r\nclass SizeIsOne extends BaseTheory\r\n{\r\n     {\r\n         asserting(that(multiset).size(), equalTo(1));\r\n     }\r\n}\r\n<\/pre>\n<p>Get it from maven central:<\/p>\n<pre lang=\"xml\">\r\n<groupId>com.lexicalscope.contest<\/groupId>\r\n<artifactId>contest<\/artifactId>\r\n<version>0.0.1<\/version>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Inspired by imunit I have put up a very early version of a concurrent testing library for junit which allows you to test your thread-safe objects with multiple execution schedules. You can specify constraints on each schedule which contest will &hellip; <a href=\"https:\/\/www.lexicalscope.com\/blog\/2012\/02\/05\/contest-concurrent-testing-using-junit\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[9],"tags":[],"class_list":["post-179","post","type-post","status-publish","format-standard","hentry","category-contest"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p2e3P7-2T","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/comments?post=179"}],"version-history":[{"count":8,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":186,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/179\/revisions\/186"}],"wp:attachment":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/media?parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/categories?post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/tags?post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}