lnd/channeldb/waitingproof_test.go
Andras Banki-Horvath 2a358327f4
multi: add reset closure to kvdb.View
This commit adds a reset() closure to the kvdb.View function which will
be called before each retry (including the first) of the view
transaction. The reset() closure can be used to reset external state
(eg slices or maps) where the view closure puts intermediate results.
2020-11-05 17:57:12 +01:00

60 lines
1.3 KiB
Go

package channeldb
import (
"testing"
"reflect"
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/lnwire"
)
// TestWaitingProofStore tests add/get/remove functions of the waiting proof
// storage.
func TestWaitingProofStore(t *testing.T) {
t.Parallel()
db, cleanup, err := MakeTestDB()
if err != nil {
t.Fatalf("failed to make test database: %s", err)
}
defer cleanup()
proof1 := NewWaitingProof(true, &lnwire.AnnounceSignatures{
NodeSignature: wireSig,
BitcoinSignature: wireSig,
})
store, err := NewWaitingProofStore(db)
if err != nil {
t.Fatalf("unable to create the waiting proofs storage: %v",
err)
}
if err := store.Add(proof1); err != nil {
t.Fatalf("unable add proof to storage: %v", err)
}
proof2, err := store.Get(proof1.Key())
if err != nil {
t.Fatalf("unable retrieve proof from storage: %v", err)
}
if !reflect.DeepEqual(proof1, proof2) {
t.Fatal("wrong proof retrieved")
}
if _, err := store.Get(proof1.OppositeKey()); err != ErrWaitingProofNotFound {
t.Fatalf("proof shouldn't be found: %v", err)
}
if err := store.Remove(proof1.Key()); err != nil {
t.Fatalf("unable remove proof from storage: %v", err)
}
if err := store.ForAll(func(proof *WaitingProof) error {
return errors.New("storage should be empty")
}, func() {}); err != nil && err != ErrWaitingProofNotFound {
t.Fatal(err)
}
}