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

     1  package test
     2  
     3  import (
     4  	"runtime"
     5  	"testing"
     6  
     7  	. "github.com/agiledragon/gomonkey/v2"
     8  	"github.com/agiledragon/gomonkey/v2/test/fake"
     9  	. "github.com/smartystreets/goconvey/convey"
    10  )
    11  
    12  func TestApplyFuncSeq(t *testing.T) {
    13  	Convey("TestApplyFuncSeq", 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 := ApplyFuncSeq(fake.ReadLeaf, outputs)
    25  			defer patches.Reset()
    26  
    27  			runtime.GC()
    28  
    29  			output, err := fake.ReadLeaf("")
    30  			So(err, ShouldEqual, nil)
    31  			So(output, ShouldEqual, info1)
    32  			output, err = fake.ReadLeaf("")
    33  			So(err, ShouldEqual, nil)
    34  			So(output, ShouldEqual, info2)
    35  			output, err = fake.ReadLeaf("")
    36  			So(err, ShouldEqual, nil)
    37  			So(output, ShouldEqual, info3)
    38  		})
    39  
    40  		Convey("retry succ util the third times", func() {
    41  			info1 := "hello cpp"
    42  			outputs := []OutputCell{
    43  				{Values: Params{"", fake.ErrActual}, Times: 2},
    44  				{Values: Params{info1, nil}},
    45  			}
    46  			patches := ApplyFuncSeq(fake.ReadLeaf, outputs)
    47  			defer patches.Reset()
    48  			output, err := fake.ReadLeaf("")
    49  			So(err, ShouldEqual, fake.ErrActual)
    50  			output, err = fake.ReadLeaf("")
    51  			So(err, ShouldEqual, fake.ErrActual)
    52  			output, err = fake.ReadLeaf("")
    53  			So(err, ShouldEqual, nil)
    54  			So(output, ShouldEqual, info1)
    55  		})
    56  
    57  		Convey("batch operations failed on the third time", func() {
    58  			info1 := "hello gomonkey"
    59  			outputs := []OutputCell{
    60  				{Values: Params{info1, nil}, Times: 2},
    61  				{Values: Params{"", fake.ErrActual}},
    62  			}
    63  			patches := ApplyFuncSeq(fake.ReadLeaf, outputs)
    64  			defer patches.Reset()
    65  			output, err := fake.ReadLeaf("")
    66  			So(err, ShouldEqual, nil)
    67  			So(output, ShouldEqual, info1)
    68  			output, err = fake.ReadLeaf("")
    69  			So(err, ShouldEqual, nil)
    70  			So(output, ShouldEqual, info1)
    71  			output, err = fake.ReadLeaf("")
    72  			So(err, ShouldEqual, fake.ErrActual)
    73  		})
    74  
    75  	})
    76  }