2014-01-09 06:54:52 +01:00
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
2013-10-13 08:59:55 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package memdb_test
|
|
|
|
|
|
|
|
import (
|
2014-07-03 02:47:24 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2015-02-06 06:18:27 +01:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2015-01-27 18:45:10 +01:00
|
|
|
"github.com/btcsuite/btcd/database"
|
|
|
|
"github.com/btcsuite/btcd/database/memdb"
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-01-15 17:33:06 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2013-10-13 08:59:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestClosed ensure calling the interface functions on a closed database
|
|
|
|
// returns appropriate errors for the interface functions that return errors
|
|
|
|
// and does not panic or otherwise misbehave for functions which do not return
|
|
|
|
// errors.
|
|
|
|
func TestClosed(t *testing.T) {
|
2015-01-27 18:45:10 +01:00
|
|
|
db, err := database.CreateDB("memdb")
|
2013-10-13 08:59:55 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to open test database %v", err)
|
|
|
|
return
|
|
|
|
}
|
2015-02-06 06:18:27 +01:00
|
|
|
_, err = db.InsertBlock(btcutil.NewBlock(chaincfg.MainNetParams.GenesisBlock))
|
2013-10-13 08:59:55 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("InsertBlock: %v", err)
|
|
|
|
}
|
2014-07-07 16:50:50 +02:00
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Errorf("Close: unexpected error %v", err)
|
|
|
|
}
|
2013-10-13 08:59:55 +02:00
|
|
|
|
2015-02-06 06:18:27 +01:00
|
|
|
genesisHash := chaincfg.MainNetParams.GenesisHash
|
2013-10-13 08:59:55 +02:00
|
|
|
if err := db.DropAfterBlockBySha(genesisHash); err != memdb.ErrDbClosed {
|
|
|
|
t.Errorf("DropAfterBlockBySha: unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-07 16:50:50 +02:00
|
|
|
if _, err := db.ExistsSha(genesisHash); err != memdb.ErrDbClosed {
|
|
|
|
t.Errorf("ExistsSha: Unexpected error: %v", err)
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := db.FetchBlockBySha(genesisHash); err != memdb.ErrDbClosed {
|
|
|
|
t.Errorf("FetchBlockBySha: unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := db.FetchBlockShaByHeight(0); err != memdb.ErrDbClosed {
|
|
|
|
t.Errorf("FetchBlockShaByHeight: unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := db.FetchHeightRange(0, 1); err != memdb.ErrDbClosed {
|
|
|
|
t.Errorf("FetchHeightRange: unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
2015-02-06 06:18:27 +01:00
|
|
|
genesisCoinbaseTx := chaincfg.MainNetParams.GenesisBlock.Transactions[0]
|
2015-04-17 08:09:21 +02:00
|
|
|
coinbaseHash := genesisCoinbaseTx.TxSha()
|
2014-07-07 16:50:50 +02:00
|
|
|
if _, err := db.ExistsTxSha(&coinbaseHash); err != memdb.ErrDbClosed {
|
|
|
|
t.Errorf("ExistsTxSha: unexpected error %v", err)
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := db.FetchTxBySha(genesisHash); err != memdb.ErrDbClosed {
|
|
|
|
t.Errorf("FetchTxBySha: unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
requestHashes := []*wire.ShaHash{genesisHash}
|
2013-10-13 08:59:55 +02:00
|
|
|
reply := db.FetchTxByShaList(requestHashes)
|
|
|
|
if len(reply) != len(requestHashes) {
|
|
|
|
t.Errorf("FetchUnSpentTxByShaList unexpected number of replies "+
|
|
|
|
"got: %d, want: %d", len(reply), len(requestHashes))
|
|
|
|
}
|
|
|
|
for i, txLR := range reply {
|
2015-01-27 18:45:10 +01:00
|
|
|
wantReply := &database.TxListReply{
|
2013-10-13 08:59:55 +02:00
|
|
|
Sha: requestHashes[i],
|
|
|
|
Err: memdb.ErrDbClosed,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(wantReply, txLR) {
|
|
|
|
t.Errorf("FetchTxByShaList unexpected reply\ngot: %v\n"+
|
|
|
|
"want: %v", txLR, wantReply)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = db.FetchUnSpentTxByShaList(requestHashes)
|
|
|
|
if len(reply) != len(requestHashes) {
|
|
|
|
t.Errorf("FetchUnSpentTxByShaList unexpected number of replies "+
|
|
|
|
"got: %d, want: %d", len(reply), len(requestHashes))
|
|
|
|
}
|
|
|
|
for i, txLR := range reply {
|
2015-01-27 18:45:10 +01:00
|
|
|
wantReply := &database.TxListReply{
|
2013-10-13 08:59:55 +02:00
|
|
|
Sha: requestHashes[i],
|
|
|
|
Err: memdb.ErrDbClosed,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(wantReply, txLR) {
|
|
|
|
t.Errorf("FetchUnSpentTxByShaList unexpected reply\n"+
|
|
|
|
"got: %v\nwant: %v", txLR, wantReply)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, _, err := db.NewestSha(); err != memdb.ErrDbClosed {
|
|
|
|
t.Errorf("NewestSha: unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-07 16:50:50 +02:00
|
|
|
if err := db.Sync(); err != memdb.ErrDbClosed {
|
|
|
|
t.Errorf("Sync: unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.RollbackClose(); err != memdb.ErrDbClosed {
|
|
|
|
t.Errorf("RollbackClose: unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.Close(); err != memdb.ErrDbClosed {
|
|
|
|
t.Errorf("Close: unexpected error %v", err)
|
|
|
|
}
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|