From c09f23c46d041e78d7bf18471f9aa8fe720ae485 Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Sat, 2 Apr 2016 18:27:54 -0700 Subject: [PATCH] *: clean up bool comparison --- contrib/raftexample/raft.go | 2 +- contrib/recipes/client.go | 2 +- contrib/recipes/key.go | 4 ++-- discovery/discovery_test.go | 4 ++-- pkg/adt/interval_tree.go | 8 +++++--- raft/log.go | 5 +---- raft/log_test.go | 6 +++--- raft/raft_paper_test.go | 6 +++--- raft/raft_snap_test.go | 4 ++-- raft/raft_test.go | 18 +++++++++--------- rafthttp/stream_test.go | 20 ++++++++++---------- 11 files changed, 39 insertions(+), 40 deletions(-) diff --git a/contrib/raftexample/raft.go b/contrib/raftexample/raft.go index a6b0c9c19..51bd3d1e9 100644 --- a/contrib/raftexample/raft.go +++ b/contrib/raftexample/raft.go @@ -133,7 +133,7 @@ func (rc *raftNode) publishEntries(ents []raftpb.Entry) bool { // openWAL returns a WAL ready for reading. func (rc *raftNode) openWAL() *wal.WAL { - if wal.Exist(rc.waldir) == false { + if !wal.Exist(rc.waldir) { if err := os.Mkdir(rc.waldir, 0750); err != nil { log.Fatalf("raftexample: cannot create dir for wal (%v)", err) } diff --git a/contrib/recipes/client.go b/contrib/recipes/client.go index 7861f0f7c..9e283d448 100644 --- a/contrib/recipes/client.go +++ b/contrib/recipes/client.go @@ -36,7 +36,7 @@ func deleteRevKey(kv v3.KV, key string, rev int64) (bool, error) { txnresp, err := kv.Txn(context.TODO()).If(cmp).Then(req).Commit() if err != nil { return false, err - } else if txnresp.Succeeded == false { + } else if !txnresp.Succeeded { return false, nil } return true, nil diff --git a/contrib/recipes/key.go b/contrib/recipes/key.go index 61a2e0416..8d0dbbe99 100644 --- a/contrib/recipes/key.go +++ b/contrib/recipes/key.go @@ -84,7 +84,7 @@ func putNewKV(kv v3.KV, key, val string, leaseID v3.LeaseID) (int64, error) { if err != nil { return 0, err } - if txnresp.Succeeded == false { + if !txnresp.Succeeded { return 0, ErrKeyExists } return txnresp.Header.Revision, nil @@ -132,7 +132,7 @@ func newSequentialKV(kv v3.KV, prefix, val string, leaseID v3.LeaseID) (*RemoteK if err != nil { return nil, err } - if txnresp.Succeeded == false { + if !txnresp.Succeeded { return newSequentialKV(kv, prefix, val, leaseID) } return &RemoteKV{kv, newKey, txnresp.Header.Revision, val}, nil diff --git a/discovery/discovery_test.go b/discovery/discovery_test.go index bd88e7008..b5a402376 100644 --- a/discovery/discovery_test.go +++ b/discovery/discovery_test.go @@ -410,14 +410,14 @@ func TestSortableNodes(t *testing.T) { for _, n := range sns.Nodes { cis = append(cis, int(n.CreatedIndex)) } - if sort.IntsAreSorted(cis) != true { + if !sort.IntsAreSorted(cis) { t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true) } cis = make([]int, 0) for _, n := range ns { cis = append(cis, int(n.CreatedIndex)) } - if sort.IntsAreSorted(cis) != true { + if !sort.IntsAreSorted(cis) { t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true) } } diff --git a/pkg/adt/interval_tree.go b/pkg/adt/interval_tree.go index 952e95065..a3ec803f6 100644 --- a/pkg/adt/interval_tree.go +++ b/pkg/adt/interval_tree.go @@ -27,10 +27,12 @@ type Comparable interface { Compare(c Comparable) int } -type rbcolor bool +type rbcolor int -const black = true -const red = false +const ( + black rbcolor = iota + red +) // Interval implements a Comparable interval [begin, end) // TODO: support different sorts of intervals: (a,b), [a,b], (a, b] diff --git a/raft/log.go b/raft/log.go index 99cd1b31e..7a2709afb 100644 --- a/raft/log.go +++ b/raft/log.go @@ -152,10 +152,7 @@ func (l *raftLog) nextEnts() (ents []pb.Entry) { // is a fast check without heavy raftLog.slice() in raftLog.nextEnts(). func (l *raftLog) hasNextEnts() bool { off := max(l.applied+1, l.firstIndex()) - if l.committed+1 > off { - return true - } - return false + return l.committed+1 > off } func (l *raftLog) snapshot() (pb.Snapshot, error) { diff --git a/raft/log_test.go b/raft/log_test.go index bb0592d75..d243f3891 100644 --- a/raft/log_test.go +++ b/raft/log_test.go @@ -243,7 +243,7 @@ func TestLogMaybeAppend(t *testing.T) { func() { defer func() { if r := recover(); r != nil { - if tt.wpanic != true { + if !tt.wpanic { t.Errorf("%d: panic = %v, want %v", i, true, tt.wpanic) } } @@ -455,7 +455,7 @@ func TestCommitTo(t *testing.T) { func() { defer func() { if r := recover(); r != nil { - if tt.wpanic != true { + if !tt.wpanic { t.Errorf("%d: panic = %v, want %v", i, true, tt.wpanic) } } @@ -548,7 +548,7 @@ func TestCompaction(t *testing.T) { func() { defer func() { if r := recover(); r != nil { - if tt.wallow == true { + if tt.wallow { t.Errorf("%d: allow = %v, want %v: %v", i, false, true, r) } } diff --git a/raft/raft_paper_test.go b/raft/raft_paper_test.go index 526fb322a..ff87bf21f 100644 --- a/raft/raft_paper_test.go +++ b/raft/raft_paper_test.go @@ -88,7 +88,7 @@ func TestRejectStaleTermMessage(t *testing.T) { r.Step(pb.Message{Type: pb.MsgApp, Term: r.Term - 1}) - if called == true { + if called { t.Errorf("stepFunc called = %v, want %v", called, false) } } @@ -169,7 +169,7 @@ func testNonleaderStartElection(t *testing.T, state StateType) { if r.state != StateCandidate { t.Errorf("state = %s, want %s", r.state, StateCandidate) } - if r.votes[r.id] != true { + if !r.votes[r.id] { t.Errorf("vote for self = false, want true") } msgs := r.readMessages() @@ -326,7 +326,7 @@ func testNonleaderElectionTimeoutRandomized(t *testing.T, state StateType) { } for d := et + 1; d < 2*et; d++ { - if timeouts[d] != true { + if !timeouts[d] { t.Errorf("timeout in %d ticks should happen", d) } } diff --git a/raft/raft_snap_test.go b/raft/raft_snap_test.go index f75c784c2..a0ff01d15 100644 --- a/raft/raft_snap_test.go +++ b/raft/raft_snap_test.go @@ -83,7 +83,7 @@ func TestSnapshotFailure(t *testing.T) { if sm.prs[2].Next != 1 { t.Fatalf("Next = %d, want 1", sm.prs[2].Next) } - if sm.prs[2].Paused != true { + if !sm.prs[2].Paused { t.Errorf("Paused = %v, want true", sm.prs[2].Paused) } } @@ -106,7 +106,7 @@ func TestSnapshotSucceed(t *testing.T) { if sm.prs[2].Next != 12 { t.Fatalf("Next = %d, want 12", sm.prs[2].Next) } - if sm.prs[2].Paused != true { + if !sm.prs[2].Paused { t.Errorf("Paused = %v, want true", sm.prs[2].Paused) } } diff --git a/raft/raft_test.go b/raft/raft_test.go index 21cd453d8..a3bfb294b 100644 --- a/raft/raft_test.go +++ b/raft/raft_test.go @@ -250,12 +250,12 @@ func TestProgressResume(t *testing.T) { Paused: true, } p.maybeDecrTo(1, 1) - if p.Paused != false { + if p.Paused { t.Errorf("paused= %v, want false", p.Paused) } p.Paused = true p.maybeUpdate(2) - if p.Paused != false { + if p.Paused { t.Errorf("paused= %v, want false", p.Paused) } } @@ -268,7 +268,7 @@ func TestProgressResumeByHeartbeat(t *testing.T) { r.prs[2].Paused = true r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgBeat}) - if r.prs[2].Paused != false { + if r.prs[2].Paused { t.Errorf("paused = %v, want false", r.prs[2].Paused) } } @@ -792,7 +792,7 @@ func TestStepIgnoreOldTermMsg(t *testing.T) { sm.step = fakeStep sm.Term = 2 sm.Step(pb.Message{Type: pb.MsgApp, Term: sm.Term - 1}) - if called == true { + if called { t.Errorf("stepFunc called = %v , want %v", called, false) } } @@ -1092,7 +1092,7 @@ func TestStateTransition(t *testing.T) { func() { defer func() { if r := recover(); r != nil { - if tt.wallow == true { + if tt.wallow { t.Errorf("%d: allow = %v, want %v", i, false, true) } } @@ -1416,7 +1416,7 @@ func TestSendAppendForProgressProbe(t *testing.T) { t.Errorf("index = %d, want %d", msg[0].Index, 0) } - if r.prs[2].Paused != true { + if !r.prs[2].Paused { t.Errorf("paused = %v, want true", r.prs[2].Paused) } for j := 0; j < 10; j++ { @@ -1686,7 +1686,7 @@ func TestStepConfig(t *testing.T) { if g := r.raftLog.lastIndex(); g != index+1 { t.Errorf("index = %d, want %d", g, index+1) } - if r.pendingConf != true { + if !r.pendingConf { t.Errorf("pendingConf = %v, want true", r.pendingConf) } } @@ -1759,7 +1759,7 @@ func TestAddNode(t *testing.T) { r := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage()) r.pendingConf = true r.addNode(2) - if r.pendingConf != false { + if r.pendingConf { t.Errorf("pendingConf = %v, want false", r.pendingConf) } nodes := r.nodes() @@ -1775,7 +1775,7 @@ func TestRemoveNode(t *testing.T) { r := newTestRaft(1, []uint64{1, 2}, 10, 1, NewMemoryStorage()) r.pendingConf = true r.removeNode(2) - if r.pendingConf != false { + if r.pendingConf { t.Errorf("pendingConf = %v, want false", r.pendingConf) } w := []uint64{1} diff --git a/rafthttp/stream_test.go b/rafthttp/stream_test.go index 79e4b77da..ace584c60 100644 --- a/rafthttp/stream_test.go +++ b/rafthttp/stream_test.go @@ -38,7 +38,7 @@ import ( func TestStreamWriterAttachOutgoingConn(t *testing.T) { sw := startStreamWriter(types.ID(1), newPeerStatus(types.ID(1)), &stats.FollowerStats{}, &fakeRaft{}) // the expected initial state of streamWriter is not working - if _, ok := sw.writec(); ok != false { + if _, ok := sw.writec(); ok { t.Errorf("initial working status = %v, want false", ok) } @@ -54,28 +54,28 @@ func TestStreamWriterAttachOutgoingConn(t *testing.T) { for j := 0; j < 3; j++ { testutil.WaitSchedule() // previous attached connection should be closed - if prevwfc != nil && prevwfc.Closed() != true { + if prevwfc != nil && !prevwfc.Closed() { continue } // write chan is available - if _, ok := sw.writec(); ok != true { + if _, ok := sw.writec(); !ok { continue } } // previous attached connection should be closed - if prevwfc != nil && prevwfc.Closed() != true { + if prevwfc != nil && !prevwfc.Closed() { t.Errorf("#%d: close of previous connection = %v, want true", i, prevwfc.Closed()) } // write chan is available - if _, ok := sw.writec(); ok != true { + if _, ok := sw.writec(); !ok { t.Errorf("#%d: working status = %v, want true", i, ok) } sw.msgc <- raftpb.Message{} testutil.WaitSchedule() // write chan is available - if _, ok := sw.writec(); ok != true { + if _, ok := sw.writec(); !ok { t.Errorf("#%d: working status = %v, want true", i, ok) } if wfc.Written() == 0 { @@ -85,10 +85,10 @@ func TestStreamWriterAttachOutgoingConn(t *testing.T) { sw.stop() // write chan is unavailable since the writer is stopped. - if _, ok := sw.writec(); ok != false { + if _, ok := sw.writec(); ok { t.Errorf("working status after stop = %v, want false", ok) } - if wfc.Closed() != true { + if !wfc.Closed() { t.Errorf("failed to close the underlying connection") } } @@ -104,10 +104,10 @@ func TestStreamWriterAttachBadOutgoingConn(t *testing.T) { sw.msgc <- raftpb.Message{} testutil.WaitSchedule() // no longer working - if _, ok := sw.writec(); ok != false { + if _, ok := sw.writec(); ok { t.Errorf("working = %v, want false", ok) } - if wfc.Closed() != true { + if !wfc.Closed() { t.Errorf("failed to close the underlying connection") } }