From dd3abbc4ef79af68b10302b01cdc827c6d7a8e74 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 7 Jun 2019 16:42:26 +0200 Subject: [PATCH] htlcswitch/payment_result_test: add TestNetworkResultStore TestNetworkResultStore tests that the networkResult store behaves as expected, and that we can store, get and subscribe to results. --- htlcswitch/payment_result_test.go | 102 ++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/htlcswitch/payment_result_test.go b/htlcswitch/payment_result_test.go index 4b45bc9a..9683f405 100644 --- a/htlcswitch/payment_result_test.go +++ b/htlcswitch/payment_result_test.go @@ -2,11 +2,14 @@ package htlcswitch import ( "bytes" + "io/ioutil" "math/rand" "reflect" "testing" + "time" "github.com/davecgh/go-spew/spew" + "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lnwire" ) @@ -88,3 +91,102 @@ func TestNetworkResultSerialization(t *testing.T) { } } } + +// TestNetworkResultStore tests that the networkResult store behaves as +// expected, and that we can store, get and subscribe to results. +func TestNetworkResultStore(t *testing.T) { + t.Parallel() + + const numResults = 4 + + tempDir, err := ioutil.TempDir("", "testdb") + db, err := channeldb.Open(tempDir) + if err != nil { + t.Fatal(err) + } + + store := newNetworkResultStore(db) + + var results []*networkResult + for i := 0; i < numResults; i++ { + n := &networkResult{ + msg: &lnwire.UpdateAddHTLC{}, + unencrypted: true, + isResolution: true, + } + results = append(results, n) + } + + // Subscribe to 2 of them. + var subs []<-chan *networkResult + for i := uint64(0); i < 2; i++ { + sub, err := store.subscribeResult(i) + if err != nil { + t.Fatalf("unable to subscribe: %v", err) + } + subs = append(subs, sub) + } + + // Store three of them. + for i := uint64(0); i < 3; i++ { + err := store.storeResult(i, results[i]) + if err != nil { + t.Fatalf("unable to store result: %v", err) + } + } + + // The two subscribers should be notified. + for _, sub := range subs { + select { + case <-sub: + case <-time.After(1 * time.Second): + t.Fatalf("no result received") + } + } + + // Let the third one subscribe now. THe result should be received + // immediately. + sub, err := store.subscribeResult(2) + if err != nil { + t.Fatalf("unable to subscribe: %v", err) + } + select { + case <-sub: + case <-time.After(1 * time.Second): + t.Fatalf("no result received") + } + + // Try fetching the result directly for the non-stored one. This should + // fail. + _, err = store.getResult(3) + if err != ErrPaymentIDNotFound { + t.Fatalf("expected ErrPaymentIDNotFound, got %v", err) + } + + // Add the result and try again. + err = store.storeResult(3, results[3]) + if err != nil { + t.Fatalf("unable to store result: %v", err) + } + + _, err = store.getResult(3) + if err != nil { + t.Fatalf("unable to get result: %v", err) + } + + // Since we don't delete results from the store (yet), make sure we + // will get subscriptions for all of them. + // TODO(halseth): check deletion when we have reliable handoff. + for i := uint64(0); i < numResults; i++ { + sub, err := store.subscribeResult(i) + if err != nil { + t.Fatalf("unable to subscribe: %v", err) + } + + select { + case <-sub: + case <-time.After(1 * time.Second): + t.Fatalf("no result received") + } + } +}