Skip to content

Commit

Permalink
tests: replace t.TempDir() for tests
Browse files Browse the repository at this point in the history
1.15 added https://pkg.go.dev/testing#T.TempDir

includes .gitattributes to ensure eol as LF
  • Loading branch information
jeffbean authored and echohead committed Apr 12, 2024
1 parent c5e730e commit 2480e3d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text eol=lf
2 changes: 0 additions & 2 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ func TestIntegration_BasicCluster(t *testing.T) {
}
defer zk2.Close()

time.Sleep(time.Second * 5)

if _, err := zk1.Create("/gozk-test", []byte("foo-cluster"), 0, WorldACL(PermAll)); err != nil {
t.Fatalf("Create failed on node 1: %+v", err)
}
Expand Down
12 changes: 4 additions & 8 deletions server_help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package zk
import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
Expand All @@ -17,10 +16,6 @@ const (
_testMyIDFileName = "myid"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

type TestServer struct {
Port int
Path string
Expand Down Expand Up @@ -54,8 +49,7 @@ func StartTestCluster(t *testing.T, size int, stdout, stderr io.Writer) (*TestCl
}
}

tmpPath, err := ioutil.TempDir("", "gozk")
requireNoError(t, err, "failed to create tmp dir for test server setup")
tmpPath := t.TempDir()

success := false
startPort := int(rand.Int31n(6000) + 10000)
Expand Down Expand Up @@ -152,7 +146,7 @@ func (tc *TestCluster) Stop() error {
for _, srv := range tc.Servers {
srv.Srv.Stop()
}
defer os.RemoveAll(tc.Path)

return tc.waitForStop(5, time.Second)
}

Expand Down Expand Up @@ -254,6 +248,8 @@ func (tc *TestCluster) StopAllServers() error {
}

func requireNoError(t *testing.T, err error, msgAndArgs ...interface{}) {
t.Helper()

if err != nil {
t.Logf("received unexpected error: %v", err)
t.Fatal(msgAndArgs...)
Expand Down
7 changes: 2 additions & 5 deletions zk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"os"
Expand Down Expand Up @@ -187,6 +186,7 @@ func TestIntegration_CreateContainer(t *testing.T) {
}

func TestIntegration_IncrementalReconfig(t *testing.T) {
// todo: semver test harness for version?
if val, ok := os.LookupEnv("zk_version"); ok {
if !strings.HasPrefix(val, "3.5") {
t.Skip("running with zookeeper that does not support this api")
Expand All @@ -199,10 +199,7 @@ func TestIntegration_IncrementalReconfig(t *testing.T) {
defer ts.Stop()

// start and add a new server.
tmpPath, err := ioutil.TempDir("", "gozk")
requireNoError(t, err, "failed to create tmp dir for test server setup")
defer os.RemoveAll(tmpPath)

tmpPath := t.TempDir()
startPort := int(rand.Int31n(6000) + 10000)

srvPath := filepath.Join(tmpPath, fmt.Sprintf("srv4"))
Expand Down

0 comments on commit 2480e3d

Please sign in to comment.