ccache/bucket_test.go

71 lines
1.4 KiB
Go
Raw Normal View History

2013-10-19 14:36:33 +02:00
package ccache
import (
"testing"
"time"
. "github.com/karlseguin/expect"
2013-10-19 14:36:33 +02:00
)
2014-10-25 02:46:18 +02:00
type BucketTests struct {
}
2019-02-14 13:54:01 +01:00
func Test_Bucket(t *testing.T) {
2014-10-25 02:46:18 +02:00
Expectify(new(BucketTests), t)
}
2014-11-14 01:41:22 +01:00
func (_ *BucketTests) GetMissFromBucket() {
bucket := testBucket()
2014-10-25 02:46:18 +02:00
Expect(bucket.get("invalid")).To.Equal(nil)
2013-10-19 14:36:33 +02:00
}
2014-11-14 01:41:22 +01:00
func (_ *BucketTests) GetHitFromBucket() {
bucket := testBucket()
item := bucket.get("power")
2014-10-25 02:46:18 +02:00
assertValue(item, "9000")
2013-10-19 14:36:33 +02:00
}
2014-11-14 01:41:22 +01:00
func (_ *BucketTests) DeleteItemFromBucket() {
bucket := testBucket()
bucket.delete("power")
2014-10-25 02:46:18 +02:00
Expect(bucket.get("power")).To.Equal(nil)
2013-10-19 14:36:33 +02:00
}
2014-11-14 01:41:22 +01:00
func (_ *BucketTests) SetsANewBucketItem() {
bucket := testBucket()
item, existing := bucket.set("spice", TestValue("flow"), time.Minute, false)
2014-10-25 02:46:18 +02:00
assertValue(item, "flow")
item = bucket.get("spice")
2014-10-25 02:46:18 +02:00
assertValue(item, "flow")
Expect(existing).To.Equal(nil)
2013-10-19 14:36:33 +02:00
}
2014-11-14 01:41:22 +01:00
func (_ *BucketTests) SetsAnExistingItem() {
bucket := testBucket()
item, existing := bucket.set("power", TestValue("9001"), time.Minute, false)
2019-02-14 13:54:01 +01:00
assertValue(item, "9001")
item = bucket.get("power")
2019-02-14 13:54:01 +01:00
assertValue(item, "9001")
assertValue(existing, "9000")
2014-11-13 16:20:12 +01:00
}
func testBucket() *bucket {
b := &bucket{lookup: make(map[string]*Item)}
b.lookup["power"] = &Item{
key: "power",
value: TestValue("9000"),
}
return b
2013-10-19 14:36:33 +02:00
}
2014-10-25 02:46:18 +02:00
func assertValue(item *Item, expected string) {
value := item.value.(TestValue)
2014-10-25 02:46:18 +02:00
Expect(value).To.Equal(TestValue(expected))
2013-10-19 14:36:33 +02:00
}
2013-10-19 14:45:30 +02:00
type TestValue string
2013-10-19 14:36:33 +02:00
2013-10-19 14:45:30 +02:00
func (v TestValue) Expires() time.Time {
return time.Now()
2013-10-19 14:36:33 +02:00
}