42 lines
883 B
Go
42 lines
883 B
Go
package coroutine
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRun(t *testing.T) {
|
|
// 测试未超时情况
|
|
fmt.Println("-----测试超时情况------")
|
|
for i := 0; i < 5; i++ {
|
|
ii := i
|
|
name := fmt.Sprintf("测试任务%d", i)
|
|
Run(name, func() {
|
|
fmt.Printf("%s开始\n", name)
|
|
w := time.Second * time.Duration(ii)
|
|
time.Sleep(w)
|
|
fmt.Printf("%s完成\n", name)
|
|
})
|
|
}
|
|
err := NewServer(nil, WithServerTimeout(2*time.Second)).Stop(context.Background())
|
|
fmt.Println(err)
|
|
|
|
// 测试超时情况
|
|
fmt.Println("\n-----测试未超时情况------")
|
|
for i := 0; i < 5; i++ {
|
|
ii := i
|
|
name := fmt.Sprintf("测试任务%d", ii)
|
|
Run(name, func() {
|
|
fmt.Printf("%s开始\n", name)
|
|
w := time.Second * time.Duration(ii)
|
|
time.Sleep(w)
|
|
fmt.Printf("%s完成\n", name)
|
|
})
|
|
}
|
|
err = NewServer(nil).Stop(context.Background())
|
|
fmt.Println(err)
|
|
|
|
}
|