Skip to content

Commit

Permalink
Add unit test to ensure tasks with a group id are processed in order
Browse files Browse the repository at this point in the history
  • Loading branch information
markushc committed Aug 23, 2023
1 parent 0196cb1 commit 39df2bb
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -243,6 +244,55 @@ public void success(TransactionOutboxEntry entry) {
MatcherAssert.assertThat(ids, containsInAnyOrder("1", "2", "4", "6"));
}

@Test
void orderedRequests() {

TransactionManager transactionManager = simpleTxnManager();

List<String> ids = new ArrayList<>();
AtomicReference<Clock> clockProvider = new AtomicReference<>(Clock.systemDefaultZone());

TransactionOutbox outbox =
TransactionOutbox.builder()
.transactionManager(transactionManager)
.listener(
new TransactionOutboxListener() {
@Override
public void success(TransactionOutboxEntry entry) {
ids.add((String) entry.getInvocation().getArgs()[0]);
}
})
.submitter(Submitter.withExecutor(Runnable::run))
.persistor(Persistor.forDialect(connectionDetails().dialect()))
.clockProvider(clockProvider::get)
.build();

clearOutbox();

String groupId = "groupId1";
transactionManager.inTransaction(
() -> outbox.with().groupId(groupId).schedule(ClassProcessor.class).process("2"));
clockProvider.set(
Clock.fixed(clockProvider.get().instant().minusSeconds(1), clockProvider.get().getZone()));
transactionManager.inTransaction(
() -> outbox.with().groupId(groupId).schedule(ClassProcessor.class).process("1"));
clockProvider.set(
Clock.fixed(clockProvider.get().instant().plusSeconds(2), clockProvider.get().getZone()));
transactionManager.inTransaction(
() -> outbox.with().groupId(groupId).schedule(ClassProcessor.class).process("3"));

// Ensure no tasks were processed immediately, as a group id was provided
MatcherAssert.assertThat(ids, equalTo(new ArrayList<>(List.of())));

// Run the clock over the threshold
clockProvider.set(
Clock.fixed(clockProvider.get().instant().plusSeconds(120), clockProvider.get().getZone()));
while (outbox.flush()) {}

// Ensure tasks were processed in order, as all shared the same group id
MatcherAssert.assertThat(ids, equalTo(new ArrayList<>(List.of("1", "2", "3"))));
}

/**
* Uses a simple data source transaction manager and attempts to fire a concrete class via
* reflection.
Expand Down

0 comments on commit 39df2bb

Please sign in to comment.