use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.
the class TransactionLogObserverTest method testCreateTransactionCommitRollback.
@Test(timeout = 10000)
public void testCreateTransactionCommitRollback() throws InterruptedException {
createObserver();
ClientTransactionManager clientManager = direct.getClientTransactionManager();
String transactionId = UUID.randomUUID().toString();
BlockingQueue<Pair<String, Response>> responseQueue = new ArrayBlockingQueue<>(1);
clientManager.begin(transactionId, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 1L, "1")));
responseQueue.take();
clientManager.commit(transactionId, Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 2L, "1")));
responseQueue.take();
clientManager.rollback(transactionId);
Pair<String, Response> response = responseQueue.take();
assertEquals(Response.Flags.ABORTED, response.getSecond().getFlags());
// now, when we start new transaction, it must be let through
transactionId = UUID.randomUUID().toString();
responseQueue.clear();
clientManager.begin(transactionId, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), KeyAttributes.ofWildcardQueryElements(user, "user", userGateways, Collections.emptyList()));
responseQueue.take();
clientManager.commit(transactionId, Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 2L, "1")));
response = responseQueue.take();
assertEquals(Response.Flags.COMMITTED, response.getSecond().getFlags());
}
use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.
the class TransactionLogObserverTest method testCreateTransactionCommitWithConflictInOutputs.
@Test(timeout = 10000)
public void testCreateTransactionCommitWithConflictInOutputs() throws InterruptedException {
createObserver();
ClientTransactionManager clientManager = direct.getClientTransactionManager();
String t1 = UUID.randomUUID().toString();
String t2 = UUID.randomUUID().toString();
BlockingQueue<Pair<String, Response>> responseQueue = new ArrayBlockingQueue<>(1);
clientManager.begin(t1, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "u1", userGateways, 1L, "1")));
Pair<String, Response> t1OpenResponse = responseQueue.take();
List<KeyAttribute> t1Outputs = Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, t1OpenResponse.getSecond().getSeqId(), "1"));
clientManager.begin(t2, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), // not conflicting with the previous one
Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "u2", userGateways, 1L, "1")));
Pair<String, Response> t2OpenResponse = responseQueue.take();
List<KeyAttribute> t2Outputs = Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, t2OpenResponse.getSecond().getSeqId(), "1"));
clientManager.commit(t2, t2Outputs);
Pair<String, Response> response = responseQueue.take();
assertEquals("commit", response.getFirst());
assertEquals(Response.Flags.COMMITTED, response.getSecond().getFlags());
clientManager.commit(t1, t1Outputs);
response = responseQueue.take();
assertEquals("commit", response.getFirst());
assertEquals(Response.Flags.ABORTED, response.getSecond().getFlags());
}
use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.
the class TransactionLogObserverTest method testCreateTransaction.
@Test(timeout = 10000)
public void testCreateTransaction() throws InterruptedException {
createObserver();
ClientTransactionManager clientManager = direct.getClientTransactionManager();
String transactionId = UUID.randomUUID().toString();
BlockingQueue<Pair<String, Response>> responseQueue = new ArrayBlockingQueue<>(1);
clientManager.begin(transactionId, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 1L, "1")));
Pair<String, Response> response = responseQueue.take();
assertTrue(response.getFirst().startsWith("open."));
assertEquals(Response.Flags.OPEN, response.getSecond().getFlags());
}
use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.
the class TransactionLogObserver method processTransactionRequest.
private void processTransactionRequest(String transactionId, String requestId, Request request, OnNextContext context) {
log.debug("Processing request to {} with {} for transaction {}", requestId, request, transactionId);
State currentState = manager.getCurrentState(transactionId);
@Nullable State newState = transitionState(transactionId, currentState, request);
if (newState != null) {
// we have successfully computed new state, produce response
Response response = getResponseForNewState(request, currentState, newState);
manager.ensureTransactionOpen(transactionId, newState);
manager.writeResponseAndUpdateState(transactionId, newState, requestId, response, context::commit);
} else if (request.getFlags() == Request.Flags.OPEN && (currentState.getFlags() == State.Flags.OPEN || currentState.getFlags() == State.Flags.COMMITTED)) {
manager.writeResponseAndUpdateState(transactionId, currentState, requestId, Response.forRequest(request).duplicate(currentState.getSequentialId()), context::commit);
} else {
log.warn("Unexpected {} request for transaction {} seqId {} when the state is {}. " + "Refusing to respond, because the correct response is unknown.", request.getFlags(), transactionId, currentState.getSequentialId(), currentState.getFlags());
context.confirm();
}
}
use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.
the class TransactionLogObserverTest method testTwoOpenedCommitWildcard.
@Test(timeout = 10000)
public void testTwoOpenedCommitWildcard() throws InterruptedException {
createObserver();
try (ClientTransactionManager clientManager = direct.getClientTransactionManager()) {
String t1 = UUID.randomUUID().toString();
String t2 = UUID.randomUUID().toString();
List<KeyAttribute> inputs = KeyAttributes.ofWildcardQueryElements(user, "user", userGateways, Collections.singletonList(userGateways.upsert(100L, "user", "1", now, new byte[] {})));
BlockingQueue<Pair<String, Response>> responseQueue = new ArrayBlockingQueue<>(1);
clientManager.begin(t1, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), inputs);
Pair<String, Response> t1OpenResponse = responseQueue.take();
clientManager.begin(t2, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), inputs);
Pair<String, Response> t2OpenResponse = responseQueue.take();
assertEquals(Response.Flags.OPEN, t1OpenResponse.getSecond().getFlags());
assertEquals(Response.Flags.OPEN, t2OpenResponse.getSecond().getFlags());
List<KeyAttribute> t1Outputs = Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, t1OpenResponse.getSecond().getSeqId(), "1"));
List<KeyAttribute> t2Outputs = Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, t2OpenResponse.getSecond().getSeqId(), "1"));
clientManager.commit(t1, t1Outputs);
Pair<String, Response> response = responseQueue.take();
assertEquals("commit", response.getFirst());
assertEquals(Response.Flags.COMMITTED, response.getSecond().getFlags());
clientManager.commit(t2, t2Outputs);
response = responseQueue.take();
assertEquals("commit", response.getFirst());
assertEquals(Response.Flags.ABORTED, response.getSecond().getFlags());
}
}
Aggregations