private void abort(String transactionId) throws Throwable { this.host.testStart(1); Operation patch = SimpleTransactionService.TxUtils.buildAbortRequest(this.host, transactionId); patch.setCompletion( (o, e) -> { if (operationFailed(o, e)) { this.host.failIteration(e); return; } this.host.completeIteration(); }); this.host.send(patch); this.host.testWait(); }
private void sendWithdrawDepositOperationPairs( String[] txids, int numOfTransfers, boolean independentTest) throws Throwable { Collection<Operation> requests = new ArrayList<Operation>(numOfTransfers); Random rand = new Random(); for (int k = 0; k < numOfTransfers; k++) { final String tid = txids[k]; int i = rand.nextInt(this.accountCount); int j = rand.nextInt(this.accountCount); if (i == j) { j = (j + 1) % this.accountCount; } final int final_j = j; int amount = 1 + rand.nextInt(3); this.host.log("Transaction %s: Transferring $%d from %d to %d", tid, amount, i, final_j); Operation withdraw = createWithdrawOperation(tid, buildAccountId(i), amount); withdraw.setCompletion( (o, e) -> { if (e != null) { this.host.log("Transaction %s: failed to withdraw, aborting...", tid); Operation abort = SimpleTransactionService.TxUtils.buildAbortRequest(this.host, tid); abort.setCompletion( (op, ex) -> { if (independentTest) { this.host.completeIteration(); } }); this.host.send(abort); return; } Operation deposit = createDepositOperation(tid, buildAccountId(final_j), amount); deposit.setCompletion( (op, ex) -> { if (ex != null) { this.host.log("Transaction %s: failed to deposit, aborting...", tid); Operation abort = SimpleTransactionService.TxUtils.buildAbortRequest(this.host, tid); abort.setCompletion( (op2, ex2) -> { if (independentTest) { this.host.completeIteration(); } }); this.host.send(abort); return; } this.host.log("Transaction %s: Committing", tid); Operation commit = SimpleTransactionService.TxUtils.buildCommitRequest(this.host, tid); commit.setCompletion( (op2, ex2) -> { if (independentTest) { this.host.completeIteration(); } }); this.host.send(commit); }); this.host.send(deposit); }); requests.add(withdraw); } if (independentTest) { this.host.testStart(numOfTransfers); } for (Operation withdraw : requests) { this.host.send(withdraw); } if (independentTest) { this.host.testWait(); } }