github.com/gogf/gf/v2@v2.7.4/os/gsession/gsession_z_unit_storage_file_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gsession_test
     8  
     9  import (
    10  	"context"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/gogf/gf/v2/frame/g"
    15  	"github.com/gogf/gf/v2/os/gsession"
    16  	"github.com/gogf/gf/v2/test/gtest"
    17  )
    18  
    19  func Test_StorageFile(t *testing.T) {
    20  	storage := gsession.NewStorageFile("", time.Second)
    21  	manager := gsession.New(time.Second, storage)
    22  	sessionId := ""
    23  	gtest.C(t, func(t *gtest.T) {
    24  		s := manager.New(context.TODO())
    25  		defer s.Close()
    26  		s.Set("k1", "v1")
    27  		s.Set("k2", "v2")
    28  		s.MustSet("k3", "v3")
    29  		s.MustSet("k4", "v4")
    30  		s.SetMap(g.Map{
    31  			"kmap1": "kval1",
    32  			"kmap2": "kval2",
    33  		})
    34  		s.MustSetMap(g.Map{
    35  			"kmap3": "kval3",
    36  			"kmap4": "kval4",
    37  		})
    38  		t.Assert(s.IsDirty(), true)
    39  		sessionId = s.MustId()
    40  	})
    41  
    42  	time.Sleep(500 * time.Millisecond)
    43  	gtest.C(t, func(t *gtest.T) {
    44  		s := manager.New(context.TODO(), sessionId)
    45  		t.Assert(s.MustGet("k1"), "v1")
    46  		t.Assert(s.MustGet("k2"), "v2")
    47  		t.Assert(s.MustGet("k3"), "v3")
    48  		t.Assert(s.MustGet("k4"), "v4")
    49  		t.Assert(len(s.MustData()), 8)
    50  		t.Assert(s.MustData()["k1"], "v1")
    51  		t.Assert(s.MustData()["k4"], "v4")
    52  		t.Assert(s.MustId(), sessionId)
    53  		t.Assert(s.MustSize(), 8)
    54  		t.Assert(s.MustContains("k1"), true)
    55  		t.Assert(s.MustContains("k3"), true)
    56  		t.Assert(s.MustContains("k5"), false)
    57  		s.Remove("k4")
    58  		s.MustRemove("k4")
    59  		t.Assert(s.MustSize(), 7)
    60  		t.Assert(s.MustContains("k3"), true)
    61  		t.Assert(s.MustContains("k4"), false)
    62  		s.RemoveAll()
    63  		t.Assert(s.MustSize(), 0)
    64  		t.Assert(s.MustContains("k1"), false)
    65  		t.Assert(s.MustContains("k2"), false)
    66  		s.SetMap(g.Map{
    67  			"k5": "v5",
    68  			"k6": "v6",
    69  		})
    70  		t.Assert(s.MustSize(), 2)
    71  		t.Assert(s.MustContains("k5"), true)
    72  		t.Assert(s.MustContains("k6"), true)
    73  	})
    74  
    75  	time.Sleep(1000 * time.Millisecond)
    76  	gtest.C(t, func(t *gtest.T) {
    77  		s := manager.New(context.TODO(), sessionId)
    78  		t.Assert(s.MustSize(), 0)
    79  		t.Assert(s.MustGet("k5"), nil)
    80  		t.Assert(s.MustGet("k6"), nil)
    81  	})
    82  }