Skip to content

Commit

Permalink
Merge pull request #4411 from bhandras/test_channeldb
Browse files Browse the repository at this point in the history
kvdb+etcd+tests: change etcd flattened bucket key derivation to make it compatible with bbolt
  • Loading branch information
cfromknecht committed Aug 1, 2020
2 parents 247b753 + 5346ed8 commit f144dea
Show file tree
Hide file tree
Showing 27 changed files with 786 additions and 709 deletions.
56 changes: 10 additions & 46 deletions channeldb/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package channeldb

import (
"bytes"
"io/ioutil"
"math/rand"
"net"
"os"
"reflect"
"runtime"
"testing"
Expand Down Expand Up @@ -86,40 +84,6 @@ var (
}
)

// makeTestDB creates a new instance of the ChannelDB for testing purposes. A
// callback which cleans up the created temporary directories is also returned
// and intended to be executed after the test completes.
func makeTestDB() (*DB, func(), error) {
// First, create a temporary directory to be used for the duration of
// this test.
tempDirName, err := ioutil.TempDir("", "channeldb")
if err != nil {
return nil, nil, err
}

// Next, create channeldb for the first time.
backend, backendCleanup, err := kvdb.GetTestBackend(tempDirName, "cdb")
if err != nil {
backendCleanup()
return nil, nil, err
}

cdb, err := CreateWithBackend(backend, OptionClock(testClock))
if err != nil {
backendCleanup()
os.RemoveAll(tempDirName)
return nil, nil, err
}

cleanUp := func() {
cdb.Close()
backendCleanup()
os.RemoveAll(tempDirName)
}

return cdb, cleanUp, nil
}

// testChannelParams is a struct which details the specifics of how a channel
// should be created.
type testChannelParams struct {
Expand Down Expand Up @@ -403,7 +367,7 @@ func createTestChannelState(t *testing.T, cdb *DB) *OpenChannel {
func TestOpenChannelPutGetDelete(t *testing.T) {
t.Parallel()

cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
Expand Down Expand Up @@ -552,7 +516,7 @@ func TestOptionalShutdown(t *testing.T) {
test := test

t.Run(test.name, func(t *testing.T) {
cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
Expand Down Expand Up @@ -609,7 +573,7 @@ func assertCommitmentEqual(t *testing.T, a, b *ChannelCommitment) {
func TestChannelStateTransition(t *testing.T) {
t.Parallel()

cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
Expand Down Expand Up @@ -914,7 +878,7 @@ func TestChannelStateTransition(t *testing.T) {
func TestFetchPendingChannels(t *testing.T) {
t.Parallel()

cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
Expand Down Expand Up @@ -993,7 +957,7 @@ func TestFetchPendingChannels(t *testing.T) {
func TestFetchClosedChannels(t *testing.T) {
t.Parallel()

cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
Expand Down Expand Up @@ -1084,7 +1048,7 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
// We'll start by creating two channels within our test database. One of
// them will have their funding transaction confirmed on-chain, while
// the other one will remain unconfirmed.
db, cleanUp, err := makeTestDB()
db, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
Expand Down Expand Up @@ -1199,7 +1163,7 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
func TestRefreshShortChanID(t *testing.T) {
t.Parallel()

cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
Expand Down Expand Up @@ -1347,7 +1311,7 @@ func TestCloseInitiator(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v",
err)
Expand Down Expand Up @@ -1392,7 +1356,7 @@ func TestCloseInitiator(t *testing.T) {
// TestCloseChannelStatus tests setting of a channel status on the historical
// channel on channel close.
func TestCloseChannelStatus(t *testing.T) {
cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v",
err)
Expand Down Expand Up @@ -1538,7 +1502,7 @@ func TestBalanceAtHeight(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v",
err)
Expand Down
35 changes: 35 additions & 0 deletions channeldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"net"
"os"

Expand Down Expand Up @@ -1260,3 +1261,37 @@ func (db *DB) FetchHistoricalChannel(outPoint *wire.OutPoint) (*OpenChannel, err

return channel, nil
}

// MakeTestDB creates a new instance of the ChannelDB for testing purposes.
// A callback which cleans up the created temporary directories is also
// returned and intended to be executed after the test completes.
func MakeTestDB(modifiers ...OptionModifier) (*DB, func(), error) {
// First, create a temporary directory to be used for the duration of
// this test.
tempDirName, err := ioutil.TempDir("", "channeldb")
if err != nil {
return nil, nil, err
}

// Next, create channeldb for the first time.
backend, backendCleanup, err := kvdb.GetTestBackend(tempDirName, "cdb")
if err != nil {
backendCleanup()
return nil, nil, err
}

cdb, err := CreateWithBackend(backend, modifiers...)
if err != nil {
backendCleanup()
os.RemoveAll(tempDirName)
return nil, nil, err
}

cleanUp := func() {
cdb.Close()
backendCleanup()
os.RemoveAll(tempDirName)
}

return cdb, cleanUp, nil
}
14 changes: 7 additions & 7 deletions channeldb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestFetchClosedChannelForID(t *testing.T) {

const numChans = 101

cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestFetchClosedChannelForID(t *testing.T) {
func TestAddrsForNode(t *testing.T) {
t.Parallel()

cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestAddrsForNode(t *testing.T) {
func TestFetchChannel(t *testing.T) {
t.Parallel()

cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
Expand Down Expand Up @@ -351,7 +351,7 @@ func genRandomChannelShell() (*ChannelShell, error) {
func TestRestoreChannelShells(t *testing.T) {
t.Parallel()

cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
Expand Down Expand Up @@ -445,7 +445,7 @@ func TestRestoreChannelShells(t *testing.T) {
func TestAbandonChannel(t *testing.T) {
t.Parallel()

cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
Expand Down Expand Up @@ -618,7 +618,7 @@ func TestFetchChannels(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test "+
"database: %v", err)
Expand Down Expand Up @@ -687,7 +687,7 @@ func TestFetchChannels(t *testing.T) {

// TestFetchHistoricalChannel tests lookup of historical channels.
func TestFetchHistoricalChannel(t *testing.T) {
cdb, cleanUp, err := makeTestDB()
cdb, cleanUp, err := MakeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions channeldb/forwarding_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestForwardingLogBasicStorageAndQuery(t *testing.T) {
// First, we'll set up a test database, and use that to instantiate the
// forwarding event log that we'll be using for the duration of the
// test.
db, cleanUp, err := makeTestDB()
db, cleanUp, err := MakeTestDB()
defer cleanUp()
if err != nil {
t.Fatalf("unable to make test db: %v", err)
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestForwardingLogQueryOptions(t *testing.T) {
// First, we'll set up a test database, and use that to instantiate the
// forwarding event log that we'll be using for the duration of the
// test.
db, cleanUp, err := makeTestDB()
db, cleanUp, err := MakeTestDB()
defer cleanUp()
if err != nil {
t.Fatalf("unable to make test db: %v", err)
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestForwardingLogQueryLimit(t *testing.T) {
// First, we'll set up a test database, and use that to instantiate the
// forwarding event log that we'll be using for the duration of the
// test.
db, cleanUp, err := makeTestDB()
db, cleanUp, err := MakeTestDB()
defer cleanUp()
if err != nil {
t.Fatalf("unable to make test db: %v", err)
Expand Down

0 comments on commit f144dea

Please sign in to comment.