Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Aug 31, 2023
1 parent b9e7730 commit 5bd61c1
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 4 deletions.
22 changes: 18 additions & 4 deletions cmd/kava/opendb/props_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import (
"fmt"
"strings"

"github.com/linxGnu/grocksdb"
"github.com/pkg/errors"
)

type propsGetter interface {
GetProperty(propName string) (value string)
GetIntProperty(propName string) (value uint64, success bool)
}

type propsLoader struct {
db *grocksdb.DB
db propsGetter
errorMsgs []string
}

func newPropsLoader(db *grocksdb.DB) *propsLoader {
func newPropsLoader(db propsGetter) *propsLoader {
return &propsLoader{
db: db,
errorMsgs: make([]string, 0),
Expand All @@ -36,7 +40,7 @@ func (l *propsLoader) load() (*properties, error) {
EstimateTableReadersMem: l.getIntProperty("rocksdb.estimate-table-readers-mem"),
LiveSSTFilesSize: l.getIntProperty("rocksdb.live-sst-files-size"),
SizeAllMemTables: l.getIntProperty("rocksdb.size-all-mem-tables"),
OptionsStatistics: l.db.GetProperty("rocksdb.options-statistics"),
OptionsStatistics: l.getProperty("rocksdb.options-statistics"),
}

if len(l.errorMsgs) != 0 {
Expand All @@ -47,6 +51,16 @@ func (l *propsLoader) load() (*properties, error) {
return props, nil
}

func (l *propsLoader) getProperty(propName string) string {
value := l.db.GetProperty(propName)
if value == "" {
l.errorMsgs = append(l.errorMsgs, fmt.Sprintf("property %v is empty", propName))
return ""
}

return value
}

func (l *propsLoader) getIntProperty(propName string) uint64 {
value, ok := l.db.GetIntProperty(propName)
if !ok {
Expand Down
112 changes: 112 additions & 0 deletions cmd/kava/opendb/props_loader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//go:build rocksdb
// +build rocksdb

package opendb

import (
"testing"

"github.com/stretchr/testify/require"
)

type mockPropsGetter struct {
props map[string]string
intProps map[string]uint64
}

func newMockPropsGetter(
props map[string]string,
intProps map[string]uint64,
) *mockPropsGetter {
return &mockPropsGetter{
props: props,
intProps: intProps,
}
}

func (m *mockPropsGetter) GetProperty(propName string) string {
return m.props[propName]
}

func (m *mockPropsGetter) GetIntProperty(propName string) (uint64, bool) {
prop, ok := m.intProps[propName]
return prop, ok
}

func TestPropsLoader(t *testing.T) {
defaultProps := map[string]string{
"rocksdb.options-statistics": "1",
}
defaultIntProps := map[string]uint64{
"rocksdb.base-level": 1,
"rocksdb.block-cache-capacity": 2,
"rocksdb.block-cache-pinned-usage": 3,
"rocksdb.block-cache-usage": 4,
"rocksdb.cur-size-active-mem-table": 5,
"rocksdb.cur-size-all-mem-tables": 6,
"rocksdb.estimate-live-data-size": 7,
"rocksdb.estimate-num-keys": 8,
"rocksdb.estimate-table-readers-mem": 9,
"rocksdb.live-sst-files-size": 10,
"rocksdb.size-all-mem-tables": 11,
}
missingProps := make(map[string]string)
missingIntProps := make(map[string]uint64)
defaultExpectedProps := properties{
BaseLevel: 1,
BlockCacheCapacity: 2,
BlockCachePinnedUsage: 3,
BlockCacheUsage: 4,
CurSizeActiveMemTable: 5,
CurSizeAllMemTables: 6,
EstimateLiveDataSize: 7,
EstimateNumKeys: 8,
EstimateTableReadersMem: 9,
LiveSSTFilesSize: 10,
SizeAllMemTables: 11,
OptionsStatistics: "1",
}

for _, tc := range []struct {
desc string
props map[string]string
intProps map[string]uint64
expectedProps *properties
success bool
}{
{
desc: "success case",
props: defaultProps,
intProps: defaultIntProps,
expectedProps: &defaultExpectedProps,
success: true,
},
{
desc: "missing props",
props: missingProps,
intProps: defaultIntProps,
expectedProps: nil,
success: false,
},
{
desc: "missing integer props",
props: defaultProps,
intProps: missingIntProps,
expectedProps: nil,
success: false,
},
} {
t.Run(tc.desc, func(t *testing.T) {
mockPropsGetter := newMockPropsGetter(tc.props, tc.intProps)

propsLoader := newPropsLoader(mockPropsGetter)
actualProps, err := propsLoader.load()
if tc.success {
require.NoError(t, err)
} else {
require.Error(t, err)
}
require.Equal(t, tc.expectedProps, actualProps)
})
}
}
3 changes: 3 additions & 0 deletions cmd/kava/opendb/run.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CGO_CFLAGS="-I/opt/homebrew/opt/rocksdb/include" \
CGO_LDFLAGS="-L/opt/homebrew/opt/rocksdb/lib -lrocksdb -lstdc++ -lm -lz -L/opt/homebrew/opt/snappy/lib -L/opt/homebrew/opt/lz4/lib -L/opt/homebrew/opt/zstd/lib" \
go test -v -tags=rocksdb

0 comments on commit 5bd61c1

Please sign in to comment.