diff options
author | Jakob Borg <jakob@kastelo.net> | 2023-03-24 09:12:56 +0100 |
---|---|---|
committer | Jakob Borg <jakob@kastelo.net> | 2023-03-24 09:15:12 +0100 |
commit | b49f53583420676e1b1caccb6b9546adaab07adc (patch) | |
tree | 5e9ab00b2755834873083c1e2273bdb596c421a0 | |
parent | 34b312b85be59d77f87efc3f1afa2d90c8a9ab04 (diff) |
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).
-rw-r--r-- | cmd/stdiscosrv/database.go | 13 | ||||
-rw-r--r-- | cmd/stdiscosrv/database_test.go | 8 |
2 files changed, 16 insertions, 5 deletions
diff --git a/cmd/stdiscosrv/database.go b/cmd/stdiscosrv/database.go index f38ec4c27b..ce151da3c2 100644 --- a/cmd/stdiscosrv/database.go +++ b/cmd/stdiscosrv/database.go @@ -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() { diff --git a/cmd/stdiscosrv/database_test.go b/cmd/stdiscosrv/database_test.go index 781ac0a2a1..2596b6b170 100644 --- a/cmd/stdiscosrv/database_test.go +++ b/cmd/stdiscosrv/database_test.go @@ -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 } |