From 2a5cc249ec08bba7fed6a239fde7d27d19493ad1 Mon Sep 17 00:00:00 2001 From: Xinyuan Du Date: Mon, 25 Mar 2024 16:30:10 +0800 Subject: [PATCH] replace t.error/fatal with assert/request (/rafttest) Signed-off-by: Xinyuan Du --- rafttest/network_test.go | 11 +++++------ rafttest/node_test.go | 14 +++++--------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/rafttest/network_test.go b/rafttest/network_test.go index 9af69fb2..e1859a99 100644 --- a/rafttest/network_test.go +++ b/rafttest/network_test.go @@ -18,6 +18,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "go.etcd.io/raft/v3/raftpb" ) @@ -45,9 +47,8 @@ func TestNetworkDrop(t *testing.T) { } drop := sent - received - if drop > int((droprate+0.1)*float64(sent)) || drop < int((droprate-0.1)*float64(sent)) { - t.Errorf("drop = %d, want around %.2f", drop, droprate*float64(sent)) - } + assert.LessOrEqual(t, drop, int((droprate+0.1)*float64(sent))) + assert.GreaterOrEqual(t, drop, int((droprate-0.1)*float64(sent))) } func TestNetworkDelay(t *testing.T) { @@ -66,7 +67,5 @@ func TestNetworkDelay(t *testing.T) { w := time.Duration(float64(sent)*delayrate/2) * delay // there is some overhead in the send call since it generates random numbers. - if total < w { - t.Errorf("total = %v, want > %v", total, w) - } + assert.GreaterOrEqual(t, total, w) } diff --git a/rafttest/node_test.go b/rafttest/node_test.go index 87d726c2..60b10411 100644 --- a/rafttest/node_test.go +++ b/rafttest/node_test.go @@ -19,6 +19,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "go.etcd.io/raft/v3" ) @@ -39,9 +41,7 @@ func TestBasicProgress(t *testing.T) { nodes[0].Propose(context.TODO(), []byte("somedata")) } - if !waitCommitConverge(nodes, 100) { - t.Errorf("commits failed to converge!") - } + assert.True(t, waitCommitConverge(nodes, 100)) for _, n := range nodes { n.stop() @@ -79,9 +79,7 @@ func TestRestart(t *testing.T) { } nodes[k1].restart() - if !waitCommitConverge(nodes, 120) { - t.Errorf("commits failed to converge!") - } + assert.True(t, waitCommitConverge(nodes, 120)) for _, n := range nodes { n.stop() @@ -118,9 +116,7 @@ func TestPause(t *testing.T) { } nodes[1].resume() - if !waitCommitConverge(nodes, 120) { - t.Errorf("commits failed to converge!") - } + assert.True(t, waitCommitConverge(nodes, 120)) for _, n := range nodes { n.stop()