-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeat_test.go
More file actions
51 lines (43 loc) · 1.04 KB
/
repeat_test.go
File metadata and controls
51 lines (43 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package repeat_test
import (
"context"
"sync"
"testing"
"time"
"github.com/gruzovator/repeat"
)
func TestRun(t *testing.T) {
t.Run("does periodic calls", func(t *testing.T) {
ctx, cancelCtx := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancelCtx()
callsCounter := 0
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
repeat.Run(ctx, time.Millisecond, func(ctx context.Context) {
callsCounter++
})
}()
wg.Wait()
if callsCounter <= 1 {
t.Fatalf("no periodic calls: calls number: %d", callsCounter)
}
})
t.Run("fn context is cancelled when Run context is cancelled", func(t *testing.T) {
ctx, cancelCtx := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancelCtx()
doneCh := make(chan struct{})
go func() {
defer close(doneCh)
repeat.Run(ctx, time.Millisecond, func(ctx context.Context) {
<-ctx.Done()
})
}()
select {
case <-doneCh:
case <-time.After(100 * time.Millisecond):
t.Fatalf("fn context is not cancelled")
}
})
}