Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dcrdtest: Minor cleanups #19

Merged
merged 2 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions dcrdtest/rpc_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"

Expand All @@ -25,14 +25,8 @@ import (
)

var (
// XXX these variables are accessed in what should be accessor
// functions yet it is all global

// current number of active test nodes.
numTestInstances = 0

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

// pathToDCRD points to the test node. It is supplied through
// NewWithDCRD or created on the first call to newNode and used
Expand Down Expand Up @@ -65,7 +59,6 @@ type Harness struct {

testNodeDir string
maxConnRetries int
nodeNum int

keepNodeDir bool

Expand Down Expand Up @@ -106,9 +99,6 @@ func SetPathToDCRD(fnScopePathToDCRD string) {
// future major version of this package. Use the global UseLogger function with
// an appropriate backend to log events from this package.
func New(t *testing.T, activeNet *chaincfg.Params, handlers *rpcclient.NotificationHandlers, extraArgs []string) (*Harness, error) {
harnessStateMtx.Lock()
defer harnessStateMtx.Unlock()

// Add a flag for the appropriate network type based on the provided
// chain params.
switch activeNet.Net {
Expand All @@ -125,8 +115,8 @@ func New(t *testing.T, activeNet *chaincfg.Params, handlers *rpcclient.Notificat
"of the supported chain networks")
}

harnessID := strconv.Itoa(numTestInstances)
nodeTestData, err := os.MkdirTemp("", "dcrdtest-"+harnessID)
nodeNum := numTestInstances.Add(1)
nodeTestData, err := os.MkdirTemp("", fmt.Sprintf("dcrdtest-%03d-*", nodeNum))
if err != nil {
return nil, err
}
Expand All @@ -138,7 +128,7 @@ func New(t *testing.T, activeNet *chaincfg.Params, handlers *rpcclient.Notificat
return nil, err
}

wallet, err := newMemWallet(activeNet, uint32(numTestInstances))
wallet, err := newMemWallet(activeNet, nodeNum)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -172,8 +162,6 @@ func New(t *testing.T, activeNet *chaincfg.Params, handlers *rpcclient.Notificat
if err != nil {
return nil, err
}
nodeNum := numTestInstances
numTestInstances++

if handlers == nil {
handlers = &rpcclient.NotificationHandlers{}
Expand Down Expand Up @@ -209,7 +197,6 @@ func New(t *testing.T, activeNet *chaincfg.Params, handlers *rpcclient.Notificat
maxConnRetries: 20,
testNodeDir: nodeTestData,
ActiveNet: activeNet,
nodeNum: nodeNum,
wallet: wallet,
}

Expand Down
5 changes: 5 additions & 0 deletions dcrdtest/rpc_harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,4 +833,9 @@ func TestKeepNodeDir(t *testing.T) {
t.Fatalf("Unexpected Stat(testNodeDir) error: got %v, want %v",
err, nil)
}

// Manually remove the dir to clean up after this test.
if err := os.RemoveAll(mainHarness.testNodeDir); err != nil {
t.Fatalf("Unable to cleanup testNodeDir: %v", err)
}
}