Skip to content

Commit

Permalink
Added more tests and coverage report.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheshir committed Jan 6, 2021
1 parent 85eda24 commit 627a271
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 24 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ dist: trusty
language: go

go:
- 1.9.x
- 1.10.x
- 1.11.x
- 1.12.x
- 1.13.x
- 1.14.x
- 1.15.x
- tip

script:
- go get ./...
- gofmt -d -s .
- go vet *.go
- go test -v -race ./...
- go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...

after_success:
bash <(curl -s https://codecov.io/bash)
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![Build Status](https://travis-ci.com/cheshir/ttlcache.svg?branch=master)](https://travis-ci.com/cheshir/ttlcache)
[![Build Status](https://travis-ci.com/cheshir/ttlcache.svg?branch=main)](https://travis-ci.com/cheshir/ttlcache)
[![codecov](https://codecov.io/gh/cheshir/ttlcache/branch/master/graph/badge.svg)](https://codecov.io/gh/cheshir/ttlcache)
[![Go Report Card](https://goreportcard.com/badge/cheshir/ttlcache)](https://goreportcard.com/report/github.com/cheshir/ttlcache)
[![GoDoc](https://godoc.org/github.com/cheshir/ttlcache?status.svg)](https://godoc.org/github.com/cheshir/ttlcache)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/cheshir/go-mq/blob/master/LICENSE)
Expand Down
95 changes: 95 additions & 0 deletions key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package ttlcache

import (
"testing"
)

func TestKeys(t *testing.T) {
tt := []struct {
name string
actual uint64
expected uint64
}{
{
name: "IntKey",
actual: IntKey(42),
expected: 42,
},
{
name: "ByteKey",
actual: ByteKey(42),
expected: 42,
},
{
name: "Int8Key",
actual: Int8Key(42),
expected: 42,
},
{
name: "Uint8Key",
actual: Uint8Key(42),
expected: 42,
},
{
name: "Int16Key",
actual: Int16Key(42),
expected: 42,
},
{
name: "Uint16Key",
actual: Uint16Key(42),
expected: 42,
},
{
name: "Int32Key",
actual: Int32Key(42),
expected: 42,
},
{
name: "Uint32Key",
actual: Uint32Key(42),
expected: 42,
},
{
name: "Int64Key",
actual: Int64Key(42),
expected: 42,
},
{
name: "Uint64Key",
actual: Uint64Key(42),
expected: 42,
},
{
name: "BytesKey",
actual: BytesKey([]byte("hello world")),
expected: 13388989860809387070,
},
{
name: "StringKey",
actual: StringKey("hello world"),
expected: 13388989860809387070,
},
{
name: "AnyKey_struct",
actual: AnyKey(struct {
X, Y int
}{
X: 1,
Y: 2,
}),
expected: 9173886622172901187,
},
{
name: "AnyKey_array",
actual: AnyKey([...]int{1, 2, 3}),
expected: 2675078694837399863,
},
}

for _, tc := range tt {
if tc.expected != tc.actual {
t.Errorf("%s expected: %v got: %v", tc.name, tc.expected, tc.actual)
}
}
}
79 changes: 61 additions & 18 deletions ttlcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,79 @@ import (
"time"
)

func TestCache(t *testing.T) {
func TestCache_GetSet(t *testing.T) {
ttl := 2 * time.Millisecond
key := StringKey("key")
value := "value"

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

{
val, ok := c.Get(key)
if !ok {
t.Error("storage missed expected value")
}
val, ok := c.Get(key)
if !ok {
t.Error("storage missed expected value")
}

v, ok2 := val.(string)
if !ok2 {
t.Error("type assertion failed")
}
v, ok2 := val.(string)
if !ok2 {
t.Error("type assertion failed")
}

if v != value {
t.Errorf("incorret value: got: %v expected: %v", v, value)
}
if v != value {
t.Errorf("incorret value: got: %v expected: %v", v, value)
}

time.Sleep(4 * time.Millisecond)

{
_, ok := c.Get(key)
if ok {
t.Error("value was not cleaned up from storage")
}
_, ok3 := c.Get(key)
if ok3 {
t.Error("record was not cleaned up")
}
}

func TestCache_Delete(t *testing.T) {
key := StringKey("key")
value := "value"

c := New(time.Second) // Cleanup should not be triggered.
c.Set(key, value, 0)

val, ok := c.Get(key)
if !ok {
t.Error("storage missed expected value")
}

v, ok2 := val.(string)
if !ok2 {
t.Error("type assertion failed")
}

if v != value {
t.Errorf("incorret value: got: %v expected: %v", v, value)
}

c.Delete(key)

_, ok3 := c.Get(key)
if ok3 {
t.Error("record was not removed")
}
}

func TestClose(t *testing.T) {
c := New(time.Second)
c.Set(IntKey(1), 1, 0)
c.Set(IntKey(2), 2, 0)
c.Set(IntKey(3), 3, 0)
c.Set(IntKey(4), 4, 0)

err := c.Close()
if err != nil {
t.Error("Unexpected error")
}

_, ok := c.Get(IntKey(1))
if ok {
t.Error("Storage was not cleaned up")
}
}

0 comments on commit 627a271

Please sign in to comment.