Skip to content

Commit

Permalink
integration: allow specifying connection behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Nov 11, 2020
1 parent 93cc7f3 commit 65d2b7a
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions integration/rpctest/rpc_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ const (
// BlockVersion is the default block version used when generating
// blocks.
BlockVersion = 4

// DefaultMaxConnectionRetries is the default number of times we re-try
// to connect to the node after starting it.
DefaultMaxConnectionRetries = 20

// DefaultConnectionRetryTimeout is the default duration we wait between
// two connection attempts.
DefaultConnectionRetryTimeout = 50 * time.Millisecond
)

var (
Expand All @@ -58,7 +66,7 @@ var (

// Used to protest concurrent access to above declared variables.
harnessStateMtx sync.RWMutex

// ListenAddressGenerator is a function that is used to generate two
// listen addresses (host:port), one for the P2P listener and one for
// the RPC listener. This is exported to allow overwriting of the
Expand All @@ -85,15 +93,22 @@ type Harness struct {
// to.
ActiveNet *chaincfg.Params

// MaxConnRetries is the maximum number of times we re-try to connect to
// the node after starting it.
MaxConnRetries int

// ConnectionRetryTimeout is the duration we wait between two connection
// attempts.
ConnectionRetryTimeout time.Duration

Node *rpcclient.Client
node *node
handlers *rpcclient.NotificationHandlers

wallet *memWallet

testNodeDir string
maxConnRetries int
nodeNum int
testNodeDir string
nodeNum int

sync.Mutex
}
Expand Down Expand Up @@ -200,13 +215,14 @@ func New(activeNet *chaincfg.Params, handlers *rpcclient.NotificationHandlers,
}

h := &Harness{
handlers: handlers,
node: node,
maxConnRetries: 20,
testNodeDir: nodeTestData,
ActiveNet: activeNet,
nodeNum: nodeNum,
wallet: wallet,
handlers: handlers,
node: node,
MaxConnRetries: DefaultMaxConnectionRetries,
ConnectionRetryTimeout: DefaultConnectionRetryTimeout,
testNodeDir: nodeTestData,
ActiveNet: activeNet,
nodeNum: nodeNum,
wallet: wallet,
}

// Track this newly created test instance within the package level
Expand Down Expand Up @@ -322,9 +338,9 @@ func (h *Harness) connectRPCClient() error {
var err error

rpcConf := h.node.config.rpcConnConfig()
for i := 0; i < h.maxConnRetries; i++ {
for i := 0; i < h.MaxConnRetries; i++ {
if client, err = rpcclient.New(&rpcConf, h.handlers); err != nil {
time.Sleep(time.Duration(i) * 50 * time.Millisecond)
time.Sleep(time.Duration(i) * h.ConnectionRetryTimeout)
continue
}
break
Expand Down

0 comments on commit 65d2b7a

Please sign in to comment.