github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/ttlcache/expiration_queue_test.go (about) 1 package ttlcache 2 3 import ( 4 "container/list" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func Test_newExpirationQueue(t *testing.T) { 13 assert.NotNil(t, newExpirationQueue[string, string]()) 14 } 15 16 func Test_expirationQueue_isEmpty(t *testing.T) { 17 assert.True(t, (expirationQueue[string, string]{}).isEmpty()) 18 assert.False(t, (expirationQueue[string, string]{{}}).isEmpty()) 19 } 20 21 func Test_expirationQueue_update(t *testing.T) { 22 q := expirationQueue[string, string]{ 23 { 24 Value: &Item[string, string]{ 25 value: "test1", 26 queueIndex: 0, 27 expiresAt: time.Now().Add(time.Hour), 28 }, 29 }, 30 { 31 Value: &Item[string, string]{ 32 value: "test2", 33 queueIndex: 1, 34 expiresAt: time.Now().Add(time.Minute), 35 }, 36 }, 37 } 38 39 q.update(q[1]) 40 require.Len(t, q, 2) 41 assert.Equal(t, "test2", q[0].Value.(*Item[string, string]).value) 42 } 43 44 func Test_expirationQueue_push(t *testing.T) { 45 q := expirationQueue[string, string]{ 46 { 47 Value: &Item[string, string]{ 48 value: "test1", 49 queueIndex: 0, 50 expiresAt: time.Now().Add(time.Hour), 51 }, 52 }, 53 } 54 elem := &list.Element{ 55 Value: &Item[string, string]{ 56 value: "test2", 57 queueIndex: 1, 58 expiresAt: time.Now().Add(time.Minute), 59 }, 60 } 61 62 q.push(elem) 63 require.Len(t, q, 2) 64 assert.Equal(t, "test2", q[0].Value.(*Item[string, string]).value) 65 } 66 67 func Test_expirationQueue_remove(t *testing.T) { 68 q := expirationQueue[string, string]{ 69 { 70 Value: &Item[string, string]{ 71 value: "test1", 72 queueIndex: 0, 73 expiresAt: time.Now().Add(time.Hour), 74 }, 75 }, 76 { 77 Value: &Item[string, string]{ 78 value: "test2", 79 queueIndex: 1, 80 expiresAt: time.Now().Add(time.Minute), 81 }, 82 }, 83 } 84 85 q.remove(q[1]) 86 require.Len(t, q, 1) 87 assert.Equal(t, "test1", q[0].Value.(*Item[string, string]).value) 88 } 89 90 func Test_expirationQueue_Len(t *testing.T) { 91 assert.Equal(t, 1, (expirationQueue[string, string]{{}}).Len()) 92 } 93 94 func Test_expirationQueue_Less(t *testing.T) { 95 q := expirationQueue[string, string]{ 96 { 97 Value: &Item[string, string]{ 98 value: "test1", 99 queueIndex: 0, 100 expiresAt: time.Now().Add(time.Hour), 101 }, 102 }, 103 { 104 Value: &Item[string, string]{ 105 value: "test2", 106 queueIndex: 1, 107 expiresAt: time.Now().Add(time.Minute), 108 }, 109 }, 110 { 111 Value: &Item[string, string]{ 112 value: "test3", 113 queueIndex: 2, 114 }, 115 }, 116 } 117 118 assert.False(t, q.Less(2, 1)) 119 assert.True(t, q.Less(1, 2)) 120 assert.True(t, q.Less(1, 0)) 121 assert.False(t, q.Less(0, 1)) 122 } 123 124 func Test_expirationQueue_Swap(t *testing.T) { 125 q := expirationQueue[string, string]{ 126 { 127 Value: &Item[string, string]{ 128 value: "test1", 129 queueIndex: 0, 130 expiresAt: time.Now().Add(time.Hour), 131 }, 132 }, 133 { 134 Value: &Item[string, string]{ 135 value: "test2", 136 queueIndex: 1, 137 expiresAt: time.Now().Add(time.Minute), 138 }, 139 }, 140 } 141 142 q.Swap(0, 1) 143 assert.Equal(t, "test2", q[0].Value.(*Item[string, string]).value) 144 assert.Equal(t, "test1", q[1].Value.(*Item[string, string]).value) 145 } 146 147 func Test_expirationQueue_Push(t *testing.T) { 148 q := expirationQueue[string, string]{ 149 { 150 Value: &Item[string, string]{ 151 value: "test1", 152 queueIndex: 0, 153 expiresAt: time.Now().Add(time.Hour), 154 }, 155 }, 156 } 157 158 elem := &list.Element{ 159 Value: &Item[string, string]{ 160 value: "test2", 161 queueIndex: 1, 162 expiresAt: time.Now().Add(time.Minute), 163 }, 164 } 165 166 q.Push(elem) 167 require.Len(t, q, 2) 168 assert.Equal(t, "test2", q[1].Value.(*Item[string, string]).value) 169 } 170 171 func Test_expirationQueue_Pop(t *testing.T) { 172 q := expirationQueue[string, string]{ 173 { 174 Value: &Item[string, string]{ 175 value: "test1", 176 queueIndex: 0, 177 expiresAt: time.Now().Add(time.Hour), 178 }, 179 }, 180 { 181 Value: &Item[string, string]{ 182 value: "test2", 183 queueIndex: 1, 184 expiresAt: time.Now().Add(time.Minute), 185 }, 186 }, 187 } 188 189 v := q.Pop() 190 require.NotNil(t, v) 191 assert.Equal(t, "test2", v.(*list.Element).Value.(*Item[string, string]).value) 192 require.Len(t, q, 1) 193 assert.Equal(t, "test1", q[0].Value.(*Item[string, string]).value) 194 }