@Test
  public void testAddPartitionIfNotExists() throws Exception {
    dropTable(new String[] {"T", "Tstage"});
    
    HiveConf.setIntVar(conf, HiveConf.ConfVars.HIVE_LOCKS_PARTITION_THRESHOLD, 1);
    driver = Mockito.spy(driver);
    driver2 = Mockito.spy(driver2);
    
    driver.run("create table if not exists T (a int, b int) partitioned by (p string) " +
      "stored as orc TBLPROPERTIES ('transactional'='true')");
    //bucketed just so that we get 2 files
    driver.run("create table if not exists Tstage (a int, b int) clustered by (a) into 2 " +
      "buckets stored as orc TBLPROPERTIES ('transactional'='false')");
    driver.run("insert into Tstage values(1,2),(3,4)");
    String exportLoc = exportFolder.newFolder("1").toString();
    driver.run("export table Tstage to '" + exportLoc + "'");
    
    driver.compileAndRespond("select * from T");
    
    driver.lockAndRespond();
    List<ShowLocksResponseElement> locks = getLocks();
    Assert.assertEquals("Unexpected lock count", 1, locks.size());

    TestTxnDbUtil.checkLock(LockType.SHARED_READ,
      LockState.ACQUIRED, "default", "T", null, locks);

    DbTxnManager txnMgr2 = (DbTxnManager) TxnManagerFactory.getTxnManagerFactory().getTxnManager(conf);
    swapTxnManager(txnMgr2);
    driver2.compileAndRespond("alter table T add if not exists partition (p='foo') location '" + exportLoc + "/data'");
    
    driver2.lockAndRespond();
    locks = getLocks();
    Assert.assertEquals("Unexpected lock count", 2, locks.size());

    TestTxnDbUtil.checkLock(LockType.EXCL_WRITE,
      LockState.ACQUIRED, "default", "T", null, locks);

    Mockito.doNothing().when(driver2).lockAndRespond();
    driver2.run();
    
    swapTxnManager(txnMgr);
    Mockito.doNothing().when(driver).lockAndRespond();
    driver.run();
    Mockito.reset(driver, driver2);

    List<String> res = new ArrayList<>();
    driver.getFetchTask().fetch(res);
    Assert.assertEquals("Expecting 0 rows and found " + res.size(), 0, res.size());
    
    driver.run("select * from T where p='foo'");
    res = new ArrayList<>();
    driver.getFetchTask().fetch(res);
    Assert.assertEquals("Expecting 2 rows and found " + res.size(), 2, res.size());
  }
T7 is a table with 2 partitions
1. run select from T7
2. run drop partition from T7
concurrently with 1 starting first so that 2 blocks
3. start another concurrent select on T7 - it should block behind waiting X (from drop) - LM should be fair
4. finish #1 so that drop unblocks
5. rollback the drop to release its X lock
6. # should unblock
ql/src/test/org/apache/hadoop/hive/ql/lockmgr/TestDbTxnManager2.java