public void testLocalDeadlock() throws Exception {
    final DldGlobalTransaction ddgt = (DldGlobalTransaction) gtf.newGlobalTransaction();

    InvocationContext localTxContext = buildLocalTxIc(ddgt);

    ddgt.setCoinToss(0);
    lockOwner.setCoinToss(1);
    assert ddgt.wouldLose(lockOwner);

    // this makes sure that we cannot acquire lock from the first try
    expect(lc.acquireLock("k", SPIN_DURATION, TimeUnit.MILLISECONDS)).andReturn(null);
    Lock mockLock = createNiceMock(Lock.class);
    expect(lc.acquireLock("k", SPIN_DURATION, TimeUnit.MILLISECONDS)).andReturn(mockLock);
    lockOwner.setRemote(false);
    lockOwner.setLockIntention("k");
    lockManager.setOwner(lockOwner);
    lockManager.setOwnsLock(true);
    replay(lc);
    try {
      lockManager.lockAndRecord("k", localTxContext);
      assert false;
    } catch (DeadlockDetectedException e) {
      // expected
    }
    assertEquals(1l, lockManager.getDetectedLocalDeadlocks());
  }
  public void testLockHeldByThread() throws Exception {
    InvocationContext localTxContext = buildLocalTxIc(new DldGlobalTransaction());

    // this makes sure that we cannot acquire lock from the first try
    expect(lc.acquireLock("k", SPIN_DURATION, TimeUnit.MILLISECONDS)).andReturn(null);
    lockManager.setOwner(Thread.currentThread());
    // next lock acquisition will succeed
    Lock mockLock = createNiceMock(Lock.class);
    expect(lc.acquireLock("k", SPIN_DURATION, TimeUnit.MILLISECONDS)).andReturn(mockLock);
    replay(lc);

    assert lockManager.lockAndRecord("k", localTxContext);
    assert lockManager.getOverlapWithNotDeadlockAwareLockOwners() == 1;
  }
  public void testNoTransaction() throws Exception {
    InvocationContext nonTx = new NonTxInvocationContext();

    //      expect(lc.acquireLock("k",config.getLockAcquisitionTimeout(),
    // TimeUnit.MILLISECONDS)).andReturn(EasyMock.<Lock>anyObject());
    Lock mockLock = createNiceMock(Lock.class);
    expect(lc.acquireLock("k", config.getLockAcquisitionTimeout(), TimeUnit.MILLISECONDS))
        .andReturn(mockLock);
    expect(lc.acquireLock("k", config.getLockAcquisitionTimeout(), TimeUnit.MILLISECONDS))
        .andReturn(null);
    replay(lc);
    assert lockManager.lockAndRecord("k", nonTx);
    assert !lockManager.lockAndRecord("k", nonTx);
    verify();
  }