samples/deadlock/test/org/apidesign/deadlock/logs/ParallelSortedTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:54:32 +0200
changeset 108 a420a1124988
child 133 50bf1b976c0d
permissions -rw-r--r--
Enough of logging, Jesse will anyway advice to delete this part
     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         Parael.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             if (runSecond == Thread.currentThread().getName().equals("2nd")) {
    41                 notify();
    42                 runSecond = !runSecond;
    43             }
    44             try {
    45                 wait(500);
    46             } catch (InterruptedException ex) {
    47             }
    48         }
    49 
    50         public void flush() {
    51         }
    52 
    53         public void close() {
    54         }
    55     }
    56 }
    57 // END: test.parallel.test.sorted