You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@any23.apache.org by an...@apache.org on 2015/10/29 23:38:57 UTC

[3/4] any23 git commit: ANY23-265 : Convert test to use CountDownLatch

ANY23-265 : Convert test to use CountDownLatch

Also add a JUnit Timeout rule to ensure completion, without having to
have specific timeouts on the await methods.

Project: http://git-wip-us.apache.org/repos/asf/any23/repo
Commit: http://git-wip-us.apache.org/repos/asf/any23/commit/2632bbf1
Tree: http://git-wip-us.apache.org/repos/asf/any23/tree/2632bbf1
Diff: http://git-wip-us.apache.org/repos/asf/any23/diff/2632bbf1

Branch: refs/heads/master
Commit: 2632bbf17de7f1b50b92f26c0bb1e5008c45ee04
Parents: a8f1bd0
Author: Peter Ansell <p_...@yahoo.com>
Authored: Fri Oct 30 09:29:38 2015 +1100
Committer: Peter Ansell <p_...@yahoo.com>
Committed: Fri Oct 30 09:29:38 2015 +1100

----------------------------------------------------------------------
 .../microdata/MicrodataParserTest.java          | 74 +++++++++++---------
 1 file changed, 39 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/any23/blob/2632bbf1/core/src/test/java/org/apache/any23/extractor/microdata/MicrodataParserTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/any23/extractor/microdata/MicrodataParserTest.java b/core/src/test/java/org/apache/any23/extractor/microdata/MicrodataParserTest.java
index 0db9fbc..b6ee6dd 100644
--- a/core/src/test/java/org/apache/any23/extractor/microdata/MicrodataParserTest.java
+++ b/core/src/test/java/org/apache/any23/extractor/microdata/MicrodataParserTest.java
@@ -29,16 +29,21 @@ import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.List;
 import java.util.Properties;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.CyclicBarrier;
 import java.util.concurrent.atomic.AtomicBoolean;
+
 import org.apache.any23.extractor.html.TagSoupParser;
 import org.apache.any23.util.StreamUtils;
 import org.apache.commons.io.IOUtils;
 import org.junit.Assert;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.Timeout;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
+
 import static org.junit.Assert.assertFalse;
 
 /**
@@ -48,6 +53,9 @@ import static org.junit.Assert.assertFalse;
  */
 public class MicrodataParserTest {
 
+	@Rule
+	public final Timeout timeout = new Timeout(100000);
+	
     private static final Logger logger = LoggerFactory.getLogger(MicrodataParserTest.class);
 
     @Test
@@ -98,56 +106,52 @@ public class MicrodataParserTest {
     }
     
     @Test
-    public void testGetDateConcurrent() throws IOException, ParseException {
+    public void testGetDateConcurrent() throws Exception {
         final Date expectedDate = new GregorianCalendar(2009, Calendar.MAY, 10).getTime(); // 2009-05-10
-        final byte [] content = IOUtils.toByteArray(getClass().getResourceAsStream("/microdata/microdata-basic.html"));
+        final byte[] content = IOUtils.toByteArray(getClass().getResourceAsStream("/microdata/microdata-basic.html"));
         final int threadCount = 10;
         final int attemptCount = 100;
         final List<Thread> threads = new ArrayList<Thread>();
-        final CyclicBarrier barrier = new CyclicBarrier(threadCount + 1);
+        final CountDownLatch beforeLatch = new CountDownLatch(1);
+        final CountDownLatch afterLatch = new CountDownLatch(threadCount);
         final AtomicBoolean foundFailure = new AtomicBoolean(false);
         for (int i = 0; i < threadCount; i++) {
             threads.add(new Thread("Test-thread-" + i) {
-                @Override
-                public void run() {
-                    await(barrier);
-                    try {
-                        int counter = 0;
-                        while (counter++ < attemptCount && !foundFailure.get()) {
-                            final Document document = getDom(content);
-                            final MicrodataParserReport report = MicrodataParser.getMicrodata(document);
-                            final ItemScope target = report.getDetectedItemScopes()[4];
-                            Date actualDate = target.getProperties().get("birthday").get(0).getValue().getAsDate();
-                            if (!expectedDate.equals(actualDate)) {
-                                foundFailure.set(true);
-                            }
-                        }
-                    }
-                    catch (Exception ex) {
-                        ex.printStackTrace();
-                        foundFailure.set(true);
-                    }
-                    await(barrier);
-                }
+					@Override
+					public void run() {
+						try {
+							beforeLatch.await();
+							int counter = 0;
+							while (counter++ < attemptCount && !foundFailure.get()) {
+								final Document document = getDom(content);
+								final MicrodataParserReport report = MicrodataParser.getMicrodata(document);
+								final ItemScope target = report.getDetectedItemScopes()[4];
+								Date actualDate = target.getProperties().get("birthday").get(0).getValue().getAsDate();
+								if (!expectedDate.equals(actualDate)) {
+									foundFailure.set(true);
+								}
+							}
+						}
+						catch (Exception ex) {
+							ex.printStackTrace();
+							foundFailure.set(true);
+						}
+						finally {
+							afterLatch.countDown();
+						}
+					}
             });
         }
         for (Thread thread : threads) {
             thread.start();
         }
-        await(barrier);
-        await(barrier);
+        // Let threads start computation
+        beforeLatch.countDown();
+        // Wait for all threads to complete
+        afterLatch.await();
         assertFalse(foundFailure.get());
     }
 
-    private void await(CyclicBarrier barrier) {
-        try {
-            barrier.await();
-        }
-        catch (Exception ex) {
-            throw new RuntimeException(ex);
-        }
-    }
-    
     /**
      * Test the main use case of {@link MicrodataParser#deferProperties(String...)}
      *