1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 11:18:50 +08:00
gf/os/gsession/gsession_z_unit_storage_redis_test.go
2022-03-10 11:36:40 +08:00

148 lines
3.7 KiB
Go

// 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 gsession_test
import (
"context"
"testing"
"time"
"github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gsession"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_StorageRedis(t *testing.T) {
redis, err := gredis.New(&gredis.Config{
Address: "127.0.0.1:6379",
Db: 0,
})
gtest.AssertNil(err)
storage := gsession.NewStorageRedis(redis)
manager := gsession.New(time.Second, storage)
sessionId := ""
gtest.C(t, func(t *gtest.T) {
s := manager.New(context.TODO())
defer s.Close()
s.Set("k1", "v1")
s.Set("k2", "v2")
s.SetMap(g.Map{
"k3": "v3",
"k4": "v4",
})
t.Assert(s.IsDirty(), true)
sessionId = s.MustId()
})
time.Sleep(500 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
s := manager.New(context.TODO(), sessionId)
t.Assert(s.MustGet("k1"), "v1")
t.Assert(s.MustGet("k2"), "v2")
t.Assert(s.MustGet("k3"), "v3")
t.Assert(s.MustGet("k4"), "v4")
t.Assert(len(s.MustData()), 4)
t.Assert(s.MustData()["k1"], "v1")
t.Assert(s.MustData()["k4"], "v4")
t.Assert(s.MustId(), sessionId)
t.Assert(s.MustSize(), 4)
t.Assert(s.MustContains("k1"), true)
t.Assert(s.MustContains("k3"), true)
t.Assert(s.MustContains("k5"), false)
s.Remove("k4")
t.Assert(s.MustSize(), 3)
t.Assert(s.MustContains("k3"), true)
t.Assert(s.MustContains("k4"), false)
s.RemoveAll()
t.Assert(s.MustSize(), 0)
t.Assert(s.MustContains("k1"), false)
t.Assert(s.MustContains("k2"), false)
s.SetMap(g.Map{
"k5": "v5",
"k6": "v6",
})
t.Assert(s.MustSize(), 2)
t.Assert(s.MustContains("k5"), true)
t.Assert(s.MustContains("k6"), true)
})
time.Sleep(1000 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
s := manager.New(context.TODO(), sessionId)
t.Assert(s.MustSize(), 0)
t.Assert(s.MustGet("k5"), nil)
t.Assert(s.MustGet("k6"), nil)
})
}
func Test_StorageRedisPrefix(t *testing.T) {
redis, err := gredis.New(&gredis.Config{
Address: "127.0.0.1:6379",
Db: 0,
})
gtest.AssertNil(err)
prefix := "s_"
storage := gsession.NewStorageRedis(redis, prefix)
manager := gsession.New(time.Second, storage)
sessionId := ""
gtest.C(t, func(t *gtest.T) {
s := manager.New(context.TODO())
defer s.Close()
s.Set("k1", "v1")
s.Set("k2", "v2")
s.SetMap(g.Map{
"k3": "v3",
"k4": "v4",
})
t.Assert(s.IsDirty(), true)
sessionId = s.MustId()
})
time.Sleep(500 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
s := manager.New(context.TODO(), sessionId)
t.Assert(s.MustGet("k1"), "v1")
t.Assert(s.MustGet("k2"), "v2")
t.Assert(s.MustGet("k3"), "v3")
t.Assert(s.MustGet("k4"), "v4")
t.Assert(len(s.MustData()), 4)
t.Assert(s.MustData()["k1"], "v1")
t.Assert(s.MustData()["k4"], "v4")
t.Assert(s.MustId(), sessionId)
t.Assert(s.MustSize(), 4)
t.Assert(s.MustContains("k1"), true)
t.Assert(s.MustContains("k3"), true)
t.Assert(s.MustContains("k5"), false)
s.Remove("k4")
t.Assert(s.MustSize(), 3)
t.Assert(s.MustContains("k3"), true)
t.Assert(s.MustContains("k4"), false)
s.RemoveAll()
t.Assert(s.MustSize(), 0)
t.Assert(s.MustContains("k1"), false)
t.Assert(s.MustContains("k2"), false)
s.SetMap(g.Map{
"k5": "v5",
"k6": "v6",
})
t.Assert(s.MustSize(), 2)
t.Assert(s.MustContains("k5"), true)
t.Assert(s.MustContains("k6"), true)
})
time.Sleep(1000 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
s := manager.New(context.TODO(), sessionId)
t.Assert(s.MustSize(), 0)
t.Assert(s.MustGet("k5"), nil)
t.Assert(s.MustGet("k6"), nil)
})
}