@ -19,14 +19,15 @@ import (
// functions fail. It does not test the mechanics of consuming events because
// these are covered in a separate set of tests.
func TestStartStoreError ( t * testing . T ) {
// Ok and erroring subscribe functions are defined here to de-clutter tests.
// Ok and erroring subscribe functions are defined here to de-clutter
// tests.
okSubscribeFunc := func ( ) ( * subscribe . Client , error ) {
return & subscribe . Client {
Cancel : func ( ) { } ,
} , nil
}
errSubscribeFunc := func ( ) ( client * subscribe . Client , e error ) {
errSubscribeFunc := func ( ) ( * subscribe . Client , error ) {
return nil , errors . New ( "intentional test err" )
}
@ -49,7 +50,7 @@ func TestStartStoreError(t *testing.T) {
name : "Get open channels fails" ,
ChannelEvents : okSubscribeFunc ,
PeerEvents : okSubscribeFunc ,
GetChannels : func ( ) ( channels [ ] * channeldb . OpenChannel , e error ) {
GetChannels : func ( ) ( [ ] * channeldb . OpenChannel , error ) {
return nil , errors . New ( "intentional test err" )
} ,
} ,
@ -66,8 +67,8 @@ func TestStartStoreError(t *testing.T) {
} )
err := store . Start ( )
// Check that we receive an error, because the test only checks for
// error cases.
// Check that we receive an error, because the test only
// checks for error cases.
if err == nil {
t . Fatalf ( "Expected error on startup, got: nil" )
}
@ -110,19 +111,22 @@ func TestMonitorChannelEvents(t *testing.T) {
tests := [ ] struct {
name string
// generateEvents takes channels which represent the updates channels
// for subscription clients and passes events in the desired order.
// This function is intended to be blocking so that the test does not
// have a data race with event consumption, so the channels should not
// be buffered.
generateEvents func ( channelEvents , peerEvents chan <- interface { } )
// generateEvents takes channels which represent the updates
// channels for subscription clients and passes events in the
// desired order. This function is intended to be blocking so
// that the test does not have a data race with event
// consumption, so the channels should not be buffered.
generateEvents func ( channelEvents ,
peerEvents chan <- interface { } )
// expectedEvents is the expected set of event types in the store.
expectedEvents [ ] eventType
} {
{
name : "Channel opened, peer comes online" ,
generateEvents : func ( channelEvents , peerEvents chan <- interface { } ) {
generateEvents : func ( channelEvents ,
peerEvents chan <- interface { } ) {
// Add an open channel event
channelEvents <- channelnotifier . OpenChannelEvent {
Channel : & channeldb . OpenChannel {
@ -132,13 +136,17 @@ func TestMonitorChannelEvents(t *testing.T) {
}
// Add a peer online event.
peerEvents <- peernotifier . PeerOnlineEvent { PubKey : vertex }
peerEvents <- peernotifier . PeerOnlineEvent {
PubKey : vertex ,
}
} ,
expectedEvents : [ ] eventType { peerOnlineEvent } ,
} ,
{
name : "Duplicate channel open events" ,
generateEvents : func ( channelEvents , peerEvents chan <- interface { } ) {
generateEvents : func ( channelEvents ,
peerEvents chan <- interface { } ) {
// Add an open channel event
channelEvents <- channelnotifier . OpenChannelEvent {
Channel : & channeldb . OpenChannel {
@ -148,7 +156,9 @@ func TestMonitorChannelEvents(t *testing.T) {
}
// Add a peer online event.
peerEvents <- peernotifier . PeerOnlineEvent { PubKey : vertex }
peerEvents <- peernotifier . PeerOnlineEvent {
PubKey : vertex ,
}
// Add a duplicate channel open event.
channelEvents <- channelnotifier . OpenChannelEvent {
@ -162,9 +172,13 @@ func TestMonitorChannelEvents(t *testing.T) {
} ,
{
name : "Channel opened, peer already online" ,
generateEvents : func ( channelEvents , peerEvents chan <- interface { } ) {
generateEvents : func ( channelEvents ,
peerEvents chan <- interface { } ) {
// Add a peer online event.
peerEvents <- peernotifier . PeerOnlineEvent { PubKey : vertex }
peerEvents <- peernotifier . PeerOnlineEvent {
PubKey : vertex ,
}
// Add an open channel event
channelEvents <- channelnotifier . OpenChannelEvent {
@ -179,7 +193,9 @@ func TestMonitorChannelEvents(t *testing.T) {
{
name : "Channel opened, peer offline, closed" ,
generateEvents : func ( channelEvents , peerEvents chan <- interface { } ) {
generateEvents : func ( channelEvents ,
peerEvents chan <- interface { } ) {
// Add an open channel event
channelEvents <- channelnotifier . OpenChannelEvent {
Channel : & channeldb . OpenChannel {
@ -189,7 +205,9 @@ func TestMonitorChannelEvents(t *testing.T) {
}
// Add a peer online event.
peerEvents <- peernotifier . PeerOfflineEvent { PubKey : vertex }
peerEvents <- peernotifier . PeerOfflineEvent {
PubKey : vertex ,
}
// Add a close channel event.
channelEvents <- channelnotifier . ClosedChannelEvent {
@ -202,7 +220,9 @@ func TestMonitorChannelEvents(t *testing.T) {
} ,
{
name : "Event after channel close not recorded" ,
generateEvents : func ( channelEvents , peerEvents chan <- interface { } ) {
generateEvents : func ( channelEvents ,
peerEvents chan <- interface { } ) {
// Add an open channel event
channelEvents <- channelnotifier . OpenChannelEvent {
Channel : & channeldb . OpenChannel {
@ -219,7 +239,9 @@ func TestMonitorChannelEvents(t *testing.T) {
}
// Add a peer online event.
peerEvents <- peernotifier . PeerOfflineEvent { PubKey : vertex }
peerEvents <- peernotifier . PeerOfflineEvent {
PubKey : vertex ,
}
} ,
} ,
}
@ -228,12 +250,12 @@ func TestMonitorChannelEvents(t *testing.T) {
test := test
t . Run ( test . name , func ( t * testing . T ) {
// Create a store with the channels and online peers specified
// by the test.
// Create a store with the channels and online peers
// specified by the test.
store := NewChannelEventStore ( & Config { } )
// Create channels which represent the subscriptions we have to peer
// and client events.
// Create channels which represent the subscriptions
// we have to peer and client events.
channelEvents := make ( chan interface { } )
peerEvents := make ( chan interface { } )
@ -244,21 +266,23 @@ func TestMonitorChannelEvents(t *testing.T) {
cancel : func ( ) { } ,
} )
// Add events to the store then kill the goroutine using store.Stop.
// Add events to the store then kill the goroutine using
// store.Stop.
test . generateEvents ( channelEvents , peerEvents )
store . Stop ( )
// Retrieve the eventLog for the channel and check that its
// contents are as expected.
// Retrieve the eventLog for the channel and check that
// its contents are as expected.
eventLog , ok := store . channels [ chanPoint ]
if ! ok {
t . Fatalf ( "Expected to find event store" )
}
for i , e := range eventLog . events {
if test . expectedEvents [ i ] != e . eventType {
expectedType := test . expectedEvents [ i ]
if expectedType != e . eventType {
t . Fatalf ( "Expected type: %v, got: %v" ,
test . expectedEvents [ i ] , e . eventType )
expectedType , e . eventType )
}
}
} )
@ -309,26 +333,30 @@ func TestGetLifetime(t *testing.T) {
// Stop the store's go routine.
defer store . Stop ( )
// Add channel to eventStore if the test indicates that it should
// be present.
// Add channel to eventStore if the test indicates that
// it should be present.
if test . channelFound {
store . channels [ test . channelPoint ] = & chanEventLog {
openedAt : test . opened ,
closedAt : test . closed ,
}
store . channels [ test . channelPoint ] =
& chanEventLog {
openedAt : test . opened ,
closedAt : test . closed ,
}
}
open , close , err := store . GetLifespan ( test . channelPoint )
if test . expectedError != err {
t . Fatalf ( "Expected: %v, got: %v" , test . expectedError , err )
t . Fatalf ( "Expected: %v, got: %v" ,
test . expectedError , err )
}
if open != test . opened {
t . Errorf ( "Expected: %v, got %v" , test . opened , open )
t . Errorf ( "Expected: %v, got %v" ,
test . opened , open )
}
if close != test . closed {
t . Errorf ( "Expected: %v, got %v" , test . closed , close )
t . Errorf ( "Expected: %v, got %v" ,
test . closed , close )
}
} )
}
@ -351,24 +379,28 @@ func TestGetUptime(t *testing.T) {
channelPoint wire . OutPoint
// events is the set of events we expect to find in the channel store.
// events is the set of events we expect to find in the channel
// store.
events [ ] * channelEvent
// openedAt is the time the channel is recorded as open by the store.
// openedAt is the time the channel is recorded as open by the
// store.
openedAt time . Time
// closedAt is the time the channel is recorded as closed by the store.
// If the channel is still open, this value is zero.
// closedAt is the time the channel is recorded as closed by the
// store. If the channel is still open, this value is zero.
closedAt time . Time
// channelFound is true if we expect to find the channel in the store.
// channelFound is true if we expect to find the channel in the
// store.
channelFound bool
// startTime specifies the beginning of the uptime range we want to
// calculate.
// startTime specifies the beginning of the uptime range we want
// to calculate.
startTime time . Time
// endTime specified the end of the uptime range we want to calculate.
// endTime specified the end of the uptime range we want to
// calculate.
endTime time . Time
expectedUptime time . Duration
@ -428,8 +460,8 @@ func TestGetUptime(t *testing.T) {
test := test
t . Run ( test . name , func ( t * testing . T ) {
// Set up event store with the events specified for the test and
// mocked time.
// Set up event store with the events specified for the
// test and mocked time.
store := NewChannelEventStore ( & Config { } )
// Start goroutine which consumes GetUptime requests.
@ -443,7 +475,8 @@ func TestGetUptime(t *testing.T) {
// Stop the store's goroutine.
defer store . Stop ( )
// Add the channel to the store if it is intended to be found.
// Add the channel to the store if it is intended to be
// found.
if test . channelFound {
store . channels [ test . channelPoint ] = & chanEventLog {
events : test . events ,
@ -453,16 +486,18 @@ func TestGetUptime(t *testing.T) {
}
}
uptime , err := store . GetUptime ( test . channelPoint , test . startTime , test . endTime )
uptime , err := store . GetUptime (
test . channelPoint , test . startTime , test . endTime ,
)
if test . expectedError != err {
t . Fatalf ( "Expected: %v, got: %v" , test . expectedError , err )
t . Fatalf ( "Expected: %v, got: %v" ,
test . expectedError , err )
}
if uptime != test . expectedUptime {
t . Fatalf ( "Expected uptime percentage: %v, got %v" ,
test . expectedUptime , uptime )
t . Fatalf ( "Expected uptime percentage: %v, " +
"got %v" , test . expectedUptime , uptime )
}
} )
}
}