From 5882e54bf3cf7ed7895fda34f7a11bc53de624d9 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Tue, 28 May 2019 11:03:48 +0200 Subject: [PATCH] route test: add a simple unit test for the TotalFees method Checks fees on 0, 1, and 2-hop routes. --- routing/route/route_test.go | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 routing/route/route_test.go diff --git a/routing/route/route_test.go b/routing/route/route_test.go new file mode 100644 index 00000000..92b0ee0d --- /dev/null +++ b/routing/route/route_test.go @@ -0,0 +1,58 @@ +package route + +import ( + "testing" + + "github.com/lightningnetwork/lnd/lnwire" +) + +// TestRouteTotalFees checks that a route reports the expected total fee. +func TestRouteTotalFees(t *testing.T) { + t.Parallel() + + // Make sure empty route returns a 0 fee. + r := &Route{} + if r.TotalFees() != 0 { + t.Fatalf("expected 0 fees, got %v", r.TotalFees()) + } + + // For one-hop routes the fee should be 0, since the last node will + // receive the full amount. + amt := lnwire.MilliSatoshi(1000) + hops := []*Hop{ + { + PubKeyBytes: Vertex{}, + ChannelID: 1, + OutgoingTimeLock: 44, + AmtToForward: amt, + }, + } + r, err := NewRouteFromHops(amt, 100, Vertex{}, hops) + if err != nil { + t.Fatal(err) + } + + if r.TotalFees() != 0 { + t.Fatalf("expected 0 fees, got %v", r.TotalFees()) + } + + // Append the route with a node, making the first one take a fee. + fee := lnwire.MilliSatoshi(100) + hops = append(hops, &Hop{ + PubKeyBytes: Vertex{}, + ChannelID: 2, + OutgoingTimeLock: 33, + AmtToForward: amt - fee, + }, + ) + + r, err = NewRouteFromHops(amt, 100, Vertex{}, hops) + if err != nil { + t.Fatal(err) + } + + if r.TotalFees() != fee { + t.Fatalf("expected %v fees, got %v", fee, r.TotalFees()) + } + +}