mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-17 10:35:11 +00:00
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:
parent
34b312b85b
commit
b49f535834
@ -16,6 +16,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/syndtr/goleveldb/leveldb"
|
"github.com/syndtr/goleveldb/leveldb"
|
||||||
|
"github.com/syndtr/goleveldb/leveldb/storage"
|
||||||
"github.com/syndtr/goleveldb/leveldb/util"
|
"github.com/syndtr/goleveldb/leveldb/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -54,6 +55,18 @@ func newLevelDBStore(dir string) (*levelDBStore, error) {
|
|||||||
}, nil
|
}, 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 {
|
func (s *levelDBStore) put(key string, rec DatabaseRecord) error {
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -9,15 +9,12 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDatabaseGetSet(t *testing.T) {
|
func TestDatabaseGetSet(t *testing.T) {
|
||||||
os.RemoveAll("_database")
|
db, err := newMemoryLevelDBStore()
|
||||||
defer os.RemoveAll("_database")
|
|
||||||
db, err := newLevelDBStore("_database")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -119,7 +116,7 @@ func TestDatabaseGetSet(t *testing.T) {
|
|||||||
|
|
||||||
// Put a record with misses
|
// 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 {
|
if err := db.put("efgh", rec); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -209,5 +206,6 @@ func (t *testClock) wind(d time.Duration) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *testClock) Now() time.Time {
|
func (t *testClock) Now() time.Time {
|
||||||
|
t.now = t.now.Add(time.Nanosecond)
|
||||||
return t.now
|
return t.now
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user