Merge pull request #1309 from cfromknecht/prevent-decayedlog-db-reuse

htlcswitch/decayedlog_test: create unique test dbs
This commit is contained in:
Olaoluwa Osuntokun 2018-06-01 13:09:37 -07:00 committed by GitHub
commit a1ad3a5011
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,7 +2,9 @@ package htlcswitch
import (
"crypto/rand"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
@ -14,8 +16,19 @@ const (
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.
func startup(notifier bool) (sphinx.ReplayLog, *mockNotifier,
func startup(dbPath string, notifier bool) (sphinx.ReplayLog, *mockNotifier,
*sphinx.HashPrefix, error) {
var log sphinx.ReplayLog
@ -28,10 +41,10 @@ func startup(notifier bool) (sphinx.ReplayLog, *mockNotifier,
}
// Initialize the DecayedLog object
log = NewDecayedLog("tempdir", chainNotifier)
log = NewDecayedLog(dbPath, chainNotifier)
} else {
// Initialize the DecayedLog object
log = NewDecayedLog("tempdir", nil)
log = NewDecayedLog(dbPath, nil)
}
// Open the channeldb (start the garbage collector)
@ -65,11 +78,13 @@ func shutdown(dir string, d sphinx.ReplayLog) {
func TestDecayedLogGarbageCollector(t *testing.T) {
t.Parallel()
d, notifier, hashedSecret, err := startup(true)
dbPath := tempDecayedLogPath(t)
d, notifier, hashedSecret, err := startup(dbPath, true)
if err != nil {
t.Fatalf("Unable to start up DecayedLog: %v", err)
}
defer shutdown("tempdir", d)
defer shutdown(dbPath, d)
// Store <hashedSecret, cltv> in the sharedHashBucket.
err = d.Put(hashedSecret, cltv)
@ -124,11 +139,13 @@ func TestDecayedLogGarbageCollector(t *testing.T) {
func TestDecayedLogPersistentGarbageCollector(t *testing.T) {
t.Parallel()
d, _, hashedSecret, err := startup(true)
dbPath := tempDecayedLogPath(t)
d, _, hashedSecret, err := startup(dbPath, true)
if err != nil {
t.Fatalf("Unable to start up DecayedLog: %v", err)
}
defer shutdown("tempdir", d)
defer shutdown(dbPath, d)
// Store <hashedSecret, cltv> in the sharedHashBucket
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.
d.Stop()
d2, notifier2, hashedSecret2, err := startup(true)
d2, notifier2, hashedSecret2, err := startup(dbPath, true)
if err != nil {
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
// the stored CLTV.
@ -172,11 +189,13 @@ func TestDecayedLogPersistentGarbageCollector(t *testing.T) {
func TestDecayedLogInsertionAndDeletion(t *testing.T) {
t.Parallel()
d, _, hashedSecret, err := startup(false)
dbPath := tempDecayedLogPath(t)
d, _, hashedSecret, err := startup(dbPath, false)
if err != nil {
t.Fatalf("Unable to start up DecayedLog: %v", err)
}
defer shutdown("tempdir", d)
defer shutdown(dbPath, d)
// Store <hashedSecret, cltv> in the sharedHashBucket.
err = d.Put(hashedSecret, cltv)
@ -208,11 +227,13 @@ func TestDecayedLogInsertionAndDeletion(t *testing.T) {
func TestDecayedLogStartAndStop(t *testing.T) {
t.Parallel()
d, _, hashedSecret, err := startup(false)
dbPath := tempDecayedLogPath(t)
d, _, hashedSecret, err := startup(dbPath, false)
if err != nil {
t.Fatalf("Unable to start up DecayedLog: %v", err)
}
defer shutdown("tempdir", d)
defer shutdown(dbPath, d)
// Store <hashedSecret, cltv> in the sharedHashBucket.
err = d.Put(hashedSecret, cltv)
@ -223,11 +244,11 @@ func TestDecayedLogStartAndStop(t *testing.T) {
// Shutdown the DecayedLog's channeldb
d.Stop()
d2, _, hashedSecret2, err := startup(false)
d2, _, hashedSecret2, err := startup(dbPath, false)
if err != nil {
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.
value, err := d2.Get(hashedSecret)
@ -250,11 +271,11 @@ func TestDecayedLogStartAndStop(t *testing.T) {
// Shutdown the DecayedLog's channeldb
d2.Stop()
d3, _, hashedSecret3, err := startup(false)
d3, _, hashedSecret3, err := startup(dbPath, false)
if err != nil {
t.Fatalf("Unable to restart DecayedLog: %v", err)
}
defer shutdown("tempdir", d3)
defer shutdown(dbPath, d3)
// Assert that hashedSecret is not in the sharedHashBucket
_, err = d3.Get(hashedSecret3)
@ -272,11 +293,13 @@ func TestDecayedLogStartAndStop(t *testing.T) {
func TestDecayedLogStorageAndRetrieval(t *testing.T) {
t.Parallel()
d, _, hashedSecret, err := startup(false)
dbPath := tempDecayedLogPath(t)
d, _, hashedSecret, err := startup(dbPath, false)
if err != nil {
t.Fatalf("Unable to start up DecayedLog: %v", err)
}
defer shutdown("tempdir", d)
defer shutdown(dbPath, d)
// Store <hashedSecret, cltv> in the sharedHashBucket
err = d.Put(hashedSecret, cltv)
@ -295,5 +318,4 @@ func TestDecayedLogStorageAndRetrieval(t *testing.T) {
if cltv != value {
t.Fatalf("Value retrieved doesn't match value stored")
}
}