go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

mr.WithContext(ctx) is not used,how set timeout in mr util ?

Open CoderAres opened this issue 1 year ago • 1 comments

please see my test code:

func TestMrTimeout(t *testing.T) {
	start := time.Now()
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
	defer cancel()
	mr.WithContext(ctx)
	var apiFuncs []func() error
	var result []string
	apiFuncs = append(apiFuncs, func() error {
		time.Sleep(1 * time.Second)
		fmt.Println("hello task1")
		result = append(result, "task1")
		return nil
	})
	apiFuncs = append(apiFuncs, func() error {
		time.Sleep(5 * time.Second)
		fmt.Println("hello task2")
		result = append(result, "task2")
		return nil
	})
	apiFuncs = append(apiFuncs, func() error {
		time.Sleep(3 * time.Second)
		fmt.Println("hello task3")
		result = append(result, "task3")
		return nil
	})
	mr.Finish(apiFuncs...)


	fmt.Println("result:", result)
	fmt.Println("time:", time.Since(start))

}```
the code output:

=== RUN TestMrTimeout hello task1 hello task3 hello task2 result: [task1 task3 task2] time: 5.000883208s --- PASS: TestMrTimeout (5.00s) PASS

Normally, the output should be 1 second ,but the output err !!!

the out put result is not me want , please help me thanks!

CoderAres avatar May 09 '23 12:05 CoderAres