package com.qiniu.android.transaction;

import com.qiniu.android.BaseTest;
import com.qiniu.android.utils.LogUtil;
import com.qiniu.android.utils.Utils;

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.getDeclaredField("executedCount");
            executedCountField.setAccessible(true);
            long executedCount = executedCountField.getLong(normal);
            System.out.print("A Transaction executedCount:" + executedCount);
            assertEquals("A Transaction executedCount was not 1", 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();

        int transactionCountBefore = manager.transactionList.size();
        long actionCountBefore = manager.actionCount;
        manager.addTransaction(normal);
        manager.addTransaction(time);


        // 由于事务堆积，执行可能会延后
        wait(new WaitConditional() {
            @Override
            public boolean shouldWait() {
<<<<<<< HEAD:library/src/androidTest/java/com/qiniu/android/TransactionManagerTest.java
                return executedTransaction[0] == 0;
=======
                return !executedTransaction[0];
>>>>>>> 22b602c17a9ac582f13c5f16f63b7d39a7200f22:library/src/androidTest/java/com/qiniu/android/transaction/TransactionManagerTest.java
            }
        }, 5 * 60);
        
        wait(null, 6);

<<<<<<< HEAD:library/src/androidTest/java/com/qiniu/android/TransactionManagerTest.java
        try {
            Field executedCountField = TransactionManager.Transaction.class.getDeclaredField("executedCount");
            executedCountField.setAccessible(true);
            long executedCount = executedCountField.getLong(normal);
            System.out.print("B Transaction executedCount:" + executedCount);
            assertEquals("B Transaction executedCount was not 1", 1, executedCount);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.print("executed time:" + executedTransaction[0]);
        assertEquals("executed time was not 1", 1, executedTransaction[0]);
=======
        String leftTransactionName = "";
        for (TransactionManager.Transaction t : manager.transactionList) {
            leftTransactionName += " " + t.name + " ";
        }
        int transactionCountAfter = manager.transactionList.size();

        long timestamp = Utils.currentSecondTimestamp();
        String assertInfo = "manager action count before:" + actionCountBefore;
        assertInfo += " manager action count after:" + manager.actionCount;
        assertInfo += " manager transaction count before:" + transactionCountBefore;
        assertInfo += " manager transaction count after:" + transactionCountAfter;
        assertInfo += " manager left transaction:" + leftTransactionName;
        assertInfo += " normal.executedCount:" + normal.executedCount;
        assertInfo += " normal nextExecutionTime:" + normal.nextExecutionTime;
        assertInfo += " now:" + timestamp;
        assertInfo += " should action:" + normal.shouldAction();
        assertInfo += " maybeCompleted:" + normal.maybeCompleted();

        assertTrue("timestamp:: " + assertInfo, normal.nextExecutionTime < timestamp);
        assertTrue("maybeCompleted:: " + assertInfo, normal.maybeCompleted());
        assertEquals("executedCount:: " + assertInfo, 1, normal.executedCount);
>>>>>>> 22b602c17a9ac582f13c5f16f63b7d39a7200f22:library/src/androidTest/java/com/qiniu/android/transaction/TransactionManagerTest.java

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

}

@@ -2574,1132 +2574,126 @@
 ) %7B%0A
-%3C%3C%3C%3C%3C%3C%3C HEAD:library/src/androidTest/java/com/qiniu/android/TransactionManagerTest.java%0A                return executedTransaction%5B0%5D == 0;%0A=======%0A                return !executedTransaction%5B0%5D;%0A%3E%3E%3E%3E%3E%3E%3E 22b602c17a9ac582f13c5f16f63b7d39a7200f22:library/src/androidTest/java/com/qiniu/android/transaction/TransactionManagerTest.java%0A            %7D%0A        %7D, 5 * 60);%0A        %0A        wait(null, 6);%0A%0A%3C%3C%3C%3C%3C%3C%3C HEAD:library/src/androidTest/java/com/qiniu/android/TransactionManagerTest.java%0A        try %7B%0A            Field executedCountField = TransactionManager.Transaction.class.getDeclaredField(%22executedCount%22);%0A            executedCountField.setAccessible(true);%0A            long executedCount = executedCountField.getLong(normal);%0A            System.out.print(%22B Transaction executedCount:%22 + executedCount);%0A            assertEquals(%22B Transaction executedCount was not 1%22, 1, executedCount);%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        System.out.print(%22executed time:%22 + executedTransaction%5B0%5D);%0A        assertEquals(%22executed time was not 1%22, 1, executedTransaction%5B0%5D);%0A=======
+                return executedTransaction%5B0%5D == 0;%0A            %7D%0A        %7D, 5 * 60);%0A        %0A        wait(null, 6);%0A
 %0A   
@@ -3970,144 +3970,8 @@
 nt);
-%0A%3E%3E%3E%3E%3E%3E%3E 22b602c17a9ac582f13c5f16f63b7d39a7200f22:library/src/androidTest/java/com/qiniu/android/transaction/TransactionManagerTest.java
 %0A%0A  

change case