File: item_test.go

package info (click to toggle)
golang-github-karlseguin-ccache 2.0.2%2Bgit20161222.2.12c7ffd-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 132 kB
  • ctags: 161
  • sloc: makefile: 6
file content (49 lines) | stat: -rw-r--r-- 1,232 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package ccache

import (
	"math"
	"testing"
	"time"

	. "github.com/karlseguin/expect"
)

type ItemTests struct{}

func Test_Item(t *testing.T) {
	Expectify(new(ItemTests), t)
}

func (_ *ItemTests) Promotability() {
	item := &Item{promotions: 4}
	Expect(item.shouldPromote(5)).To.Equal(true)
	Expect(item.shouldPromote(5)).To.Equal(false)
}

func (_ *ItemTests) Expired() {
	now := time.Now().UnixNano()
	item1 := &Item{expires: now + (10 * int64(time.Millisecond))}
	item2 := &Item{expires: now - (10 * int64(time.Millisecond))}
	Expect(item1.Expired()).To.Equal(false)
	Expect(item2.Expired()).To.Equal(true)
}

func (_ *ItemTests) TTL() {
	now := time.Now().UnixNano()
	item1 := &Item{expires: now + int64(time.Second)}
	item2 := &Item{expires: now - int64(time.Second)}
	Expect(int(math.Ceil(item1.TTL().Seconds()))).To.Equal(1)
	Expect(int(math.Ceil(item2.TTL().Seconds()))).To.Equal(-1)
}

func (_ *ItemTests) Expires() {
	now := time.Now().UnixNano()
	item := &Item{expires: now + (10)}
	Expect(item.Expires().UnixNano()).To.Equal(now + 10)
}

func (_ *ItemTests) Extend() {
	item := &Item{expires: time.Now().UnixNano() + 10}
	item.Extend(time.Minute * 2)
	Expect(item.Expires().Unix()).To.Equal(time.Now().Unix() + 120)
}