@Test
  public void testConcurrentPut() throws Exception {
    final RegionNormalizerWorkQueue<Integer> queue = new RegionNormalizerWorkQueue<>();
    final int maxValue = 100;
    final Runnable producer = () -> {
      final Random rand = ThreadLocalRandom.current();
      for (int i = 0; i < 1_000; i++) {
        final Action action = Action.values()[rand.nextInt(Action.values().length)];
        switch (action) {
          case PUT: {
            final int val = rand.nextInt(maxValue);
            queue.put(val);
            break;
          }
          case PUT_FIRST: {
            final int val = rand.nextInt(maxValue);
            queue.putFirst(val);
            break;
          }
          case PUT_ALL: {
            final List<Integer> vals =
              rand.ints(5, 0, maxValue).boxed().collect(Collectors.toList());
            queue.putAll(vals);
            break;
          }
          case PUT_ALL_FIRST: {
            final List<Integer> vals =
              rand.ints(5, 0, maxValue).boxed().collect(Collectors.toList());
            queue.putAllFirst(vals);
            break;
          }
          default:
            fail("Unrecognized action " + action);
        }
      }
    };

    final int numThreads = 5;
    final CompletableFuture<?>[] futures = IntStream.range(0, numThreads)
      .mapToObj(val -> CompletableFuture.runAsync(producer)).toArray(CompletableFuture<?>[]::new);
    CompletableFuture.allOf(futures).join();

    final List<Integer> content = new ArrayList<>(queue.size());
    drainTo(queue, content);
    assertThat("at most `maxValue` items should be present.", content.size(),
      lessThanOrEqualTo(maxValue));
    assertEquals("all items should be unique.", content.size(), new HashSet<>(content).size());
  }
Test that the uniqueness constraint is honored in the face of concurrent modification.
hbase-server/src/test/java/org/apache/hadoop/hbase/master/normalizer/TestRegionNormalizerWorkQueue.java