Enough of logging, Jesse will anyway advice to delete this part
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:54:32 +0200
changeset 108a420a1124988
parent 107 907f5d8e343c
child 109 050e2dab350a
Enough of logging, Jesse will anyway advice to delete this part
samples/deadlock/src/org/apidesign/deadlock/LabelProvider.java
samples/deadlock/src/org/apidesign/deadlock/logs/Parallel.java
samples/deadlock/test/org/apidesign/deadlock/logs/ParallelControlFlowTest.java
samples/deadlock/test/org/apidesign/deadlock/logs/ParallelSortedTest.java
samples/deadlock/test/org/apidesign/deadlock/logs/ParallelTest.java
     1.1 --- a/samples/deadlock/src/org/apidesign/deadlock/LabelProvider.java	Sat Jun 14 09:54:32 2008 +0200
     1.2 +++ b/samples/deadlock/src/org/apidesign/deadlock/LabelProvider.java	Sat Jun 14 09:54:32 2008 +0200
     1.3 @@ -13,9 +13,11 @@
     1.4      private HashSet<JLabel> allCreated = new HashSet<JLabel>();
     1.5  
     1.6      public synchronized JLabel createLabel () {
     1.7 +        // BEGIN: deadlock.logs
     1.8          LOG.log(Level.INFO, "Will create JLabel");
     1.9          JLabel l = new JLabel ();
    1.10          LOG.log(Level.INFO, "Label created {0}", l);
    1.11 +        // END: deadlock.logs
    1.12          allCreated.add (l);
    1.13          return l;
    1.14      }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/deadlock/src/org/apidesign/deadlock/logs/Parallel.java	Sat Jun 14 09:54:32 2008 +0200
     2.3 @@ -0,0 +1,25 @@
     2.4 +package org.apidesign.deadlock.logs;
     2.5 +
     2.6 +import java.util.Random;
     2.7 +import java.util.logging.Level;
     2.8 +import java.util.logging.Logger;
     2.9 +
    2.10 +// BEGIN: test.parallel
    2.11 +class Parael implements Runnable {
    2.12 +    public void run() {
    2.13 +        Random r = new Random();
    2.14 +        for (int i = 0; i < 10; i++) {
    2.15 +            try {
    2.16 +                Thread.sleep(r.nextInt(100));
    2.17 +            } catch (InterruptedException ex) {}
    2.18 +            Logger.global.log(Level.WARNING, "cnt: {0}", new Integer(i));
    2.19 +        }
    2.20 +    }
    2.21 +    public static void main(String[] args) throws InterruptedException {
    2.22 +        Thread t1 = new Thread(new Parael(), "1st");
    2.23 +        Thread t2 = new Thread(new Parael(), "2nd");
    2.24 +        t1.start(); t2.start();
    2.25 +        t1.join(); t2.join();
    2.26 +    }
    2.27 +}
    2.28 +// END: test.parallel
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/deadlock/test/org/apidesign/deadlock/logs/ParallelControlFlowTest.java	Sat Jun 14 09:54:32 2008 +0200
     3.3 @@ -0,0 +1,69 @@
     3.4 +package org.apidesign.deadlock.logs;
     3.5 +
     3.6 +import java.util.logging.Handler;
     3.7 +import java.util.logging.Level;
     3.8 +import java.util.logging.LogRecord;
     3.9 +import java.util.logging.Logger;
    3.10 +import org.junit.After;
    3.11 +import org.junit.AfterClass;
    3.12 +import org.junit.Before;
    3.13 +import org.junit.BeforeClass;
    3.14 +import org.junit.Test;
    3.15 +import org.netbeans.junit.NbTestCase;
    3.16 +import static org.junit.Assert.*;
    3.17 +
    3.18 +public class ParallelControlFlowTest extends NbTestCase {
    3.19 +    public ParallelControlFlowTest(String testName) {
    3.20 +        super(testName);
    3.21 +    }
    3.22 +
    3.23 +    @Override
    3.24 +    protected Level logLevel() {
    3.25 +        return Level.WARNING;
    3.26 +    }
    3.27 +
    3.28 +// BEGIN: test.parallel.test.controlflow
    3.29 +    public void testMain() throws Exception {
    3.30 +        org.netbeans.junit.Log.controlFlow(Logger.global, null,
    3.31 +            "THREAD: 1st MSG: cnt: 0" +
    3.32 +            "THREAD: 2nd MSG: .*0" +
    3.33 +            "THREAD: 1st MSG: ...: 1" +
    3.34 +            "THREAD: 2nd MSG: cnt: 1" +
    3.35 +            "THREAD: 1st MSG: cnt: 2" +
    3.36 +            "THREAD: 2nd MSG: cnt: 2" +
    3.37 +            "THREAD: 1st MSG: cnt: 3" +
    3.38 +            "THREAD: 2nd MSG: cnt: 3" +
    3.39 +            "THREAD: 1st MSG: cnt: 4" +
    3.40 +            "THREAD: 2nd MSG: cnt: 4" +
    3.41 +            "THREAD: 1st MSG: cnt: 5" +
    3.42 +            "THREAD: 2nd MSG: cnt: 5" +
    3.43 +            "THREAD: 1st MSG: cnt: 6" +
    3.44 +            "THREAD: 2nd MSG: cnt: 6" +
    3.45 +            "THREAD: 1st MSG: cnt: 7" +
    3.46 +            "THREAD: 2nd MSG: cnt: 7" +
    3.47 +            "THREAD: 1st MSG: cnt: 8" +
    3.48 +            "THREAD: 2nd MSG: cnt: 8" +
    3.49 +            "THREAD: 1st MSG: cnt: 9" +
    3.50 +            "THREAD: 2nd MSG: cnt: 9",
    3.51 +            500
    3.52 +        );
    3.53 +        Parael.main(null);
    3.54 +        fail("Ok, just print the logged output");
    3.55 +    }
    3.56 +// END: test.parallel.test.controlflow
    3.57 +    
    3.58 +    
    3.59 +    // BEGIN: test.parallel.test.fivetwo
    3.60 +    public void testFiveAndThenTwo() throws Exception {
    3.61 +        org.netbeans.junit.Log.controlFlow(Logger.global, null,
    3.62 +            "THREAD: 1st MSG: cnt: 5" +
    3.63 +            "THREAD: 2nd MSG: cnt: 2" +
    3.64 +            "THREAD: 1st MSG: cnt: 6",
    3.65 +            5000
    3.66 +        );
    3.67 +        Parael.main(null);
    3.68 +        fail("Ok, just print the logged output");
    3.69 +    }
    3.70 +    // END: test.parallel.test.fivetwo
    3.71 +    
    3.72 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/deadlock/test/org/apidesign/deadlock/logs/ParallelSortedTest.java	Sat Jun 14 09:54:32 2008 +0200
     4.3 @@ -0,0 +1,57 @@
     4.4 +package org.apidesign.deadlock.logs;
     4.5 +
     4.6 +import java.util.logging.Handler;
     4.7 +import java.util.logging.Level;
     4.8 +import java.util.logging.LogRecord;
     4.9 +import java.util.logging.Logger;
    4.10 +import org.junit.After;
    4.11 +import org.junit.AfterClass;
    4.12 +import org.junit.Before;
    4.13 +import org.junit.BeforeClass;
    4.14 +import org.junit.Test;
    4.15 +import org.netbeans.junit.NbTestCase;
    4.16 +import static org.junit.Assert.*;
    4.17 +
    4.18 +// BEGIN: test.parallel.test.sorted
    4.19 +public class ParallelSortedTest extends NbTestCase {
    4.20 +    public ParallelSortedTest(String testName) {
    4.21 +        super(testName);
    4.22 +    }
    4.23 +
    4.24 +    @Override
    4.25 +    protected Level logLevel() {
    4.26 +        return Level.WARNING;
    4.27 +    }
    4.28 +
    4.29 +    public void testMain() throws Exception {
    4.30 +        Logger.global.addHandler(new BlockingHandler());
    4.31 +        Parael.main(null);
    4.32 +        fail("Ok, just print the logged output");
    4.33 +    }
    4.34 +
    4.35 +    private static final class BlockingHandler extends Handler {
    4.36 +
    4.37 +        boolean runSecond;
    4.38 +
    4.39 +        public synchronized void publish(LogRecord record) {
    4.40 +            if (!record.getMessage().startsWith("cnt")) {
    4.41 +                return;
    4.42 +            }
    4.43 +            if (runSecond == Thread.currentThread().getName().equals("2nd")) {
    4.44 +                notify();
    4.45 +                runSecond = !runSecond;
    4.46 +            }
    4.47 +            try {
    4.48 +                wait(500);
    4.49 +            } catch (InterruptedException ex) {
    4.50 +            }
    4.51 +        }
    4.52 +
    4.53 +        public void flush() {
    4.54 +        }
    4.55 +
    4.56 +        public void close() {
    4.57 +        }
    4.58 +    }
    4.59 +}
    4.60 +// END: test.parallel.test.sorted
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/deadlock/test/org/apidesign/deadlock/logs/ParallelTest.java	Sat Jun 14 09:54:32 2008 +0200
     5.3 @@ -0,0 +1,28 @@
     5.4 +package org.apidesign.deadlock.logs;
     5.5 +
     5.6 +import java.util.logging.Level;
     5.7 +import org.junit.After;
     5.8 +import org.junit.AfterClass;
     5.9 +import org.junit.Before;
    5.10 +import org.junit.BeforeClass;
    5.11 +import org.junit.Test;
    5.12 +import org.netbeans.junit.NbTestCase;
    5.13 +import static org.junit.Assert.*;
    5.14 +
    5.15 +// BEGIN: test.parallel.test
    5.16 +public class ParallelTest extends NbTestCase {
    5.17 +    public ParallelTest(String testName) {
    5.18 +        super(testName);
    5.19 +    }
    5.20 +
    5.21 +    @Override
    5.22 +    protected Level logLevel() {
    5.23 +        return Level.WARNING;
    5.24 +    }
    5.25 +
    5.26 +    public void testMain() throws Exception {
    5.27 +        Parael.main(null);
    5.28 +        fail("Ok, just print logged messages");
    5.29 +    }
    5.30 +}
    5.31 +// END: test.parallel.test