Skip to content

Commit

Permalink
Investigate flakey test: shouldReject #89
Browse files Browse the repository at this point in the history
  • Loading branch information
cer committed Oct 6, 2022
1 parent 96f2ac3 commit 7bf9840
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import io.eventuate.tram.messaging.common.Message;
import io.eventuate.tram.reactive.commands.consumer.ReactiveCommandHandlers;
import io.eventuate.tram.sagas.reactive.participant.ReactiveSagaCommandHandlersBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;

import static io.eventuate.tram.reactive.commands.consumer.ReactiveCommandHandlerReplyBuilder.withFailure;
import static io.eventuate.tram.reactive.commands.consumer.ReactiveCommandHandlerReplyBuilder.withSuccess;

public class CustomerCommandHandler {
private final Logger logger = LoggerFactory.getLogger(getClass());

private CustomerService customerService;

Expand All @@ -33,11 +36,22 @@ public ReactiveCommandHandlers commandHandlerDefinitions() {
public Mono<Message> reserveCredit(CommandMessage<ReserveCreditCommand> cm) {
ReserveCreditCommand cmd = cm.getCommand();

logger.info("reserveCredit OrderId={}, CustomerID={}", cmd.getOrderId(), cmd.getCustomerId());

return customerService
.reserveCredit(cmd.getOrderId(), cmd.getCustomerId(), cmd.getOrderTotal())
.flatMap(m -> withSuccess(new CustomerCreditReserved()))
.onErrorResume(CustomerNotFoundException.class, e -> withFailure(new CustomerNotFound()))
.onErrorResume(CustomerCreditLimitExceededException.class, e -> withFailure(new CustomerCreditLimitExceeded()));
.flatMap(m -> {
logger.info("reservedCredit Success OrderId={}, CustomerID={}", cmd.getOrderId(), cmd.getCustomerId());
return withSuccess(new CustomerCreditReserved());
})
.onErrorResume(CustomerNotFoundException.class, e -> {
logger.info("reservedCredit CustomerNotFoundException OrderId={}, CustomerID={}", cmd.getOrderId(), cmd.getCustomerId());
return withFailure(new CustomerNotFound());
})
.onErrorResume(CustomerCreditLimitExceededException.class, e -> {
logger.info("reservedCredit CustomerCreditLimitExceededException OrderId={}, CustomerID={}", cmd.getOrderId(), cmd.getCustomerId());
return withFailure(new CustomerCreditLimitExceeded());
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
import io.eventuate.tram.commands.consumer.CommandWithDestination;
import io.eventuate.tram.sagas.reactive.orchestration.ReactiveSagaDefinition;
import io.eventuate.tram.sagas.reactive.simpledsl.SimpleReactiveSaga;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;

import static io.eventuate.tram.commands.consumer.CommandWithDestinationBuilder.send;

public class CreateOrderSaga implements SimpleReactiveSaga<CreateOrderSagaData> {
private final Logger logger = LoggerFactory.getLogger(getClass());

private OrderService orderService;
private final OrderService orderService;

public CreateOrderSaga(OrderService orderService) {
this.orderService = orderService;
}

private ReactiveSagaDefinition<CreateOrderSagaData> sagaDefinition =
private final ReactiveSagaDefinition<CreateOrderSagaData> sagaDefinition =
step()
.invokeLocal(this::create)
.withCompensation(this::reject)
Expand Down Expand Up @@ -52,6 +55,7 @@ private Mono<?> create(CreateOrderSagaData data) {
return orderService
.createOrder(data.getOrderDetails())
.map(o -> {
logger.info("saga createOrder() with ID={}", o.getId());
data.setOrderId(o.getId());
return o;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import io.eventuate.util.test.async.Eventually;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
Expand All @@ -31,6 +33,8 @@
webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class CustomersAndOrdersIntegrationTest {

private final Logger logger = LoggerFactory.getLogger(getClass());

@Configuration
@Import({OrderConfiguration.class, CustomerConfiguration.class})
public static class Config {
Expand All @@ -52,7 +56,7 @@ public static class Config {
public void shouldApprove() {
Customer customer = customerService.createCustomer(CUSTOMER_NAME, new Money("15.00")).block();

System.out.println("CustomerID=" + customer.getId());
logger.info("shouldApprove CustomerID={}", customer.getId());

Order order = orderSagaService.createOrder(new OrderDetails(customer.getId(), new Money("12.34"))).block();

Expand All @@ -63,7 +67,7 @@ public void shouldApprove() {
public void shouldReject() {
Customer customer = customerService.createCustomer(CUSTOMER_NAME, new Money("10.00")).block();

System.out.println("CustomerID=" + customer.getId());
logger.info("shouldReject CustomerID={}", customer.getId());

Order order = orderSagaService.createOrder(new OrderDetails(customer.getId(), new Money("12.34"))).block();

Expand All @@ -79,7 +83,11 @@ public void shouldThrowLocalException() {

@Test
public void shouldRejectBecauseOfNonExistingUser() {
Order order = orderSagaService.createOrder(new OrderDetails(System.currentTimeMillis(), new Money("12.34"))).block();
long customerId = System.currentTimeMillis();

logger.info("shouldRejectBecauseOfNonExistingUser CustomerID={}", customerId);

Order order = orderSagaService.createOrder(new OrderDetails(customerId, new Money("12.34"))).block();

assertOrderState(order.getId(), Optional.of(OrderState.REJECTED), Optional.of(RejectionReason.UNKNOWN_CUSTOMER));
}
Expand Down

0 comments on commit 7bf9840

Please sign in to comment.