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());
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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);
}
Aggregations