@Test
  public void testEntityLock() throws Exception {
    final long procId = 100;
    final long workerSleepTime = 200; // in ms
    EntityLock lock = admin.namespaceLock("namespace", "description", null);
    lock.setTestingSleepTime(workerSleepTime);

    when(master.requestLock(any(), any()))
      .thenReturn(LockResponse.newBuilder().setProcId(procId).build());
    when(master.lockHeartbeat(any(), any())).thenReturn(UNLOCKED_RESPONSE, UNLOCKED_RESPONSE,
      UNLOCKED_RESPONSE, LOCKED_RESPONSE);

    lock.requestLock();
    // we return unlock response 3 times, so actual wait time should be around 2 * workerSleepTime
    lock.await(4 * workerSleepTime, TimeUnit.MILLISECONDS);
    assertTrue(lock.isLocked());
    lock.unlock();
    assertTrue(!lock.getWorker().isAlive());
    assertFalse(lock.isLocked());

    // check LockRequest in requestLock()
    verify(master, times(1)).requestLock(any(), lockReqArgCaptor.capture());
    LockRequest request = lockReqArgCaptor.getValue();
    assertEquals("namespace", request.getNamespace());
    assertEquals("description", request.getDescription());
    assertEquals(LockType.EXCLUSIVE, request.getLockType());
    assertEquals(0, request.getRegionInfoCount());

    // check LockHeartbeatRequest in lockHeartbeat()
    verify(master, atLeastOnce()).lockHeartbeat(any(), lockHeartbeatReqArgCaptor.capture());
    for (LockHeartbeatRequest req : lockHeartbeatReqArgCaptor.getAllValues()) {
      assertEquals(procId, req.getProcId());
    }
  }
Test basic lock function - requestLock, await, unlock.
hbase-server/src/test/java/org/apache/hadoop/hbase/client/locking/TestEntityLocks.java