-
Notifications
You must be signed in to change notification settings - Fork 121
/
bucket_test.go
56 lines (47 loc) · 1.28 KB
/
bucket_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ccache
import (
"testing"
"time"
"github.com/karlseguin/ccache/v3/assert"
)
func Test_Bucket_GetMissFromBucket(t *testing.T) {
bucket := testBucket()
assert.Nil(t, bucket.get("invalid"))
}
func Test_Bucket_GetHitFromBucket(t *testing.T) {
bucket := testBucket()
item := bucket.get("power")
assertValue(t, item, "9000")
}
func Test_Bucket_DeleteItemFromBucket(t *testing.T) {
bucket := testBucket()
bucket.delete("power")
assert.Nil(t, bucket.get("power"))
}
func Test_Bucket_SetsANewBucketItem(t *testing.T) {
bucket := testBucket()
item, existing := bucket.set("spice", "flow", time.Minute, false)
assertValue(t, item, "flow")
item = bucket.get("spice")
assertValue(t, item, "flow")
assert.Equal(t, existing, nil)
}
func Test_Bucket_SetsAnExistingItem(t *testing.T) {
bucket := testBucket()
item, existing := bucket.set("power", "9001", time.Minute, false)
assertValue(t, item, "9001")
item = bucket.get("power")
assertValue(t, item, "9001")
assertValue(t, existing, "9000")
}
func testBucket() *bucket[string] {
b := &bucket[string]{lookup: make(map[string]*Item[string])}
b.lookup["power"] = &Item[string]{
key: "power",
value: "9000",
}
return b
}
func assertValue(t *testing.T, item *Item[string], expected string) {
assert.Equal(t, item.value, expected)
}