Search in sources :

Example 1 with DefaultCore

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();
    }
}
Also used : GlobalStatus(io.seata.core.model.GlobalStatus) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultCore(io.seata.server.coordinator.DefaultCore) DefaultCoordinator(io.seata.server.coordinator.DefaultCoordinator) RemotingServer(io.seata.core.rpc.RemotingServer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) GlobalTransactionEvent(io.seata.core.event.GlobalTransactionEvent) Test(org.junit.jupiter.api.Test) DefaultCoordinatorTest(io.seata.server.coordinator.DefaultCoordinatorTest)

Aggregations

GlobalTransactionEvent (io.seata.core.event.GlobalTransactionEvent)1 GlobalStatus (io.seata.core.model.GlobalStatus)1 RemotingServer (io.seata.core.rpc.RemotingServer)1 DefaultCoordinator (io.seata.server.coordinator.DefaultCoordinator)1 DefaultCoordinatorTest (io.seata.server.coordinator.DefaultCoordinatorTest)1 DefaultCore (io.seata.server.coordinator.DefaultCore)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.jupiter.api.Test)1