package com.sjyt.icom.asst.service;

import com.sjyt.icom.asst.context.pools.One2OneRoomPool;
import com.sjyt.icom.asst.common.model.room.OneToOneRoom;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>TODO</p>
 *
 * @author renyanru
 * @version V1.0.0
 * @date 2022/6/16 12:45
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class TestOne2OnePools {
    @Autowired
    private One2OneRoomPool one2OneRoomPool;

    @Test
     public void testOne2OneRoomPool1(){
        int size = 20;
        List<OneToOneRoom> list=new ArrayList<>(Math.max(1,size));
        try {
            OneToOneRoom oneToOneRoom;
        for (int i = 0; i < size; i++) {
            if (one2OneRoomPool.getNumActive()>=one2OneRoomPool.getMaxTotal()){
                oneToOneRoom=list.get(0);
                one2OneRoomPool.returnObject(oneToOneRoom);
                list.remove(0);
            }
                 oneToOneRoom = one2OneRoomPool.borrowObject();
            log.info("一对一房间对象池:{}，房间id：{}，下标：{}，list{}，活跃：{}，空闲：{}",oneToOneRoom,oneToOneRoom.getRid(),i,list.size(),one2OneRoomPool.getNumActive(),one2OneRoomPool.getNumIdle());
            list.add(oneToOneRoom);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void testOne2OneRoomPool2() throws Exception {
        OneToOneRoom oneToOneRoom1 = one2OneRoomPool.borrowObject();
        OneToOneRoom oneToOneRoom2 = one2OneRoomPool.borrowObject();
        System.out.println(oneToOneRoom1.equals(oneToOneRoom2));
        log.info("可用对象数:{}",one2OneRoomPool.getNumActive());
        one2OneRoomPool.returnObject(oneToOneRoom1);
        log.info("可用对象数:{}",one2OneRoomPool.getNumActive());
        one2OneRoomPool.returnObject(oneToOneRoom2);
        log.info("可用对象数:{}",one2OneRoomPool.getNumActive());
    }
}
