autopilot/agent_test: remove Fatalf calls from goroutines

This commit is contained in:
Conner Fromknecht 2018-09-01 18:30:24 -07:00
parent 2b578e06fc
commit e702a6a266
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

@ -187,22 +187,13 @@ func TestAgentChannelOpenSignal(t *testing.T) {
} }
defer agent.Stop() defer agent.Stop()
var wg sync.WaitGroup
// We'll send an initial "no" response to advance the agent past its // We'll send an initial "no" response to advance the agent past its
// initial check. // initial check.
wg.Add(1) select {
go func() { case heuristic.moreChansResps <- moreChansResp{false, 0, 0}:
select { case <-time.After(time.Second * 10):
case heuristic.moreChansResps <- moreChansResp{false, 0, 0}: t.Fatalf("heuristic wasn't queried in time")
wg.Done() }
return
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
}
}()
wg.Wait()
// Next we'll signal a new channel being opened by the backing LN node, // Next we'll signal a new channel being opened by the backing LN node,
// with a capacity of 1 BTC. // with a capacity of 1 BTC.
@ -212,34 +203,20 @@ func TestAgentChannelOpenSignal(t *testing.T) {
} }
agent.OnChannelOpen(newChan) agent.OnChannelOpen(newChan)
wg = sync.WaitGroup{}
// The agent should now query the heuristic in order to determine its // The agent should now query the heuristic in order to determine its
// next action as it local state has now been modified. // next action as it local state has now been modified.
wg.Add(1) select {
go func() { case heuristic.moreChansResps <- moreChansResp{false, 0, 0}:
select { // At this point, the local state of the agent should
case heuristic.moreChansResps <- moreChansResp{false, 0, 0}: // have also been updated to reflect that the LN node
// At this point, the local state of the agent should // now has an additional channel with one BTC.
// have also been updated to reflect that the LN node if _, ok := agent.chanState[newChan.ChanID]; !ok {
// now has an additional channel with one BTC. t.Fatalf("internal channel state wasn't updated")
if _, ok := agent.chanState[newChan.ChanID]; !ok {
t.Fatalf("internal channel state wasn't updated")
}
// With all of our assertions passed, we'll signal the
// main test goroutine to continue the test.
wg.Done()
return
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
} }
}()
// We'll wait here for either the agent to query the heuristic to be case <-time.After(time.Second * 10):
// queried, or for the timeout above to tick. t.Fatalf("heuristic wasn't queried in time")
wg.Wait() }
// There shouldn't be a call to the Select method as we've returned // There shouldn't be a call to the Select method as we've returned
// "false" for NeedMoreChans above. // "false" for NeedMoreChans above.
@ -443,55 +420,32 @@ func TestAgentChannelCloseSignal(t *testing.T) {
} }
defer agent.Stop() defer agent.Stop()
var wg sync.WaitGroup
// We'll send an initial "no" response to advance the agent past its // We'll send an initial "no" response to advance the agent past its
// initial check. // initial check.
wg.Add(1) select {
go func() { case heuristic.moreChansResps <- moreChansResp{false, 0, 0}:
select { case <-time.After(time.Second * 10):
case heuristic.moreChansResps <- moreChansResp{false, 0, 0}: t.Fatalf("heuristic wasn't queried in time")
wg.Done() }
return
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
}
}()
wg.Wait()
// Next, we'll close both channels which should force the agent to // Next, we'll close both channels which should force the agent to
// re-query the heuristic. // re-query the heuristic.
agent.OnChannelClose(initialChans[0].ChanID, initialChans[1].ChanID) agent.OnChannelClose(initialChans[0].ChanID, initialChans[1].ChanID)
wg = sync.WaitGroup{}
// The agent should now query the heuristic in order to determine its // The agent should now query the heuristic in order to determine its
// next action as it local state has now been modified. // next action as it local state has now been modified.
wg.Add(1) select {
go func() { case heuristic.moreChansResps <- moreChansResp{false, 0, 0}:
select { // At this point, the local state of the agent should
case heuristic.moreChansResps <- moreChansResp{false, 0, 0}: // have also been updated to reflect that the LN node
// At this point, the local state of the agent should // has no existing open channels.
// have also been updated to reflect that the LN node if len(agent.chanState) != 0 {
// has no existing open channels. t.Fatalf("internal channel state wasn't updated")
if len(agent.chanState) != 0 {
t.Fatalf("internal channel state wasn't updated")
}
// With all of our assertions passed, we'll signal the
// main test goroutine to continue the test.
wg.Done()
return
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
} }
}()
// We'll wait here for either the agent to query the heuristic to be case <-time.After(time.Second * 10):
// queried, or for the timeout above to tick. t.Fatalf("heuristic wasn't queried in time")
wg.Wait() }
// There shouldn't be a call to the Select method as we've returned // There shouldn't be a call to the Select method as we've returned
// "false" for NeedMoreChans above. // "false" for NeedMoreChans above.
@ -569,22 +523,13 @@ func TestAgentBalanceUpdate(t *testing.T) {
} }
defer agent.Stop() defer agent.Stop()
var wg sync.WaitGroup
// We'll send an initial "no" response to advance the agent past its // We'll send an initial "no" response to advance the agent past its
// initial check. // initial check.
wg.Add(1) select {
go func() { case heuristic.moreChansResps <- moreChansResp{false, 0, 0}:
select { case <-time.After(time.Second * 10):
case heuristic.moreChansResps <- moreChansResp{false, 0, 0}: t.Fatalf("heuristic wasn't queried in time")
wg.Done() }
return
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
}
}()
wg.Wait()
// Next we'll send a new balance update signal to the agent, adding 5 // Next we'll send a new balance update signal to the agent, adding 5
// BTC to the amount of available funds. // BTC to the amount of available funds.
@ -594,36 +539,22 @@ func TestAgentBalanceUpdate(t *testing.T) {
agent.OnBalanceChange() agent.OnBalanceChange()
wg = sync.WaitGroup{}
// The agent should now query the heuristic in order to determine its // The agent should now query the heuristic in order to determine its
// next action as it local state has now been modified. // next action as it local state has now been modified.
wg.Add(1) select {
go func() { case heuristic.moreChansResps <- moreChansResp{false, 0, 0}:
select { // At this point, the local state of the agent should
case heuristic.moreChansResps <- moreChansResp{false, 0, 0}: // have also been updated to reflect that the LN node
// At this point, the local state of the agent should // now has an additional 5BTC available.
// have also been updated to reflect that the LN node if agent.totalBalance != walletBalance {
// now has an additional 5BTC available. t.Fatalf("expected %v wallet balance "+
if agent.totalBalance != walletBalance { "instead have %v", agent.totalBalance,
t.Fatalf("expected %v wallet balance "+ walletBalance)
"instead have %v", agent.totalBalance,
walletBalance)
}
// With all of our assertions passed, we'll signal the
// main test goroutine to continue the test.
wg.Done()
return
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
} }
}()
// We'll wait here for either the agent to query the heuristic to be case <-time.After(time.Second * 10):
// queried, or for the timeout above to tick. t.Fatalf("heuristic wasn't queried in time")
wg.Wait() }
// There shouldn't be a call to the Select method as we've returned // There shouldn't be a call to the Select method as we've returned
// "false" for NeedMoreChans above. // "false" for NeedMoreChans above.
@ -697,31 +628,24 @@ func TestAgentImmediateAttach(t *testing.T) {
} }
defer agent.Stop() defer agent.Stop()
var wg sync.WaitGroup
const numChans = 5 const numChans = 5
// The very first thing the agent should do is query the NeedMoreChans // The very first thing the agent should do is query the NeedMoreChans
// method on the passed heuristic. So we'll provide it with a response // method on the passed heuristic. So we'll provide it with a response
// that will kick off the main loop. // that will kick off the main loop.
wg.Add(1) select {
go func() {
select {
// We'll send over a response indicating that it should // We'll send over a response indicating that it should
// establish more channels, and give it a budget of 5 BTC to do // establish more channels, and give it a budget of 5 BTC to do
// so. // so.
case heuristic.moreChansResps <- moreChansResp{true, numChans, 5 * btcutil.SatoshiPerBitcoin}: case heuristic.moreChansResps <- moreChansResp{
wg.Done() needMore: true,
return numMore: numChans,
case <-time.After(time.Second * 10): amt: 5 * btcutil.SatoshiPerBitcoin,
t.Fatalf("heuristic wasn't queried in time") }:
} case <-time.After(time.Second * 10):
}() t.Fatalf("heuristic wasn't queried in time")
}
// We'll wait here for the agent to query the heuristic. If ti doesn't
// do so within 10 seconds, then the test will fail out.
wg.Wait()
// At this point, the agent should now be querying the heuristic to // At this point, the agent should now be querying the heuristic to
// requests attachment directives. We'll generate 5 mock directives so // requests attachment directives. We'll generate 5 mock directives so
@ -747,24 +671,13 @@ func TestAgentImmediateAttach(t *testing.T) {
nodeKeys[nodeID] = struct{}{} nodeKeys[nodeID] = struct{}{}
} }
wg = sync.WaitGroup{}
// With our fake directives created, we'll now send then to the agent // With our fake directives created, we'll now send then to the agent
// as a return value for the Select function. // as a return value for the Select function.
wg.Add(1) select {
go func() { case heuristic.directiveResps <- directives:
select { case <-time.After(time.Second * 10):
case heuristic.directiveResps <- directives: t.Fatalf("heuristic wasn't queried in time")
wg.Done() }
return
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
}
}()
// We'll wait here for either the agent to query the heuristic to be
// queried, or for the timeout above to tick.
wg.Wait()
// Finally, we should receive 5 calls to the OpenChannel method with // Finally, we should receive 5 calls to the OpenChannel method with
// the exact same parameters that we specified within the attachment // the exact same parameters that we specified within the attachment
@ -850,34 +763,22 @@ func TestAgentPrivateChannels(t *testing.T) {
defer agent.Stop() defer agent.Stop()
const numChans = 5 const numChans = 5
var wg sync.WaitGroup
// The very first thing the agent should do is query the NeedMoreChans // The very first thing the agent should do is query the NeedMoreChans
// method on the passed heuristic. So we'll provide it with a response // method on the passed heuristic. So we'll provide it with a response
// that will kick off the main loop. // that will kick off the main loop. We'll send over a response
wg.Add(1) // indicating that it should establish more channels, and give it a
go func() { // budget of 5 BTC to do so.
defer wg.Done() resp := moreChansResp{
needMore: true,
// We'll send over a response indicating that it should numMore: numChans,
// establish more channels, and give it a budget of 5 BTC to do amt: 5 * btcutil.SatoshiPerBitcoin,
// so. }
resp := moreChansResp{ select {
needMore: true, case heuristic.moreChansResps <- resp:
numMore: numChans, case <-time.After(time.Second * 10):
amt: 5 * btcutil.SatoshiPerBitcoin, t.Fatalf("heuristic wasn't queried in time")
} }
select {
case heuristic.moreChansResps <- resp:
return
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
}
}()
// We'll wait here for the agent to query the heuristic. If it doesn't
// do so within 10 seconds, then the test will fail out.
wg.Wait()
// At this point, the agent should now be querying the heuristic to // At this point, the agent should now be querying the heuristic to
// requests attachment directives. We'll generate 5 mock directives so // requests attachment directives. We'll generate 5 mock directives so
@ -902,21 +803,11 @@ func TestAgentPrivateChannels(t *testing.T) {
// With our fake directives created, we'll now send then to the agent // With our fake directives created, we'll now send then to the agent
// as a return value for the Select function. // as a return value for the Select function.
wg.Add(1) select {
go func() { case heuristic.directiveResps <- directives:
defer wg.Done() case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
select { }
case heuristic.directiveResps <- directives:
return
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
}
}()
// We'll wait here for either the agent to query the heuristic to be
// queried, or for the timeout above to tick.
wg.Wait()
// Finally, we should receive 5 calls to the OpenChannel method, each // Finally, we should receive 5 calls to the OpenChannel method, each
// specifying that it's for a private channel. // specifying that it's for a private channel.
@ -995,29 +886,19 @@ func TestAgentPendingChannelState(t *testing.T) {
} }
defer agent.Stop() defer agent.Stop()
var wg sync.WaitGroup
// Once again, we'll start by telling the agent as part of its first // Once again, we'll start by telling the agent as part of its first
// query, that it needs more channels and has 3 BTC available for // query, that it needs more channels and has 3 BTC available for
// attachment. // attachment. We'll send over a response indicating that it should
wg.Add(1) // establish more channels, and give it a budget of 1 BTC to do so.
go func() { select {
select { case heuristic.moreChansResps <- moreChansResp{
needMore: true,
// We'll send over a response indicating that it should numMore: 1,
// establish more channels, and give it a budget of 1 BTC to do amt: btcutil.SatoshiPerBitcoin,
// so. }:
case heuristic.moreChansResps <- moreChansResp{true, 1, btcutil.SatoshiPerBitcoin}: case <-time.After(time.Second * 10):
wg.Done() t.Fatalf("heuristic wasn't queried in time")
return }
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
}
}()
// We'll wait for the first query to be consumed. If this doesn't
// happen then the above goroutine will timeout, and fail the test.
wg.Wait()
heuristic.moreChanArgs = make(chan moreChanArg) heuristic.moreChanArgs = make(chan moreChanArg)
@ -1175,16 +1056,11 @@ func TestAgentPendingOpenChannel(t *testing.T) {
// We'll send an initial "no" response to advance the agent past its // We'll send an initial "no" response to advance the agent past its
// initial check. // initial check.
var wg sync.WaitGroup select {
wg.Add(1) case heuristic.moreChansResps <- moreChansResp{false, 0, 0}:
go func() { case <-time.After(time.Second * 10):
defer wg.Done() t.Fatalf("heuristic wasn't queried in time")
select { }
case heuristic.moreChansResps <- moreChansResp{false, 0, 0}:
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
}
}()
// Next, we'll signal that a new channel has been opened, but it is // Next, we'll signal that a new channel has been opened, but it is
// still pending. // still pending.
@ -1192,19 +1068,11 @@ func TestAgentPendingOpenChannel(t *testing.T) {
// The agent should now query the heuristic in order to determine its // The agent should now query the heuristic in order to determine its
// next action as its local state has now been modified. // next action as its local state has now been modified.
wg.Add(1) select {
go func() { case heuristic.moreChansResps <- moreChansResp{false, 0, 0}:
defer wg.Done() case <-time.After(time.Second * 10):
select { t.Fatalf("heuristic wasn't queried in time")
case heuristic.moreChansResps <- moreChansResp{false, 0, 0}: }
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
}
}()
// We'll wait here for either the agent to query the heuristic to be
// queried, or for the timeout above to tick.
wg.Wait()
// There shouldn't be a call to the Select method as we've returned // There shouldn't be a call to the Select method as we've returned
// "false" for NeedMoreChans above. // "false" for NeedMoreChans above.
@ -1272,21 +1140,15 @@ func TestAgentOnNodeUpdates(t *testing.T) {
// We'll send an initial "yes" response to advance the agent past its // We'll send an initial "yes" response to advance the agent past its
// initial check. This will cause it to try to get directives from an // initial check. This will cause it to try to get directives from an
// empty graph. // empty graph.
var wg sync.WaitGroup select {
wg.Add(1) case heuristic.moreChansResps <- moreChansResp{
go func() { needMore: true,
defer wg.Done() numMore: 2,
select { amt: walletBalance,
case heuristic.moreChansResps <- moreChansResp{ }:
needMore: true, case <-time.After(time.Second * 10):
numMore: 2, t.Fatalf("heuristic wasn't queried in time")
amt: walletBalance, }
}:
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
}
}()
wg.Wait()
// Send over an empty list of attachment directives, which should cause // Send over an empty list of attachment directives, which should cause
// the agent to return to waiting on a new signal. // the agent to return to waiting on a new signal.
@ -1303,21 +1165,15 @@ func TestAgentOnNodeUpdates(t *testing.T) {
// In response, the agent should wake up and see if it needs more // In response, the agent should wake up and see if it needs more
// channels. Since we haven't done anything, we will send the same // channels. Since we haven't done anything, we will send the same
// response as before since we are still trying to open channels. // response as before since we are still trying to open channels.
var wg2 sync.WaitGroup select {
wg2.Add(1) case heuristic.moreChansResps <- moreChansResp{
go func() { needMore: true,
defer wg2.Done() numMore: 2,
select { amt: walletBalance,
case heuristic.moreChansResps <- moreChansResp{ }:
needMore: true, case <-time.After(time.Second * 10):
numMore: 2, t.Fatalf("heuristic wasn't queried in time")
amt: walletBalance, }
}:
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
}
}()
wg2.Wait()
// Again the agent should pull in the next set of attachment directives. // Again the agent should pull in the next set of attachment directives.
// It's not important that this list is also empty, so long as the node // It's not important that this list is also empty, so long as the node