2021-08-01 16:35:40 +00:00
|
|
|
/*
|
|
|
|
* @Author: your name
|
|
|
|
* @Date: 2021-08-02 00:02:17
|
2021-08-25 16:53:20 +00:00
|
|
|
* @LastEditTime: 2021-08-24 23:18:40
|
2021-08-01 16:35:40 +00:00
|
|
|
* @LastEditors: Please set LastEditors
|
|
|
|
* @Description: In User Settings Edit
|
|
|
|
* @FilePath: \background\test\goroutine_test.go
|
|
|
|
*/
|
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWaitGroup(t *testing.T) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
|
2023-09-26 16:32:29 +00:00
|
|
|
go func() {
|
|
|
|
time.Sleep(3 * time.Second)
|
2021-08-01 16:35:40 +00:00
|
|
|
t.Logf("job 1 done")
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2023-09-26 16:32:29 +00:00
|
|
|
go func() {
|
2021-08-01 16:35:40 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
t.Logf("job 2 done")
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
t.Log("all finish")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-26 16:32:29 +00:00
|
|
|
func TestChanel(t *testing.T) {
|
2021-08-01 16:35:40 +00:00
|
|
|
exit := make(chan bool)
|
2023-09-26 16:32:29 +00:00
|
|
|
go func() {
|
2021-08-01 16:35:40 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-exit:
|
|
|
|
t.Logf("exit 1")
|
|
|
|
return
|
2023-09-26 16:32:29 +00:00
|
|
|
case <-time.After(1 * time.Second):
|
2021-08-01 16:35:40 +00:00
|
|
|
t.Logf("time out ")
|
|
|
|
}
|
2023-09-26 16:32:29 +00:00
|
|
|
}
|
2021-08-01 16:35:40 +00:00
|
|
|
}()
|
2023-09-26 16:32:29 +00:00
|
|
|
go func() {
|
2021-08-01 16:35:40 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-exit:
|
|
|
|
t.Logf("exit 2")
|
|
|
|
return
|
2023-09-26 16:32:29 +00:00
|
|
|
case <-time.After(1 * time.Second):
|
2021-08-01 16:35:40 +00:00
|
|
|
t.Logf("time out ")
|
|
|
|
}
|
2023-09-26 16:32:29 +00:00
|
|
|
}
|
2021-08-01 16:35:40 +00:00
|
|
|
}()
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
exit <- true
|
|
|
|
t.Logf("finish")
|
2023-09-26 16:32:29 +00:00
|
|
|
|
2021-08-01 16:35:40 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 16:32:29 +00:00
|
|
|
type TestInherit struct {
|
2021-08-25 16:53:20 +00:00
|
|
|
context.Context
|
|
|
|
x int32
|
|
|
|
}
|
2021-08-15 16:19:25 +00:00
|
|
|
|
2023-09-26 16:32:29 +00:00
|
|
|
func TestTimeoutContext(t *testing.T) {
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), time.Second*1)
|
|
|
|
aa := TestInherit{ctx, 1}
|
|
|
|
|
|
|
|
go func(aa *TestInherit) {
|
2021-08-15 16:19:25 +00:00
|
|
|
for {
|
|
|
|
select {
|
2021-08-25 16:53:20 +00:00
|
|
|
case <-aa.Done():
|
2021-08-15 16:19:25 +00:00
|
|
|
t.Logf("exit")
|
|
|
|
return
|
2023-09-26 16:32:29 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2021-08-15 16:19:25 +00:00
|
|
|
t.Logf("time out ")
|
|
|
|
}
|
2023-09-26 16:32:29 +00:00
|
|
|
}
|
2021-08-25 16:53:20 +00:00
|
|
|
}(&aa)
|
2023-09-26 16:32:29 +00:00
|
|
|
|
2021-08-25 16:53:20 +00:00
|
|
|
time.Sleep(10 * time.Second)
|
2021-08-15 16:19:25 +00:00
|
|
|
}
|
2021-08-25 16:53:20 +00:00
|
|
|
|
2023-09-26 16:32:29 +00:00
|
|
|
func TestContext(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2021-08-01 16:35:40 +00:00
|
|
|
|
2023-09-26 16:32:29 +00:00
|
|
|
go func() {
|
2021-08-01 16:35:40 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.Logf("exit")
|
|
|
|
return
|
2023-09-26 16:32:29 +00:00
|
|
|
case <-time.After(1 * time.Second):
|
2021-08-01 16:35:40 +00:00
|
|
|
t.Logf("time out ")
|
|
|
|
}
|
2023-09-26 16:32:29 +00:00
|
|
|
}
|
2021-08-01 16:35:40 +00:00
|
|
|
}()
|
2023-09-26 16:32:29 +00:00
|
|
|
|
|
|
|
go func() {
|
2021-08-01 16:35:40 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.Logf("exit")
|
|
|
|
return
|
2023-09-26 16:32:29 +00:00
|
|
|
case <-time.After(1 * time.Second):
|
2021-08-01 16:35:40 +00:00
|
|
|
t.Logf("time out ")
|
|
|
|
}
|
2023-09-26 16:32:29 +00:00
|
|
|
}
|
2021-08-01 16:35:40 +00:00
|
|
|
}()
|
2023-09-26 16:32:29 +00:00
|
|
|
|
|
|
|
go func() {
|
2021-08-01 16:35:40 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.Logf("exit")
|
|
|
|
return
|
2023-09-26 16:32:29 +00:00
|
|
|
case <-time.After(1 * time.Second):
|
2021-08-01 16:35:40 +00:00
|
|
|
t.Logf("time out ")
|
|
|
|
}
|
2023-09-26 16:32:29 +00:00
|
|
|
}
|
2021-08-01 16:35:40 +00:00
|
|
|
}()
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
cancel()
|
2021-08-15 16:19:25 +00:00
|
|
|
|
2021-08-01 16:35:40 +00:00
|
|
|
t.Logf("finish")
|
2023-09-26 16:32:29 +00:00
|
|
|
}
|