gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/myx/cachex/expired_test.go (about)

     1  package cachex
     2  
     3  import (
     4  	. "github.com/smartystreets/goconvey/convey"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestCacheSync_Expire(t *testing.T) {
    10  	Convey("输入了正确的参数,成功返回", t, func() {
    11  		cache := NewCacheSyncNoExpire()
    12  		cache.Set("testkey-1", 0)
    13  		cache.Set("testkey-2", 0)
    14  		cache.Set("testkey-3", 0)
    15  		cache.Expire("testkey-1", time.Millisecond*100, nil)
    16  		cache.Expire("testkey-2", time.Millisecond*500, nil)
    17  		cache.Expire("testkey-3", time.Millisecond*300, nil)
    18  
    19  		So(len(cache.expiredSet), ShouldEqual, 3)
    20  		So(cache.expiredRoot.key == "testkey-1", ShouldBeTrue)
    21  
    22  		So(cache.expiredSet["testkey-1"].prev == nil, ShouldBeTrue)
    23  		So(cache.expiredSet["testkey-1"].next != nil && cache.expiredSet["testkey-1"].next.key == "testkey-3", ShouldBeTrue)
    24  
    25  		So(cache.expiredSet["testkey-3"].prev != nil && cache.expiredSet["testkey-3"].prev.key == "testkey-1", ShouldBeTrue)
    26  		So(cache.expiredSet["testkey-3"].next != nil && cache.expiredSet["testkey-3"].next.key == "testkey-2", ShouldBeTrue)
    27  
    28  		So(cache.expiredSet["testkey-2"].prev != nil && cache.expiredSet["testkey-2"].prev.key == "testkey-3", ShouldBeTrue)
    29  		So(cache.expiredSet["testkey-2"].next == nil, ShouldBeTrue)
    30  
    31  		//
    32  		time.Sleep(time.Millisecond * 200)
    33  		cache.cleanExpireList()
    34  		So(len(cache.expiredSet), ShouldEqual, 2)
    35  		So(cache.expiredRoot.key == "testkey-3", ShouldBeTrue)
    36  
    37  		So(cache.expiredSet["testkey-3"].prev == nil, ShouldBeTrue)
    38  		So(cache.expiredSet["testkey-3"].next != nil && cache.expiredSet["testkey-3"].next.key == "testkey-2", ShouldBeTrue)
    39  
    40  		So(cache.expiredSet["testkey-2"].prev != nil && cache.expiredSet["testkey-2"].prev.key == "testkey-3", ShouldBeTrue)
    41  		So(cache.expiredSet["testkey-2"].next == nil, ShouldBeTrue)
    42  
    43  		//
    44  		time.Sleep(time.Millisecond * 200)
    45  		cache.cleanExpireList()
    46  		So(len(cache.expiredSet), ShouldEqual, 1)
    47  		So(cache.expiredRoot.key == "testkey-2", ShouldBeTrue)
    48  
    49  		So(cache.expiredSet["testkey-2"].prev == nil, ShouldBeTrue)
    50  		So(cache.expiredSet["testkey-2"].next == nil, ShouldBeTrue)
    51  
    52  		//
    53  		time.Sleep(time.Millisecond * 200)
    54  		cache.cleanExpireList()
    55  		So(len(cache.expiredSet), ShouldEqual, 0)
    56  		So(cache.expiredRoot == nil, ShouldBeTrue)
    57  
    58  	})
    59  
    60  }
    61  
    62  func TestCacheSync_Expire2(t *testing.T) {
    63  	Convey("输入了正确的参数,成功返回", t, func() {
    64  		cache := NewCacheSyncNoExpire()
    65  		cache.Set("testkey-1", 0)
    66  		cache.Set("testkey-2", 0)
    67  		cache.Expire("testkey-1", time.Millisecond*300, nil)
    68  		cache.Expire("testkey-2", time.Millisecond*100, nil)
    69  
    70  		So(len(cache.expiredSet), ShouldEqual, 2)
    71  		So(cache.expiredRoot.key == "testkey-2", ShouldBeTrue)
    72  
    73  		So(cache.expiredSet["testkey-2"].prev == nil, ShouldBeTrue)
    74  		So(cache.expiredSet["testkey-2"].next != nil && cache.expiredSet["testkey-2"].next.key == "testkey-1", ShouldBeTrue)
    75  
    76  		So(cache.expiredSet["testkey-1"].prev != nil && cache.expiredSet["testkey-1"].prev.key == "testkey-2", ShouldBeTrue)
    77  		So(cache.expiredSet["testkey-1"].next == nil, ShouldBeTrue)
    78  
    79  		//
    80  		cache.Expire("testkey-2", time.Millisecond*500, nil)
    81  
    82  		So(len(cache.expiredSet), ShouldEqual, 2)
    83  		So(cache.expiredRoot.key == "testkey-1", ShouldBeTrue)
    84  
    85  		So(cache.expiredSet["testkey-1"].prev == nil, ShouldBeTrue)
    86  		So(cache.expiredSet["testkey-1"].next != nil && cache.expiredSet["testkey-1"].next.key == "testkey-2", ShouldBeTrue)
    87  
    88  		So(cache.expiredSet["testkey-2"].prev != nil && cache.expiredSet["testkey-2"].prev.key == "testkey-1", ShouldBeTrue)
    89  		So(cache.expiredSet["testkey-2"].next == nil, ShouldBeTrue)
    90  	})
    91  
    92  }