-
Notifications
You must be signed in to change notification settings - Fork 8
/
worker_test.go
110 lines (94 loc) · 2.98 KB
/
worker_test.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package service
import (
"context"
"fmt"
"testing"
"time"
"github.com/acronis/go-appkit/log"
"github.com/stretchr/testify/require"
)
func TestPeriodicWorker_Run(t *testing.T) {
t.Run("run and stop by context timeout", func(t *testing.T) {
const iterations = 5
c := 0
periodicWorker := NewPeriodicWorker(WorkerFunc(func(ctx context.Context) error {
c++
return nil
}), time.Millisecond*100, log.NewDisabledLogger())
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Millisecond*100*iterations)
defer ctxCancel()
runErr := make(chan error)
go func() {
runErr <- periodicWorker.Run(ctx)
}()
require.NoError(t, <-runErr)
require.GreaterOrEqual(t, c, iterations)
require.LessOrEqual(t,
c, iterations+1) // it's possible that the last iteration will be executed after the context is canceled
require.Error(t, context.DeadlineExceeded, ctx.Err())
})
t.Run("run and stop by error", func(t *testing.T) {
c := 0
periodicWorker := NewPeriodicWorker(WorkerFunc(func(ctx context.Context) error {
c++
if c == 2 {
return ErrPeriodicWorkerStop
}
return nil
}), time.Millisecond*100, log.NewDisabledLogger())
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Minute)
defer ctxCancel()
runErr := make(chan error)
go func() {
runErr <- periodicWorker.Run(ctx)
}()
require.Error(t, ErrPeriodicWorkerStop, <-runErr)
require.Equal(t, 2, c)
require.NoError(t, ctx.Err())
})
t.Run("run (initial delay > 0) and stop by context timeout", func(t *testing.T) {
c := 0
periodicWorker := NewPeriodicWorkerWithOpts(WorkerFunc(func(ctx context.Context) error {
c++
return nil
}), time.Millisecond*100, log.NewDisabledLogger(), PeriodicWorkerOpts{InitialDelay: time.Millisecond * 250})
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Millisecond*500)
defer ctxCancel()
runErr := make(chan error)
go func() {
runErr <- periodicWorker.Run(ctx)
}()
require.NoError(t, <-runErr)
require.Equal(t, 3, c)
require.Error(t, ctx.Err(), context.DeadlineExceeded)
})
t.Run("run (fail delay > 0), got one error, and stop by context timeout", func(t *testing.T) {
intervalDelayFunc := func(worker Worker, err error) time.Duration {
if err != nil {
return time.Millisecond * 250
}
return time.Millisecond * 100
}
c := 0
periodicWorker := NewPeriodicWorkerWithOpts(WorkerFunc(func(ctx context.Context) error {
c++
if c == 1 {
return fmt.Errorf("non-stop error")
}
return nil
}), time.Millisecond*100, log.NewDisabledLogger(), PeriodicWorkerOpts{IntervalDelayFunc: intervalDelayFunc})
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Millisecond*500)
defer ctxCancel()
runErr := make(chan error)
go func() {
runErr <- periodicWorker.Run(ctx)
}()
require.NoError(t, <-runErr)
require.Equal(t, 4, c)
require.Error(t, ctx.Err(), context.DeadlineExceeded)
})
}