#69740: waiting using System.gc is not that reliable is it could be grgich_hills_base
authorjtulach@netbeans.org
Tue, 06 Dec 2005 17:11:40 +0000
changeset 97f9ca049fcb43
parent 96 a3835c7bcdfa
child 98 1742afcac4a2
#69740: waiting using System.gc is not that reliable is it could be
openide.util/test/unit/src/org/openide/util/RequestProcessorTest.java
     1.1 --- a/openide.util/test/unit/src/org/openide/util/RequestProcessorTest.java	Tue Dec 06 15:39:19 2005 +0000
     1.2 +++ b/openide.util/test/unit/src/org/openide/util/RequestProcessorTest.java	Tue Dec 06 17:11:40 2005 +0000
     1.3 @@ -34,7 +34,7 @@
     1.4      protected void setUp () throws Exception {
     1.5          super.setUp();
     1.6          
     1.7 -        log = ErrorManager.getDefault().getInstance(getName());
     1.8 +        log = ErrorManager.getDefault().getInstance("TEST-" + getName());
     1.9      }
    1.10  
    1.11      protected void runTest() throws Throwable {
    1.12 @@ -1008,6 +1008,7 @@
    1.13              private String name;
    1.14              public RequestProcessor.Task wait;
    1.15              public Object lock;
    1.16 +            public Exception ex;
    1.17              
    1.18              public boolean checkBefore;
    1.19              public boolean checkAfter;
    1.20 @@ -1015,6 +1016,7 @@
    1.21              public void run () {
    1.22                  synchronized (this) {
    1.23                      checkBefore = Thread.interrupted();
    1.24 +                    log("checkBefore: " + checkBefore);
    1.25                      notifyAll();
    1.26                  }
    1.27                  if (lock != null) {
    1.28 @@ -1023,9 +1025,11 @@
    1.29                          try {
    1.30                              lock.wait();
    1.31                          } catch (InterruptedException ex) {
    1.32 +                            this.ex = ex;
    1.33                              ex.printStackTrace();
    1.34                              fail ("No InterruptedException");
    1.35                          }
    1.36 +                        log.log("wait for lock over");
    1.37                      }
    1.38                  }
    1.39                  
    1.40 @@ -1036,6 +1040,7 @@
    1.41                  
    1.42                  synchronized (this) {
    1.43                      checkAfter = Thread.interrupted();
    1.44 +                    log.log("checkAfter: " + checkAfter);
    1.45                      notifyAll();
    1.46                  }
    1.47              }
    1.48 @@ -1059,13 +1064,17 @@
    1.49          bigger.wait = smallerTask;
    1.50          
    1.51          synchronized (initLock) {
    1.52 +            log.log("schedule 0");
    1.53              biggerTask.schedule(0);
    1.54              initLock.wait();
    1.55              initLock.notifyAll();
    1.56 +            log.log("doing cancel");
    1.57              assertFalse ("Already running", biggerTask.cancel());
    1.58 +            log.log("biggerTask cancelled");
    1.59          }
    1.60  
    1.61          biggerTask.waitFinished();
    1.62 +        log.log("waitFinished over");
    1.63          
    1.64          assertFalse("bigger not interrupted at begining", bigger.checkBefore);
    1.65          assertFalse("smaller not interrupted at all", smaller.checkBefore);
    1.66 @@ -1078,15 +1087,23 @@
    1.67          RequestProcessor rp = new RequestProcessor ("testInterruptedStatusWorksInInversedTasksWhenInterruptedSoon", 1, true);
    1.68          
    1.69          class Fail implements Runnable {
    1.70 +            public Fail(String n) {
    1.71 +                name = n;
    1.72 +            }
    1.73 +            
    1.74 +            private String name;
    1.75              public RequestProcessor.Task wait;
    1.76              public Object lock;
    1.77              
    1.78              public boolean checkBefore;
    1.79              public boolean checkAfter;
    1.80              
    1.81 +            public volatile boolean alreadyCanceled;
    1.82 +            
    1.83              public void run () {
    1.84                  synchronized (this) {
    1.85                      checkBefore = Thread.interrupted();
    1.86 +                    log.log(name + " checkBefore: " + checkBefore);
    1.87                      notifyAll();
    1.88                  }
    1.89                  if (lock != null) {
    1.90 @@ -1098,15 +1115,22 @@
    1.91                  if (wait != null) {
    1.92                      // we cannot call Thread.sleep, so lets slow things own 
    1.93                      // in other way
    1.94 -                    for (int i = 0; i < 10; i++) {
    1.95 +
    1.96 +                    log(name + " do waitFinished");
    1.97 +                    wait.waitFinished();
    1.98 +                    log(name + " waitFinished in task is over");
    1.99 +                    
   1.100 +                    log.log(name + " slowing by using System.gc");
   1.101 +                    while (!alreadyCanceled) {
   1.102                          System.gc ();
   1.103                      }
   1.104 +                    log.log(name + " ended slowing");
   1.105                      
   1.106 -                    wait.waitFinished();
   1.107                  }
   1.108                  
   1.109                  synchronized (this) {
   1.110                      checkAfter = Thread.interrupted();
   1.111 +                    log.log(name + " checkAfter: " + checkAfter);
   1.112                      notifyAll();
   1.113                  }
   1.114              }
   1.115 @@ -1114,8 +1138,8 @@
   1.116          
   1.117          Object initLock = new Object();
   1.118          
   1.119 -        Fail smaller = new Fail();
   1.120 -        Fail bigger = new Fail();
   1.121 +        Fail smaller = new Fail("smaller");
   1.122 +        Fail bigger = new Fail("bigger");
   1.123          RequestProcessor.Task smallerTask, biggerTask;
   1.124          
   1.125          
   1.126 @@ -1127,18 +1151,22 @@
   1.127          bigger.wait = smallerTask;
   1.128          
   1.129          synchronized (initLock) {
   1.130 +            log.log("Do schedule");
   1.131              biggerTask.schedule(0);
   1.132              initLock.wait();
   1.133 +            log.log("do cancel");
   1.134              assertFalse ("Already running", biggerTask.cancel());
   1.135 +            bigger.alreadyCanceled = true;
   1.136 +            log.log("cancel done");
   1.137          }
   1.138  
   1.139          biggerTask.waitFinished();
   1.140 +        log.log("waitFinished is over");
   1.141          
   1.142          assertFalse("bigger not interrupted at begining", bigger.checkBefore);
   1.143          assertFalse("smaller not interrupted at all", smaller.checkBefore);
   1.144          assertFalse("smaller not interrupted at all2", smaller.checkAfter);
   1.145          assertTrue("bigger interrupted at end", bigger.checkAfter);
   1.146 -        
   1.147      }
   1.148      
   1.149      public void testTaskFinishedOnCancelFiredAfterTaskHasReallyFinished() throws Exception {
   1.150 @@ -1300,7 +1328,7 @@
   1.151          public org.openide.ErrorManager getInstance (String name) {
   1.152              if (
   1.153                  name.startsWith ("org.openide.util.RequestProcessor") ||
   1.154 -                name.startsWith("test")
   1.155 +                name.startsWith("TEST")
   1.156              ) {
   1.157                  return new ErrManager ('[' + name + ']');
   1.158              } else {