gf/database/gredis/gredis_z_unit_conn_test.go

69 lines
1.5 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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.
package gredis_test
import (
2021-09-23 19:12:02 +08:00
"context"
2021-11-13 23:23:55 +08:00
"testing"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/test/gtest"
)
2021-09-23 19:12:02 +08:00
var (
ctx = context.TODO()
)
func TestConn_DoWithTimeout(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
2021-09-23 19:12:02 +08:00
redis, err := gredis.New(config)
t.AssertNil(err)
2021-09-23 21:23:22 +08:00
t.AssertNE(redis, nil)
defer redis.Close(ctx)
2021-09-23 19:12:02 +08:00
conn, err := redis.Conn(ctx)
t.AssertNil(err)
defer conn.Close(ctx)
2021-09-26 23:22:32 +08:00
_, err = conn.Do(ctx, "set", "test", "123")
t.Assert(err, nil)
2021-09-23 21:23:22 +08:00
defer conn.Do(ctx, "del", "test")
2021-09-26 23:22:32 +08:00
r, err := conn.Do(ctx, "get", "test")
t.Assert(err, nil)
2021-09-23 21:23:22 +08:00
t.Assert(r.String(), "123")
})
}
func TestConn_ReceiveVarWithTimeout(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
2021-09-23 21:23:22 +08:00
redis, err := gredis.New(config)
t.AssertNil(err)
t.AssertNE(redis, nil)
2021-09-23 21:23:22 +08:00
defer redis.Close(ctx)
2021-09-23 21:23:22 +08:00
conn, err := redis.Conn(ctx)
t.AssertNil(err)
defer conn.Close(ctx)
2021-09-26 23:22:32 +08:00
_, err = conn.Do(ctx, "Subscribe", "gf")
2021-09-23 21:23:22 +08:00
t.AssertNil(err)
2021-09-26 23:22:32 +08:00
v, err := redis.Do(ctx, "PUBLISH", "gf", "test")
v, err = conn.Receive(ctx)
t.AssertNil(err)
t.Assert(v.Val().(*gredis.Subscription).Channel, "gf")
v, err = conn.Receive(ctx)
t.AssertNil(err)
t.Assert(v.Val().(*gredis.Message).Channel, "gf")
t.Assert(v.Val().(*gredis.Message).Payload, "test")
})
}