cmd/stdiscorv: Fix database test (fixes #8828)

The problem was that a statistics/cleanup run is triggered when the
database started and runs concurrently with the test. That cleanup run
removes old entries without valid addresses, and one of the test objects
matched this. The test object would thus randomly be removed in the
middle of the test, causing a failure. This fixes it so the object looks
recent when the cleaner-upper looks, and also uses a RAM database
(faster).
This commit is contained in:
Jakob Borg 2023-03-24 09:12:56 +01:00
parent 34b312b85b
commit b49f535834
2 changed files with 16 additions and 5 deletions

View File

@ -16,6 +16,7 @@ import (
"time"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/storage"
"github.com/syndtr/goleveldb/leveldb/util"
)
@ -54,6 +55,18 @@ func newLevelDBStore(dir string) (*levelDBStore, error) {
}, nil
}
func newMemoryLevelDBStore() (*levelDBStore, error) {
db, err := leveldb.Open(storage.NewMemStorage(), nil)
if err != nil {
return nil, err
}
return &levelDBStore{
db: db,
inbox: make(chan func(), 16),
clock: defaultClock{},
}, nil
}
func (s *levelDBStore) put(key string, rec DatabaseRecord) error {
t0 := time.Now()
defer func() {

View File

@ -9,15 +9,12 @@ package main
import (
"context"
"fmt"
"os"
"testing"
"time"
)
func TestDatabaseGetSet(t *testing.T) {
os.RemoveAll("_database")
defer os.RemoveAll("_database")
db, err := newLevelDBStore("_database")
db, err := newMemoryLevelDBStore()
if err != nil {
t.Fatal(err)
}
@ -119,7 +116,7 @@ func TestDatabaseGetSet(t *testing.T) {
// Put a record with misses
rec = DatabaseRecord{Misses: 42}
rec = DatabaseRecord{Misses: 42, Missed: tc.Now().UnixNano()}
if err := db.put("efgh", rec); err != nil {
t.Fatal(err)
}
@ -209,5 +206,6 @@ func (t *testClock) wind(d time.Duration) {
}
func (t *testClock) Now() time.Time {
t.now = t.now.Add(time.Nanosecond)
return t.now
}