Search in sources :

Example 1 with TransactionCheckBuilder

use of org.apache.curator.framework.api.transaction.TransactionCheckBuilder in project elastic-job by dangdangdotcom.

the class JobNodeStorageTest method assertExecuteInTransactionFailure.

@Test(expected = RuntimeException.class)
public void assertExecuteInTransactionFailure() throws Exception {
    CuratorFramework client = mock(CuratorFramework.class);
    CuratorTransaction curatorTransaction = mock(CuratorTransaction.class);
    TransactionCheckBuilder transactionCheckBuilder = mock(TransactionCheckBuilder.class);
    CuratorTransactionBridge curatorTransactionBridge = mock(CuratorTransactionBridge.class);
    CuratorTransactionFinal curatorTransactionFinal = mock(CuratorTransactionFinal.class);
    when(regCenter.getRawClient()).thenReturn(client);
    when(client.inTransaction()).thenReturn(curatorTransaction);
    when(curatorTransaction.check()).thenReturn(transactionCheckBuilder);
    when(transactionCheckBuilder.forPath("/")).thenReturn(curatorTransactionBridge);
    when(curatorTransactionBridge.and()).thenReturn(curatorTransactionFinal);
    TransactionCreateBuilder transactionCreateBuilder = mock(TransactionCreateBuilder.class);
    when(curatorTransactionFinal.create()).thenReturn(transactionCreateBuilder);
    when(transactionCreateBuilder.forPath("/test_transaction")).thenReturn(curatorTransactionBridge);
    when(curatorTransactionBridge.and()).thenThrow(new RuntimeException());
    jobNodeStorage.executeInTransaction(new TransactionExecutionCallback() {

        @Override
        public void execute(final CuratorTransactionFinal curatorTransactionFinal) throws Exception {
            curatorTransactionFinal.create().forPath("/test_transaction").and();
        }
    });
    verify(regCenter).getRawClient();
    verify(client).inTransaction();
    verify(curatorTransaction).check();
    verify(transactionCheckBuilder).forPath("/");
    verify(curatorTransactionBridge, times(2)).and();
    verify(curatorTransactionFinal).create();
    verify(transactionCreateBuilder).forPath("/test_transaction");
    verify(curatorTransactionFinal, times(0)).commit();
}
Also used : TransactionCreateBuilder(org.apache.curator.framework.api.transaction.TransactionCreateBuilder) CuratorTransactionBridge(org.apache.curator.framework.api.transaction.CuratorTransactionBridge) CuratorFramework(org.apache.curator.framework.CuratorFramework) TransactionCheckBuilder(org.apache.curator.framework.api.transaction.TransactionCheckBuilder) CuratorTransaction(org.apache.curator.framework.api.transaction.CuratorTransaction) CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) Test(org.junit.Test)

Example 2 with TransactionCheckBuilder

use of org.apache.curator.framework.api.transaction.TransactionCheckBuilder in project elastic-job by dangdangdotcom.

the class JobNodeStorageTest method assertExecuteInTransactionSuccess.

@Test
public void assertExecuteInTransactionSuccess() throws Exception {
    CuratorFramework client = mock(CuratorFramework.class);
    CuratorTransaction curatorTransaction = mock(CuratorTransaction.class);
    TransactionCheckBuilder transactionCheckBuilder = mock(TransactionCheckBuilder.class);
    CuratorTransactionBridge curatorTransactionBridge = mock(CuratorTransactionBridge.class);
    CuratorTransactionFinal curatorTransactionFinal = mock(CuratorTransactionFinal.class);
    when(regCenter.getRawClient()).thenReturn(client);
    when(client.inTransaction()).thenReturn(curatorTransaction);
    when(curatorTransaction.check()).thenReturn(transactionCheckBuilder);
    when(transactionCheckBuilder.forPath("/")).thenReturn(curatorTransactionBridge);
    when(curatorTransactionBridge.and()).thenReturn(curatorTransactionFinal);
    TransactionCreateBuilder transactionCreateBuilder = mock(TransactionCreateBuilder.class);
    when(curatorTransactionFinal.create()).thenReturn(transactionCreateBuilder);
    when(transactionCreateBuilder.forPath("/test_transaction")).thenReturn(curatorTransactionBridge);
    when(curatorTransactionBridge.and()).thenReturn(curatorTransactionFinal);
    jobNodeStorage.executeInTransaction(new TransactionExecutionCallback() {

        @Override
        public void execute(final CuratorTransactionFinal curatorTransactionFinal) throws Exception {
            curatorTransactionFinal.create().forPath("/test_transaction").and();
        }
    });
    verify(regCenter).getRawClient();
    verify(client).inTransaction();
    verify(curatorTransaction).check();
    verify(transactionCheckBuilder).forPath("/");
    verify(curatorTransactionBridge, times(2)).and();
    verify(curatorTransactionFinal).create();
    verify(transactionCreateBuilder).forPath("/test_transaction");
    verify(curatorTransactionFinal).commit();
}
Also used : TransactionCreateBuilder(org.apache.curator.framework.api.transaction.TransactionCreateBuilder) CuratorTransactionBridge(org.apache.curator.framework.api.transaction.CuratorTransactionBridge) CuratorFramework(org.apache.curator.framework.CuratorFramework) TransactionCheckBuilder(org.apache.curator.framework.api.transaction.TransactionCheckBuilder) CuratorTransaction(org.apache.curator.framework.api.transaction.CuratorTransaction) CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) Test(org.junit.Test)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)2 CuratorTransaction (org.apache.curator.framework.api.transaction.CuratorTransaction)2 CuratorTransactionBridge (org.apache.curator.framework.api.transaction.CuratorTransactionBridge)2 CuratorTransactionFinal (org.apache.curator.framework.api.transaction.CuratorTransactionFinal)2 TransactionCheckBuilder (org.apache.curator.framework.api.transaction.TransactionCheckBuilder)2 TransactionCreateBuilder (org.apache.curator.framework.api.transaction.TransactionCreateBuilder)2 Test (org.junit.Test)2