github.com/decred/dcrlnd@v0.7.6/internal/testutils/rpctest.go (about)

     1  package testutils
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/decred/dcrd/chaincfg/v3"
     9  	"github.com/decred/dcrd/rpcclient/v8"
    10  	rpctest "github.com/decred/dcrtest/dcrdtest"
    11  )
    12  
    13  // NewSetupRPCTest attempts up to maxTries to setup an rpctest harness or
    14  // errors. This is used to get around the fact the rpctest does not validate
    15  // listening addresses beforehand and might try to listen on an used address in
    16  // CI servers.
    17  //
    18  // The returned rpctest is already setup for use.
    19  func NewSetupRPCTest(ctx context.Context, maxTries int, netParams *chaincfg.Params,
    20  	handlers *rpcclient.NotificationHandlers,
    21  	args []string, setupChain bool, numMatureOutputs uint32) (*rpctest.Harness, error) {
    22  
    23  	// Append --nobanning because dcrd is now fast enough to block peers on
    24  	// simnet.
    25  	args = append(args, "--nobanning")
    26  
    27  	var harness *rpctest.Harness
    28  	var err error
    29  	for i := 0; i < maxTries; i++ {
    30  		harness, err = rpctest.New(nil, netParams, handlers, args)
    31  		if err == nil {
    32  			// It shouldn't take more than a minute to start a
    33  			// node.
    34  			ctx, cancel := context.WithTimeout(ctx, time.Minute)
    35  			defer cancel()
    36  			err = harness.SetUp(ctx, setupChain, numMatureOutputs)
    37  			if err == nil {
    38  				return harness, nil
    39  			} else {
    40  				err = fmt.Errorf("unable to setup node: %v", err)
    41  			}
    42  		} else {
    43  			err = fmt.Errorf("unable to create harness node: %v", err)
    44  		}
    45  
    46  		time.Sleep(time.Second)
    47  	}
    48  
    49  	return nil, err
    50  }
    51  
    52  func init() {
    53  	rpctest.SetPathToDCRD("dcrd-dcrlnd")
    54  }