Skip to content

Commit

Permalink
100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
parMaster committed Jul 13, 2023
1 parent 13b7e70 commit 9173f1f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# mcache

`mcache` is a simple and thread-safe in-memory cache library for Go.
`mcache` is a simple, fast, thread-safe in-memory cache library with by-key TTL written in Go.

## Features

Expand Down Expand Up @@ -144,9 +144,19 @@ go func() {

See the [examples](https://github.com/parMaster/mcache/tree/main/examples) directory for more examples.

## Benchmarks
## Tests and Benchmarks

100% test coverage:

```shell
$ go test -race .

ok github.com/parMaster/mcache 8.239s coverage: 100.0% of statements
```
Blinding fast and efficient:

```shell
Running tool: /usr/local/go/bin/go test -benchmem -run=^$ -coverprofile=/tmp/vscode-gopk5hiw/go-code-cover -bench . github.com/parMaster/mcache
$ go test -bench . -benchmem

goos: linux
goarch: amd64
Expand Down
29 changes: 29 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,36 @@ func Test_SimpleTest_Mcache(t *testing.T) {
assert.Equal(t, ErrKeyNotFound, err.Error())
}

c.Set("key", "value", 1)
time.Sleep(time.Second * 2)
err = c.Set("key", "newvalue", 1)
assert.NoError(t, err)

// old value should be rewritten
value, err := c.Get("key")
assert.NoError(t, err)
assert.Equal(t, "newvalue", value)

err = c.Set("key", "not a newer value", 1)
assert.Equal(t, ErrKeyExists, err.Error())

time.Sleep(time.Second * 2)
err = c.Set("key", "even newer value", 1)
// key should be silently rewritten
assert.NoError(t, err)
value, err = c.Get("key")
assert.NoError(t, err)
assert.Equal(t, "even newer value", value)

time.Sleep(time.Second * 2)
c.Cleanup()
// key should be deleted
has, err = c.Has("key")
assert.False(t, has)

// del should return error if key doesn't exist
err = c.Del("noSuchKey")
assert.Error(t, err)

err = c.Clear()
assert.NoError(t, err)
Expand Down

0 comments on commit 9173f1f

Please sign in to comment.