From 627a2713c2a99465b30ba838b95f4b3088729cdd Mon Sep 17 00:00:00 2001 From: cheshir Date: Thu, 7 Jan 2021 01:02:17 +0200 Subject: [PATCH] Added more tests and coverage report. --- .travis.yml | 10 ++--- README.md | 3 +- key_test.go | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ ttlcache_test.go | 79 +++++++++++++++++++++++++++++++--------- 4 files changed, 163 insertions(+), 24 deletions(-) create mode 100644 key_test.go diff --git a/.travis.yml b/.travis.yml index 6113e16..87c9268 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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) \ No newline at end of file diff --git a/README.md b/README.md index c3ade64..2678731 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/key_test.go b/key_test.go new file mode 100644 index 0000000..fbae698 --- /dev/null +++ b/key_test.go @@ -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) + } + } +} diff --git a/ttlcache_test.go b/ttlcache_test.go index f949d10..23b6188 100644 --- a/ttlcache_test.go +++ b/ttlcache_test.go @@ -5,7 +5,7 @@ import ( "time" ) -func TestCache(t *testing.T) { +func TestCache_GetSet(t *testing.T) { ttl := 2 * time.Millisecond key := StringKey("key") value := "value" @@ -13,28 +13,71 @@ func TestCache(t *testing.T) { 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") } }