package rds

import (
	"os"
	"testing"
	"time"

	"github.com/go-redis/redis"
)

var (
	rdb *redis.Client
)

func setup() {
	rdb = redis.NewClient(&redis.Options{
		Addr:         "192.168.0.198:6379",
		Password:     "",
		DB:           2,
		DialTimeout:  3 * time.Second,
		ReadTimeout:  3 * time.Second,
		WriteTimeout: 5 * time.Second,
		PoolSize:     10,
		PoolTimeout:  60 * time.Second,
	})
}

func teardown() {
	rdb.Close()
}

func TestPoolSize(t *testing.T) {
	res, err := rdb.Set("ddd", "ddd", time.Second*1).Result()
	if err != nil {
		t.Errorf("failed:%v", err)
	}
	t.Logf("%v", res)
	res, err = rdb.HMSet("dddd", map[string]interface{}{
		"d1":    123,
		"value": "good",
	}).Result()
	if err != nil {
		t.Errorf("failed:%v", err)
	}
	t.Logf("%v", res)
	s, err := rdb.HMGet("dddd", "d1", "value").Result()
	if err != nil {
		t.Errorf("failed:%v", err)
	}
	_ = rdb.HDel("dddd")

	d1 := s[0].(string)
	value := s[1].(string)
	t.Logf("%v,%v,%v", s, d1, value)
	t.Log(rdb.PoolStats().TotalConns)

	r, _ := rdb.HMGet("user:168004", "account", "bankPass").Result()
	t.Logf("%v\n", r)

	rr, _ := rdb.Get("conf:clientaddrs").Result()
	t.Logf("%v\n", rr)
}

func TestHMGet(t *testing.T) {
	rs := rdb.HMGet("a", []string{"bb", "cc"}...)
	err := rs.Err()
	if err != nil {
		t.Log(err)
		return
	}
}

func TestHGet(t *testing.T) {
	rs, err := rdb.HGet("rt:vip:bonus", "168014").Result()
	if err != nil {
		if err == redis.Nil {
			t.Log("0")
			return
		}
		t.Error(err)
	}
	t.Log(rs)
}

func TestRedisLua(t*testing.T){

}

func TestMain(m *testing.M) {
	setup()
	code := m.Run()
	teardown()
	os.Exit(code)
}
