Skip to content

Commit

Permalink
Merge d62d142 into 7cf5ebe
Browse files Browse the repository at this point in the history
  • Loading branch information
cfromknecht committed May 31, 2018
2 parents 7cf5ebe + d62d142 commit 5fda3f3
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions htlcswitch/decayedlog_test.go
Expand Up @@ -2,7 +2,9 @@ package htlcswitch

import (
"crypto/rand"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -295,5 +318,4 @@ func TestDecayedLogStorageAndRetrieval(t *testing.T) {
if cltv != value {
t.Fatalf("Value retrieved doesn't match value stored")
}

}

0 comments on commit 5fda3f3

Please sign in to comment.