2013-10-19 14:36:33 +02:00
|
|
|
package ccache
|
|
|
|
|
|
|
|
import (
|
2014-02-28 13:10:42 +01:00
|
|
|
"github.com/karlseguin/gspec"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2013-10-19 14:36:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetMissFromBucket(t *testing.T) {
|
2014-02-28 13:10:42 +01:00
|
|
|
bucket := testBucket()
|
|
|
|
gspec.New(t).Expect(bucket.get("invalid")).ToBeNil()
|
2013-10-19 14:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetHitFromBucket(t *testing.T) {
|
2014-02-28 13:10:42 +01:00
|
|
|
bucket := testBucket()
|
|
|
|
item := bucket.get("power")
|
|
|
|
assertValue(t, item, "9000")
|
2013-10-19 14:36:33 +02:00
|
|
|
}
|
|
|
|
|
2013-10-30 13:18:51 +01:00
|
|
|
func TestDeleteItemFromBucket(t *testing.T) {
|
2014-02-28 13:10:42 +01:00
|
|
|
bucket := testBucket()
|
|
|
|
bucket.delete("power")
|
|
|
|
gspec.New(t).Expect(bucket.get("power")).ToBeNil()
|
2013-10-19 14:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetsANewBucketItem(t *testing.T) {
|
2014-02-28 13:10:42 +01:00
|
|
|
spec := gspec.New(t)
|
|
|
|
bucket := testBucket()
|
|
|
|
item, new := bucket.set("spice", TestValue("flow"), time.Minute)
|
|
|
|
assertValue(t, item, "flow")
|
|
|
|
item = bucket.get("spice")
|
|
|
|
assertValue(t, item, "flow")
|
|
|
|
spec.Expect(new).ToEqual(true)
|
2013-10-19 14:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetsAnExistingItem(t *testing.T) {
|
2014-02-28 13:10:42 +01:00
|
|
|
spec := gspec.New(t)
|
|
|
|
bucket := testBucket()
|
|
|
|
item, new := bucket.set("power", TestValue("9002"), time.Minute)
|
|
|
|
assertValue(t, item, "9002")
|
|
|
|
item = bucket.get("power")
|
|
|
|
assertValue(t, item, "9002")
|
|
|
|
spec.Expect(new).ToEqual(false)
|
2013-10-19 14:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func testBucket() *Bucket {
|
2014-02-28 13:10:42 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func assertValue(t *testing.T, item *Item, expected string) {
|
2014-02-28 13:10:42 +01:00
|
|
|
value := item.value.(TestValue)
|
|
|
|
gspec.New(t).Expect(value).ToEqual(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
|
|
|
}
|