@@ -3,78 +3,76 @@ package sdk
3
3
import (
4
4
"bytes"
5
5
"context"
6
- "sync"
7
6
"testing"
8
7
"time"
9
8
10
- "github.com/stretchr/testify/assert"
11
9
"github.com/stretchr/testify/require"
12
10
)
13
11
14
12
func Test_GoroutineTools (t * testing.T ) {
15
13
t .Run ("GoroutineID()" , func (t * testing.T ) {
16
- id := GoroutineID ()
17
- var zero uint64
18
- assert .NotEqual (t , zero , id )
14
+ require .NotEqual (t , uint64 (0 ), GoroutineID ())
19
15
})
20
16
21
- t .Run ("writeGoroutineStacks(...)" , func (t * testing.T ) {
22
- ctx := context .Background ()
23
- var wg = new (sync.WaitGroup )
24
- ctx , cancel := context .WithTimeout (ctx , 10 * time .Second )
25
- defer cancel ()
26
- NewGoRoutines (ctx ).Exec (ctx , "test_goroutine" , func (ctx context.Context ) {
27
- wg .Add (1 )
28
- <- ctx .Done ()
29
- wg .Done ()
30
- })
31
-
17
+ t .Run ("GoRoutineStacks(...)" , func (t * testing.T ) {
32
18
var w = new (bytes.Buffer )
33
- err := writeGoroutineStacks (w )
34
- assert .NoError (t , err )
35
- t .Log (w .String ())
36
- wg .Wait ()
19
+ require .NoError (t , writeGoroutineStacks (w ))
20
+ _ , err := parseGoRoutineStacks (w , nil )
21
+ require .NoError (t , err )
37
22
})
38
23
39
- t .Run ("parseGoRoutineStacks(...)" , func (t * testing.T ) {
40
- ctx := context .Background ()
41
- var wg = new (sync.WaitGroup )
42
- ctx , cancel := context .WithTimeout (ctx , 10 * time .Second )
43
- defer cancel ()
24
+ t .Run ("GoRoutineRun" , func (t * testing.T ) {
25
+ ctx , cancel := context .WithCancel (context .TODO ())
26
+ t .Cleanup (cancel )
27
+ m := NewGoRoutines (ctx )
44
28
45
- NewGoRoutines (ctx ).Exec (ctx , "test_goroutine" , func (ctx context.Context ) {
46
- wg .Add (1 )
47
- <- ctx .Done ()
48
- wg .Done ()
29
+ m .Run (context .TODO (), "test_goroutine_run" , func (ctx context.Context ) {
30
+ time .Sleep (1 * time .Second )
49
31
})
50
32
51
- var w = new (bytes.Buffer )
52
- err := writeGoroutineStacks (w )
53
- assert .NoError (t , err )
33
+ s := m .GoRoutine ("test_goroutine_run" )
34
+ require .NotNil (t , s )
35
+ require .True (t , s .Active )
36
+ require .Len (t , m .GetStatus (), 1 )
54
37
55
- _ , err = parseGoRoutineStacks (w , nil )
56
- assert .NoError (t , err )
57
- wg .Wait ()
58
- })
38
+ time .Sleep (1 * time .Second )
59
39
60
- t .Run ("GoRoutineLoop" , func (t * testing.T ) {
61
- ctx := context .Background ()
62
- var wg = new (sync.WaitGroup )
63
- ctx , cancel := context .WithTimeout (ctx , 10 * time .Second )
64
- defer cancel ()
40
+ s = m .GoRoutine ("test_goroutine_run" )
41
+ require .NotNil (t , s )
42
+ require .False (t , s .Active )
43
+ })
65
44
45
+ t .Run ("GoRoutineRunCancel" , func (t * testing.T ) {
46
+ ctx , cancel := context .WithCancel (context .TODO ())
47
+ t .Cleanup (cancel )
66
48
m := NewGoRoutines (ctx )
67
- m .Run (ctx , "test_goroutine_loop" , func (ctx context.Context ) {
68
- wg .Add (1 )
69
- s := m .GoRoutine ("test_goroutine_loop" )
70
- require .NotNil (t , s )
71
- require .True (t , s .Active )
49
+
50
+ ctxToCancelled , cancelRoutine := context .WithTimeout (context .TODO (), 5 * time .Second )
51
+ var cancelled bool
52
+ m .Run (context .TODO (), "test_goroutine_run_cancel" , func (ctx context.Context ) {
72
53
<- ctx .Done ()
73
- wg .Done ()
54
+ cancelled = true
55
+ cancelRoutine ()
74
56
})
75
57
76
- s := m .GoRoutine ("test_goroutine_loop" )
77
- require .NotNil (t , s )
78
- require .Equal (t , 1 , len (m .GetStatus ()))
58
+ require .False (t , cancelled )
59
+ m .Stop ("test_goroutine_run_cancel" )
60
+ <- ctxToCancelled .Done ()
61
+ require .True (t , cancelled )
62
+ })
63
+
64
+ t .Run ("GoRoutineRunWithRestart" , func (t * testing.T ) {
65
+ ctx , cancel := context .WithTimeout (context .TODO (), 15 * time .Second )
66
+ t .Cleanup (cancel )
67
+ m := NewGoRoutines (ctx )
68
+
69
+ var count int
70
+ m .RunWithRestart (context .TODO (), "test_goroutine_run_with_restart" , func (ctx context.Context ) {
71
+ count ++
72
+ })
73
+
74
+ // the routine should have restart 1 time
75
+ <- ctx .Done ()
76
+ require .Equal (t , 2 , count )
79
77
})
80
78
}
0 commit comments