@Test(expected = TransactionException.class)
  public void test_lockClusterState_fail() throws Exception {
    Address initiator = newAddress();
    final ClusterState newState = FROZEN;
    clusterStateManager.lockClusterState(newState, initiator, TXN, 1000, 0);

    clusterStateManager.lockClusterState(newState, initiator, ANOTHER_TXN, 1000, 0);
  }
  @Test
  public void test_lockClusterState_extendLease() throws Exception {
    final Address initiator = newAddress();
    clusterStateManager.lockClusterState(FROZEN, initiator, TXN, 10000, 0);
    clusterStateManager.lockClusterState(FROZEN, initiator, TXN, TimeUnit.DAYS.toMillis(1), 0);

    final ClusterStateLock stateLock = clusterStateManager.getStateLock();
    assertTrue(
        Clock.currentTimeMillis() + TimeUnit.HOURS.toMillis(12) < stateLock.getLockExpiryTime());
  }
  @Test(expected = IllegalStateException.class)
  public void test_lockClusterState_forFrozenState_whenHasOnGoingMigration() throws Exception {
    when(partitionService.hasOnGoingMigrationLocal()).thenReturn(true);

    Address initiator = newAddress();
    final ClusterState newState = FROZEN;
    clusterStateManager.lockClusterState(newState, initiator, TXN, 1000, 0);
  }
  @Test
  public void test_lockClusterState_success() throws Exception {
    Address initiator = newAddress();
    final ClusterState newState = FROZEN;
    clusterStateManager.lockClusterState(newState, initiator, TXN, 1000, 0);

    assertLockedBy(initiator);
  }
  @Test
  public void changeLocalClusterState_shouldChangeNodeStateToShuttingDown_whenStateBecomes_PASSIVE()
      throws Exception {
    final ClusterState newState = PASSIVE;
    final Address initiator = newAddress();
    clusterStateManager.lockClusterState(newState, initiator, TXN, 10000, 0);
    clusterStateManager.commitClusterState(newState, initiator, TXN);

    assertEquals(newState, clusterStateManager.getState());
    verify(node, times(1)).changeNodeStateToPassive();
  }
  @Test
  public void test_changeLocalClusterState_success() throws Exception {
    final ClusterState newState = FROZEN;
    final Address initiator = newAddress();
    clusterStateManager.lockClusterState(newState, initiator, TXN, 10000, 0);
    clusterStateManager.commitClusterState(newState, initiator, TXN);

    assertEquals(newState, clusterStateManager.getState());
    final ClusterStateLock stateLock = clusterStateManager.getStateLock();
    assertFalse(stateLock.isLocked());
  }
  @Test
  public void changeLocalClusterState_shouldRemoveMembersDeadWhileFrozen_whenStateBecomes_ACTIVE()
      throws Exception {
    final ClusterState newState = ACTIVE;
    final Address initiator = newAddress();
    clusterStateManager.initialClusterState(FROZEN);
    clusterStateManager.lockClusterState(newState, initiator, TXN, 10000, 0);
    clusterStateManager.commitClusterState(newState, initiator, TXN);

    assertEquals(newState, clusterStateManager.getState());
    verify(clusterService, times(1)).removeMembersDeadWhileClusterIsNotActive();
  }
 @Test
 public void test_lockClusterState_expiry() throws Exception {
   clusterStateManager.lockClusterState(FROZEN, newAddress(), TXN, 1, 0);
   assertTrueEventually(
       new AssertTask() {
         @Override
         public void run() throws Exception {
           final ClusterStateLock stateLock = clusterStateManager.getStateLock();
           assertFalse(stateLock.isLocked());
           assertEquals(ACTIVE, clusterStateManager.getState());
         }
       });
 }
 @Test(expected = TransactionException.class)
 public void test_changeLocalClusterState_fail_whenLockedByElse() throws Exception {
   final Address initiator = newAddress();
   clusterStateManager.lockClusterState(FROZEN, initiator, TXN, 10000, 0);
   clusterStateManager.commitClusterState(FROZEN, initiator, ANOTHER_TXN);
 }
 @Test
 public void test_unlockClusterState_success() throws Exception {
   clusterStateManager.lockClusterState(FROZEN, newAddress(), TXN, 1000, 0);
   assertTrue(clusterStateManager.rollbackClusterState(TXN));
 }
 @Test(expected = IllegalStateException.class)
 public void test_lockClusterState_fail_withDifferentPartitionStateVersions() throws Exception {
   clusterStateManager.lockClusterState(FROZEN, newAddress(), TXN, 1000, 1);
 }
 @Test
 public void test_unlockClusterState_fail_whenLockedByElse() throws Exception {
   clusterStateManager.lockClusterState(FROZEN, newAddress(), TXN, 1000, 0);
   assertFalse(clusterStateManager.rollbackClusterState(ANOTHER_TXN));
 }
 @Test(expected = IllegalArgumentException.class)
 public void test_lockClusterState_nonPositiveLeaseTime() throws Exception {
   Address initiator = newAddress();
   clusterStateManager.lockClusterState(FROZEN, initiator, TXN, -1000, 0);
 }
 @Test(expected = NullPointerException.class)
 public void test_lockClusterState_nullTransactionId() throws Exception {
   Address initiator = newAddress();
   clusterStateManager.lockClusterState(FROZEN, initiator, null, 1000, 0);
 }
 @Test(expected = NullPointerException.class)
 public void test_lockClusterState_nullInitiator() throws Exception {
   clusterStateManager.lockClusterState(FROZEN, null, TXN, 1000, 0);
 }
 @Test(expected = NullPointerException.class)
 public void test_lockClusterState_nullState() throws Exception {
   Address initiator = newAddress();
   clusterStateManager.lockClusterState(null, initiator, TXN, 1000, 0);
 }