use of io.seata.server.coordinator.DefaultCore in project seata by seata.
the class DefaultCoreForEventBusTest method test.
@Test
public void test() throws IOException, TransactionException, InterruptedException {
class GlobalTransactionEventSubscriber {
private final Map<GlobalStatus, AtomicInteger> eventCounters;
public Map<GlobalStatus, AtomicInteger> getEventCounters() {
return eventCounters;
}
public GlobalTransactionEventSubscriber() {
this.eventCounters = new ConcurrentHashMap<>();
}
@Subscribe
public void processGlobalTransactionEvent(GlobalTransactionEvent event) {
AtomicInteger counter = eventCounters.computeIfAbsent(event.getStatus(), status -> new AtomicInteger(0));
counter.addAndGet(1);
}
}
SessionHolder.init(null);
RemotingServer remotingServer = new DefaultCoordinatorTest.MockServerMessageSender();
DefaultCoordinator coordinator = new DefaultCoordinator(remotingServer);
coordinator.init();
try {
DefaultCore core = new DefaultCore(remotingServer);
GlobalTransactionEventSubscriber subscriber = new GlobalTransactionEventSubscriber();
// avoid transactional interference from other unit tests
Thread.sleep(1500);
EventBusManager.get().register(subscriber);
// start a transaction
String xid = core.begin("test_app_id", "default_group", "test_tran_name", 30000);
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Begin).get());
// commit this transaction
core.commit(xid);
// we need sleep for a short while because default canBeCommittedAsync() is true
Thread.sleep(1000);
// check
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.AsyncCommitting).get());
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Committed).get());
// start another new transaction
xid = core.begin("test_app_id", "default_group", "test_tran_name2", 30000);
Assertions.assertEquals(2, subscriber.getEventCounters().get(GlobalStatus.Begin).get());
core.rollback(xid);
// check
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Rollbacking).get());
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Rollbacked).get());
// start more one new transaction for test timeout and let this transaction immediately timeout
xid = core.begin("test_app_id", "default_group", "test_tran_name3", 0);
// sleep for check -> DefaultCoordinator.timeoutCheck
Thread.sleep(1000);
// at lease retry once because DefaultCoordinator.timeoutCheck is 1 second
Assertions.assertTrue(subscriber.getEventCounters().get(GlobalStatus.TimeoutRollbacking).get() >= 1);
} finally {
coordinator.destroy();
SessionHolder.destroy();
}
}
Aggregations