Commit c52fd30e authored by Zahoor Mohamed's avatar Zahoor Mohamed Committed by GitHub

Add in-memory support for testing using DB (#95)

* Add inmemory support for testing using DB
* Disable race detector due to windows high mem usage on CI
* Change tests to use in-mem badger
Co-authored-by: default avataracud <12988138+acud@users.noreply.github.com>
Co-authored-by: default avatarJanos Guljas <janos@resenje.org>
parent 0f5bc1ff
...@@ -28,7 +28,7 @@ vet: ...@@ -28,7 +28,7 @@ vet:
.PHONY: test .PHONY: test
test: test:
$(GO) test -v -race ./... $(GO) test -v ./...
.PHONY: build .PHONY: build
build: export CGO_ENABLED=0 build: export CGO_ENABLED=0
......
...@@ -22,7 +22,6 @@ import ( ...@@ -22,7 +22,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"os"
"runtime" "runtime"
"sort" "sort"
"sync" "sync"
...@@ -143,17 +142,12 @@ func TestDB_updateGCSem(t *testing.T) { ...@@ -143,17 +142,12 @@ func TestDB_updateGCSem(t *testing.T) {
func newTestDB(t testing.TB, o *Options) (db *DB, cleanupFunc func()) { func newTestDB(t testing.TB, o *Options) (db *DB, cleanupFunc func()) {
t.Helper() t.Helper()
dir, err := ioutil.TempDir("", "localstore-test")
if err != nil {
t.Fatal(err)
}
cleanupFunc = func() { os.RemoveAll(dir) }
baseKey := make([]byte, 32) baseKey := make([]byte, 32)
if _, err := rand.Read(baseKey); err != nil { if _, err := rand.Read(baseKey); err != nil {
t.Fatal(err) t.Fatal(err)
} }
logger := logging.New(ioutil.Discard, 0) logger := logging.New(ioutil.Discard, 0)
db, err = New(dir, baseKey, o, logger) db, err := New("", baseKey, o, logger)
if err != nil { if err != nil {
cleanupFunc() cleanupFunc()
t.Fatal(err) t.Fatal(err)
...@@ -163,7 +157,6 @@ func newTestDB(t testing.TB, o *Options) (db *DB, cleanupFunc func()) { ...@@ -163,7 +157,6 @@ func newTestDB(t testing.TB, o *Options) (db *DB, cleanupFunc func()) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
os.RemoveAll(dir)
} }
return db, cleanupFunc return db, cleanupFunc
} }
......
...@@ -280,10 +280,7 @@ func TestModePut_sameChunk(t *testing.T) { ...@@ -280,10 +280,7 @@ func TestModePut_sameChunk(t *testing.T) {
}, },
} { } {
t.Run(tcn.name, func(t *testing.T) { t.Run(tcn.name, func(t *testing.T) {
t.Parallel()
db, cleanupFunc := newTestDB(t, nil) db, cleanupFunc := newTestDB(t, nil)
defer cleanupFunc()
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
exist, err := db.Put(context.Background(), tcn.mode, chunks...) exist, err := db.Put(context.Background(), tcn.mode, chunks...)
...@@ -314,6 +311,8 @@ func TestModePut_sameChunk(t *testing.T) { ...@@ -314,6 +311,8 @@ func TestModePut_sameChunk(t *testing.T) {
newItemsCountTest(db.pullIndex, count(tcn.pullIndex))(t) newItemsCountTest(db.pullIndex, count(tcn.pullIndex))(t)
newItemsCountTest(db.pushIndex, count(tcn.pushIndex))(t) newItemsCountTest(db.pushIndex, count(tcn.pushIndex))(t)
} }
cleanupFunc()
}) })
} }
}) })
......
...@@ -178,7 +178,10 @@ func NewBee(o Options) (*Bee, error) { ...@@ -178,7 +178,10 @@ func NewBee(o Options) (*Bee, error) {
var storer storage.Storer var storer storage.Storer
if o.DataDir == "" { if o.DataDir == "" {
// TODO: this needs to support in-mem localstore implementation somehow storer, err = localstore.New("", address.Bytes(), nil, logger)
if err != nil {
return nil, fmt.Errorf("localstore: %w", err)
}
} else { } else {
storer, err = localstore.New(filepath.Join(o.DataDir, "localstore"), address.Bytes(), nil, logger) storer, err = localstore.New(filepath.Join(o.DataDir, "localstore"), address.Bytes(), nil, logger)
if err != nil { if err != nil {
......
...@@ -55,11 +55,20 @@ type DB struct { ...@@ -55,11 +55,20 @@ type DB struct {
// NewDB opens the badger DB with options that make the DB useful for // NewDB opens the badger DB with options that make the DB useful for
// Chunk, State as well as Index stores // Chunk, State as well as Index stores
func NewDB(path string, logger logging.Logger) (db *DB, err error) { func NewDB(path string, logger logging.Logger) (db *DB, err error) {
o := badger.DefaultOptions(path) var o badger.Options
// an empty path is used for implicit in-memory badgerdb
if path == "" {
o = badger.DefaultOptions("").WithInMemory(true)
} else {
o = badger.DefaultOptions(path)
}
o.SyncWrites = DefaultSyncWrites o.SyncWrites = DefaultSyncWrites
o.ValueThreshold = DefaultValueThreshold o.ValueThreshold = DefaultValueThreshold
o.ValueLogMaxEntries = DefaultValueLogMaxEntries o.ValueLogMaxEntries = DefaultValueLogMaxEntries
o.Logger = nil // Dont enable the badger logs o.Logger = nil // Dont enable the badger logs
database, err := badger.Open(o) database, err := badger.Open(o)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -98,19 +98,12 @@ func TestDB_persistence(t *testing.T) { ...@@ -98,19 +98,12 @@ func TestDB_persistence(t *testing.T) {
// be called to remove the data. // be called to remove the data.
func newTestDB(t *testing.T) (db *DB, cleanupFunc func()) { func newTestDB(t *testing.T) (db *DB, cleanupFunc func()) {
t.Helper() t.Helper()
dir, err := ioutil.TempDir("", "shed-test")
if err != nil {
t.Fatal(err)
}
logger := logging.New(ioutil.Discard, 0) logger := logging.New(ioutil.Discard, 0)
db, err = NewDB(dir, logger) db, err := NewDB("", logger)
if err != nil { if err != nil {
os.RemoveAll(dir)
t.Fatal(err) t.Fatal(err)
} }
return db, func() { return db, func() {
db.Close() db.Close()
os.RemoveAll(dir)
} }
} }
...@@ -23,7 +23,6 @@ import ( ...@@ -23,7 +23,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"time" "time"
"github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/logging"
...@@ -300,13 +299,7 @@ func (s *Store) Close() error { ...@@ -300,13 +299,7 @@ func (s *Store) Close() error {
// Example_store constructs a simple storage implementation using shed package. // Example_store constructs a simple storage implementation using shed package.
func Example_store() { func Example_store() {
dir, err := ioutil.TempDir("", "ephemeral") s, err := New("")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
s, err := New(dir)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment