-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_test.go
94 lines (77 loc) · 1.96 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
package goworker
import (
"context"
"fmt"
"testing"
"github.com/lufred/goworker/common"
"github.com/lufred/goworker/config"
)
type HandlerOne struct{}
func (h *HandlerOne) Before(ctx context.Context) error {
fmt.Println("before")
return nil
}
func (h *HandlerOne) Do(ctx context.Context) error {
fmt.Println("do")
return nil
}
func (h *HandlerOne) After(ctx context.Context) error {
fmt.Println("after")
return nil
}
func (h *HandlerOne) ErrorMessage(ctx context.Context, err error) {
fmt.Printf("err:%+v\n", err)
}
type HandlerTwo struct {
ID int
}
func (h HandlerTwo) Before(ctx context.Context) error {
fmt.Println("before")
return nil
}
func (h HandlerTwo) Do(ctx context.Context) error {
fmt.Println("do")
return nil
}
func (h HandlerTwo) After(ctx context.Context) error {
fmt.Println("after")
return nil
}
func (h HandlerTwo) ErrorMessage(ctx context.Context, err error) {
fmt.Printf("err:%+v\n", err)
}
func TestStartWorkerCronWrong(t *testing.T) {
cfg := config.WorkerConfig{}
ctx := context.Background()
w := NewWorker(ctx, cfg, &HandlerOne{},
WithCronSchedule("******"))
if err := w.Run(); err != common.ErrInvalidCron {
t.Fatalf("expected %v, got %v", common.ErrInvalidCron, err)
}
}
func TestStartWorkerHandlerWrong(t *testing.T) {
cfg := config.WorkerConfig{}
ctx := context.Background()
w := NewWorker(ctx, cfg, HandlerTwo{})
if err := w.Run(); err != common.ErrHandlerMostBePtr {
t.Fatalf("expected %v, got %v", common.ErrHandlerMostBePtr, err)
}
}
func TestValidateHandler(t *testing.T) {
h := HandlerTwo{}
err := ValidateHandler(h)
if err != common.ErrHandlerMostBePtr {
t.Fatalf("expected %v, got %v", common.ErrHandlerMostBePtr, err)
}
}
func TestStart(t *testing.T) {
cfg := config.WorkerConfig{}
ctx := context.Background()
w := NewWorker(ctx, cfg, &HandlerTwo{})
if err := w.Run(); err != nil {
t.Fatalf("expected nil, got %v", err)
}
if err := w.Wait(); err != nil {
t.Fatalf("expected nil, got %v", err)
}
}