From 883797b844e929752b63419dd8e7da0404322a44 Mon Sep 17 00:00:00 2001 From: Dhia Ayachi Date: Wed, 9 Nov 2022 15:46:09 -0500 Subject: [PATCH] clean some linter errors and remove use of ioutils as it's deprecated --- file_snapshot.go | 3 +-- file_snapshot_test.go | 21 ++++++++++---------- fuzzy/cluster.go | 3 +-- fuzzy/go.sum | 23 ---------------------- inmem_snapshot.go | 3 +-- integ_test.go | 3 +-- net_transport.go | 8 +++----- peersjson.go | 6 +++--- peersjson_test.go | 13 ++++++------ raft.go | 10 +++------- raft_test.go | 46 ++++++++++--------------------------------- testing.go | 5 ++--- 12 files changed, 41 insertions(+), 103 deletions(-) diff --git a/file_snapshot.go b/file_snapshot.go index ba238f914..33d6f3599 100644 --- a/file_snapshot.go +++ b/file_snapshot.go @@ -8,7 +8,6 @@ import ( "hash" "hash/crc64" "io" - "io/ioutil" "os" "path/filepath" "runtime" @@ -242,7 +241,7 @@ func (f *FileSnapshotStore) List() ([]*SnapshotMeta, error) { // getSnapshots returns all the known snapshots. func (f *FileSnapshotStore) getSnapshots() ([]*fileSnapshotMeta, error) { // Get the eligible snapshots - snapshots, err := ioutil.ReadDir(f.path) + snapshots, err := os.ReadDir(f.path) if err != nil { f.logger.Error("failed to scan snapshot directory", "error", err) return nil, err diff --git a/file_snapshot_test.go b/file_snapshot_test.go index 007b44ead..2afe6cad0 100644 --- a/file_snapshot_test.go +++ b/file_snapshot_test.go @@ -3,7 +3,6 @@ package raft import ( "bytes" "io" - "io/ioutil" "os" "reflect" "runtime" @@ -25,13 +24,13 @@ func TestFileSnapshotSinkImpl(t *testing.T) { } func TestFileSS_CreateSnapshotMissingParentDir(t *testing.T) { - parent, err := ioutil.TempDir("", "raft") + parent, err := os.MkdirTemp("", "raft") if err != nil { t.Fatalf("err: %v ", err) } defer os.RemoveAll(parent) - dir, err := ioutil.TempDir(parent, "raft") + dir, err := os.MkdirTemp(parent, "raft") if err != nil { t.Fatalf("err: %v ", err) } @@ -51,7 +50,7 @@ func TestFileSS_CreateSnapshotMissingParentDir(t *testing.T) { } func TestFileSS_CreateSnapshot(t *testing.T) { // Create a test dir - dir, err := ioutil.TempDir("", "raft") + dir, err := os.MkdirTemp("", "raft") if err != nil { t.Fatalf("err: %v ", err) } @@ -159,7 +158,7 @@ func TestFileSS_CreateSnapshot(t *testing.T) { func TestFileSS_CancelSnapshot(t *testing.T) { // Create a test dir - dir, err := ioutil.TempDir("", "raft") + dir, err := os.MkdirTemp("", "raft") if err != nil { t.Fatalf("err: %v ", err) } @@ -197,7 +196,7 @@ func TestFileSS_Retention(t *testing.T) { var err error // Create a test dir var dir string - dir, err = ioutil.TempDir("", "raft") + dir, err = os.MkdirTemp("", "raft") if err != nil { t.Fatalf("err: %v ", err) } @@ -250,7 +249,7 @@ func TestFileSS_BadPerm(t *testing.T) { // Create a temp dir var dir1 string - dir1, err = ioutil.TempDir("", "raft") + dir1, err = os.MkdirTemp("", "raft") if err != nil { t.Fatalf("err: %s", err) } @@ -258,7 +257,7 @@ func TestFileSS_BadPerm(t *testing.T) { // Create a sub dir and remove all permissions var dir2 string - dir2, err = ioutil.TempDir(dir1, "badperm") + dir2, err = os.MkdirTemp(dir1, "badperm") if err != nil { t.Fatalf("err: %s", err) } @@ -274,13 +273,13 @@ func TestFileSS_BadPerm(t *testing.T) { } func TestFileSS_MissingParentDir(t *testing.T) { - parent, err := ioutil.TempDir("", "raft") + parent, err := os.MkdirTemp("", "raft") if err != nil { t.Fatalf("err: %v ", err) } defer os.RemoveAll(parent) - dir, err := ioutil.TempDir(parent, "raft") + dir, err := os.MkdirTemp(parent, "raft") if err != nil { t.Fatalf("err: %v ", err) } @@ -294,7 +293,7 @@ func TestFileSS_MissingParentDir(t *testing.T) { func TestFileSS_Ordering(t *testing.T) { // Create a test dir - dir, err := ioutil.TempDir("", "raft") + dir, err := os.MkdirTemp("", "raft") if err != nil { t.Fatalf("err: %v ", err) } diff --git a/fuzzy/cluster.go b/fuzzy/cluster.go index 57abfc4c8..cd611939e 100644 --- a/fuzzy/cluster.go +++ b/fuzzy/cluster.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "path/filepath" "testing" @@ -291,7 +290,7 @@ func (c *cluster) VerifyFSM(t *testing.T) { } func (c *cluster) RecordState(t *testing.T) { - td, _ := ioutil.TempDir(os.Getenv("TEST_FAIL_DIR"), "failure") + td, _ := os.MkdirTemp(os.Getenv("TEST_FAIL_DIR"), "failure") sd, _ := resolveDirectory("data", false) copyDir(td, sd) dump := func(n *raftNode) { diff --git a/fuzzy/go.sum b/fuzzy/go.sum index 8b8798340..787f47d66 100644 --- a/fuzzy/go.sum +++ b/fuzzy/go.sum @@ -9,20 +9,14 @@ github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= -github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-hclog v0.9.1 h1:9PZfAcVEvez4yhLH2TBU64/h/z4xlFI80cWXRrxuKuM= github.com/hashicorp/go-hclog v0.9.1/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= -github.com/hashicorp/go-hclog v0.16.0 h1:uCeOEwSWGMwhJUdpUjk+1cVKIEfGu2/1nFXukimi2MU= -github.com/hashicorp/go-hclog v0.16.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= github.com/hashicorp/go-immutable-radix v1.0.0 h1:AKDB1HM5PWEA7i4nhcpwOrO2byshxBjXVn/J/3+z5/0= github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= github.com/hashicorp/go-msgpack v0.5.5 h1:i9R9JSrqIz0QVLz3sz+i3YJdT7TTSLcfLLzJi9aZTuI= github.com/hashicorp/go-msgpack v0.5.5/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= -github.com/hashicorp/go-msgpack v1.1.5 h1:9byZdVjKTe5mce63pRVNP1L7UAmdHOTEMGehn6KvJWs= -github.com/hashicorp/go-msgpack v1.1.5/go.mod h1:gWVc3sv/wbDmR3rQsj1CAktEZzoz1YNK9NfGLXJ69/4= github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs= github.com/hashicorp/go-uuid v1.0.0 h1:RS8zrF7PhGwyNPOtxSClXXj9HA8feRnJzgnI1RJCSnM= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= @@ -30,13 +24,6 @@ github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCO github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/raft-boltdb v0.0.0-20171010151810-6e5ba93211ea h1:xykPFhrBAS2J0VBzVa5e80b5ZtYuNQtgXjN40qBZlD4= github.com/hashicorp/raft-boltdb v0.0.0-20171010151810-6e5ba93211ea/go.mod h1:pNv7Wc3ycL6F5oOWn+tPGo2gWD4a5X+yp/ntwdKLjRk= -github.com/hashicorp/raft-boltdb v0.0.0-20210409134258-03c10cc3d4ea h1:RxcPJuutPRM8PUOyiweMmkuNO+RJyfy2jds2gfvgNmU= -github.com/hashicorp/raft-boltdb v0.0.0-20210409134258-03c10cc3d4ea/go.mod h1:qRd6nFJYYS6Iqnc/8HcUmko2/2Gw8qTFEmxDLii6W5I= -github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= -github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10= -github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= @@ -52,17 +39,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190523142557-0e01d883c5c5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU= -golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210414055047-fe65e336abe0 h1:g9s1Ppvvun/fI+BptTMj909BBIcGrzQ32k9FNlcevOE= golang.org/x/sys v0.0.0-20210414055047-fe65e336abe0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20190424220101-1e8e1cfdf96b/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= diff --git a/inmem_snapshot.go b/inmem_snapshot.go index 5e0c202fa..f1fa658f7 100644 --- a/inmem_snapshot.go +++ b/inmem_snapshot.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "sync" ) @@ -85,7 +84,7 @@ func (m *InmemSnapshotStore) Open(id string) (*SnapshotMeta, io.ReadCloser, erro // Make a copy of the contents, since a bytes.Buffer can only be read // once. contents := bytes.NewBuffer(m.latest.contents.Bytes()) - return &m.latest.meta, ioutil.NopCloser(contents), nil + return &m.latest.meta, io.NopCloser(contents), nil } // Write appends the given bytes to the snapshot contents diff --git a/integ_test.go b/integ_test.go index ad158334a..259fc073c 100644 --- a/integ_test.go +++ b/integ_test.go @@ -3,7 +3,6 @@ package raft import ( "bytes" "fmt" - "io/ioutil" "os" "sync/atomic" "testing" @@ -74,7 +73,7 @@ func MakeRaft(t *testing.T, conf *Config, bootstrap bool) *RaftEnv { conf = inmemConfig(t) } - dir, err := ioutil.TempDir("", "raft") + dir, err := os.MkdirTemp("", "raft") if err != nil { t.Fatalf("err: %v ", err) } diff --git a/net_transport.go b/net_transport.go index 2e98bd36d..5b0209fab 100644 --- a/net_transport.go +++ b/net_transport.go @@ -48,7 +48,6 @@ var ( ) /* - NetworkTransport provides a network based transport that can be used to communicate with Raft on remote machines. It requires an underlying stream layer to provide a stream abstraction, which can @@ -64,7 +63,6 @@ both are encoded using MsgPack. InstallSnapshot is special, in that after the RPC request we stream the entire state. That socket is not re-used as the connection state is not known if there is an error. - */ type NetworkTransport struct { connPool map[ServerAddress][]*netConn @@ -233,7 +231,7 @@ func (n *NetworkTransport) getStreamContext() context.Context { return n.streamCtx } -// SetHeartbeatHandler is used to setup a heartbeat handler +// SetHeartbeatHandler is used to set up a heartbeat handler // as a fast-pass. This is to avoid head-of-line blocking from // disk IO. func (n *NetworkTransport) SetHeartbeatHandler(cb func(rpc RPC)) { @@ -368,7 +366,7 @@ func (n *NetworkTransport) returnConn(conn *netConn) { defer n.connPoolLock.Unlock() key := conn.target - conns, _ := n.connPool[key] + conns := n.connPool[key] if !n.IsShutdown() && len(conns) < n.maxPool { n.connPool[key] = append(conns, conn) @@ -794,7 +792,7 @@ func (n *netPipeline) Consumer() <-chan AppendFuture { return n.doneCh } -// Closed is used to shutdown the pipeline connection. +// Close is used to shut down the pipeline connection. func (n *netPipeline) Close() error { n.shutdownLock.Lock() defer n.shutdownLock.Unlock() diff --git a/peersjson.go b/peersjson.go index 38ca2a8b8..a81ae4e26 100644 --- a/peersjson.go +++ b/peersjson.go @@ -3,7 +3,7 @@ package raft import ( "bytes" "encoding/json" - "io/ioutil" + "os" ) // ReadPeersJSON consumes a legacy peers.json file in the format of the old JSON @@ -14,7 +14,7 @@ import ( // support for these, nor non-voter suffrage types. func ReadPeersJSON(path string) (Configuration, error) { // Read in the file. - buf, err := ioutil.ReadFile(path) + buf, err := os.ReadFile(path) if err != nil { return Configuration{}, err } @@ -63,7 +63,7 @@ type configEntry struct { // versions that use server IDs. func ReadConfigJSON(path string) (Configuration, error) { // Read in the file. - buf, err := ioutil.ReadFile(path) + buf, err := os.ReadFile(path) if err != nil { return Configuration{}, err } diff --git a/peersjson_test.go b/peersjson_test.go index a0504af7f..a119fd655 100644 --- a/peersjson_test.go +++ b/peersjson_test.go @@ -1,7 +1,6 @@ package raft import ( - "io/ioutil" "os" "path/filepath" "reflect" @@ -12,14 +11,14 @@ import ( func TestPeersJSON_BadConfiguration(t *testing.T) { var err error var base string - base, err = ioutil.TempDir("", "") + base, err = os.MkdirTemp("", "") if err != nil { t.Fatalf("err: %v", err) } defer os.RemoveAll(base) peers := filepath.Join(base, "peers.json") - if err = ioutil.WriteFile(peers, []byte("null"), 0666); err != nil { + if err = os.WriteFile(peers, []byte("null"), 0666); err != nil { t.Fatalf("err: %v", err) } @@ -32,7 +31,7 @@ func TestPeersJSON_BadConfiguration(t *testing.T) { func TestPeersJSON_ReadPeersJSON(t *testing.T) { var err error var base string - base, err = ioutil.TempDir("", "") + base, err = os.MkdirTemp("", "") if err != nil { t.Fatalf("err: %v", err) } @@ -44,7 +43,7 @@ func TestPeersJSON_ReadPeersJSON(t *testing.T) { "127.0.0.3:123"] `) peers := filepath.Join(base, "peers.json") - if err = ioutil.WriteFile(peers, content, 0666); err != nil { + if err = os.WriteFile(peers, content, 0666); err != nil { t.Fatalf("err: %v", err) } var configuration Configuration @@ -80,7 +79,7 @@ func TestPeersJSON_ReadPeersJSON(t *testing.T) { func TestPeersJSON_ReadConfigJSON(t *testing.T) { var err error var base string - base, err = ioutil.TempDir("", "") + base, err = os.MkdirTemp("", "") if err != nil { t.Fatalf("err: %v", err) } @@ -105,7 +104,7 @@ func TestPeersJSON_ReadConfigJSON(t *testing.T) { ] `) peers := filepath.Join(base, "peers.json") - if err = ioutil.WriteFile(peers, content, 0666); err != nil { + if err = os.WriteFile(peers, content, 0666); err != nil { t.Fatalf("err: %v", err) } diff --git a/raft.go b/raft.go index 5ad23a04b..0a7c119ac 100644 --- a/raft.go +++ b/raft.go @@ -5,7 +5,6 @@ import ( "container/list" "fmt" "io" - "io/ioutil" "sync/atomic" "time" @@ -214,7 +213,7 @@ func (r *Raft) runFollower() { // Check if we have had a successful contact lastContact := r.LastContact() - if time.Now().Sub(lastContact) < hbTimeout { + if time.Since(lastContact) < hbTimeout { continue } @@ -1501,7 +1500,6 @@ func (r *Raft) appendEntries(rpc RPC, a *AppendEntriesRequest) { // Everything went well, set success resp.Success = true r.setLastContact() - return } // processConfigurationLogEntry takes a log entry and updates the latest @@ -1621,7 +1619,7 @@ func (r *Raft) requestVote(rpc RPC, req *RequestVoteRequest) { // Check if we've voted in this election before if lastVoteTerm == req.Term && lastVoteCandBytes != nil { r.logger.Info("duplicate requestVote for same term", "term", req.Term) - if bytes.Compare(lastVoteCandBytes, candidateBytes) == 0 { + if bytes.Equal(lastVoteCandBytes, candidateBytes) { r.logger.Warn("duplicate requestVote from", "candidate", candidate) resp.Granted = true } @@ -1654,7 +1652,6 @@ func (r *Raft) requestVote(rpc RPC, req *RequestVoteRequest) { resp.Granted = true r.setLastContact() - return } // installSnapshot is invoked when we get a InstallSnapshot RPC call. @@ -1670,7 +1667,7 @@ func (r *Raft) installSnapshot(rpc RPC, req *InstallSnapshotRequest) { } var rpcErr error defer func() { - io.Copy(ioutil.Discard, rpc.Reader) // ensure we always consume all the snapshot data from the stream [see issue #212] + _, _ = io.Copy(io.Discard, rpc.Reader) // ensure we always consume all the snapshot data from the stream [see issue #212] rpc.Respond(resp, rpcErr) }() @@ -1795,7 +1792,6 @@ func (r *Raft) installSnapshot(rpc RPC, req *InstallSnapshotRequest) { r.logger.Info("Installed remote snapshot") resp.Success = true r.setLastContact() - return } // setLastContact is used to set the last contact time to now diff --git a/raft_test.go b/raft_test.go index 84234e7f1..e3fe08152 100644 --- a/raft_test.go +++ b/raft_test.go @@ -5,8 +5,6 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" - "log" "os" "path/filepath" "reflect" @@ -424,12 +422,9 @@ func TestRaft_LeaderFail(t *testing.T) { if len(fsm.logs) != 2 { t.Fatalf("did not apply both to FSM! %v", fsm.logs) } - if bytes.Compare(fsm.logs[0], []byte("test")) != 0 { - t.Fatalf("first entry should be 'test'") - } - if bytes.Compare(fsm.logs[1], []byte("apply")) != 0 { - t.Fatalf("second entry should be 'apply'") - } + + require.Equal(t, fsm.logs[0], []byte("test")) + require.Equal(t, fsm.logs[1], []byte("apply")) fsm.Unlock() } } @@ -1223,13 +1218,13 @@ func TestRaft_SnapshotRestore_PeerChange(t *testing.T) { content := []byte(fmt.Sprintf("[%s]", strings.Join(peers, ","))) // Perform a manual recovery on the cluster. - base, err := ioutil.TempDir("", "") + base, err := os.MkdirTemp("", "") if err != nil { t.Fatalf("err: %v", err) } defer os.RemoveAll(base) peersFile := filepath.Join(base, "peers.json") - if err = ioutil.WriteFile(peersFile, content, 0666); err != nil { + if err = os.WriteFile(peersFile, content, 0666); err != nil { t.Fatalf("[ERR] err: %v", err) } configuration, err := ReadPeersJSON(peersFile) @@ -1415,9 +1410,7 @@ func snapshotAndRestore(t *testing.T, offset uint64) { } for i, entry := range fsm.logs { expected := []byte(fmt.Sprintf("test %d", i)) - if bytes.Compare(entry, expected) != 0 { - t.Fatalf("Log entry bad: %v", entry) - } + require.Equal(t, entry, expected) } fsm.Unlock() @@ -2744,8 +2737,8 @@ func TestRaft_FollowerRemovalNoElection(t *testing.T) { c := MakeCluster(3, t, inmemConf) defer c.Close() - waitForLeader(c) - + err := waitForLeader(c) + require.NoError(t, err) leader := c.Leader() // Wait until we have 2 followers @@ -2798,8 +2791,8 @@ func TestRaft_VoteWithNoIDNoAddr(t *testing.T) { c := MakeCluster(3, t, nil) defer c.Close() - waitForLeader(c) - + err := waitForLeader(c) + require.NoError(t, err) leader := c.Leader() // Wait until we have 2 followers @@ -2868,25 +2861,6 @@ func waitForLeader(c *cluster) error { return errors.New("no leader elected") } -func waitForNewLeader(c *cluster, id ServerID) error { - count := 0 - for count < 100 { - r := c.GetInState(Leader) - if len(r) >= 1 && r[0].localID != id { - return nil - } else { - if len(r) == 0 { - log.Println("no leader yet") - } else { - log.Printf("leader still %s\n", id) - } - } - count++ - time.Sleep(100 * time.Millisecond) - } - return errors.New("no leader elected") -} - func TestRaft_runFollower_State_Transition(t *testing.T) { type fields struct { conf *Config diff --git a/testing.go b/testing.go index 3c5d42e69..c48123d38 100644 --- a/testing.go +++ b/testing.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" "reflect" "sync" @@ -700,7 +699,7 @@ func makeCluster(t *testing.T, opts *MakeClusterOpts) *cluster { // Setup the stores and transports for i := 0; i < opts.Peers; i++ { - dir, err := ioutil.TempDir("", "raft") + dir, err := os.MkdirTemp("", "raft") if err != nil { t.Fatalf("err: %v", err) } @@ -801,7 +800,7 @@ func MakeClusterCustom(t *testing.T, opts *MakeClusterOpts) *cluster { // NOTE: This is exposed for middleware testing purposes and is not a stable API func FileSnapTest(t *testing.T) (string, *FileSnapshotStore) { // Create a test dir - dir, err := ioutil.TempDir("", "raft") + dir, err := os.MkdirTemp("", "raft") if err != nil { t.Fatalf("err: %v ", err) }