test/jdk/java/nio/channels/Selector/RacyDeregister.java
changeset 50794 0ab1cac936b8
parent 50258 25f93c5406bf
child 55525 c9093341cfe2
equal deleted inserted replaced
50793:ca4eea543d23 50794:0ab1cac936b8
    32 import java.nio.channels.ServerSocketChannel;
    32 import java.nio.channels.ServerSocketChannel;
    33 import java.nio.channels.SocketChannel;
    33 import java.nio.channels.SocketChannel;
    34 
    34 
    35 /*
    35 /*
    36  * @test
    36  * @test
    37  * @bug 6429204 8203766
    37  * @bug 6429204 8203766 8205641
    38  * @summary SelectionKey.interestOps does not update interest set on Windows.
    38  * @summary SelectionKey.interestOps does not update interest set on Windows.
    39  * @author Frank Ding
    39  * @author Frank Ding
    40  * @run main/timeout=1200 RacyDeregister
    40  * @run main/timeout=1200 RacyDeregister
    41  */
    41  */
    42 public class RacyDeregister {
    42 public class RacyDeregister {
    89         new Thread() {
    89         new Thread() {
    90 
    90 
    91             public void run() {
    91             public void run() {
    92                 try {
    92                 try {
    93                     for (int k = 0; k < NUM_OUTER_LOOP_ITERATIONS; k++) {
    93                     for (int k = 0; k < NUM_OUTER_LOOP_ITERATIONS; k++) {
       
    94                         System.out.format("outer loop %3d at %7d ms%n", k,
       
    95                             System.currentTimeMillis() - t0);
       
    96                         System.out.flush();
    94                         for (int i = 0; i < 10000; i++) {
    97                         for (int i = 0; i < 10000; i++) {
    95                             synchronized (notifyLock) {
    98                             synchronized (notifyLock) {
    96                                 synchronized (selectorLock) {
    99                                 synchronized (selectorLock) {
    97                                     sel.wakeup();
   100                                     sel.wakeup();
    98                                     key[0].interestOps(SelectionKey.OP_READ
   101                                     key[0].interestOps(SelectionKey.OP_READ
   113                                                 long t =
   116                                                 long t =
   114                                                     System.currentTimeMillis();
   117                                                     System.currentTimeMillis();
   115                                                 System.err.printf
   118                                                 System.err.printf
   116                                                     ("Notified after %d ms%n",
   119                                                     ("Notified after %d ms%n",
   117                                                      t - beginTime);
   120                                                      t - beginTime);
       
   121                                                 System.err.flush();
   118                                                 break;
   122                                                 break;
   119                                             }
   123                                             }
   120                                         }
   124                                         }
   121                                         succTermination = false;
   125                                         succTermination = false;
   122                                         // wake up main thread doing select()
   126                                         // wake up main thread doing select()
   128                         }
   132                         }
   129                         long t = System.currentTimeMillis();
   133                         long t = System.currentTimeMillis();
   130                         if (t - t0 > TIMEOUT_THRESHOLD_MILLIS) {
   134                         if (t - t0 > TIMEOUT_THRESHOLD_MILLIS) {
   131                             System.err.format
   135                             System.err.format
   132                                 ("Timeout after %d outer loop iterations%n", k);
   136                                 ("Timeout after %d outer loop iterations%n", k);
       
   137                             System.err.flush();
   133                             succTermination = false;
   138                             succTermination = false;
   134                             // wake up main thread doing select()
   139                             // wake up main thread doing select()
   135                             sel.wakeup();
   140                             sel.wakeup();
   136                             return;
   141                             return;
   137                         }
   142                         }
   139                     succTermination = true;
   144                     succTermination = true;
   140                     // wake up main thread doing select()
   145                     // wake up main thread doing select()
   141                     sel.wakeup();
   146                     sel.wakeup();
   142                 } catch (Exception e) {
   147                 } catch (Exception e) {
   143                     System.out.println(e);
   148                     System.out.println(e);
       
   149                     System.out.flush();
   144                     succTermination = true;
   150                     succTermination = true;
   145                     // wake up main thread doing select()
   151                     // wake up main thread doing select()
   146                     sel.wakeup();
   152                     sel.wakeup();
   147                 }
   153                 }
   148             }
   154             }
   151         // main thread will be doing registering/deregistering with the sel
   157         // main thread will be doing registering/deregistering with the sel
   152         while (true) {
   158         while (true) {
   153             sel.select();
   159             sel.select();
   154             if (Boolean.TRUE.equals(succTermination)) {
   160             if (Boolean.TRUE.equals(succTermination)) {
   155                 System.out.println("Test passed");
   161                 System.out.println("Test passed");
       
   162                 System.out.flush();
   156                 sel.close();
   163                 sel.close();
   157                 sc.close();
   164                 sc.close();
   158                 break;
   165                 break;
   159             } else if (Boolean.FALSE.equals(succTermination)) {
   166             } else if (Boolean.FALSE.equals(succTermination)) {
   160                 System.err.println("Failed to pass the test");
   167                 System.err.println("Failed to pass the test");
       
   168                 System.err.flush();
   161                 sel.close();
   169                 sel.close();
   162                 sc.close();
   170                 sc.close();
   163                 throw new RuntimeException("Failed to pass the test");
   171                 throw new RuntimeException("Failed to pass the test");
   164             }
   172             }
   165             synchronized (selectorLock) {
   173             synchronized (selectorLock) {