From f733ece4ffb145df58ff2738f665a9b2e0f244ce Mon Sep 17 00:00:00 2001 From: marun Date: Wed, 4 Sep 2024 08:54:03 -0700 Subject: [PATCH] [tmpnet] Avoid calling Stat in advance of Read/Open (#3356) --- tests/fixture/tmpnet/node_process.go | 8 +++----- tests/fixture/tmpnet/subnet.go | 9 +++------ 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/tests/fixture/tmpnet/node_process.go b/tests/fixture/tmpnet/node_process.go index d7d95a697dd..b33fe32f073 100644 --- a/tests/fixture/tmpnet/node_process.go +++ b/tests/fixture/tmpnet/node_process.go @@ -77,14 +77,12 @@ func (p *NodeProcess) setProcessContext(processContext node.NodeProcessContext) func (p *NodeProcess) readState() error { path := p.getProcessContextPath() - if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) { + bytes, err := os.ReadFile(path) + if errors.Is(err, fs.ErrNotExist) { // The absence of the process context file indicates the node is not running p.setProcessContext(node.NodeProcessContext{}) return nil - } - - bytes, err := os.ReadFile(path) - if err != nil { + } else if err != nil { return fmt.Errorf("failed to read node process context: %w", err) } processContext := node.NodeProcessContext{} diff --git a/tests/fixture/tmpnet/subnet.go b/tests/fixture/tmpnet/subnet.go index a9062f2fb5c..1e1d3c749c7 100644 --- a/tests/fixture/tmpnet/subnet.go +++ b/tests/fixture/tmpnet/subnet.go @@ -6,6 +6,7 @@ package tmpnet import ( "context" "encoding/json" + "errors" "fmt" "io" "os" @@ -339,14 +340,10 @@ func WaitForActiveValidators( // Reads subnets from [network dir]/subnets/[subnet name].json func readSubnets(subnetDir string) ([]*Subnet, error) { - if _, err := os.Stat(subnetDir); os.IsNotExist(err) { + entries, err := os.ReadDir(subnetDir) + if errors.Is(err, os.ErrNotExist) { return nil, nil } else if err != nil { - return nil, err - } - - entries, err := os.ReadDir(subnetDir) - if err != nil { return nil, fmt.Errorf("failed to read subnet dir: %w", err) }