2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-03-12 23:26:10 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
2020-03-16 22:47:39 +08:00
|
|
|
package gins_test
|
2019-03-12 23:26:10 +08:00
|
|
|
|
|
|
|
import (
|
2020-03-16 22:47:39 +08:00
|
|
|
"github.com/gogf/gf/debug/gdebug"
|
|
|
|
"github.com/gogf/gf/frame/gins"
|
2021-09-19 10:01:09 +08:00
|
|
|
"github.com/gogf/gf/os/gcfg"
|
2020-03-16 22:47:39 +08:00
|
|
|
"github.com/gogf/gf/os/gtime"
|
2019-06-19 09:06:52 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/os/gfile"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
2019-03-12 23:26:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Redis(t *testing.T) {
|
2020-03-20 08:56:17 +08:00
|
|
|
redisContent := gfile.GetContents(
|
2020-03-21 19:41:05 +08:00
|
|
|
gdebug.TestDataPath("redis", "config.toml"),
|
2020-03-20 08:56:17 +08:00
|
|
|
)
|
2020-03-16 22:47:39 +08:00
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var err error
|
2020-03-28 00:41:12 +08:00
|
|
|
dirPath := gfile.TempDir(gtime.TimestampNanoStr())
|
2020-03-19 23:53:03 +08:00
|
|
|
err = gfile.Mkdir(dirPath)
|
|
|
|
t.Assert(err, nil)
|
|
|
|
defer gfile.Remove(dirPath)
|
2020-03-16 22:47:39 +08:00
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
name := "config.toml"
|
|
|
|
err = gfile.PutContents(gfile.Join(dirPath, name), redisContent)
|
|
|
|
t.Assert(err, nil)
|
2020-03-16 22:47:39 +08:00
|
|
|
|
2021-09-19 10:01:09 +08:00
|
|
|
err = gins.Config().GetAdapter().(*gcfg.AdapterFile).AddPath(dirPath)
|
2020-03-19 23:53:03 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-16 22:47:39 +08:00
|
|
|
|
2021-09-19 10:01:09 +08:00
|
|
|
defer gins.Config().GetAdapter().(*gcfg.AdapterFile).Clear()
|
2019-03-12 23:26:10 +08:00
|
|
|
|
2020-03-20 08:56:17 +08:00
|
|
|
// for gfsnotify callbacks to refresh cache of config file
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2019-03-14 23:28:56 +08:00
|
|
|
|
2019-11-28 23:19:37 +08:00
|
|
|
//fmt.Println("gins Test_Redis", Config().Get("test"))
|
2019-03-14 00:23:46 +08:00
|
|
|
|
2020-03-16 22:47:39 +08:00
|
|
|
redisDefault := gins.Redis()
|
|
|
|
redisCache := gins.Redis("cache")
|
|
|
|
redisDisk := gins.Redis("disk")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(redisDefault, nil)
|
|
|
|
t.AssertNE(redisCache, nil)
|
|
|
|
t.AssertNE(redisDisk, nil)
|
2019-03-12 23:26:10 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
r, err := redisDefault.Do("PING")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, "PONG")
|
2019-03-12 23:26:10 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
r, err = redisCache.Do("PING")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, "PONG")
|
2019-03-21 00:14:23 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
_, err = redisDisk.Do("SET", "k", "v")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-06-19 09:06:52 +08:00
|
|
|
r, err = redisDisk.Do("GET", "k")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, []byte("v"))
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-03-12 23:26:10 +08:00
|
|
|
}
|