test/jdk/java/util/concurrent/tck/ConcurrentLinkedDequeTest.java
changeset 47729 1563167c9520
parent 47340 83f933b97787
child 50697 3ef0862bbb3d
equal deleted inserted replaced
47728:0a65c8231efa 47729:1563167c9520
   934                 shouldThrow();
   934                 shouldThrow();
   935             } catch (NullPointerException success) {}
   935             } catch (NullPointerException success) {}
   936         }
   936         }
   937     }
   937     }
   938 
   938 
       
   939     void runAsync(Runnable r1, Runnable r2) {
       
   940         boolean b = ThreadLocalRandom.current().nextBoolean();
       
   941         CompletableFuture<Void> f1 = CompletableFuture.runAsync(b ? r1 : r2);
       
   942         CompletableFuture<Void> f2 = CompletableFuture.runAsync(b ? r2 : r1);
       
   943         f1.join();
       
   944         f2.join();
       
   945     }
       
   946 
   939     /**
   947     /**
   940      * Non-traversing Deque operations are linearizable.
   948      * Non-traversing Deque operations are linearizable.
   941      * https://bugs.openjdk.java.net/browse/JDK-8188900
   949      * https://bugs.openjdk.java.net/browse/JDK-8188900
   942      * ant -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ConcurrentLinkedDequeTest -Djsr166.methodFilter=testBug8188900 tck
   950      * ant -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ConcurrentLinkedDequeTest -Djsr166.methodFilter=testBug8188900 tck
   943      */
   951      */
   957                         String.format(
   965                         String.format(
   958                             "unexpected value %d after %d nulls and %d zeros",
   966                             "unexpected value %d after %d nulls and %d zeros",
   959                             x, nulls.sum(), zeros.sum()));
   967                             x, nulls.sum(), zeros.sum()));
   960             };
   968             };
   961 
   969 
   962             Runnable adder = () -> {
   970             Runnable adder = () -> { d.addFirst(0); d.addLast(42); };
   963                 d.addFirst(0);
   971 
   964                 d.addLast(42);
   972             runAsync(getter, adder);
   965             };
       
   966 
       
   967             boolean b = rnd.nextBoolean();
       
   968             Runnable r1 = b ? getter : adder;
       
   969             Runnable r2 = b ? adder : getter;
       
   970             CompletableFuture<Void> f1 = CompletableFuture.runAsync(r1);
       
   971             CompletableFuture<Void> f2 = CompletableFuture.runAsync(r2);
       
   972             f1.join();
       
   973             f2.join();
       
   974         }
   973         }
   975     }
   974     }
   976 
   975 
   977     /**
   976     /**
   978      * Reverse direction variant of testBug8188900
   977      * Reverse direction variant of testBug8188900
   993                         String.format(
   992                         String.format(
   994                             "unexpected value %d after %d nulls and %d zeros",
   993                             "unexpected value %d after %d nulls and %d zeros",
   995                             x, nulls.sum(), zeros.sum()));
   994                             x, nulls.sum(), zeros.sum()));
   996             };
   995             };
   997 
   996 
   998             Runnable adder = () -> {
   997             Runnable adder = () -> { d.addLast(0); d.addFirst(42); };
   999                 d.addLast(0);
   998 
  1000                 d.addFirst(42);
   999             runAsync(getter, adder);
  1001             };
  1000         }
  1002 
  1001     }
  1003             boolean b = rnd.nextBoolean();
  1002 
  1004             Runnable r1 = b ? getter : adder;
  1003     <T> T chooseRandomly(T... choices) {
  1005             Runnable r2 = b ? adder : getter;
  1004         return choices[ThreadLocalRandom.current().nextInt(choices.length)];
  1006             CompletableFuture<Void> f1 = CompletableFuture.runAsync(r1);
  1005     }
  1007             CompletableFuture<Void> f2 = CompletableFuture.runAsync(r2);
  1006 
  1008             f1.join();
  1007     /**
  1009             f2.join();
  1008      * Non-traversing Deque operations (that return null) are linearizable.
       
  1009      * Don't return null when the deque is observably never empty.
       
  1010      * https://bugs.openjdk.java.net/browse/JDK-8189387
       
  1011      * ant -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ConcurrentLinkedDequeTest -Djsr166.methodFilter=testBug8189387 tck
       
  1012      */
       
  1013     public void testBug8189387() {
       
  1014         final ThreadLocalRandom rnd = ThreadLocalRandom.current();
       
  1015         Object x = new Object();
       
  1016         for (int n = expensiveTests ? 100_000 : 10; n--> 0; ) {
       
  1017             ConcurrentLinkedDeque<Object> d = new ConcurrentLinkedDeque<>();
       
  1018             Runnable add = chooseRandomly(
       
  1019                 () -> d.addFirst(x),
       
  1020                 () -> d.offerFirst(x),
       
  1021                 () -> d.addLast(x),
       
  1022                 () -> d.offerLast(x));
       
  1023 
       
  1024             Runnable get = chooseRandomly(
       
  1025                 () -> assertFalse(d.isEmpty()),
       
  1026                 () -> assertSame(x, d.peekFirst()),
       
  1027                 () -> assertSame(x, d.peekLast()),
       
  1028                 () -> assertSame(x, d.pollFirst()),
       
  1029                 () -> assertSame(x, d.pollLast()));
       
  1030 
       
  1031             Runnable addRemove = chooseRandomly(
       
  1032                 () -> { d.addFirst(x); d.pollLast(); },
       
  1033                 () -> { d.offerFirst(x); d.removeFirst(); },
       
  1034                 () -> { d.offerLast(x); d.removeLast(); },
       
  1035                 () -> { d.addLast(x); d.pollFirst(); });
       
  1036 
       
  1037             add.run();
       
  1038             runAsync(get, addRemove);
  1010         }
  1039         }
  1011     }
  1040     }
  1012 }
  1041 }