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"
|
2014-10-25 12:15:47 +02:00
|
|
|
"time"
|
2013-10-19 14:36:33 +02:00
|
|
|
)
|
|
|
|
|
2014-10-25 02:46:18 +02:00
|
|
|
type ItemTests struct{}
|
|
|
|
|
|
|
|
func Test_Item(t *testing.T) {
|
|
|
|
Expectify(new(ItemTests), t)
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:41:22 +01:00
|
|
|
func (_ *ItemTests) Promotability() {
|
2014-02-28 13:10:42 +01:00
|
|
|
item := &Item{promotions: 4}
|
2014-10-25 02:46:18 +02:00
|
|
|
Expect(item.shouldPromote(5)).To.Equal(true)
|
|
|
|
Expect(item.shouldPromote(5)).To.Equal(false)
|
2013-10-19 14:36:33 +02:00
|
|
|
}
|
2014-10-25 12:15:47 +02:00
|
|
|
|
2014-11-14 01:41:22 +01:00
|
|
|
func (_ *ItemTests) Expired() {
|
2014-10-25 12:15:47 +02:00
|
|
|
now := time.Now().Unix()
|
|
|
|
item1 := &Item{expires: now + 1}
|
|
|
|
item2 := &Item{expires: now - 1}
|
|
|
|
Expect(item1.Expired()).To.Equal(false)
|
|
|
|
Expect(item2.Expired()).To.Equal(true)
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:41:22 +01:00
|
|
|
func (_ *ItemTests) TTL() {
|
2014-10-25 12:15:47 +02:00
|
|
|
now := time.Now().Unix()
|
|
|
|
item1 := &Item{expires: now + 10}
|
|
|
|
item2 := &Item{expires: now - 10}
|
|
|
|
Expect(item1.TTL()).To.Equal(time.Second * 10)
|
|
|
|
Expect(item2.TTL()).To.Equal(time.Second * -10)
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:41:22 +01:00
|
|
|
func (_ *ItemTests) Expires() {
|
2014-10-25 12:15:47 +02:00
|
|
|
now := time.Now().Unix()
|
2014-10-27 02:27:26 +01:00
|
|
|
item := &Item{expires: now + 10}
|
|
|
|
Expect(item.Expires().Unix()).To.Equal(now + 10)
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:41:22 +01:00
|
|
|
func (_ *ItemTests) Extend() {
|
2014-10-27 02:27:26 +01:00
|
|
|
item := &Item{expires: time.Now().Unix() + 10}
|
|
|
|
item.Extend(time.Minute * 2)
|
|
|
|
Expect(item.Expires().Unix()).To.Equal(time.Now().Unix() + 120)
|
2014-10-25 12:15:47 +02:00
|
|
|
}
|