package com.qiniu.android;

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

import java.lang.reflect.Field;
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 int[] executedTransaction = {0};
        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] += 1;
            }
        });

        try {
            Field executedCountField = TransactionManager.Transaction.class.getField("executedCount");
            long executedCount = executedCountField.getLong(normal);
            assertEquals("Transaction executedCount was not 0", 0, executedCount);
        } catch (Exception e) {
            e.printStackTrace();
        }

        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] == 0;
            }
        }, 60);
        
        wait(null, 6);

        try {
            Field executedCountField = TransactionManager.Transaction.class.getField("executedCount");
            long executedCount = executedCountField.getLong(normal);
            assertEquals("Transaction executedCount was not 1", 1, executedCount);
        } catch (Exception e) {
            e.printStackTrace();
        }

        assertEquals("executed time was not 1", 1, executedTransaction[0]);

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

}

@@ -1398,32 +1398,40 @@
 action.class.get
+Declared
 Field(%22executedC
@@ -1430,32 +1430,84 @@
 xecutedCount%22);%0A
+            executedCountField.setAccessible(true);%0A
             long
@@ -1551,32 +1551,110 @@
 etLong(normal);%0A
+            System.out.print(%22A Transaction executedCount:%22 + executedCount);%0A
             asse
@@ -1655,32 +1655,34 @@
   assertEquals(%22
+A 
 Transaction exec
@@ -1703,13 +1703,13 @@
 not 
-0%22, 0
+1%22, 1
 , ex
@@ -2600,16 +2600,24 @@
 lass.get
+Declared
 Field(%22e
@@ -2624,32 +2624,84 @@
 xecutedCount%22);%0A
+            executedCountField.setAccessible(true);%0A
             long
@@ -2745,32 +2745,110 @@
 etLong(normal);%0A
+            System.out.print(%22B Transaction executedCount:%22 + executedCount);%0A
             asse
@@ -2857,16 +2857,18 @@
 Equals(%22
+B 
 Transact
@@ -2984,32 +2984,101 @@
 e();%0A        %7D%0A%0A
+        System.out.print(%22executed time:%22 + executedTransaction%5B0%5D);%0A
         assertEq

change case