samples/deadlock/test/org/apidesign/deadlock/logs/ParallelSortedTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:04:53 +0200
changeset 210 acf2c31e22d4
parent 209 1c999569643b
child 263 7e8e995065c5
permissions -rw-r--r--
Merge: Geertjan's changes to the end of the chapter
     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.junit.After;
     8 import org.junit.AfterClass;
     9 import org.junit.Before;
    10 import org.junit.BeforeClass;
    11 import org.junit.Test;
    12 import org.netbeans.junit.NbTestCase;
    13 import static org.junit.Assert.*;
    14 
    15 // BEGIN: test.parallel.test.sorted
    16 public class ParallelSortedTest extends NbTestCase {
    17     public ParallelSortedTest(String testName) {
    18         super(testName);
    19     }
    20 
    21     @Override
    22     protected Level logLevel() {
    23         return Level.WARNING;
    24     }
    25 
    26     public void testMain() throws Exception {
    27         Logger.global.addHandler(new BlockingHandler());
    28         Parallel.main(null);
    29         fail("Ok, just print the logged output");
    30     }
    31 
    32     private static final class BlockingHandler extends Handler {
    33 
    34         boolean runSecond;
    35 
    36         public synchronized void publish(LogRecord record) {
    37             if (!record.getMessage().startsWith("cnt")) {
    38                 return;
    39             }
    40             boolean snd = Thread.currentThread().getName().equals("2nd");
    41             if (runSecond == snd) {
    42                 notify();
    43                 runSecond = !runSecond;
    44             }
    45             try {
    46                 wait(500);
    47             } catch (InterruptedException ex) {
    48             }
    49         }
    50 
    51         public void flush() {
    52         }
    53 
    54         public void close() {
    55         }
    56     }
    57 }
    58 // END: test.parallel.test.sorted