{"id":1830,"date":"2012-09-13T10:00:00","date_gmt":"2012-09-13T10:00:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/5-tips-for-unit-testing-threaded-code.html"},"modified":"2012-10-22T06:54:18","modified_gmt":"2012-10-22T06:54:18","slug":"5-tips-for-unit-testing-threaded-code","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html","title":{"rendered":"5 Tips for Unit Testing Threaded Code"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\">Here&#8217;s a few tips on how take make testing your code for logical correctness (as opposed to multi-threaded correctness).<\/p>\n<p>I find that there are essentially two stereotypical patterns with threaded code:              <\/p>\n<ol>\n<li>Task orientated &#8211; many, short running, homogeneous tasks, often run within the Java 5 executor framework,<\/li>\n<li>Process orientated &#8211; few, long running, heterogeneous tasks, often event based (waiting on notification), or polling (sleeping between cycles), often expressed using a thread or runnable.<\/li>\n<\/ol>\n<p>Testing either type of code can be hard; the work is done in another thread, and therefore notification of completion can be opaque, or is hidden behind a level of abstraction.              <\/p>\n<p>The code is <a href=\"https:\/\/github.com\/alexec\/threaded-code-testing\">on GitHub<\/a>.<br \/>\n<strong><br \/>\n<\/strong><strong>Tip 1 &#8211; Life-cycle Manage Your Objects<\/strong>             <\/p>\n<p>Object that have a managed life-cycle are are easier to test, the life-cycle allows for set-up and tear-down, which means you can clean-up after your test and no spurious threads are lying around to pollute other tests. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <\/p>\n<pre class=\"brush:java\">public class Foo {\r\n private ExecutorService executorService;\r\n\r\n public void start() {\r\n  executorService = Executors.newSingleThreadExecutor();\r\n }\r\n\r\n public void stop() {\r\n  executorService.shutdown();\r\n }\r\n}\r\n<\/pre>\n<p><strong>Tip 2 &#8211; Set a Timeout on Your Tests<\/strong>             <\/p>\n<p>Bugs in code (as you&#8217;ll see below) can result in a multi-threaded test never completing, as (for example) you&#8217;re waiting on some flag that never gets set. JUnit lets you set a timeout on your test.               <\/p>\n<pre class=\"brush:java\">... \r\n@Test(timeout = 100) \/\/ in case we never get a notification \r\npublic void testGivenNewFooWhenIncrThenGetOne() throws Exception { \r\n...\r\n<\/pre>\n<p><strong>Tip 3 &#8211; Run Tasks in the Same Thread as Your Test<\/strong>             <\/p>\n<p>Typically you&#8217;ll have an object that runs tasks in a thread pool. This means that your unit test might have to wait for the task to complete, but you&#8217;re not able to know when it would complete. You might guess, for example: <\/p>\n<pre class=\"brush:java\">public class Foo {\r\n private final AtomicLong foo = new AtomicLong();\r\n...\r\n public void incr() {\r\n  executorService.submit(new Runnable() {\r\n   @Override\r\n   public void run() {\r\n    foo.incrementAndGet();\r\n   }\r\n  });\r\n }\r\n...\r\n public long get() {\r\n  return foo.get();\r\n }\r\n}\r\n<\/pre>\n<pre class=\"brush:java\"> \r\n\r\npublic class FooTest {\r\n\r\n private Foo sut; \/\/ system under test\r\n\r\n @Before\r\n public void setUp() throws Exception {\r\n  sut = new Foo();\r\n  sut.start();\r\n }\r\n\r\n @After\r\n public void tearDown() throws Exception {\r\n  sut.stop();\r\n }\r\n\r\n @Test\r\n public void testGivenFooWhenIncrementGetOne() throws Exception {\r\n  sut.incr();\r\n  Thread.sleep(1000); \/\/ yuk - a slow test - don't do this\r\n  assertEquals(\"foo\", 1, sut.get());\r\n }\r\n}<\/pre>\n<p>But this is problematic. Execution is non-uniform so there&#8217;s no guarantee that this will work on another machine. It&#8217;s fragile, changes to the code can cause the test to fail as it suddenly take a bit too long. Its slow, as you will be generous with sleep when it fails.              <div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>A trick is to make the task run synchronously, i.e. in the same thread as the test. Here this can be achieved by injecting the executor:<\/p>\n<pre class=\"brush:java\">public class Foo {\r\n...\r\n public Foo(ExecutorService executorService) {\r\n  this.executorService = executorService;\r\n }\r\n...\r\n public void stop() {\r\n     \/\/ nop\r\n}<\/pre>\n<p>Then you can have use a synchronous executor service (similar in concept to a SynchronousQueue) to test:<\/p>\n<pre class=\"brush:java\">public class SynchronousExecutorService extends AbstractExecutorService {\r\n private boolean shutdown;\r\n\r\n @Override\r\n public void shutdown() {shutdown = true;}\r\n\r\n @Override\r\n public List&lt;Runnable&gt; shutdownNow() {shutdown = true; return Collections.emptyList();}\r\n\r\n @Override\r\n public boolean isShutdown() {shutdown = true; return shutdown;}\r\n\r\n @Override\r\n public boolean isTerminated() {return shutdown;}\r\n\r\n @Override\r\n public boolean awaitTermination(final long timeout, final TimeUnit unit) {return true;}\r\n\r\n @Override\r\n public void execute(final Runnable command) {command.run();}\r\n}<\/pre>\n<p>An updated test that doesn&#8217;t need to sleep:              <\/p>\n<pre class=\"brush:java\">public class FooTest {\r\n\r\n private Foo sut; \/\/ system under test\r\n private ExecutorService executorService;\r\n\r\n @Before\r\n public void setUp() throws Exception {\r\n  executorService = new SynchronousExecutorService();\r\n  sut = new Foo(executorService);\r\n  sut.start();\r\n }\r\n\r\n @After\r\n public void tearDown() throws Exception {\r\n  sut.stop();\r\n  executorService.shutdown();\r\n }\r\n\r\n @Test\r\n public void testGivenFooWhenIncrementGetOne() throws Exception {\r\n  sut.incr();\r\n  assertEquals(\"foo\", 1, sut.get());\r\n }\r\n}<\/pre>\n<p>Note that you need to life-cycle manage the executor externally to Foo.<br \/>\n<strong><br \/>\n<\/strong><strong>Tip 4 &#8211; Extract the Work from the Threading<\/strong>             <\/p>\n<p>If your thread is waiting for an event, or a time before it does any work, extract the work to its own method and call it directly. Consider this:<\/p>\n<pre class=\"brush:java\">public class FooThread extends Thread {\r\n private final Object ready = new Object();\r\n private volatile boolean cancelled;\r\n private final AtomicLong foo = new AtomicLong();\r\n\r\n @Override\r\n public void run() {\r\n  try {\r\n   synchronized (ready) {\r\n    while (!cancelled) {\r\n     ready.wait();\r\n     foo.incrementAndGet();\r\n    }\r\n   }\r\n  } catch (InterruptedException e) {\r\n   e.printStackTrace(); \/\/ bad practise generally, but good enough for this example\r\n  }\r\n }\r\n\r\n public void incr() {\r\n  synchronized (ready) {\r\n   ready.notifyAll();\r\n  }\r\n }\r\n\r\n public long get() {\r\n  return foo.get();\r\n }\r\n\r\n public void cancel() throws InterruptedException {\r\n  cancelled = true;\r\n  synchronized (ready) {\r\n   ready.notifyAll();\r\n  }\r\n }\r\n}<\/pre>\n<p>And this test:              <\/p>\n<pre class=\"brush:java\">public class FooThreadTest {\r\n\r\n private FooThread sut;\r\n\r\n @Before\r\n public void setUp() throws Exception {\r\n  sut = new FooThread();\r\n  sut.start();\r\n  Thread.sleep(1000); \/\/ yuk\r\n  assertEquals(\"thread state\", Thread.State.WAITING, sut.getState());\r\n }\r\n\r\n @After\r\n public void tearDown() throws Exception {\r\n  sut.cancel();\r\n }\r\n\r\n @After\r\n public void tearDown() throws Exception {\r\n  sut.cancel();\r\n }\r\n\r\n @Test\r\n public void testGivenNewFooWhenIncrThenGetOne() throws Exception {\r\n  sut.incr();\r\n  Thread.sleep(1000); \/\/ yuk\r\n  assertEquals(\"foo\", 1, sut.get());\r\n }\r\n}<\/pre>\n<p>Now extract the work:              <\/p>\n<pre class=\"brush:java\">@Override\r\n public void run() {\r\n  try {\r\n   synchronized (ready) {\r\n    while (!cancelled) {\r\n     ready.wait();\r\n     undertakeWork();\r\n    }\r\n   }\r\n  } catch (InterruptedException e) {\r\n   e.printStackTrace(); \/\/ bad practise generally, but good enough for this example\r\n  }\r\n }\r\n\r\n void undertakeWork() {\r\n  foo.incrementAndGet();\r\n }<\/pre>\n<p>Re-factor the test:              <\/p>\n<pre class=\"brush:java\">public class FooThreadTest {\r\n\r\n    private FooThread sut;\r\n\r\n    @Before\r\n    public void setUp() throws Exception {\r\n        sut = new FooThread();\r\n    }\r\n\r\n    @Test\r\n    public void testGivenNewFooWhenIncrThenGetOne() throws Exception {\r\n        sut.incr();\r\n        sut.undertakeWork();\r\n        assertEquals(\"foo\", 1, sut.get());\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Tip 5 &#8211; Notify State Change via Events<\/strong>             <\/p>\n<p>An alternative to the previous two tips is to use a notification system, so your test can listen to the threaded object.              <\/p>\n<p>Here&#8217;s a task oriented example:<\/p>\n<pre class=\"brush:java\">public class ObservableFoo extends Observable {\r\n private final AtomicLong foo = new AtomicLong();\r\n private ExecutorService executorService;\r\n\r\n public void start() {\r\n  executorService = Executors.newSingleThreadExecutor();\r\n }\r\n\r\n public void stop() {\r\n  executorService.shutdown();\r\n }\r\n\r\n public void incr() {\r\n  executorService.submit(new Runnable() {\r\n   @Override\r\n   public void run() {\r\n    foo.incrementAndGet();\r\n    setChanged();\r\n    notifyObservers(); \/\/ lazy use of observable\r\n   }\r\n  });\r\n }\r\n\r\n public long get() {\r\n  return foo.get();\r\n }\r\n}<\/pre>\n<p>And its corresponding test (note the use of timeout):              <\/p>\n<pre class=\"brush:java\">public class ObservableFooTest implements Observer {\r\n\r\n private ObservableFoo sut;\r\n private CountDownLatch updateLatch; \/\/ used to react to event\r\n\r\n @Before\r\n public void setUp() throws Exception {\r\n  updateLatch = new CountDownLatch(1);\r\n  sut = new ObservableFoo();\r\n  sut.addObserver(this);\r\n  sut.start();\r\n }\r\n\r\n @Override\r\n public void update(final Observable o, final Object arg) {\r\n  assert o == sut;\r\n  updateLatch.countDown();\r\n }\r\n\r\n @After\r\n public void tearDown() throws Exception {\r\n  sut.deleteObserver(this);\r\n  sut.stop();\r\n }\r\n\r\n @Test(timeout = 100) \/\/ in case we never get a notification\r\n public void testGivenNewFooWhenIncrThenGetOne() throws Exception {\r\n  sut.incr();\r\n  updateLatch.await();\r\n  assertEquals(\"foo\", 1, sut.get());\r\n }\r\n} <\/pre>\n<p>This has pros and cons:              <\/p>\n<p>Pros:              <\/p>\n<ol>\n<li>Creates useful code for listening to the object.<\/li>\n<li>Can take advantage of existing notification code, which makes it a good choice where that already exists.<\/li>\n<li>Is more flexible, can apply to both tasks and process orientated code.<\/li>\n<li>It is more cohesive than extracting the work.<\/li>\n<\/ol>\n<p>Cons:              <\/p>\n<ol>\n<li>Listener code can be complex and introduce its own problems, creating additional production code that ought to be tested.<\/li>\n<li>De-couples submission from notification.<\/li>\n<li>Requires you to deal with the scenario that no notification is sent (e.g. due to bug).<\/li>\n<li>Test code can be quite verbose and therefore prone to having bugs.<\/li>\n<\/ol>\n<p><strong><i>Reference: <\/i><\/strong><a href=\"http:\/\/www.alexecollins.com\/?q=content\/5-tips-unit-testing-threaded-code\">5 Tips for Unit Testing Threaded Code<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG partner<\/a> Alex Collins  at the <a href=\"http:\/\/www.alexecollins.com\/\">Alex Collins &#8216;s blog<\/a> blog.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a few tips on how take make testing your code for logical correctness (as opposed to multi-threaded correctness). I find that there are essentially two stereotypical patterns with threaded code: Task orientated &#8211; many, short running, homogeneous tasks, often run within the Java 5 executor framework, Process orientated &#8211; few, long running, heterogeneous tasks, &hellip;<\/p>\n","protected":false},"author":202,"featured_media":176,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[274,273],"class_list":["post-1830","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-junit","tag-testing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>5 Tips for Unit Testing Threaded Code - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Here&#039;s a few tips on how take make testing your code for logical correctness (as opposed to multi-threaded correctness). I find that there are essentially\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"5 Tips for Unit Testing Threaded Code - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Here&#039;s a few tips on how take make testing your code for logical correctness (as opposed to multi-threaded correctness). I find that there are essentially\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2012-09-13T10:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-22T06:54:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Alex Collins\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alex Collins\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/09\\\/5-tips-for-unit-testing-threaded-code.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/09\\\/5-tips-for-unit-testing-threaded-code.html\"},\"author\":{\"name\":\"Alex Collins\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/d8297de25c5373886136a5d9bd8e3f02\"},\"headline\":\"5 Tips for Unit Testing Threaded Code\",\"datePublished\":\"2012-09-13T10:00:00+00:00\",\"dateModified\":\"2012-10-22T06:54:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/09\\\/5-tips-for-unit-testing-threaded-code.html\"},\"wordCount\":596,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/09\\\/5-tips-for-unit-testing-threaded-code.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"keywords\":[\"JUnit\",\"Testing\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/09\\\/5-tips-for-unit-testing-threaded-code.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/09\\\/5-tips-for-unit-testing-threaded-code.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/09\\\/5-tips-for-unit-testing-threaded-code.html\",\"name\":\"5 Tips for Unit Testing Threaded Code - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/09\\\/5-tips-for-unit-testing-threaded-code.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/09\\\/5-tips-for-unit-testing-threaded-code.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"datePublished\":\"2012-09-13T10:00:00+00:00\",\"dateModified\":\"2012-10-22T06:54:18+00:00\",\"description\":\"Here's a few tips on how take make testing your code for logical correctness (as opposed to multi-threaded correctness). I find that there are essentially\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/09\\\/5-tips-for-unit-testing-threaded-code.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/09\\\/5-tips-for-unit-testing-threaded-code.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/09\\\/5-tips-for-unit-testing-threaded-code.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/09\\\/5-tips-for-unit-testing-threaded-code.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"5 Tips for Unit Testing Threaded Code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/d8297de25c5373886136a5d9bd8e3f02\",\"name\":\"Alex Collins\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/51ce4c7aceacb45e0ca4ae9d90c010b8b72f7aeaca8bba26b7828d5cfe36622c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/51ce4c7aceacb45e0ca4ae9d90c010b8b72f7aeaca8bba26b7828d5cfe36622c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/51ce4c7aceacb45e0ca4ae9d90c010b8b72f7aeaca8bba26b7828d5cfe36622c?s=96&d=mm&r=g\",\"caption\":\"Alex Collins\"},\"sameAs\":[\"http:\\\/\\\/www.alexecollins.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/Alex-Collins\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"5 Tips for Unit Testing Threaded Code - Java Code Geeks","description":"Here's a few tips on how take make testing your code for logical correctness (as opposed to multi-threaded correctness). I find that there are essentially","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html","og_locale":"en_US","og_type":"article","og_title":"5 Tips for Unit Testing Threaded Code - Java Code Geeks","og_description":"Here's a few tips on how take make testing your code for logical correctness (as opposed to multi-threaded correctness). I find that there are essentially","og_url":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2012-09-13T10:00:00+00:00","article_modified_time":"2012-10-22T06:54:18+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","type":"image\/jpeg"}],"author":"Alex Collins","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Alex Collins","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html"},"author":{"name":"Alex Collins","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/d8297de25c5373886136a5d9bd8e3f02"},"headline":"5 Tips for Unit Testing Threaded Code","datePublished":"2012-09-13T10:00:00+00:00","dateModified":"2012-10-22T06:54:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html"},"wordCount":596,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","keywords":["JUnit","Testing"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html","url":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html","name":"5 Tips for Unit Testing Threaded Code - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","datePublished":"2012-09-13T10:00:00+00:00","dateModified":"2012-10-22T06:54:18+00:00","description":"Here's a few tips on how take make testing your code for logical correctness (as opposed to multi-threaded correctness). I find that there are essentially","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2012\/09\/5-tips-for-unit-testing-threaded-code.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"5 Tips for Unit Testing Threaded Code"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/d8297de25c5373886136a5d9bd8e3f02","name":"Alex Collins","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/51ce4c7aceacb45e0ca4ae9d90c010b8b72f7aeaca8bba26b7828d5cfe36622c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/51ce4c7aceacb45e0ca4ae9d90c010b8b72f7aeaca8bba26b7828d5cfe36622c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/51ce4c7aceacb45e0ca4ae9d90c010b8b72f7aeaca8bba26b7828d5cfe36622c?s=96&d=mm&r=g","caption":"Alex Collins"},"sameAs":["http:\/\/www.alexecollins.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/Alex-Collins"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/1830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/202"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=1830"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/1830\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/176"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=1830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=1830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=1830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}