From 285c54f1a0bdb0d76619cda7963f4b1ed8ef254c Mon Sep 17 00:00:00 2001 From: Xinyuan Du Date: Mon, 18 Mar 2024 11:22:56 +0800 Subject: [PATCH] replace t.error/fatal with assert/request (/confchange) Signed-off-by: Xinyuan Du --- confchange/quick_test.go | 28 ++++++++++------------------ confchange/restore_test.go | 16 ++++++---------- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/confchange/quick_test.go b/confchange/quick_test.go index 06784298..4818eb50 100644 --- a/confchange/quick_test.go +++ b/confchange/quick_test.go @@ -15,12 +15,14 @@ package confchange import ( - "fmt" "math/rand" "reflect" "testing" "testing/quick" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + pb "go.etcd.io/raft/v3/raftpb" "go.etcd.io/raft/v3/tracker" ) @@ -48,10 +50,8 @@ func TestConfChangeQuick(t *testing.T) { return err } cfg2a.AutoLeave = false - if !reflect.DeepEqual(cfg, cfg2a) || !reflect.DeepEqual(trk, trk2a) { - return fmt.Errorf("cfg: %+v\ncfg2a: %+v\ntrk: %+v\ntrk2a: %+v", - cfg, cfg2a, trk, trk2a) - } + assert.Equal(t, cfg, cfg2a) + assert.Equal(t, trk, trk2a) c.Tracker.Config = cfg c.Tracker.Progress = trk cfg2b, trk2b, err := c.LeaveJoint() @@ -65,10 +65,8 @@ func TestConfChangeQuick(t *testing.T) { if err != nil { return err } - if !reflect.DeepEqual(cfg, cfg2b) || !reflect.DeepEqual(trk, trk2b) { - return fmt.Errorf("cfg: %+v\ncfg2b: %+v\ntrk: %+v\ntrk2b: %+v", - cfg, cfg2b, trk, trk2b) - } + assert.Equal(t, cfg, cfg2b) + assert.Equal(t, trk, trk2b) c.Tracker.Config = cfg c.Tracker.Progress = trk return nil @@ -107,9 +105,7 @@ func TestConfChangeQuick(t *testing.T) { var n int f1 := func(setup initialChanges, ccs confChanges) *Changer { c, err := wrapper(runWithSimple)(setup, ccs) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) if n < infoCount { t.Log("initial setup:", Describe(setup...)) t.Log("changes:", Describe(ccs...)) @@ -121,9 +117,7 @@ func TestConfChangeQuick(t *testing.T) { } f2 := func(setup initialChanges, ccs confChanges) *Changer { c, err := wrapper(runWithJoint)(setup, ccs) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) return c } err := quick.CheckEqual(f1, f2, cfg) @@ -131,9 +125,7 @@ func TestConfChangeQuick(t *testing.T) { return } cErr, ok := err.(*quick.CheckEqualError) - if !ok { - t.Fatal(err) - } + require.True(t, ok, err) t.Error("setup:", Describe(cErr.In[0].([]pb.ConfChangeSingle)...)) t.Error("ccs:", Describe(cErr.In[1].([]pb.ConfChangeSingle)...)) diff --git a/confchange/restore_test.go b/confchange/restore_test.go index eefc85ea..2dbfd2ca 100644 --- a/confchange/restore_test.go +++ b/confchange/restore_test.go @@ -21,6 +21,8 @@ import ( "testing" "testing/quick" + "github.com/stretchr/testify/assert" + pb "go.etcd.io/raft/v3/raftpb" "go.etcd.io/raft/v3/tracker" ) @@ -90,8 +92,7 @@ func TestRestore(t *testing.T) { LastIndex: 10, } cfg, trk, err := Restore(chg, cs) - if err != nil { - t.Error(err) + if !assert.NoError(t, err) { return false } chg.Tracker.Config = cfg @@ -109,12 +110,9 @@ func TestRestore(t *testing.T) { cs2 := chg.Tracker.ConfState() // NB: cs.Equivalent does the same "sorting" dance internally, but let's // test it a bit here instead of relying on it. - if reflect.DeepEqual(cs, cs2) && cs.Equivalent(cs2) == nil && cs2.Equivalent(cs) == nil { + if assert.Equal(t, cs, cs2) && assert.NoError(t, cs.Equivalent(cs2)) && assert.NoError(t, cs2.Equivalent(cs)) { return true // success } - t.Errorf(` -before: %+#v -after: %+#v`, cs, cs2) return false } @@ -134,9 +132,7 @@ after: %+#v`, cs, cs2) } } - if err := quick.Check(func(cs rndConfChange) bool { + assert.NoError(t, quick.Check(func(cs rndConfChange) bool { return f(pb.ConfState(cs)) - }, &cfg); err != nil { - t.Error(err) - } + }, &cfg)) }