Skip to content

Commit

Permalink
add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Oct 20, 2023
1 parent 4a755dd commit 39ab615
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rotom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ jobs:
run: go test -race -coverprofile=coverage.txt -covermode=atomic
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4-beta
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
rotom.db
*.db
coverage.html
coverage.*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ run-gc:
GODEBUG=gctrace=1 go run main.go

test-cover:
go test -coverprofile=c.out
go test -race -coverprofile=c.out -covermode=atomic
go tool cover -html=c.out -o coverage.html
rm c.out
rm *.db
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *Client) Set(key string, val []byte) ([]byte, error) {

// SetEx
func (c *Client) SetEx(key string, val []byte, ttl time.Duration) ([]byte, error) {
return c.SetTx(key, val, cache.GetUnixNano()+int64(ttl))
return c.SetTx(key, val, cache.GetClock()+int64(ttl))
}

// SetTx
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/sakeven/RbTree v0.0.0-20220710124251-94e35f9fed6c
github.com/sourcegraph/conc v0.3.0
github.com/stretchr/testify v1.8.4
github.com/xgzlucario/GigaCache v0.0.0-20231015082606-97872112f8fe
github.com/xgzlucario/GigaCache v0.0.0-20231020075600-61001d347387
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/xgzlucario/GigaCache v0.0.0-20231015082606-97872112f8fe h1:A3t9xRF/MOx4wP+Zp59ICSH1Xk0z2TJcWmp8Meh9Q5k=
github.com/xgzlucario/GigaCache v0.0.0-20231015082606-97872112f8fe/go.mod h1:n0gu6svrq5UYwUWv8RRYgt06u8e5E3AMNg5eqflP74Y=
github.com/xgzlucario/GigaCache v0.0.0-20231020075600-61001d347387 h1:1B7fhItxnyPHVI95u1uyXL7DkqT7F22RAN+LdirTl0M=
github.com/xgzlucario/GigaCache v0.0.0-20231020075600-61001d347387/go.mod h1:n0gu6svrq5UYwUWv8RRYgt06u8e5E3AMNg5eqflP74Y=
github.com/zeebo/assert v1.3.1 h1:vukIABvugfNMZMQO1ABsyQDJDTVQbn+LWSMy1ol1h6A=
github.com/zeebo/assert v1.3.1/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
Expand Down
26 changes: 18 additions & 8 deletions rotom.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,13 @@ type (
var (
// Default config for db
DefaultConfig = &Config{
Path: "rotom.db",
ShardCount: 1024,
SyncPolicy: base.EveryInterval,
SyncInterval: time.Second,
ShrinkInterval: time.Minute,
Logger: slog.Default(),
Path: "rotom.db",
ShardCount: 1024,
SyncPolicy: base.EveryInterval,
SyncInterval: time.Second,
ShrinkInterval: time.Minute,
RunSkipLoadError: true,
Logger: slog.Default(),
}

// No persistent config
Expand All @@ -170,6 +171,8 @@ type Config struct {
SyncInterval time.Duration // Job for db sync to disk.
ShrinkInterval time.Duration // Job for shrink db file size.

RunSkipLoadError bool // Starts when loading database file error.

Logger *slog.Logger // Logger for db, set <nil> if you don't want to use it.
}

Expand Down Expand Up @@ -197,6 +200,7 @@ func Open(conf *Config) (*Engine, error) {
// load db from disk.
if err := e.load(); err != nil {
e.logError("db load error: %v", err)
return nil, err
}

// runtime monitor.
Expand Down Expand Up @@ -289,7 +293,7 @@ func (e *Engine) Set(key string, val []byte) {

// SetEx store key-value pair with ttl.
func (e *Engine) SetEx(key string, val []byte, ttl time.Duration) {
e.SetTx(key, val, cache.GetUnixNano()+int64(ttl))
e.SetTx(key, val, cache.GetClock()+int64(ttl))
}

// SetTx store key-value pair with deadline.
Expand Down Expand Up @@ -771,6 +775,12 @@ func (e *Engine) load() error {
// <OP><argsNum><args...>
for len(line) > 2 {
op := Operation(line[0])

// if operation valid
if int(op) >= len(cmdTable) {
return base.ErrParseRecordLine
}

argsNum := cmdTable[op].ArgsNum
line = line[1:]

Expand All @@ -788,7 +798,7 @@ func (e *Engine) load() error {

case OpSetTx: // type, key, ts, val
ts := base.ParseInt[int64](args[2]) * timeCarry
if ts < cache.GetUnixNano() && ts != NoTTL {
if ts < cache.GetClock() && ts != NoTTL {
continue
}

Expand Down
116 changes: 113 additions & 3 deletions rotom_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rotom

import (
"os"
"testing"
"time"

Expand All @@ -15,6 +16,60 @@ type vItem struct {
Ts int64
}

var (
nilBytes []byte
)

func TestDB(t *testing.T) {
assert := assert.New(t)

cfg := DefaultConfig
cfg.Path = gofakeit.UUID() + ".db"

db, err := Open(cfg)
assert.Nil(err)
assert.NotNil(db)

// Set
db.Set("foo", []byte("bar"))
db.Set("num", []byte("1"))
db.HSet("hm", "foo", []byte("bar"))

// Get
val, ts, err := db.GetBytes("hm")
assert.Equal(val, nilBytes)
assert.Equal(ts, int64(0))
assert.Equal(err, base.ErrTypeAssert)

val, ts, err = db.GetBytes("none")
assert.Equal(val, nilBytes)
assert.Equal(ts, int64(0))
assert.Equal(err, base.ErrKeyNotFound)

// Incr
res, err := db.Incr("hm", 3.5)
assert.Equal(res, float64(0))
assert.Equal(err, base.ErrTypeAssert)

res, err = db.Incr("foo", 3.5)
assert.Equal(res, float64(0))
assert.NotNil(err)

res, err = db.Incr("num", 3.5)
assert.Equal(res, 4.5)
assert.Nil(err)

// close
assert.Nil(db.Close())
assert.NotNil(db.Close())

// load error
os.WriteFile(cfg.Path, []byte("fake data"), 0644)
db, err = Open(cfg)
assert.NotNil(err)
assert.Nil(db)
}

// Test cache set operation
func TestCacheSet(t *testing.T) {
assert := assert.New(t)
Expand Down Expand Up @@ -44,7 +99,7 @@ func TestCacheSet(t *testing.T) {
// get
for k, v := range kvdata {
// expired
if v.Ts < cache.GetUnixNano() {
if v.Ts < cache.GetClock() {
val, ts, ok := db.Get(k)
assert.Equal(val, nil)
assert.Equal(ts, int64(0))
Expand Down Expand Up @@ -72,7 +127,7 @@ func TestCacheSet(t *testing.T) {
v.Ts *= (1000 * 1000 * 1000)

// expired
if v.Ts < cache.GetUnixNano() {
if v.Ts < cache.GetClock() {
_, _, ok := db.Get(k)
assert.False(ok)

Expand Down Expand Up @@ -132,6 +187,61 @@ func TestBitmap(t *testing.T) {
test()
}

func TestMap(t *testing.T) {
assert := assert.New(t)

cfg := DefaultConfig
cfg.Path = gofakeit.UUID() + ".db"

db, err := Open(cfg)
if err != nil {
panic(err)
}

// valid
vmap := make(map[string][]byte, 10000)

for i := 0; i < 10000; i++ {
k := gofakeit.UUID()
v := []byte(gofakeit.Username())

db.HSet("hm", k, v)
vmap[k] = v
}

// len
c, err := db.HLen("hm")
assert.Nil(err)
assert.Equal(c, len(vmap))

// remove
for k := range vmap {
err := db.HRemove("hm", k)
delete(vmap, k)
assert.Nil(err)
break
}

test := func() {
for k, v := range vmap {
res, err := db.HGet("hm", k)
assert.Equal(res, v)
assert.Nil(err)
}
}

test()

err = db.Close()
assert.Nil(err)

// load
db, err = Open(cfg)
assert.Nil(err)

test()
}

func FuzzSet(f *testing.F) {
db, err := Open(NoPersistentConfig)
if err != nil {
Expand All @@ -141,7 +251,7 @@ func FuzzSet(f *testing.F) {
f.Fuzz(func(t *testing.T, key string, val []byte, ts int64) {
assert := assert.New(t)
db.SetTx(key, val, ts)
now := cache.GetUnixNano()
now := cache.GetClock()

v, ttl, err := db.GetBytes(key)

Expand Down

0 comments on commit 39ab615

Please sign in to comment.