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