chanfitness: define and export ErrChannelNotFound

This commit is contained in:
carla 2019-12-17 17:36:21 +02:00
parent 46a994350c
commit ec12463a95
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91
2 changed files with 42 additions and 39 deletions

@ -12,7 +12,6 @@ package chanfitness
import ( import (
"errors" "errors"
"fmt"
"sync" "sync"
"time" "time"
@ -23,9 +22,15 @@ import (
"github.com/lightningnetwork/lnd/subscribe" "github.com/lightningnetwork/lnd/subscribe"
) )
// errShuttingDown is returned when the store cannot respond to a query because var (
// it has received the shutdown signal. // errShuttingDown is returned when the store cannot respond to a query because
var errShuttingDown = errors.New("channel event store shutting down") // 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 // ChannelEventStore maintains a set of event logs for the node's channels to
// provide insight into the performance and health of channels. // 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] channel, ok := c.channels[req.channelID]
if !ok { if !ok {
resp.err = fmt.Errorf("channel %v not found", req.channelID) resp.err = ErrChannelNotFound
} else { } else {
resp.start = channel.openedAt resp.start = channel.openedAt
resp.end = channel.closedAt resp.end = channel.closedAt
@ -312,7 +317,7 @@ func (c *ChannelEventStore) consume(subscriptions *subscriptions) {
channel, ok := c.channels[req.channelID] channel, ok := c.channels[req.channelID]
if !ok { if !ok {
resp.err = fmt.Errorf("channel %v not found", req.channelID) resp.err = ErrChannelNotFound
} else { } else {
uptime, err := channel.uptime(req.startTime, req.endTime) uptime, err := channel.uptime(req.startTime, req.endTime)
resp.uptime = uptime resp.uptime = uptime

@ -263,23 +263,23 @@ func TestGetLifetime(t *testing.T) {
now := time.Now() now := time.Now()
tests := []struct { tests := []struct {
name string name string
channelFound bool channelFound bool
chanID uint64 chanID uint64
opened time.Time opened time.Time
closed time.Time closed time.Time
expectErr bool expectedError error
}{ }{
{ {
name: "Channel found", name: "Channel found",
channelFound: true, channelFound: true,
opened: now, opened: now,
closed: now.Add(time.Hour * -1), closed: now.Add(time.Hour * -1),
expectErr: false, expectedError: nil,
}, },
{ {
name: "Channel not found", name: "Channel not found",
expectErr: true, expectedError: ErrChannelNotFound,
}, },
} }
@ -311,11 +311,8 @@ func TestGetLifetime(t *testing.T) {
} }
open, close, err := store.GetLifespan(test.chanID) open, close, err := store.GetLifespan(test.chanID)
if test.expectErr && err == nil { if test.expectedError != err {
t.Fatal("Expected an error, got nil") t.Fatalf("Expected: %v, got: %v", test.expectedError, err)
}
if !test.expectErr && err != nil {
t.Fatalf("Expected no error, got: %v", err)
} }
if open != test.opened { if open != test.opened {
@ -367,13 +364,15 @@ func TestGetUptime(t *testing.T) {
endTime time.Time endTime time.Time
expectedUptime time.Duration expectedUptime time.Duration
expectErr bool
expectedError error
}{ }{
{ {
name: "No events", name: "No events",
startTime: twoHoursAgo, startTime: twoHoursAgo,
endTime: now, endTime: now,
channelFound: true, channelFound: true,
expectedError: nil,
}, },
{ {
name: "50% Uptime", name: "50% Uptime",
@ -392,6 +391,7 @@ func TestGetUptime(t *testing.T) {
startTime: fourHoursAgo, startTime: fourHoursAgo,
endTime: now, endTime: now,
channelFound: true, channelFound: true,
expectedError: nil,
}, },
{ {
name: "Zero start time", name: "Zero start time",
@ -405,13 +405,14 @@ func TestGetUptime(t *testing.T) {
expectedUptime: time.Hour * 4, expectedUptime: time.Hour * 4,
endTime: now, endTime: now,
channelFound: true, channelFound: true,
expectedError: nil,
}, },
{ {
name: "Channel not found", name: "Channel not found",
startTime: twoHoursAgo, startTime: twoHoursAgo,
endTime: now, endTime: now,
channelFound: false, channelFound: false,
expectErr: true, expectedError: ErrChannelNotFound,
}, },
} }
@ -445,11 +446,8 @@ func TestGetUptime(t *testing.T) {
} }
uptime, err := store.GetUptime(test.chanID, test.startTime, test.endTime) uptime, err := store.GetUptime(test.chanID, test.startTime, test.endTime)
if test.expectErr && err == nil { if test.expectedError != err {
t.Fatal("Expected an error, got nil") t.Fatalf("Expected: %v, got: %v", test.expectedError, err)
}
if !test.expectErr && err != nil {
t.Fatalf("Expcted no error, got: %v", err)
} }
if uptime != test.expectedUptime { if uptime != test.expectedUptime {