Search in sources :

Example 1 with BranchSession

use of io.seata.server.session.BranchSession in project seata by seata.

the class FileTransactionStoreManagerTest method testFindTimeoutAndSave.

@Test
public void testFindTimeoutAndSave() throws Exception {
    File seataFile = Files.newTemporaryFile();
    Method findTimeoutAndSaveMethod = FileTransactionStoreManager.class.getDeclaredMethod("findTimeoutAndSave");
    findTimeoutAndSaveMethod.setAccessible(true);
    FileSessionManager sessionManager = null;
    FileTransactionStoreManager fileTransactionStoreManager = null;
    try {
        List<GlobalSession> timeoutSessions = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            GlobalSession globalSession = new GlobalSession("", "", "", 60000);
            BranchSession branchSessionA = Mockito.mock(BranchSession.class);
            Mockito.when(branchSessionA.encode()).thenReturn(createBigBranchSessionData(globalSession, (byte) 'A'));
            Mockito.when(branchSessionA.getApplicationData()).thenReturn(new String(createBigApplicationData((byte) 'A')));
            globalSession.addBranch(branchSessionA);
            BranchSession branchSessionB = Mockito.mock(BranchSession.class);
            Mockito.when(branchSessionB.encode()).thenReturn(createBigBranchSessionData(globalSession, (byte) 'B'));
            Mockito.when(branchSessionB.getApplicationData()).thenReturn(new String(createBigApplicationData((byte) 'B')));
            globalSession.addBranch(branchSessionB);
            timeoutSessions.add(globalSession);
        }
        SessionManager sessionManagerMock = Mockito.mock(SessionManager.class);
        Mockito.when(sessionManagerMock.findGlobalSessions(Mockito.any())).thenReturn(timeoutSessions);
        fileTransactionStoreManager = new FileTransactionStoreManager(seataFile.getAbsolutePath(), sessionManagerMock);
        Assertions.assertTrue((boolean) findTimeoutAndSaveMethod.invoke(fileTransactionStoreManager));
        sessionManager = new FileSessionManager(seataFile.getName(), seataFile.getParent());
        sessionManager.reload();
        Collection<GlobalSession> globalSessions = sessionManager.allSessions();
        Assertions.assertNotNull(globalSessions);
        globalSessions.forEach(g -> {
            Assertions.assertNotNull(g);
            List<BranchSession> branches = g.getBranchSessions();
            Assertions.assertEquals(2, branches.size());
            Assertions.assertEquals(new String(createBigApplicationData((byte) 'A')), branches.get(0).getApplicationData());
            Assertions.assertEquals(new String(createBigApplicationData((byte) 'B')), branches.get(1).getApplicationData());
        });
    } finally {
        findTimeoutAndSaveMethod.setAccessible(false);
        if (fileTransactionStoreManager != null) {
            fileTransactionStoreManager.shutdown();
        }
        if (sessionManager != null) {
            sessionManager.destroy();
        }
        Assertions.assertTrue(seataFile.delete());
    }
}
Also used : FileSessionManager(io.seata.server.storage.file.session.FileSessionManager) SessionManager(io.seata.server.session.SessionManager) BranchSession(io.seata.server.session.BranchSession) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) FileSessionManager(io.seata.server.storage.file.session.FileSessionManager) GlobalSession(io.seata.server.session.GlobalSession) File(java.io.File) FileTransactionStoreManager(io.seata.server.storage.file.store.FileTransactionStoreManager) Test(org.junit.jupiter.api.Test)

Example 2 with BranchSession

use of io.seata.server.session.BranchSession in project seata by seata.

the class RedisSessionManagerTest method test_addBranchSession.

@Test
public void test_addBranchSession() throws TransactionException {
    GlobalSession globalSession = GlobalSession.createGlobalSession("test", "test", "test123", 100);
    String xid = XID.generateXID(globalSession.getTransactionId());
    globalSession.setXid(xid);
    globalSession.setTransactionId(globalSession.getTransactionId());
    globalSession.setBeginTime(System.currentTimeMillis());
    globalSession.setApplicationData("abc=878s");
    globalSession.setStatus(GlobalStatus.Begin);
    sessionManager.addGlobalSession(globalSession);
    BranchSession branchSession = new BranchSession();
    branchSession.setBranchId(UUIDGenerator.generateUUID());
    branchSession.setXid(xid);
    branchSession.setTransactionId(globalSession.getTransactionId());
    branchSession.setBranchId(1L);
    branchSession.setResourceGroupId("my_test_tx_group");
    branchSession.setResourceId("tb_1");
    branchSession.setLockKey("t_1");
    branchSession.setBranchType(BranchType.AT);
    branchSession.setApplicationData("{\"data\":\"test\"}");
    branchSession.setClientId("storage-server:192.168.158.80:11934");
    sessionManager.addBranchSession(globalSession, branchSession);
    sessionManager.removeBranchSession(globalSession, branchSession);
    sessionManager.removeGlobalSession(globalSession);
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) Test(org.junit.jupiter.api.Test)

Example 3 with BranchSession

use of io.seata.server.session.BranchSession in project seata by seata.

the class RedisSessionManagerTest method testReadSessionWithBranch.

@Test
public void testReadSessionWithBranch() throws TransactionException {
    GlobalSession session = GlobalSession.createGlobalSession("test", "test", "test123", 100);
    String xid = XID.generateXID(session.getTransactionId());
    session.setXid(xid);
    session.setTransactionId(session.getTransactionId());
    session.setBeginTime(System.currentTimeMillis());
    session.setApplicationData("abc=878s");
    session.setStatus(GlobalStatus.Begin);
    sessionManager.addGlobalSession(session);
    BranchSession branchSession = new BranchSession();
    branchSession.setBranchId(UUIDGenerator.generateUUID());
    branchSession.setXid(xid);
    branchSession.setTransactionId(session.getTransactionId());
    branchSession.setBranchId(1L);
    branchSession.setResourceGroupId("my_test_tx_group");
    branchSession.setResourceId("tb_1");
    branchSession.setLockKey("t_1");
    branchSession.setBranchType(BranchType.AT);
    branchSession.setApplicationData("{\"data\":\"test\"}");
    branchSession.setClientId("storage-server:192.168.158.80:11934");
    sessionManager.addBranchSession(session, branchSession);
    GlobalSession globalSession = sessionManager.findGlobalSession(xid, false);
    Assertions.assertEquals(session.getXid(), globalSession.getXid());
    Assertions.assertEquals(session.getTransactionId(), globalSession.getTransactionId());
    Assertions.assertEquals(0, globalSession.getBranchSessions().size());
    globalSession = sessionManager.findGlobalSession(xid, true);
    Assertions.assertEquals(branchSession.getXid(), globalSession.getBranchSessions().get(0).getXid());
    Assertions.assertEquals(branchSession.getBranchId(), globalSession.getBranchSessions().get(0).getBranchId());
    Assertions.assertEquals(branchSession.getClientId(), globalSession.getBranchSessions().get(0).getClientId());
    sessionManager.removeBranchSession(session, branchSession);
    sessionManager.removeGlobalSession(session);
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) Test(org.junit.jupiter.api.Test)

Example 4 with BranchSession

use of io.seata.server.session.BranchSession in project seata by seata.

the class RedisSessionManagerTest method testReadSessionWithConditionStatus.

@Test
public void testReadSessionWithConditionStatus() throws TransactionException {
    GlobalSession session = GlobalSession.createGlobalSession("test", "test", "test123", 100);
    String xid = XID.generateXID(session.getTransactionId());
    session.setXid(xid);
    session.setTransactionId(session.getTransactionId());
    session.setBeginTime(System.currentTimeMillis());
    session.setApplicationData("abc=878s");
    session.setStatus(GlobalStatus.Begin);
    sessionManager.addGlobalSession(session);
    BranchSession branchSession = new BranchSession();
    branchSession.setBranchId(UUIDGenerator.generateUUID());
    branchSession.setXid(xid);
    branchSession.setTransactionId(session.getTransactionId());
    branchSession.setBranchId(1L);
    branchSession.setResourceGroupId("my_test_tx_group");
    branchSession.setResourceId("tb_1");
    branchSession.setLockKey("t_1");
    branchSession.setBranchType(BranchType.AT);
    branchSession.setApplicationData("{\"data\":\"test\"}");
    branchSession.setClientId("storage-server:192.168.158.80:11934");
    sessionManager.addBranchSession(session, branchSession);
    SessionCondition condition = new SessionCondition();
    condition.setStatus(GlobalStatus.Begin);
    sessionManager.findGlobalSessions(condition);
    condition.setStatus(null);
    GlobalStatus[] statuses = { GlobalStatus.Begin };
    condition.setStatuses(statuses);
    sessionManager.findGlobalSessions(condition);
    sessionManager.removeBranchSession(session, branchSession);
    sessionManager.removeGlobalSession(session);
}
Also used : GlobalStatus(io.seata.core.model.GlobalStatus) GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) SessionCondition(io.seata.server.session.SessionCondition) Test(org.junit.jupiter.api.Test)

Example 5 with BranchSession

use of io.seata.server.session.BranchSession in project seata by seata.

the class RedisSessionManagerTest method testReadSession.

@Test
public void testReadSession() throws TransactionException {
    GlobalSession session = GlobalSession.createGlobalSession("test", "test", "test123", 100);
    String xid = XID.generateXID(session.getTransactionId());
    session.setXid(xid);
    session.setTransactionId(session.getTransactionId());
    session.setBeginTime(System.currentTimeMillis());
    session.setApplicationData("abc=878s");
    session.setStatus(GlobalStatus.Begin);
    sessionManager.addGlobalSession(session);
    BranchSession branchSession = new BranchSession();
    branchSession.setBranchId(UUIDGenerator.generateUUID());
    branchSession.setXid(xid);
    branchSession.setTransactionId(session.getTransactionId());
    branchSession.setBranchId(1L);
    branchSession.setResourceGroupId("my_test_tx_group");
    branchSession.setResourceId("tb_1");
    branchSession.setLockKey("t_1");
    branchSession.setBranchType(BranchType.AT);
    branchSession.setApplicationData("{\"data\":\"test\"}");
    branchSession.setClientId("storage-server:192.168.158.80:11934");
    sessionManager.addBranchSession(session, branchSession);
    GlobalSession globalSession = sessionManager.findGlobalSession(xid);
    Assertions.assertEquals(session.getXid(), globalSession.getXid());
    Assertions.assertEquals(session.getTransactionId(), globalSession.getTransactionId());
    Assertions.assertEquals(branchSession.getXid(), globalSession.getBranchSessions().get(0).getXid());
    Assertions.assertEquals(branchSession.getBranchId(), globalSession.getBranchSessions().get(0).getBranchId());
    Assertions.assertEquals(branchSession.getClientId(), globalSession.getBranchSessions().get(0).getClientId());
    sessionManager.removeBranchSession(globalSession, branchSession);
    sessionManager.removeGlobalSession(globalSession);
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) Test(org.junit.jupiter.api.Test)

Aggregations

BranchSession (io.seata.server.session.BranchSession)60 GlobalSession (io.seata.server.session.GlobalSession)33 Test (org.junit.jupiter.api.Test)29 Connection (java.sql.Connection)9 SessionCondition (io.seata.server.session.SessionCondition)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 MethodSource (org.junit.jupiter.params.provider.MethodSource)7 BranchTransactionDO (io.seata.core.store.BranchTransactionDO)5 LockManager (io.seata.server.lock.LockManager)5 FileLockManagerForTest (io.seata.server.lock.file.FileLockManagerForTest)5 FileTransactionStoreManager (io.seata.server.storage.file.store.FileTransactionStoreManager)5 ResultSet (java.sql.ResultSet)5 ArrayList (java.util.ArrayList)5 BranchTransactionException (io.seata.core.exception.BranchTransactionException)4 TransactionException (io.seata.core.exception.TransactionException)4 BranchStatus (io.seata.core.model.BranchStatus)4 TransactionWriteStore (io.seata.server.storage.file.TransactionWriteStore)4 GlobalStatus (io.seata.core.model.GlobalStatus)3 SessionManager (io.seata.server.session.SessionManager)3 TransactionStoreManager (io.seata.server.store.TransactionStoreManager)3