Skip to content

Commit 627a271

Browse files
committed
Added more tests and coverage report.
1 parent 85eda24 commit 627a271

File tree

4 files changed

+163
-24
lines changed

4 files changed

+163
-24
lines changed

.travis.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ dist: trusty
33
language: go
44

55
go:
6-
- 1.9.x
7-
- 1.10.x
8-
- 1.11.x
9-
- 1.12.x
106
- 1.13.x
117
- 1.14.x
8+
- 1.15.x
129
- tip
1310

1411
script:
1512
- go get ./...
1613
- gofmt -d -s .
1714
- go vet *.go
18-
- go test -v -race ./...
15+
- go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
16+
17+
after_success:
18+
bash <(curl -s https://codecov.io/bash)

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
[![Build Status](https://travis-ci.com/cheshir/ttlcache.svg?branch=master)](https://travis-ci.com/cheshir/ttlcache)
1+
[![Build Status](https://travis-ci.com/cheshir/ttlcache.svg?branch=main)](https://travis-ci.com/cheshir/ttlcache)
2+
[![codecov](https://codecov.io/gh/cheshir/ttlcache/branch/master/graph/badge.svg)](https://codecov.io/gh/cheshir/ttlcache)
23
[![Go Report Card](https://goreportcard.com/badge/cheshir/ttlcache)](https://goreportcard.com/report/github.com/cheshir/ttlcache)
34
[![GoDoc](https://godoc.org/github.com/cheshir/ttlcache?status.svg)](https://godoc.org/github.com/cheshir/ttlcache)
45
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/cheshir/go-mq/blob/master/LICENSE)

key_test.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package ttlcache
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestKeys(t *testing.T) {
8+
tt := []struct {
9+
name string
10+
actual uint64
11+
expected uint64
12+
}{
13+
{
14+
name: "IntKey",
15+
actual: IntKey(42),
16+
expected: 42,
17+
},
18+
{
19+
name: "ByteKey",
20+
actual: ByteKey(42),
21+
expected: 42,
22+
},
23+
{
24+
name: "Int8Key",
25+
actual: Int8Key(42),
26+
expected: 42,
27+
},
28+
{
29+
name: "Uint8Key",
30+
actual: Uint8Key(42),
31+
expected: 42,
32+
},
33+
{
34+
name: "Int16Key",
35+
actual: Int16Key(42),
36+
expected: 42,
37+
},
38+
{
39+
name: "Uint16Key",
40+
actual: Uint16Key(42),
41+
expected: 42,
42+
},
43+
{
44+
name: "Int32Key",
45+
actual: Int32Key(42),
46+
expected: 42,
47+
},
48+
{
49+
name: "Uint32Key",
50+
actual: Uint32Key(42),
51+
expected: 42,
52+
},
53+
{
54+
name: "Int64Key",
55+
actual: Int64Key(42),
56+
expected: 42,
57+
},
58+
{
59+
name: "Uint64Key",
60+
actual: Uint64Key(42),
61+
expected: 42,
62+
},
63+
{
64+
name: "BytesKey",
65+
actual: BytesKey([]byte("hello world")),
66+
expected: 13388989860809387070,
67+
},
68+
{
69+
name: "StringKey",
70+
actual: StringKey("hello world"),
71+
expected: 13388989860809387070,
72+
},
73+
{
74+
name: "AnyKey_struct",
75+
actual: AnyKey(struct {
76+
X, Y int
77+
}{
78+
X: 1,
79+
Y: 2,
80+
}),
81+
expected: 9173886622172901187,
82+
},
83+
{
84+
name: "AnyKey_array",
85+
actual: AnyKey([...]int{1, 2, 3}),
86+
expected: 2675078694837399863,
87+
},
88+
}
89+
90+
for _, tc := range tt {
91+
if tc.expected != tc.actual {
92+
t.Errorf("%s expected: %v got: %v", tc.name, tc.expected, tc.actual)
93+
}
94+
}
95+
}

ttlcache_test.go

+61-18
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,79 @@ import (
55
"time"
66
)
77

8-
func TestCache(t *testing.T) {
8+
func TestCache_GetSet(t *testing.T) {
99
ttl := 2 * time.Millisecond
1010
key := StringKey("key")
1111
value := "value"
1212

1313
c := New(time.Millisecond)
1414
c.Set(key, value, ttl)
1515

16-
{
17-
val, ok := c.Get(key)
18-
if !ok {
19-
t.Error("storage missed expected value")
20-
}
16+
val, ok := c.Get(key)
17+
if !ok {
18+
t.Error("storage missed expected value")
19+
}
2120

22-
v, ok2 := val.(string)
23-
if !ok2 {
24-
t.Error("type assertion failed")
25-
}
21+
v, ok2 := val.(string)
22+
if !ok2 {
23+
t.Error("type assertion failed")
24+
}
2625

27-
if v != value {
28-
t.Errorf("incorret value: got: %v expected: %v", v, value)
29-
}
26+
if v != value {
27+
t.Errorf("incorret value: got: %v expected: %v", v, value)
3028
}
3129

3230
time.Sleep(4 * time.Millisecond)
3331

34-
{
35-
_, ok := c.Get(key)
36-
if ok {
37-
t.Error("value was not cleaned up from storage")
38-
}
32+
_, ok3 := c.Get(key)
33+
if ok3 {
34+
t.Error("record was not cleaned up")
35+
}
36+
}
37+
38+
func TestCache_Delete(t *testing.T) {
39+
key := StringKey("key")
40+
value := "value"
41+
42+
c := New(time.Second) // Cleanup should not be triggered.
43+
c.Set(key, value, 0)
44+
45+
val, ok := c.Get(key)
46+
if !ok {
47+
t.Error("storage missed expected value")
48+
}
49+
50+
v, ok2 := val.(string)
51+
if !ok2 {
52+
t.Error("type assertion failed")
53+
}
54+
55+
if v != value {
56+
t.Errorf("incorret value: got: %v expected: %v", v, value)
57+
}
58+
59+
c.Delete(key)
60+
61+
_, ok3 := c.Get(key)
62+
if ok3 {
63+
t.Error("record was not removed")
64+
}
65+
}
66+
67+
func TestClose(t *testing.T) {
68+
c := New(time.Second)
69+
c.Set(IntKey(1), 1, 0)
70+
c.Set(IntKey(2), 2, 0)
71+
c.Set(IntKey(3), 3, 0)
72+
c.Set(IntKey(4), 4, 0)
73+
74+
err := c.Close()
75+
if err != nil {
76+
t.Error("Unexpected error")
77+
}
78+
79+
_, ok := c.Get(IntKey(1))
80+
if ok {
81+
t.Error("Storage was not cleaned up")
3982
}
4083
}

0 commit comments

Comments
 (0)