2019-11-05 17:33:06 +08:00
|
|
|
// Copyright 2019 gf Author(https://github.com/gogf/gf). 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 (
|
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
|
"github.com/gogf/gf/os/gsession"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_StorageMemory(t *testing.T) {
|
|
|
|
storage := gsession.NewStorageMemory()
|
|
|
|
manager := gsession.New(time.Second, storage)
|
|
|
|
sessionId := ""
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-11-05 17:33:06 +08:00
|
|
|
s := manager.New()
|
|
|
|
defer s.Close()
|
|
|
|
s.Set("k1", "v1")
|
|
|
|
s.Set("k2", "v2")
|
|
|
|
s.Sets(g.Map{
|
|
|
|
"k3": "v3",
|
|
|
|
"k4": "v4",
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(s.IsDirty(), true)
|
2019-11-05 17:33:06 +08:00
|
|
|
sessionId = s.Id()
|
|
|
|
})
|
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-11-05 17:33:06 +08:00
|
|
|
s := manager.New(sessionId)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(s.Get("k1"), "v1")
|
|
|
|
t.Assert(s.Get("k2"), "v2")
|
|
|
|
t.Assert(s.Get("k3"), "v3")
|
|
|
|
t.Assert(s.Get("k4"), "v4")
|
|
|
|
t.Assert(len(s.Map()), 4)
|
|
|
|
t.Assert(s.Map()["k1"], "v1")
|
|
|
|
t.Assert(s.Map()["k4"], "v4")
|
|
|
|
t.Assert(s.Id(), sessionId)
|
|
|
|
t.Assert(s.Size(), 4)
|
|
|
|
t.Assert(s.Contains("k1"), true)
|
|
|
|
t.Assert(s.Contains("k3"), true)
|
|
|
|
t.Assert(s.Contains("k5"), false)
|
2019-11-05 17:33:06 +08:00
|
|
|
s.Remove("k4")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(s.Size(), 3)
|
|
|
|
t.Assert(s.Contains("k3"), true)
|
|
|
|
t.Assert(s.Contains("k4"), false)
|
2019-11-05 17:33:06 +08:00
|
|
|
s.RemoveAll()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(s.Size(), 0)
|
|
|
|
t.Assert(s.Contains("k1"), false)
|
|
|
|
t.Assert(s.Contains("k2"), false)
|
2019-11-05 17:33:06 +08:00
|
|
|
s.Sets(g.Map{
|
|
|
|
"k5": "v5",
|
|
|
|
"k6": "v6",
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(s.Size(), 2)
|
|
|
|
t.Assert(s.Contains("k5"), true)
|
|
|
|
t.Assert(s.Contains("k6"), true)
|
2019-11-05 17:33:06 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-11-05 17:33:06 +08:00
|
|
|
s := manager.New(sessionId)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(s.Size(), 0)
|
|
|
|
t.Assert(s.Get("k5"), nil)
|
|
|
|
t.Assert(s.Get("k6"), nil)
|
2019-11-05 17:33:06 +08:00
|
|
|
})
|
|
|
|
}
|