Search in sources :

Example 1 with FileSessionManager

use of io.seata.server.storage.file.session.FileSessionManager 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 FileSessionManager

use of io.seata.server.storage.file.session.FileSessionManager in project seata by seata.

the class GlobalSessionTest method globalSessionProvider.

/**
 * Global session provider object [ ] [ ].
 *
 * @return the object [ ] [ ]
 */
static Stream<Arguments> globalSessionProvider() throws IOException {
    GlobalSession globalSession = new GlobalSession("demo-app", "my_test_tx_group", "test", 6000);
    globalSession.setActive(true);
    globalSession.addSessionLifecycleListener(new FileSessionManager("default", null));
    return Stream.of(Arguments.of(globalSession));
}
Also used : FileSessionManager(io.seata.server.storage.file.session.FileSessionManager)

Aggregations

FileSessionManager (io.seata.server.storage.file.session.FileSessionManager)2 BranchSession (io.seata.server.session.BranchSession)1 GlobalSession (io.seata.server.session.GlobalSession)1 SessionManager (io.seata.server.session.SessionManager)1 FileTransactionStoreManager (io.seata.server.storage.file.store.FileTransactionStoreManager)1 File (java.io.File)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1