Skip to content

Commit

Permalink
Fix (pogreb): 'With' method returns
Browse files Browse the repository at this point in the history
  • Loading branch information
yunginnanet committed Jul 6, 2024
1 parent 95cdc65 commit 6bd1316
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ require (
)

retract (
v0.4.7 // riddled
v0.4.6 // with
v0.4.5 // pre v1
v0.5.0 // riddled
v0.4.7 // with
v0.4.6 // pre
v0.4.5 // v1
v0.4.4 // bugs
//
v0.4.3 // deadlock
Expand Down
17 changes: 16 additions & 1 deletion pogreb/pogreb.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,16 @@ type Store struct {
metrics *pogreb.Metrics
}

var nilBackend = &pogreb.DB{}

// Backend returns the underlying pogreb instance.
func (pstore *Store) Backend() any {
if pstore == nil {
return nilBackend
}
if pstore.DB == nil {
return nilBackend
}
return pstore.DB
}

Expand Down Expand Up @@ -300,6 +308,10 @@ func (db *DB) With(storeName string) database.Filer {
}
db.mu.RLock()
d, ok := db.store[storeName]
if !ok {
db.mu.RUnlock()
return nil
}
if ok {
if d.closed == nil || d.DB == nil || d.closed.Load() {
db.mu.RUnlock()
Expand All @@ -317,6 +329,9 @@ func (db *DB) With(storeName string) database.Filer {
return d
}
db.mu.RUnlock()
if d != nil && d.DB == nil {
d = nil
}
return d
}

Expand Down Expand Up @@ -398,7 +413,7 @@ func (db *DB) withAll(action withAllAction) error {
var errs = make([]error, len(db.store))
for name, store := range db.store {
var err error
if store == nil || store.Backend().(*pogreb.DB) == nil {
if store == nil || store.Backend() == nilBackend || store.Backend().(*pogreb.DB) == nil {
errs = append(errs, namedErr(name, ErrBogusStore))
continue
}
Expand Down
6 changes: 3 additions & 3 deletions pogreb/pogreb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ func TestDB_Init(t *testing.T) { //nolint:funlen,gocognit,cyclop
}
})
t.Run("withNewStoreDoesntExist", func(t *testing.T) {
if nope := db.WithNew("asdfqwerty"); nope.Backend() == nil {
if nope := db.WithNew("asdfqwerty"); nope.Backend() == nil || nope.Backend() == nilBackend {
t.Fatalf("[FAIL] got nil result for nonexistent store when it should have made itself: %T, %v", nope, nope)
} else {
t.Logf("[SUCCESS] got nil Value for store that doesn't exist")
t.Logf("[SUCCESS] got new store with valid backend when calling WithNew for store that doesn't exist")
}
})
t.Run("withStoreDoesntExist", func(t *testing.T) {
nope := db.With(c.RandStr(10))
if nope != nil {
if nope != nil && nope.Backend() != nilBackend {
t.Fatalf("[FAIL] got non nil result for nonexistent store: %T, %v", nope.Backend(), nope.Backend())
} else {
t.Logf("[SUCCESS] got nil Value for store that doesn't exist")
Expand Down

0 comments on commit 6bd1316

Please sign in to comment.