package com.qiniu.android;

import com.qiniu.android.transaction.TransactionManager;
import com.qiniu.android.utils.LogUtil;

import java.util.Date;

/**
 * Created by yangsen on 2020/6/9
 */
public class TransactionManagerTest extends BaseTest {

    public void testTransaction(){

        TransactionManager.Transaction normal = new TransactionManager.Transaction("1", 0, new Runnable() {
            @Override
            public void run() {
                LogUtil.d("1");
            }
        });
        assertNotNull(normal);


        TransactionManager.Transaction time = new TransactionManager.Transaction("1", 0, 1, new Runnable() {
            @Override
            public void run() {
                LogUtil.d("2");
            }
        });
        assertNotNull(time);
    }

    public void testTransactionManagerAddAndRemove(){

        final boolean[] executedTransaction = {false};
        String normalName = "testNormalTransaction";
        TransactionManager.Transaction normal = new TransactionManager.Transaction(normalName, 0, new Runnable() {
            @Override
            public void run() {
                LogUtil.d("1: thread:" + Thread.currentThread().getId() + new Date().toString());
                executedTransaction[0] = true;
            }
        });

        String timeName = "testTimeTransaction";
        TransactionManager.Transaction time = new TransactionManager.Transaction(timeName, 3, 2, new Runnable() {
            @Override
            public void run() {
                LogUtil.d("2: thread:" + Thread.currentThread().getId() + new Date().toString());
            }
        });

        TransactionManager manager = TransactionManager.getInstance();
        manager.addTransaction(normal);
        manager.addTransaction(time);


        wait(new WaitConditional() {
            @Override
            public boolean shouldWait() {
                return executedTransaction[0];
            }
        }, 60);
        
        wait(null, 6);

        boolean exist = manager.existTransactionsForName(normalName);
        assertFalse(exist);
        exist = manager.existTransactionsForName(timeName);
        assertTrue(exist);
    }

}

@@ -119,16 +119,48 @@
 gUtil;%0A%0A
+import java.lang.reflect.Field;%0A
 import j
@@ -888,23 +888,19 @@
   final 
-boolean
+int
 %5B%5D execu
@@ -921,13 +921,9 @@
  = %7B
-false
+0
 %7D;%0A 
@@ -1284,41 +1284,384 @@
 %5B0%5D 
++
 = 
-true;%0A            %7D%0A        %7D);
+1;%0A            %7D%0A        %7D);%0A%0A        try %7B%0A            Field executedCountField = TransactionManager.Transaction.class.getField(%22executedCount%22);%0A            long executedCount = executedCountField.getLong(normal);%0A            assertEquals(%22Transaction executedCount was not 0%22, 0, executedCount);%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A%0A  
@@ -2297,16 +2297,20 @@
 ction%5B0%5D
+ %3E 0
 ;%0A      
@@ -2362,24 +2362,446 @@
 (null, 6);%0A%0A
+        try %7B%0A            Field executedCountField = TransactionManager.Transaction.class.getField(%22executedCount%22);%0A            long executedCount = executedCountField.getLong(normal);%0A            assertEquals(%22Transaction executedCount was not 1%22, 1, executedCount);%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        assertEquals(%22executed time was not 1%22, 1, executedTransaction%5B0%5D);%0A%0A
         bool

change transaction test case