use of io.seata.server.session.GlobalSession 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());
}
}
use of io.seata.server.session.GlobalSession 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);
}
use of io.seata.server.session.GlobalSession 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);
}
use of io.seata.server.session.GlobalSession 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);
}
use of io.seata.server.session.GlobalSession 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);
}
Aggregations