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 23, 2023
1 parent f9aa275 commit c0a3f5d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
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-20231020075600-61001d347387
github.com/xgzlucario/GigaCache v0.0.0-20231022133204-b0a967d404cb
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-20231020075600-61001d347387 h1:1B7fhItxnyPHVI95u1uyXL7DkqT7F22RAN+LdirTl0M=
github.com/xgzlucario/GigaCache v0.0.0-20231020075600-61001d347387/go.mod h1:n0gu6svrq5UYwUWv8RRYgt06u8e5E3AMNg5eqflP74Y=
github.com/xgzlucario/GigaCache v0.0.0-20231022133204-b0a967d404cb h1:HqKnS65YqDtZtDNwKDRMIiZKfV3eFWYuomKban0KY+k=
github.com/xgzlucario/GigaCache v0.0.0-20231022133204-b0a967d404cb/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
25 changes: 10 additions & 15 deletions rotom.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ type Engine struct {
closed bool
buf *bytes.Buffer
rwbuf *bytes.Buffer
m *cache.GigaCache[string]
m *cache.GigaCache
}

// Open opens a database specified by config.
Expand All @@ -193,7 +193,7 @@ func Open(conf *Config) (*Engine, error) {
Config: conf,
buf: bytes.NewBuffer(nil),
rwbuf: bytes.NewBuffer(nil),
m: cache.New[string](conf.ShardCount),
m: cache.New(conf.ShardCount),
}
e.tmpPath = e.Path + ".tmp"

Expand All @@ -208,7 +208,7 @@ func Open(conf *Config) (*Engine, error) {
e.printRuntimeStats()
})

if e.SyncPolicy != base.Never {
if e.SyncPolicy == base.EveryInterval {
// sync buffer to disk.
e.backend(e.SyncInterval, func() {
e.Lock()
Expand Down Expand Up @@ -1119,6 +1119,7 @@ func fetch[T any](e *Engine, key string, new func() T, setWhenNotExist ...bool)
return vptr, nil
}

// formatSize
func formatSize[T base.Integer](size T) string {
switch {
case size < KB:
Expand All @@ -1132,6 +1133,7 @@ func formatSize[T base.Integer](size T) string {
}
}

// backend
func (e *Engine) backend(t time.Duration, f func()) {
if t <= 0 {
panic("invalid interval")
Expand All @@ -1147,6 +1149,7 @@ func (e *Engine) backend(t time.Duration, f func()) {
}()
}

// printRuntimeStats
func (e *Engine) printRuntimeStats() {
if e.Logger == nil {
return
Expand All @@ -1166,26 +1169,18 @@ func (e *Engine) printRuntimeStats() {
Info("[Runtime]")
}

// logInfo
func (e *Engine) logInfo(msg string, args ...any) {
if e.Logger == nil {
return
}

if len(args) == 0 {
e.Logger.Info(msg)
} else {
e.Logger.Info(fmt.Sprintf(msg, args...))
}
e.Logger.Info(fmt.Sprintf(msg, args...))
}

// logError
func (e *Engine) logError(msg string, args ...any) {
if e.Logger == nil {
return
}

if len(args) == 0 {
e.Logger.Error(msg)
} else {
e.Logger.Error(fmt.Sprintf(msg, args...))
}
e.Logger.Error(fmt.Sprintf(msg, args...))
}
42 changes: 39 additions & 3 deletions rotom_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rotom

import (
"log/slog"
"os"
"strconv"
"testing"
Expand All @@ -27,9 +28,15 @@ var (
func TestDB(t *testing.T) {
assert := assert.New(t)

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

cfg := &Config{
Path: gofakeit.UUID() + ".db",
ShardCount: 1024,
SyncPolicy: base.EveryInterval,
SyncInterval: time.Second,
ShrinkInterval: time.Second * 3,
RunSkipLoadError: true,
Logger: slog.Default(),
}
db, err := Open(cfg)
assert.Nil(err)
assert.NotNil(db)
Expand Down Expand Up @@ -63,6 +70,24 @@ func TestDB(t *testing.T) {
assert.Equal(res, 4.5)
assert.Nil(err)

// Keys
assert.ElementsMatch(db.Keys(), []string{"foo", "num", "hm"})

// Rename
ok := db.Rename("num", "num-new")
assert.True(ok)
res, err = db.Incr("num-new", 0.5)
assert.Equal(res, float64(5))
assert.Nil(err)

// Remove
assert.True(db.Remove("num-new"))
assert.False(db.Remove("num-new"))

db.printRuntimeStats()
go db.Listen("localhost:7676")
time.Sleep(time.Second * 5)

// close
assert.Nil(db.Close())
assert.NotNil(db.Close())
Expand Down Expand Up @@ -382,6 +407,17 @@ func TestSetAndBitmap(t *testing.T) {
assert.Equal(db.BitAnd(kp1.skey, kp2.bkey, ""), base.ErrWrongType)
assert.Equal(db.BitXor(kp1.bkey, kp2.skey, ""), base.ErrWrongType)
assert.Equal(db.BitXor(kp1.skey, kp2.bkey, ""), base.ErrWrongType)
// Test Bitmap other errors
_, err = db.BitTest(kp1.skey, 100)
assert.Equal(err, base.ErrWrongType)
_, err = db.BitSet(kp1.skey, 100, true)
assert.Equal(err, base.ErrWrongType)
err = db.BitFlip(kp1.skey, 100)
assert.Equal(err, base.ErrWrongType)
_, err = db.BitArray(kp1.skey)
assert.Equal(err, base.ErrWrongType)
_, err = db.BitCount(kp1.skey)
assert.Equal(err, base.ErrWrongType)

setEqualBitmap(assert, db, kp1.skey, kp1.bkey)
setEqualBitmap(assert, db, kp2.skey, kp2.bkey)
Expand Down

0 comments on commit c0a3f5d

Please sign in to comment.