package com.elab.redis.redisson.doc;

import com.elab.redis.RedissonApplication;
import com.elab.redis.config.CacheAutoConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RFuture;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 配置方法测试
 *
 * @author ： liukx
 * @time ： 2020/7/9 - 13:57
 */
@RunWith(SpringRunner.class)
@SpringBootTest(
        classes = {RedissonApplication.class, CacheAutoConfiguration.class}
)
public class ConfigurationTest {
    @Autowired
    private RedissonClient client;


    private void start() {
        new Thread(() -> {

        }).start();
    }

    /**
     * 同步和异步
     */
    @Test
    public void testApp() throws Exception {
        RAtomicLong longObject = client.getAtomicLong("myLong");
        // 同步执行方式
        boolean b = longObject.compareAndSet(3, 401);
        System.out.println(b);
        // 异步执行方式
        RFuture<Boolean> result = longObject.compareAndSetAsync(3, 401);
        result.thenAccept(res -> {
            // 处理返回
            System.out.println("=============成功==========" + res);
        }).exceptionally(exception -> {
            // 处理错误
            System.out.println("============异常===========");
            return null;
        });
    }

    @Test
    public void testMap() {
        RMap<Object, Object> liu = client.getMap("liu");
        boolean b = liu.fastPut("xxx", "aaa");
//        RMapCache<Object, Object> map = client.getMapCache("{xxx}_lkx");
//        boolean b = map.fastPut("{xxx}_xxx", "aaa", 30, TimeUnit.SECONDS);
        System.out.println(b);
    }
}
