package com.elab.core.async.pruducer;

import com.elab.core.async.consumer.TaskConsumer;
import com.elab.core.async.store.TaskExecutorQueue;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class TaskProducerTest {
    private Logger logger = LoggerFactory.getLogger(getClass());

    private AbstractTaskProducer producer;

    @Before
    public void init() {
        TaskExecutorQueue queue = new TaskExecutorQueue();
        TaskConsumer taskConsumer = new TaskConsumer(queue);
        taskConsumer.startRealTimeConsumer();
        taskConsumer.startSchedulingConsumer(5);
        producer = new AbstractTaskProducer(queue);
    }

    @Test
    public void addRealTimeQueue() throws IOException {
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            System.out.println("==业务执行开始==" + i);
            producer.sendRealTimeQueue(() -> {
                System.out.println("-------操作异步开始----------" + finalI);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("-----异步操作结束-----------" + finalI);
            });
            System.out.println("==业务执行完成==" + i);
        }
        System.in.read();
    }

    @Test
    public void addSchedulingList() throws IOException {
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            System.out.println("==业务执行开始==" + i);
            producer.sendSchedulingList(() -> {
                logger.info("-------操作异步开始----------" + finalI);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                logger.info("-----异步操作结束-----------" + finalI);
            });
            System.out.println("==业务执行完成==" + i);
        }
        System.in.read();
    }
}
