Search in sources :

Example 26 with BranchSession

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

the class FileTransactionStoreManagerTest method testBigDataWrite.

@Test
public void testBigDataWrite() throws Exception {
    File seataFile = Files.newTemporaryFile();
    FileTransactionStoreManager fileTransactionStoreManager = null;
    try {
        fileTransactionStoreManager = new FileTransactionStoreManager(seataFile.getAbsolutePath(), null);
        BranchSession branchSessionA = Mockito.mock(BranchSession.class);
        GlobalSession global = new GlobalSession();
        Mockito.when(branchSessionA.encode()).thenReturn(createBigBranchSessionData(global, (byte) 'A'));
        Mockito.when(branchSessionA.getApplicationData()).thenReturn(new String(createBigApplicationData((byte) 'A')));
        BranchSession branchSessionB = Mockito.mock(BranchSession.class);
        Mockito.when(branchSessionB.encode()).thenReturn(createBigBranchSessionData(global, (byte) 'B'));
        Mockito.when(branchSessionB.getApplicationData()).thenReturn(new String(createBigApplicationData((byte) 'B')));
        Assertions.assertTrue(fileTransactionStoreManager.writeSession(TransactionStoreManager.LogOperation.BRANCH_ADD, branchSessionA));
        Assertions.assertTrue(fileTransactionStoreManager.writeSession(TransactionStoreManager.LogOperation.BRANCH_ADD, branchSessionB));
        List<TransactionWriteStore> list = fileTransactionStoreManager.readWriteStore(2000, false);
        Assertions.assertNotNull(list);
        Assertions.assertEquals(2, list.size());
        BranchSession loadedBranchSessionA = (BranchSession) list.get(0).getSessionRequest();
        Assertions.assertEquals(branchSessionA.getApplicationData(), loadedBranchSessionA.getApplicationData());
        BranchSession loadedBranchSessionB = (BranchSession) list.get(1).getSessionRequest();
        Assertions.assertEquals(branchSessionB.getApplicationData(), loadedBranchSessionB.getApplicationData());
    } finally {
        if (fileTransactionStoreManager != null) {
            fileTransactionStoreManager.shutdown();
        }
        Assertions.assertTrue(seataFile.delete());
    }
}
Also used : TransactionWriteStore(io.seata.server.storage.file.TransactionWriteStore) GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) File(java.io.File) FileTransactionStoreManager(io.seata.server.storage.file.store.FileTransactionStoreManager) Test(org.junit.jupiter.api.Test)

Example 27 with BranchSession

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

the class DataBaseSessionManagerTest method test_allSessions.

@Test
public void test_allSessions() throws Exception {
    GlobalSession globalSession = GlobalSession.createGlobalSession("test", "test", "test123", 100);
    String xid = XID.generateXID(globalSession.getTransactionId());
    globalSession.setXid(xid);
    globalSession.setTransactionId(146757978);
    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.setClientId("abc-123");
    branchSession.setApplicationData("{\"data\":\"test\"}");
    branchSession.setStatus(BranchStatus.PhaseOne_Done);
    sessionManager.addBranchSession(globalSession, branchSession);
    BranchSession branchSession2 = new BranchSession();
    branchSession2.setBranchId(UUIDGenerator.generateUUID());
    branchSession2.setXid(xid);
    branchSession2.setTransactionId(globalSession.getTransactionId());
    branchSession2.setBranchId(2L);
    branchSession2.setResourceGroupId("my_test_tx_group");
    branchSession2.setResourceId("tb_1");
    branchSession2.setLockKey("t_1");
    branchSession2.setBranchType(BranchType.TCC);
    branchSession2.setClientId("abc-123");
    branchSession2.setApplicationData("{\"data\":\"test\"}");
    branchSession2.setStatus(BranchStatus.PhaseOne_Done);
    sessionManager.addBranchSession(globalSession, branchSession2);
    Collection<GlobalSession> rets = sessionManager.allSessions();
    Assertions.assertNotNull(rets);
    Assertions.assertEquals(1, rets.size());
    GlobalSession globalSession_db = (GlobalSession) new ArrayList(rets).get(0);
    Assertions.assertNotNull(globalSession_db.getReverseSortedBranches());
    Assertions.assertEquals(2, globalSession_db.getReverseSortedBranches().size());
    Assertions.assertNotNull(globalSession_db.getBranch(1L));
    Assertions.assertNotNull(globalSession_db.getBranch(2L));
    String delSql = "delete from branch_table where xid= '" + xid + "'" + ";" + "delete from global_table where xid= '" + xid + "'";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        conn.createStatement().execute(delSql);
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) Test(org.junit.jupiter.api.Test)

Example 28 with BranchSession

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

the class DataBaseSessionManagerTest method test_findGlobalSessions.

@Test
public void test_findGlobalSessions() throws TransactionException, SQLException {
    String xid = null;
    {
        GlobalSession globalSession = GlobalSession.createGlobalSession("test", "test", "test123", 100);
        xid = XID.generateXID(globalSession.getTransactionId());
        globalSession.setXid(xid);
        globalSession.setTransactionId(146757978);
        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.setClientId("abc-123");
        branchSession.setApplicationData("{\"data\":\"test\"}");
        branchSession.setStatus(BranchStatus.PhaseOne_Done);
        sessionManager.addBranchSession(globalSession, branchSession);
    }
    String xid2 = null;
    {
        GlobalSession globalSession = GlobalSession.createGlobalSession("test", "test", "test123", 100);
        xid2 = XID.generateXID(globalSession.getTransactionId());
        globalSession.setXid(xid);
        globalSession.setTransactionId(146757978);
        globalSession.setBeginTime(System.currentTimeMillis());
        globalSession.setApplicationData("abc=878s");
        globalSession.setStatus(GlobalStatus.CommitRetrying);
        sessionManager.addGlobalSession(globalSession);
        BranchSession branchSession = new BranchSession();
        branchSession.setBranchId(UUIDGenerator.generateUUID());
        branchSession.setXid(xid2);
        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.setClientId("abc-123");
        branchSession.setApplicationData("{\"data\":\"test\"}");
        branchSession.setStatus(BranchStatus.PhaseOne_Done);
        sessionManager.addBranchSession(globalSession, branchSession);
    }
    Collection<GlobalSession> rets = sessionManager.findGlobalSessions(new SessionCondition(GlobalStatus.Begin));
    Assertions.assertNotNull(rets);
    Assertions.assertEquals(1, rets.size());
    GlobalSession globalSession_db = (GlobalSession) new ArrayList(rets).get(0);
    Assertions.assertNotNull(globalSession_db.getReverseSortedBranches());
    Assertions.assertEquals(1, globalSession_db.getReverseSortedBranches().size());
    Assertions.assertNotNull(globalSession_db.getBranch(1L));
    String delSql = "delete from branch_table where xid= '" + xid + "'" + ";" + "delete from global_table where xid= '" + xid + "'";
    String delSql2 = "delete from branch_table where xid= '" + xid2 + "'" + ";" + "delete from global_table where xid= '" + xid2 + "'";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        conn.createStatement().execute(delSql);
        conn.createStatement().execute(delSql2);
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) SessionCondition(io.seata.server.session.SessionCondition) Test(org.junit.jupiter.api.Test)

Example 29 with BranchSession

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

the class DataBaseSessionManagerTest method test_addBranchSession.

@Test
public void test_addBranchSession() throws Exception {
    GlobalSession globalSession = GlobalSession.createGlobalSession("test", "test", "test123", 100);
    String xid = XID.generateXID(globalSession.getTransactionId());
    globalSession.setXid(xid);
    globalSession.setTransactionId(146757978);
    globalSession.setBeginTime(System.currentTimeMillis());
    globalSession.setApplicationData("abc=878s");
    globalSession.setStatus(GlobalStatus.Begin);
    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\"}");
    sessionManager.addBranchSession(globalSession, branchSession);
    String sql = "select * from branch_table where xid= '" + xid + "'";
    String delSql = "delete from branch_table where xid= '" + xid + "'" + ";" + "delete from global_table where xid= '" + xid + "'";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        ResultSet rs = conn.createStatement().executeQuery(sql);
        if (rs.next()) {
            Assertions.assertTrue(true);
        } else {
            Assertions.assertTrue(false);
        }
        conn.createStatement().execute(delSql);
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.jupiter.api.Test)

Example 30 with BranchSession

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

the class SessionConverterTest method testConvertBranchSessionNull.

@Test
public void testConvertBranchSessionNull() {
    BranchTransactionDO branchTransactionDO = null;
    BranchSession branchSession = SessionConverter.convertBranchSession(branchTransactionDO);
    Assertions.assertNull(branchSession);
}
Also used : BranchSession(io.seata.server.session.BranchSession) BranchTransactionDO(io.seata.core.store.BranchTransactionDO) 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