package com.qiniu.android.transaction;

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

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);

        long timestamp = Utils.currentSecondTimestamp();
        assertTrue("timestamp:: manager action count:" + manager.actionCount + " normal nextExecutionTime:" + normal.nextExecutionTime + " now:" + timestamp, normal.nextExecutionTime < timestamp);
        assertTrue("maybeCompleted:: manager action count:" + manager.actionCount + " normal nextExecutionTime:" + normal.nextExecutionTime + " now:" + timestamp, normal.maybeCompleted());
        assertEquals("executedCount:: manager action count:" + manager.actionCount + " normal.executedCount:" + normal.executedCount, 1, normal.executedCount);

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

}

@@ -2093,32 +2093,29 @@
     
-assertTrue(%22timestamp:: 
+String assertInfo = %22
 mana
@@ -2146,34 +2146,126 @@
 ager.actionCount
- +
+;%0A        assertInfo += %22 normal.executedCount:%22 + normal.executedCount;%0A        assertInfo +=
  %22 normal nextEx
@@ -2296,34 +2296,55 @@
 extExecutionTime
+;%0A        assertInfo
  +
+=
  %22 now:%22 + times
@@ -2351,46 +2351,72 @@
 tamp
-, normal.nextExecutionTime %3C timestamp
+;%0A        assertInfo += %22 should action:%22 + normal.shouldAction(
 );%0A 
@@ -2432,14 +2432,18 @@
 sert
-True(%22
+Info += %22 
 mayb
@@ -2457,66 +2457,93 @@
 ted:
-: manager action count:%22 + manager.actionCount + %22
+%22 + normal.maybeCompleted();%0A%0A        assertTrue(%22timestamp:: %22 + assertInfo,
  normal
- 
+.
 next
@@ -2559,219 +2559,154 @@
 Time
-:%22 + normal.nextExecutionTime + %22 now:%22 + timestamp, normal.maybeCompleted());%0A        assertEquals(%22executedCount:: manager action count:%22 + manager.actionCount + %22 normal.executedCount:%22 + normal.executedCount
+ %3C timestamp);%0A        assertTrue(%22maybeCompleted:: %22 + assertInfo, normal.maybeCompleted());%0A        assertEquals(%22executedCount:: %22 + assertInfo
 , 1,

optimize transaction manager