github.com/agiledragon/gomonkey/v2@v2.11.1-0.20240427155748-d56c6823ec17/test/apply_method_seq_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/agiledragon/gomonkey/v2"
     7  	"github.com/agiledragon/gomonkey/v2/test/fake"
     8  	. "github.com/smartystreets/goconvey/convey"
     9  )
    10  
    11  func TestApplyMethodSeq(t *testing.T) {
    12  	e := &fake.Etcd{}
    13  	Convey("TestApplyMethodSeq", t, func() {
    14  
    15  		Convey("default times is 1", func() {
    16  			info1 := "hello cpp"
    17  			info2 := "hello golang"
    18  			info3 := "hello gomonkey"
    19  			outputs := []OutputCell{
    20  				{Values: Params{info1, nil}},
    21  				{Values: Params{info2, nil}},
    22  				{Values: Params{info3, nil}},
    23  			}
    24  			patches := ApplyMethodSeq(e, "Retrieve", outputs)
    25  			defer patches.Reset()
    26  			output, err := e.Retrieve("")
    27  			So(err, ShouldEqual, nil)
    28  			So(output, ShouldEqual, info1)
    29  			output, err = e.Retrieve("")
    30  			So(err, ShouldEqual, nil)
    31  			So(output, ShouldEqual, info2)
    32  			output, err = e.Retrieve("")
    33  			So(err, ShouldEqual, nil)
    34  			So(output, ShouldEqual, info3)
    35  		})
    36  
    37  		Convey("retry succ util the third times", func() {
    38  			info1 := "hello cpp"
    39  			outputs := []OutputCell{
    40  				{Values: Params{"", fake.ErrActual}, Times: 2},
    41  				{Values: Params{info1, nil}},
    42  			}
    43  			patches := ApplyMethodSeq(e, "Retrieve", outputs)
    44  			defer patches.Reset()
    45  			output, err := e.Retrieve("")
    46  			So(err, ShouldEqual, fake.ErrActual)
    47  			output, err = e.Retrieve("")
    48  			So(err, ShouldEqual, fake.ErrActual)
    49  			output, err = e.Retrieve("")
    50  			So(err, ShouldEqual, nil)
    51  			So(output, ShouldEqual, info1)
    52  		})
    53  
    54  		Convey("batch operations failed on the third time", func() {
    55  			info1 := "hello gomonkey"
    56  			outputs := []OutputCell{
    57  				{Values: Params{info1, nil}, Times: 2},
    58  				{Values: Params{"", fake.ErrActual}},
    59  			}
    60  			patches := ApplyMethodSeq(e, "Retrieve", outputs)
    61  			defer patches.Reset()
    62  			output, err := e.Retrieve("")
    63  			So(err, ShouldEqual, nil)
    64  			So(output, ShouldEqual, info1)
    65  			output, err = e.Retrieve("")
    66  			So(err, ShouldEqual, nil)
    67  			So(output, ShouldEqual, info1)
    68  			output, err = e.Retrieve("")
    69  			So(err, ShouldEqual, fake.ErrActual)
    70  		})
    71  
    72  	})
    73  }