ccache/bucket_test.go

70 lines
1.4 KiB
Go
Raw Normal View History

2013-10-19 14:36:33 +02:00
package ccache
import (
2014-10-25 02:46:18 +02:00
. "github.com/karlseguin/expect"
"testing"
"time"
2013-10-19 14:36:33 +02:00
)
2014-10-25 02:46:18 +02:00
type BucketTests struct {
}
func Tests_Bucket(t *testing.T) {
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)
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)
2014-10-25 02:46:18 +02:00
assertValue(item, "9002")
item = bucket.get("power")
2014-10-25 02:46:18 +02:00
assertValue(item, "9002")
assertValue(existing, "9001")
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
}