htlcswitch/decayedlog_test: create unique test dbs

This commit changes the decayed log tests to create
a new temporary database for each test. Previously, all
instances referenced the same db path. Since the tests
are run in parallel, the tests would create/delete the
shared db out from under each other, causing flakes in
the unit tests.
This commit is contained in:
Conner Fromknecht 2018-05-31 16:05:02 -07:00
parent 7cf5ebe265
commit d62d142d18
No known key found for this signature in database
GPG Key ID: 39DE78FBE6ACB0EF

@ -2,7 +2,9 @@ package htlcswitch
import ( import (
"crypto/rand" "crypto/rand"
"io/ioutil"
"os" "os"
"path/filepath"
"testing" "testing"
"time" "time"
@ -14,8 +16,19 @@ const (
cltv uint32 = 100000 cltv uint32 = 100000
) )
// tempDecayedLogPath creates a new temporary database path to back a single
// deccayed log instance.
func tempDecayedLogPath(t *testing.T) string {
dir, err := ioutil.TempDir("", "decayedlog")
if err != nil {
t.Fatalf("unable to create temporary decayed log dir: %v", err)
}
return filepath.Join(dir, "sphinxreplay.db")
}
// startup sets up the DecayedLog and possibly the garbage collector. // startup sets up the DecayedLog and possibly the garbage collector.
func startup(notifier bool) (sphinx.ReplayLog, *mockNotifier, func startup(dbPath string, notifier bool) (sphinx.ReplayLog, *mockNotifier,
*sphinx.HashPrefix, error) { *sphinx.HashPrefix, error) {
var log sphinx.ReplayLog var log sphinx.ReplayLog
@ -28,10 +41,10 @@ func startup(notifier bool) (sphinx.ReplayLog, *mockNotifier,
} }
// Initialize the DecayedLog object // Initialize the DecayedLog object
log = NewDecayedLog("tempdir", chainNotifier) log = NewDecayedLog(dbPath, chainNotifier)
} else { } else {
// Initialize the DecayedLog object // Initialize the DecayedLog object
log = NewDecayedLog("tempdir", nil) log = NewDecayedLog(dbPath, nil)
} }
// Open the channeldb (start the garbage collector) // Open the channeldb (start the garbage collector)
@ -65,11 +78,13 @@ func shutdown(dir string, d sphinx.ReplayLog) {
func TestDecayedLogGarbageCollector(t *testing.T) { func TestDecayedLogGarbageCollector(t *testing.T) {
t.Parallel() t.Parallel()
d, notifier, hashedSecret, err := startup(true) dbPath := tempDecayedLogPath(t)
d, notifier, hashedSecret, err := startup(dbPath, true)
if err != nil { if err != nil {
t.Fatalf("Unable to start up DecayedLog: %v", err) t.Fatalf("Unable to start up DecayedLog: %v", err)
} }
defer shutdown("tempdir", d) defer shutdown(dbPath, d)
// Store <hashedSecret, cltv> in the sharedHashBucket. // Store <hashedSecret, cltv> in the sharedHashBucket.
err = d.Put(hashedSecret, cltv) err = d.Put(hashedSecret, cltv)
@ -124,11 +139,13 @@ func TestDecayedLogGarbageCollector(t *testing.T) {
func TestDecayedLogPersistentGarbageCollector(t *testing.T) { func TestDecayedLogPersistentGarbageCollector(t *testing.T) {
t.Parallel() t.Parallel()
d, _, hashedSecret, err := startup(true) dbPath := tempDecayedLogPath(t)
d, _, hashedSecret, err := startup(dbPath, true)
if err != nil { if err != nil {
t.Fatalf("Unable to start up DecayedLog: %v", err) t.Fatalf("Unable to start up DecayedLog: %v", err)
} }
defer shutdown("tempdir", d) defer shutdown(dbPath, d)
// Store <hashedSecret, cltv> in the sharedHashBucket // Store <hashedSecret, cltv> in the sharedHashBucket
if err = d.Put(hashedSecret, cltv); err != nil { if err = d.Put(hashedSecret, cltv); err != nil {
@ -141,11 +158,11 @@ func TestDecayedLogPersistentGarbageCollector(t *testing.T) {
// Shut down DecayedLog and the garbage collector along with it. // Shut down DecayedLog and the garbage collector along with it.
d.Stop() d.Stop()
d2, notifier2, hashedSecret2, err := startup(true) d2, notifier2, hashedSecret2, err := startup(dbPath, true)
if err != nil { if err != nil {
t.Fatalf("Unable to restart DecayedLog: %v", err) t.Fatalf("Unable to restart DecayedLog: %v", err)
} }
defer shutdown("tempdir", d2) defer shutdown(dbPath, d2)
// Send a block notification to the garbage collector that expires // Send a block notification to the garbage collector that expires
// the stored CLTV. // the stored CLTV.
@ -172,11 +189,13 @@ func TestDecayedLogPersistentGarbageCollector(t *testing.T) {
func TestDecayedLogInsertionAndDeletion(t *testing.T) { func TestDecayedLogInsertionAndDeletion(t *testing.T) {
t.Parallel() t.Parallel()
d, _, hashedSecret, err := startup(false) dbPath := tempDecayedLogPath(t)
d, _, hashedSecret, err := startup(dbPath, false)
if err != nil { if err != nil {
t.Fatalf("Unable to start up DecayedLog: %v", err) t.Fatalf("Unable to start up DecayedLog: %v", err)
} }
defer shutdown("tempdir", d) defer shutdown(dbPath, d)
// Store <hashedSecret, cltv> in the sharedHashBucket. // Store <hashedSecret, cltv> in the sharedHashBucket.
err = d.Put(hashedSecret, cltv) err = d.Put(hashedSecret, cltv)
@ -208,11 +227,13 @@ func TestDecayedLogInsertionAndDeletion(t *testing.T) {
func TestDecayedLogStartAndStop(t *testing.T) { func TestDecayedLogStartAndStop(t *testing.T) {
t.Parallel() t.Parallel()
d, _, hashedSecret, err := startup(false) dbPath := tempDecayedLogPath(t)
d, _, hashedSecret, err := startup(dbPath, false)
if err != nil { if err != nil {
t.Fatalf("Unable to start up DecayedLog: %v", err) t.Fatalf("Unable to start up DecayedLog: %v", err)
} }
defer shutdown("tempdir", d) defer shutdown(dbPath, d)
// Store <hashedSecret, cltv> in the sharedHashBucket. // Store <hashedSecret, cltv> in the sharedHashBucket.
err = d.Put(hashedSecret, cltv) err = d.Put(hashedSecret, cltv)
@ -223,11 +244,11 @@ func TestDecayedLogStartAndStop(t *testing.T) {
// Shutdown the DecayedLog's channeldb // Shutdown the DecayedLog's channeldb
d.Stop() d.Stop()
d2, _, hashedSecret2, err := startup(false) d2, _, hashedSecret2, err := startup(dbPath, false)
if err != nil { if err != nil {
t.Fatalf("Unable to restart DecayedLog: %v", err) t.Fatalf("Unable to restart DecayedLog: %v", err)
} }
defer shutdown("tempdir", d2) defer shutdown(dbPath, d2)
// Retrieve the stored cltv value given the hashedSecret key. // Retrieve the stored cltv value given the hashedSecret key.
value, err := d2.Get(hashedSecret) value, err := d2.Get(hashedSecret)
@ -250,11 +271,11 @@ func TestDecayedLogStartAndStop(t *testing.T) {
// Shutdown the DecayedLog's channeldb // Shutdown the DecayedLog's channeldb
d2.Stop() d2.Stop()
d3, _, hashedSecret3, err := startup(false) d3, _, hashedSecret3, err := startup(dbPath, false)
if err != nil { if err != nil {
t.Fatalf("Unable to restart DecayedLog: %v", err) t.Fatalf("Unable to restart DecayedLog: %v", err)
} }
defer shutdown("tempdir", d3) defer shutdown(dbPath, d3)
// Assert that hashedSecret is not in the sharedHashBucket // Assert that hashedSecret is not in the sharedHashBucket
_, err = d3.Get(hashedSecret3) _, err = d3.Get(hashedSecret3)
@ -272,11 +293,13 @@ func TestDecayedLogStartAndStop(t *testing.T) {
func TestDecayedLogStorageAndRetrieval(t *testing.T) { func TestDecayedLogStorageAndRetrieval(t *testing.T) {
t.Parallel() t.Parallel()
d, _, hashedSecret, err := startup(false) dbPath := tempDecayedLogPath(t)
d, _, hashedSecret, err := startup(dbPath, false)
if err != nil { if err != nil {
t.Fatalf("Unable to start up DecayedLog: %v", err) t.Fatalf("Unable to start up DecayedLog: %v", err)
} }
defer shutdown("tempdir", d) defer shutdown(dbPath, d)
// Store <hashedSecret, cltv> in the sharedHashBucket // Store <hashedSecret, cltv> in the sharedHashBucket
err = d.Put(hashedSecret, cltv) err = d.Put(hashedSecret, cltv)
@ -295,5 +318,4 @@ func TestDecayedLogStorageAndRetrieval(t *testing.T) {
if cltv != value { if cltv != value {
t.Fatalf("Value retrieved doesn't match value stored") t.Fatalf("Value retrieved doesn't match value stored")
} }
} }