@Ignore("Test requires available CPU resources for background threads")
  @Test
  public void testWithContention() throws Exception {
    final long execTime = 200;

    MetricsSource ms  = ReadWriteLockMetrics.createLockMetricsSource("test1");
    ReadWriteLock rwl = create(new ReentrantReadWriteLock(), ms);
    LockHolder    lhR = new LockHolder(rwl.readLock(), execTime);

    // get a write lock for half of the execution time
    try {
      long endOfLock = nanoTime() + toNano(execTime / 2);
      rwl.writeLock().lock();

      while (nanoTime() < endOfLock) {
        // spin until end time is reached
      }
    } finally {
      rwl.writeLock().unlock();
    }

    // wait for the thread to do its locks and waits (for 100ms)
    lhR.join();

    MockMetricsCollector tmc = new MockMetricsCollector();
    ms.getMetrics(tmc, true);

    List<MockMetricsCollector.MockRecord> result = tmc.getRecords();
    assertEquals("Unexpected amount of metrics", 1, result.size());
    MockMetricsCollector.MockRecord rec = result.get(0);

    // sanity checks for read lock values
    assertEquals("Verifying the loop count (read lock)",
                 lhR.getLockCount(),
                 rec.getMetrics().get(ReadLockCount).longValue());

    assertWithTolerance("Only half of possible read locks expected",
                        (execTime / LockHolder.LOCK_HOLD_TIME) / 2,
                        rec.getMetrics().get(ReadLockCount).longValue());

    assertWithTolerance("Max read lock wait time close to write lock hold",
                        toNano(execTime / 2),
                        rec.getMetrics().get(ReadLockWaitTimeMax).longValue());

    assertTrue("Total read lock wait time larger than max",
               rec.getMetrics().get(ReadLockWaitTimeMax).longValue()
               < rec.getMetrics().get(ReadLockWaitTimeTotal).longValue());

    // sanity check for write locks
    assertEquals("Write lock count supposed to be one",
                 1, rec.getMetrics().get(WriteLockCount).longValue());

    assertTrue("Write lock wait time non zero",
               0L < rec.getMetrics().get(WriteLockWaitTimeTotal).longValue());
    assertEquals("With one lock, total should me max",
                 rec.getMetrics().get(WriteLockWaitTimeTotal),
                 rec.getMetrics().get(WriteLockWaitTimeMax));
  }
Test where read/write lock contention is tested.
This test has a background thread that tries to get read locks within a
loop while the main thread holds a write lock for half of the tex
execution time. The test verifies that the reported metric for read lock
wait time reflects that the thread was blocked until the write lock was
released. It also performs basic sanity checks on the read and write lock
metrics.
llap-common/src/test/org/apache/hadoop/hive/llap/metrics/TestReadWriteLockMetrics.java