samples/deadlock/test/org/apidesign/deadlock/logs/ParallelSortedTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 18:51:38 +0200
changeset 263 7e8e995065c5
parent 210 acf2c31e22d4
permissions -rw-r--r--
Tests of all modules are executed and can fail the build
     1 package org.apidesign.deadlock.logs;
     2 
     3 import java.util.logging.Handler;
     4 import java.util.logging.Level;
     5 import java.util.logging.LogRecord;
     6 import java.util.logging.Logger;
     7 import org.netbeans.junit.NbTestCase;
     8 import static org.junit.Assert.*;
     9 
    10 // BEGIN: test.parallel.test.sorted
    11 public class ParallelSortedTest extends NbTestCase {
    12     public ParallelSortedTest(String testName) {
    13         super(testName);
    14     }
    15 
    16     @Override
    17     protected Level logLevel() {
    18         return Level.WARNING;
    19     }
    20 
    21     public void testMain() throws Exception {
    22         Logger.global.addHandler(new BlockingHandler());
    23         Parallel.main(null);
    24         if (Boolean.getBoolean("no.failures")) return;
    25         fail("Ok, just print the logged output");
    26     }
    27 
    28     private static final class BlockingHandler extends Handler {
    29 
    30         boolean runSecond;
    31 
    32         public synchronized void publish(LogRecord record) {
    33             if (!record.getMessage().startsWith("cnt")) {
    34                 return;
    35             }
    36             boolean snd = Thread.currentThread().getName().equals("2nd");
    37             if (runSecond == snd) {
    38                 notify();
    39                 runSecond = !runSecond;
    40             }
    41             try {
    42                 wait(500);
    43             } catch (InterruptedException ex) {
    44             }
    45         }
    46 
    47         public void flush() {
    48         }
    49 
    50         public void close() {
    51         }
    52     }
    53 }
    54 // END: test.parallel.test.sorted