forked from karlseguin/ccache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket_test.go
69 lines (57 loc) · 1.42 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
57
58
59
60
61
62
63
64
65
66
67
68
69
package ccache
import (
. "github.com/karlseguin/expect"
"testing"
"time"
)
type BucketTests struct {
}
func Tests_Bucket(t *testing.T) {
Expectify(new(BucketTests), t)
}
func (_ *BucketTests) GetMissFromBucket() {
bucket := testBucket()
Expect(bucket.get("invalid")).To.Equal(nil)
}
func (_ *BucketTests) GetHitFromBucket() {
bucket := testBucket()
item := bucket.get("power")
assertValue(item, "9000")
}
func (_ *BucketTests) DeleteItemFromBucket() {
bucket := testBucket()
bucket.delete("power")
Expect(bucket.get("power")).To.Equal(nil)
}
func (_ *BucketTests) SetsANewBucketItem() {
bucket := testBucket()
item, existing := bucket.set("spice", TestValue("flow"), time.Minute)
assertValue(item, "flow")
item = bucket.get("spice")
assertValue(item, "flow")
Expect(existing).To.Equal(nil)
}
func (_ *BucketTests) SetsAnExistingItem() {
bucket := testBucket()
item, existing := bucket.set("power", TestValue("9001"), time.Minute)
assertValue(item, "9002")
item = bucket.get("power")
assertValue(item, "9002")
assertValue(existing, "9001")
}
func testBucket() *bucket {
b := &bucket{lookup: make(map[string]*Item)}
b.lookup["power"] = &Item{
key: "power",
value: TestValue("9000"),
}
return b
}
func assertValue(item *Item, expected string) {
value := item.value.(TestValue)
Expect(value).To.Equal(TestValue(expected))
}
type TestValue string
func (v TestValue) Expires() time.Time {
return time.Now()
}