From ec12463a950d77216e878812222d19ed5061f926 Mon Sep 17 00:00:00 2001 From: carla Date: Tue, 17 Dec 2019 17:36:21 +0200 Subject: [PATCH] chanfitness: define and export ErrChannelNotFound --- chanfitness/chaneventstore.go | 17 +++++--- chanfitness/chaneventstore_test.go | 64 +++++++++++++++--------------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/chanfitness/chaneventstore.go b/chanfitness/chaneventstore.go index a4339765..31ac2d56 100644 --- a/chanfitness/chaneventstore.go +++ b/chanfitness/chaneventstore.go @@ -12,7 +12,6 @@ package chanfitness import ( "errors" - "fmt" "sync" "time" @@ -23,9 +22,15 @@ import ( "github.com/lightningnetwork/lnd/subscribe" ) -// errShuttingDown is returned when the store cannot respond to a query because -// it has received the shutdown signal. -var errShuttingDown = errors.New("channel event store shutting down") +var ( + // errShuttingDown is returned when the store cannot respond to a query because + // it has received the shutdown signal. + errShuttingDown = errors.New("channel event store shutting down") + + // ErrChannelNotFound is returned when a query is made for a channel that + // the event store does not have knowledge of. + ErrChannelNotFound = errors.New("channel not found in event store") +) // ChannelEventStore maintains a set of event logs for the node's channels to // provide insight into the performance and health of channels. @@ -298,7 +303,7 @@ func (c *ChannelEventStore) consume(subscriptions *subscriptions) { channel, ok := c.channels[req.channelID] if !ok { - resp.err = fmt.Errorf("channel %v not found", req.channelID) + resp.err = ErrChannelNotFound } else { resp.start = channel.openedAt resp.end = channel.closedAt @@ -312,7 +317,7 @@ func (c *ChannelEventStore) consume(subscriptions *subscriptions) { channel, ok := c.channels[req.channelID] if !ok { - resp.err = fmt.Errorf("channel %v not found", req.channelID) + resp.err = ErrChannelNotFound } else { uptime, err := channel.uptime(req.startTime, req.endTime) resp.uptime = uptime diff --git a/chanfitness/chaneventstore_test.go b/chanfitness/chaneventstore_test.go index a30f7d15..6bce3c8c 100644 --- a/chanfitness/chaneventstore_test.go +++ b/chanfitness/chaneventstore_test.go @@ -263,23 +263,23 @@ func TestGetLifetime(t *testing.T) { now := time.Now() tests := []struct { - name string - channelFound bool - chanID uint64 - opened time.Time - closed time.Time - expectErr bool + name string + channelFound bool + chanID uint64 + opened time.Time + closed time.Time + expectedError error }{ { - name: "Channel found", - channelFound: true, - opened: now, - closed: now.Add(time.Hour * -1), - expectErr: false, + name: "Channel found", + channelFound: true, + opened: now, + closed: now.Add(time.Hour * -1), + expectedError: nil, }, { - name: "Channel not found", - expectErr: true, + name: "Channel not found", + expectedError: ErrChannelNotFound, }, } @@ -311,11 +311,8 @@ func TestGetLifetime(t *testing.T) { } open, close, err := store.GetLifespan(test.chanID) - if test.expectErr && err == nil { - t.Fatal("Expected an error, got nil") - } - if !test.expectErr && err != nil { - t.Fatalf("Expected no error, got: %v", err) + if test.expectedError != err { + t.Fatalf("Expected: %v, got: %v", test.expectedError, err) } if open != test.opened { @@ -367,13 +364,15 @@ func TestGetUptime(t *testing.T) { endTime time.Time expectedUptime time.Duration - expectErr bool + + expectedError error }{ { - name: "No events", - startTime: twoHoursAgo, - endTime: now, - channelFound: true, + name: "No events", + startTime: twoHoursAgo, + endTime: now, + channelFound: true, + expectedError: nil, }, { name: "50% Uptime", @@ -392,6 +391,7 @@ func TestGetUptime(t *testing.T) { startTime: fourHoursAgo, endTime: now, channelFound: true, + expectedError: nil, }, { name: "Zero start time", @@ -405,13 +405,14 @@ func TestGetUptime(t *testing.T) { expectedUptime: time.Hour * 4, endTime: now, channelFound: true, + expectedError: nil, }, { - name: "Channel not found", - startTime: twoHoursAgo, - endTime: now, - channelFound: false, - expectErr: true, + name: "Channel not found", + startTime: twoHoursAgo, + endTime: now, + channelFound: false, + expectedError: ErrChannelNotFound, }, } @@ -445,11 +446,8 @@ func TestGetUptime(t *testing.T) { } uptime, err := store.GetUptime(test.chanID, test.startTime, test.endTime) - if test.expectErr && err == nil { - t.Fatal("Expected an error, got nil") - } - if !test.expectErr && err != nil { - t.Fatalf("Expcted no error, got: %v", err) + if test.expectedError != err { + t.Fatalf("Expected: %v, got: %v", test.expectedError, err) } if uptime != test.expectedUptime {