package com.elab.spring.utils;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Transaction;
import com.elab.spring.config.ThreadConfiguration;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

/**
 * @Module TODO
 * @Description TODO
 * @Author liukaixiong
 * @Date 2020/11/18 16:16
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ThreadConfiguration.class)
public class ThreadProcessUtilsTest extends TestCase {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Test
    public void testAddRealTimeQueue() throws Exception {
        Transaction transaction = Cat.newTransaction("test", "msg");
        logger.info("这时候开始处理 : " + Cat.getCurrentMessageId());
        System.out.println("LOG : " + Cat.getCurrentMessageId());
        ThreadProcessUtils.addRealTimeQueue(() -> {
            logger.info("这是一个异步消息" + Cat.getCurrentMessageId());
        }, null);
        logger.info("处理完成了");
        transaction.setSuccessStatus();
        transaction.complete();
        System.in.read();
    }

    @Test
    public void testAddSchedulingList() throws Exception {
        logger.info("sadfasdfsdfsad:::" + Cat.getCurrentMessageId());
        ThreadProcessUtils.addSchedulingList(() -> {
            System.out.println("这是一个调度操作" + Cat.getCurrentMessageId());
        });
        System.in.read();
    }

    @Test
    public void testSupplyAsync() throws Exception {
        Transaction transaction = Cat.newTransaction("test", "msg");
        CompletableFuture<String> stringCompletableFuture = ThreadProcessUtils.supplyAsync(() -> {
            logger.info("哈哈哈之前:" + Cat.getCurrentMessageId());
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
            }
            logger.info("哈哈之后..." + Cat.getCurrentMessageId());
            return "ABC";
        });
        String s = stringCompletableFuture.get();
        logger.info("结果:" + s);
        transaction.setSuccessStatus();
        transaction.complete();
        System.in.read();
    }

    @Test
    public void testBatchSupplyAsync() throws Exception {
        Transaction transaction = Cat.newTransaction("test", "msg");
        CompletableFuture<String>[] completableFutures = ThreadProcessUtils.supplyAsync(() -> {
            logger.info("任务1 - 开始" + Cat.getCurrentMessageId());
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
            }
            logger.info("任务1 - 结束" + Cat.getCurrentMessageId());
            return "ABC1";
        }, () -> {
            logger.info("任务2 - 开始:" + Cat.getCurrentMessageId());
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
            }
            logger.info("任务2 - 结束" + Cat.getCurrentMessageId());
            return "ABC2";
        });

        for (int i = 0; i < completableFutures.length; i++) {
            String s = completableFutures[i].get();
            logger.info("结果:" + s);
        }

        transaction.setSuccessStatus();
        transaction.complete();
        System.in.read();
    }

    @Test
    public void testSupplyAsyncMap() throws Exception {
        Transaction transaction = Cat.newTransaction("test", "msg");


        Map<String, Supplier<String>> taskMap = new LinkedHashMap<>();
        taskMap.put("A", () -> {
            logger.info("A 开始" + Cat.getCurrentMessageId());
            sleep(1000);
            logger.info("A 结束" + Cat.getCurrentMessageId());
            return "A-result";
        });
        taskMap.put("B", () -> {
            logger.info("B 开始" + Cat.getCurrentMessageId());
            sleep(1000);
            logger.info("B 结束" + Cat.getCurrentMessageId());
            return "B-result";
        });
        taskMap.put("C", () -> {
            logger.info("C 开始" + Cat.getCurrentMessageId());
            sleep(1000);
            logger.info("C 结束" + Cat.getCurrentMessageId());
            return "C-result";
        });

        Map<String, CompletableFuture<String>> completableFutureMap = ThreadProcessUtils.supplyAsync(taskMap);
        String a = completableFutureMap.get("A").get();
        String b = completableFutureMap.get("B").get();
        String c = completableFutureMap.get("C").get();

        logger.info("获取对应的结果:" + a);
        logger.info("获取对应的结果:" + b);
        logger.info("获取对应的结果:" + c);

        transaction.setSuccessStatus();
        transaction.complete();
        System.in.read();
    }

    private void sleep(int ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}