import com.elab.cache.ICacheClient;
import model.RedisModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import service.IService;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * redis操作测试
 *
 * @author Liukx
 * @create 2017-05-17 10:36
 * @email liukx@elab-plus.com
 **/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:cacheCloud/applicationContext-redis.xml")
public class RedisTest {

    private final String text = "redisSentinelBuilder";


    // redisStandaloneClient - 单机
    // redisSentineClient 主备
    // redisClusterClient 集群
    @Qualifier("redisSentineClient")
    @Autowired
    private ICacheClient redisClient;


    /**
     * jedis中处理list的方式，但是只能处理list泛型为String的方式，不太能满足具体业务
     */
    @Test
    public void redisListByString() {
        //http://192.168.0.24:9999/cache/client/redis/standalone/10024.json?clientVersion=1.0-SNAPSHOT
        //http://192.168.0.24:9999/cache/client/redis/sentinel/10026.json?clientVersion=1.0-SNAPSHOT
//        List<String> list = new ArrayList<String>();
//        list.add("aaa123");
//        list.add("bbb456");
//        list.add("ccc789");
//        String[] strings = new String[list.size()];
//        strings = list.toArray(strings);
//        redisClient.delete("redisListByString");
//        redisClient.lpush("redisListByString", strings);
//        List<String> resultList = redisClient.lrange("redisListByString");
//        System.out.println("redisListByString:" + resultList);

//        RedisModel redisModel = new RedisModel();
//        redisModel.setId(1);
//        redisModel.setName("某某某");
//        redisModel.setText("洗香香");
//        redisModel.setAge(15);
//
//        redisClient.set("test", redisModel);
//        RedisModel test = redisClient.get("test", RedisModel.class);
//        System.out.println("======================" + JSON.toJSONString(test));

        redisClient.set("test", 0);
        Long lkx1 = redisClient.incrby("test", 10);
        System.out.println("-------------2----------" + lkx1);
        Long lkx2 = redisClient.decrby("test", 5);
        System.out.println("-------------3----------" + lkx2);
        Long lkx3 = redisClient.ttl("lkx");
        System.out.println("-------------4----------" + lkx3);
        boolean lkx4 = redisClient.exsit("lkx");
        System.out.println("-------------5----------" + lkx4);
        boolean lkx5 = redisClient.setNX("lkx", "555");
        System.out.println("-------------6----------" + lkx5);
        String lkx = redisClient.get("lkx");
        System.out.println("--------------->>>" + lkx);
//        try {
//            TimeUnit.MILLISECONDS.sleep(70000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }


    /**
     * 自己封装的redis中存放List任意泛型的方式，存储用的是json，不是序列化
     */
    @Test
    public void redisListByObj() {
        List<RedisModel> list = new ArrayList();
        RedisModel model1 = new RedisModel();
        model1.setAge(11);
        model1.setName("name11");
        model1.setText("text11");
        list.add(model1);
        RedisModel model2 = new RedisModel();
        model2.setAge(12);
        model2.setName("name12");
        model2.setText("text12");
        list.add(model2);
        redisClient.delete("redisListByObj");
        redisClient.setList("redisListByObj", list);
        List<RedisModel> models = redisClient.getList("redisListByObj", RedisModel.class);
        System.out.println("redisListByObj:" + models.toString());
    }

    /**
     * 自己封装的redis中存放map任意泛型的方式，存储用的是json，不是序列化
     */
    @Test
    public void redisMapByString() {
        Map<String, String> map = new LinkedHashMap();
        map.put("haixiao1", "海啸小哥");
        map.put("haixiao2", "海啸大哥");
        redisClient.delete("redisMapByString");
        redisClient.set("redisMapByString", map);
        Map<String, String> strMap = redisClient.getMap("redisMapByString", String.class);
        System.out.println("redisMapByString:" + strMap);
    }

    /**
     * 自己封装的redis中存放map任意泛型的方式，存储用的是json，不是序列化
     */
    @Test
    public void redisMapByObj() {
        Map<String, RedisModel> map = new LinkedHashMap();
        RedisModel r1 = new RedisModel();
        r1.setAge(22);
        r1.setName("姓名22");
        r1.setText("text22");
        map.put("r1", r1);
        RedisModel r2 = new RedisModel();
        r2.setAge(23);
        r2.setName("姓名23");
        r2.setText("text23");
        map.put("r2", r2);
        redisClient.delete("redisMapByObj");
        redisClient.set("redisMapByObj", map);
        Map<String, RedisModel> mapResult = redisClient.getMap("redisMapByObj", RedisModel.class);
        System.out.println("mapRequest:" + mapResult);

    }

    /**
     * 自己封装的redis中存放任意泛型的方式，存储用的是json，不是序列化
     */
    @Test
    public void storageJavaBean() {
//        RedisModel model = new RedisModel();
//        model.setAge(28);
//        model.setName("海啸");
//        model.setText("你好，大世界");
//        model.setId(98);
//        redisClient.delete("storageJavaBean");
//        ////////////////////存储自定义对象方式///////////////
//        redisClient.set("storageJavaBean", model);
//        RedisModel redisModel = redisClient.get("storageJavaBean", RedisModel.class);
//        System.out.println(redisModel);
//        ////////////////////存储包装类对象方式-String///////////////
//        redisClient.set("haixiaoString", "沈杜公路沃尔");
//        String haixiaoString = redisClient.get("haixiaoString", String.class);
//        System.out.println("haixiaoString:"+haixiaoString);
//        ////////////////////存储包装类对象方式-Integer///////////////
//        redisClient.set("haixiaoInteger", 78909);
//        Integer haixiaoInteger = redisClient.get("haixiaoInteger", Integer.class);
//        System.out.println("haixiaoInteger:"+haixiaoInteger);
//        ////////////////////存储包装类对象方式-Double///////////////
//        redisClient.set("haixiaoDouble", 888.49);
//        Double haixiaoDouble = redisClient.get("haixiaoDouble", Double.class);
//        System.out.println("haixiaoDouble:"+haixiaoDouble);
//        ////////////////////存储包装类对象方式-Long///////////////
//        redisClient.set("haixiaoLong", 999);
//        Long haixiaoLong = redisClient.get("haixiaoLong", Long.class);
//        System.out.println("haixiaoLong:"+haixiaoLong);
//        ////////////////////存储包装类对象方式-Float///////////////
//        redisClient.set("haixiaoFloat", 525.52f);
//        Float haixiaoFloat = redisClient.get("haixiaoFloat", Float.class);
//        System.out.println("haixiaoFloat:"+haixiaoFloat);
//
//
//        Map<String, String> map = new LinkedHashMap();
//        map.put("haixiao1", "海啸小哥");
//        map.put("haixiao2", "海啸大哥");
//        redisClient.set("haixiaoMap", map);
//        Map haixiaoMap = redisClient.get("haixiaoMap", Map.class);
//
        Map<String, String> map1 = new LinkedHashMap();
        map1.put("haixiao1", "海啸小哥");
        map1.put("haixiao2", "海啸大哥");
//        for (int i = 0; i < 10; i++) {
//            System.out.println(redisClient.toString());
        redisClient.set("jjj", map1);
//        }

        Map aaaa = redisClient.get("jjj", Map.class);
        System.out.println("-->" + aaaa.toString());
    }

    @Test
    public void testRedisError() throws InterruptedException {
        for (int i = 0; i < 20; i++) {
            redisClient.set("aaa", "asdasd");
            System.out.println("添加成功~~~");
            String aaa = redisClient.get("aaa");
            System.out.println("aaaaa==" + aaa);
            Thread.sleep(5000);
        }

    }

    @Autowired
    private IService service;

    /**
     * 缓存注解测试
     */
    @Test
    public void testCache() {
        RedisModel model = new RedisModel();
        model.setAge(5655);
        model.setId(2);
        model.setText("aaaaa");
//        Info test2 = service.test(model);
//        Info test = service.test(model);
//        System.out.println("111---->" + JSON.toJSONString(test));
//        RedisModel redisModel = service.testParams(model);
//        RedisModel redisModel1 = service.testParams(model);
        List<String> strings = service.testList();
        List<String> strings1 = service.testList();

//        System.out.println("222---->" + JSON.toJSONString(redisModel1));
//        System.out.println("----->" + JSON.toJSONString(redisModel1));
//        service.updateCache(model);
//        service.updateCache(model);

//        service.CacheEvict()
//        service.CacheEvict(model);
//        service.CacheEvict(model);

    }

    /**
     * 连接关闭测试
     *
     * @throws InterruptedException
     */
    @Test
    public void connectClose() throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            Thread.sleep(1000);
            String lkx = redisClient.get("lkx");
            System.out.println("==========" + lkx);
        }
    }


    /**
     * 并发测试
     *
     * @throws InterruptedException
     */
    @Test
    public void testThread() throws Exception {
        final AtomicInteger count = new AtomicInteger();
        for (int i = 0; i < 300; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
//                    redisListByString();
                    int andIncrement = count.getAndIncrement();
                    System.out.println("=============>" + andIncrement);
                    redisClient.set("lkx", "tttttt");
                    System.out.println("设置完成" + andIncrement);
                }
            });
            thread.start();
        }
        System.out.println("初始化线程OK");
        System.in.read();
    }

}
