-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_test.go
82 lines (64 loc) · 1.98 KB
/
plugin_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
package gawe
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type pluginExampleCtxKey uint8
const (
pluginExampleKey pluginExampleCtxKey = iota
pluginExample2Key
)
type pluginExample struct{}
var _ Plugin = (*pluginExample)(nil)
func (p *pluginExample) OnJobStart(ctx context.Context, job IdentifiableJob) context.Context {
if valCtx := ctx.Value(pluginExampleKey); valCtx != nil {
if val := valCtx.(string); val != "fromError" {
panic(errors.New("OnJobStart"))
}
}
if valCtx := ctx.Value(pluginExample2Key); valCtx == nil {
panic(errors.New("OnJobStart"))
} else if val := valCtx.(string); val != "beforeStart" {
panic(errors.New("OnJobStart"))
}
return context.WithValue(ctx, pluginExampleKey, "fromStart")
}
func (p *pluginExample) OnJobEnd(ctx context.Context, job IdentifiableJob) {
if valCtx := ctx.Value(pluginExampleKey); valCtx == nil {
panic(errors.New("OnJobEnd"))
} else if val := valCtx.(string); val != "fromStart" {
panic(errors.New("OnJobError"))
}
}
func (p *pluginExample) OnJobError(ctx context.Context, job IdentifiableJob, err error) context.Context {
if valCtx := ctx.Value(pluginExampleKey); valCtx == nil {
panic(errors.New("OnJobError"))
} else if val := valCtx.(string); val != "fromStart" {
panic(errors.New("OnJobError"))
}
return context.WithValue(ctx, pluginExampleKey, "fromError")
}
func TestPlugin(t *testing.T) {
t.Parallel()
engine := NewEngine(
WithMaxAttempts(2),
WithPlugins(&pluginExample{}),
)
assert.Equal(t, 2, engine.maxAttempts)
assert.Equal(t, 1, len(engine.plugins))
engine.Start()
defer engine.Stop()
ctx := context.Background()
ctx = context.WithValue(ctx, pluginExample2Key, "beforeStart")
err := engine.Enqueue(ctx, &jobExample{})
time.Sleep(100 * time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, 1, len(engine.cw))
err = engine.Enqueue(ctx, &jobExample{IsError: true})
time.Sleep(100 * time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, 1, len(engine.cw))
}