@@ -2,71 +2,61 @@ package tasks
2
2
3
3
import (
4
4
"context"
5
- "sync"
6
5
"sync/atomic"
7
6
"testing"
8
7
"time"
9
8
10
- "github.com/ssvlabs/ssv/logging"
11
9
"github.com/stretchr/testify/require"
12
10
)
13
11
14
12
func TestExecWithTimeout (t * testing.T ) {
15
- ctxWithTimeout , cancel := context .WithTimeout (context .TODO (), 10 * time .Millisecond )
16
- defer cancel ()
17
- tests := []struct {
18
- name string
19
- ctx context.Context
20
- t time.Duration
21
- expectedCount uint32
22
- }{
23
- {
24
- "Cancelled_context" ,
25
- ctxWithTimeout ,
26
- 20 * time .Millisecond ,
27
- 3 ,
28
- },
29
- {
30
- "Long_function" ,
31
- context .TODO (),
32
- 8 * time .Millisecond ,
33
- 3 ,
34
- },
35
- }
13
+ t .Run ("success" , func (t * testing.T ) {
14
+ longExec := func (stopper Stopper ) (interface {}, error ) {
15
+ time .Sleep (2 * time .Millisecond )
16
+ return true , nil
17
+ }
18
+ completed , res , err := ExecWithTimeout (context .TODO (), longExec , 1 * time .Minute )
19
+ require .True (t , completed )
20
+ require .True (t , res .(bool ))
21
+ require .NoError (t , err )
22
+ })
23
+ t .Run ("ctx timed out" , func (t * testing.T ) {
24
+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Millisecond )
25
+ defer cancel ()
36
26
37
- for _ , test := range tests {
38
- test := test
39
- t .Run (test .name , func (t * testing.T ) {
40
- var count uint32
41
- var stopped sync.WaitGroup
27
+ var started atomic.Bool
42
28
43
- stopped .Add (1 )
44
- fn := func (stopper Stopper ) (interface {}, error ) {
45
- for {
46
- if stopper .IsStopped () {
47
- stopped .Done ()
48
- return true , nil
49
- }
50
- atomic .AddUint32 (& count , 1 )
51
- time .Sleep (2 * time .Millisecond )
29
+ fn := func (stopper Stopper ) (interface {}, error ) {
30
+ started .Store (true )
31
+ for {
32
+ if stopper .IsStopped () {
33
+ return true , nil
52
34
}
35
+ time .Sleep (time .Millisecond )
53
36
}
54
- completed , _ , err := ExecWithTimeout (test .ctx , logging .TestLogger (t ), fn , test .t )
55
- stopped .Wait ()
56
- require .False (t , completed )
57
- require .NoError (t , err )
58
- require .GreaterOrEqual (t , count , test .expectedCount )
59
- })
60
- }
61
- }
37
+ }
38
+ completed , _ , err := ExecWithTimeout (ctx , fn , 1 * time .Hour )
39
+ require .True (t , started .Load ())
40
+ require .False (t , completed )
41
+ require .NoError (t , err )
42
+ })
43
+ t .Run ("timed out" , func (t * testing.T ) {
44
+ ctx := context .Background ()
45
+
46
+ var started atomic.Bool
62
47
63
- func TestExecWithTimeout_ShortFunc (t * testing.T ) {
64
- longExec := func (stopper Stopper ) (interface {}, error ) {
65
- time .Sleep (2 * time .Millisecond )
66
- return true , nil
67
- }
68
- completed , res , err := ExecWithTimeout (context .TODO (), logging .TestLogger (t ), longExec , 10 * time .Millisecond )
69
- require .True (t , completed )
70
- require .True (t , res .(bool ))
71
- require .NoError (t , err )
48
+ fn := func (stopper Stopper ) (interface {}, error ) {
49
+ started .Store (true )
50
+ for {
51
+ if stopper .IsStopped () {
52
+ return true , nil
53
+ }
54
+ time .Sleep (time .Millisecond )
55
+ }
56
+ }
57
+ completed , _ , err := ExecWithTimeout (ctx , fn , 10 * time .Millisecond )
58
+ require .True (t , started .Load ())
59
+ require .False (t , completed )
60
+ require .NoError (t , err )
61
+ })
72
62
}
0 commit comments