2013-10-19 14:36:33 +02:00
|
|
|
package ccache
|
|
|
|
|
|
|
|
import (
|
2014-10-25 02:46:18 +02:00
|
|
|
. "github.com/karlseguin/expect"
|
2014-02-28 13:10:42 +01:00
|
|
|
"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() {
|
2014-02-28 13:10:42 +01:00
|
|
|
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() {
|
2014-02-28 13:10:42 +01:00
|
|
|
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() {
|
2014-02-28 13:10:42 +01:00
|
|
|
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() {
|
2014-02-28 13:10:42 +01:00
|
|
|
bucket := testBucket()
|
2014-12-28 05:11:32 +01:00
|
|
|
item, existing := bucket.set("spice", TestValue("flow"), time.Minute)
|
2014-10-25 02:46:18 +02:00
|
|
|
assertValue(item, "flow")
|
2014-02-28 13:10:42 +01:00
|
|
|
item = bucket.get("spice")
|
2014-10-25 02:46:18 +02:00
|
|
|
assertValue(item, "flow")
|
2014-12-28 05:11:32 +01:00
|
|
|
Expect(existing).To.Equal(nil)
|
2013-10-19 14:36:33 +02:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:41:22 +01:00
|
|
|
func (_ *BucketTests) SetsAnExistingItem() {
|
2014-02-28 13:10:42 +01:00
|
|
|
bucket := testBucket()
|
2014-12-28 05:11:32 +01:00
|
|
|
item, existing := bucket.set("power", TestValue("9001"), time.Minute)
|
2014-10-25 02:46:18 +02:00
|
|
|
assertValue(item, "9002")
|
2014-02-28 13:10:42 +01:00
|
|
|
item = bucket.get("power")
|
2014-10-25 02:46:18 +02:00
|
|
|
assertValue(item, "9002")
|
2014-12-28 05:11:32 +01:00
|
|
|
assertValue(existing, "9001")
|
2014-11-13 16:20:12 +01:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:56:24 +01:00
|
|
|
func testBucket() *bucket {
|
|
|
|
b := &bucket{lookup: make(map[string]*Item)}
|
2014-02-28 13:10:42 +01:00
|
|
|
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) {
|
2014-02-28 13:10:42 +01:00
|
|
|
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 {
|
2014-02-28 13:10:42 +01:00
|
|
|
return time.Now()
|
2013-10-19 14:36:33 +02:00
|
|
|
}
|