github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zsync/promise_test.go (about) 1 package zsync_test 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 "time" 8 9 zls "github.com/sohaha/zlsgo" 10 "github.com/sohaha/zlsgo/zsync" 11 ) 12 13 func TestNewPromise(t *testing.T) { 14 tt := zls.NewTest(t) 15 16 i := 0 17 p := zsync.NewPromise(func() (int, error) { 18 return 2, nil 19 }).Finally(func() { 20 i++ 21 }) 22 23 p = p.Then(func(i int) (int, error) { 24 return i * 2, nil 25 }) 26 27 res, err := p.Done() 28 tt.NoError(err) 29 tt.Equal(4, res) 30 31 p = p.Then(func(i int) (int, error) { 32 return i * 2, errors.New("this is an error") 33 }). 34 Finally(func() { 35 i++ 36 }). 37 Then(func(i int) (int, error) { 38 return i * 2, errors.New("this is an new error") 39 }). 40 Finally(func() { 41 i++ 42 }). 43 Catch(func(err error) (int, error) { 44 return 0, errors.New("catch: " + err.Error()) 45 }) 46 47 res, err = p.Done() 48 if err == nil { 49 t.Fatal("expected error") 50 } 51 tt.Equal("catch: this is an error", err.Error()) 52 tt.Equal(0, res) 53 54 res, err = p.Finally(func() { 55 i++ 56 }). 57 Catch(func(err error) (int, error) { 58 return 10, nil 59 }). 60 Then(func(i int) (int, error) { 61 return i + 2, nil 62 }).Done() 63 tt.NoError(err) 64 tt.Equal(12, res) 65 66 tt.Equal(i, 4) 67 } 68 69 func TestNewPromiseContext(t *testing.T) { 70 tt := zls.NewTest(t) 71 72 ctx, cancel := context.WithTimeout(context.Background(), time.Second/5) 73 defer cancel() 74 75 p1 := zsync.NewPromiseContext(ctx, func() (int, error) { 76 time.Sleep(time.Second / 2) 77 return 2, nil 78 }) 79 80 var wg zsync.WaitGroup 81 for i := 0; i < 5; i++ { 82 wg.Go(func() { 83 val, err := p1.Done() 84 tt.EqualTrue(err != nil) 85 tt.Equal(0, val) 86 tt.Equal(context.Canceled, err) 87 }) 88 } 89 wg.Wait() 90 91 p2 := zsync.NewPromiseContext(context.Background(), func() (int, error) { 92 time.Sleep(time.Second / 2) 93 return 2, nil 94 }) 95 p3 := p2.Then(func(i int) (int, error) { 96 return i * 2, nil 97 }) 98 99 val, err := p3.Done() 100 tt.Log(val, err) 101 tt.NoError(err) 102 tt.Equal(4, val) 103 104 p3 = p3.Then(func(i int) (int, error) { 105 return i * 5, nil 106 }) 107 108 val, err = p3.Done() 109 tt.Log(val, err) 110 tt.NoError(err) 111 tt.Equal(20, val) 112 113 val, err = p2.Done() 114 tt.Log(val, err) 115 tt.NoError(err) 116 tt.Equal(2, val) 117 } 118 119 func TestPromiseAll(t *testing.T) { 120 tt := zls.NewTest(t) 121 122 p1 := zsync.NewPromise(func() (int, error) { 123 time.Sleep(time.Second / 5) 124 return 2, nil 125 }) 126 127 p2 := zsync.NewPromise(func() (int, error) { 128 time.Sleep(time.Second / 2) 129 return 20, nil 130 }) 131 132 p3 := zsync.NewPromise(func() (int, error) { 133 time.Sleep(time.Second) 134 return 30, nil 135 }) 136 137 all, err := zsync.PromiseAll(p1, p2, p3).Done() 138 tt.NoError(err) 139 tt.Equal([]int{2, 20, 30}, all) 140 141 p4 := zsync.NewPromise(func() (int, error) { 142 return 80, errors.New("this is an error") 143 }) 144 145 p5 := zsync.NewPromise(func() (int, error) { 146 return 100, nil 147 }) 148 149 all, err = zsync.PromiseAll(p1, p4, p5).Done() 150 tt.EqualTrue(err != nil) 151 tt.Equal(0, len(all)) 152 } 153 154 func TestPromiseRace(t *testing.T) { 155 tt := zls.NewTest(t) 156 157 p1 := zsync.NewPromise(func() (int, error) { 158 time.Sleep(time.Second / 2) 159 return 2, nil 160 }) 161 162 p2 := zsync.NewPromise(func() (int, error) { 163 time.Sleep(time.Second / 5) 164 return 20, nil 165 }) 166 167 p3 := zsync.NewPromise(func() (int, error) { 168 time.Sleep(time.Second) 169 return 30, nil 170 }) 171 172 val, err := zsync.PromiseRace(p1, p2, p3).Done() 173 tt.NoError(err) 174 tt.Equal(20, val) 175 176 p4 := zsync.NewPromise(func() (int, error) { 177 return 80, errors.New("this is an error") 178 }) 179 180 p5 := zsync.NewPromise(func() (int, error) { 181 return 100, errors.New("p5 error") 182 }) 183 184 val, err = zsync.PromiseRace(p3, p4, p5).Done() 185 tt.EqualTrue(err != nil) 186 tt.Equal(0, val) 187 } 188 189 func TestPromiseAny(t *testing.T) { 190 tt := zls.NewTest(t) 191 192 p1 := zsync.NewPromise(func() (int, error) { 193 time.Sleep(time.Second / 2) 194 return 2, nil 195 }) 196 197 p2 := zsync.NewPromise(func() (int, error) { 198 time.Sleep(time.Second / 5) 199 return 20, nil 200 }) 201 202 p3 := zsync.NewPromise(func() (int, error) { 203 time.Sleep(time.Second) 204 return 30, nil 205 }) 206 207 val, err := zsync.PromiseAny(p1, p2, p3).Done() 208 tt.NoError(err) 209 tt.Equal(20, val) 210 211 p4 := zsync.NewPromise(func() (int, error) { 212 return 80, errors.New("this is an error") 213 }) 214 215 p5 := zsync.NewPromise(func() (int, error) { 216 return 100, errors.New("p5 error") 217 }) 218 219 val, err = zsync.PromiseAny(p3, p4, p5).Done() 220 tt.NoError(err) 221 tt.Equal(30, val) 222 223 val, err = zsync.PromiseAny(p4, p5).Done() 224 tt.EqualTrue(err != nil) 225 tt.Equal(0, val) 226 tt.Log(err) 227 }