-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserve_test.go
156 lines (138 loc) · 3.41 KB
/
serve_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package core
import (
"bytes"
"context"
"errors"
"io/ioutil"
"net/http"
"os"
"runtime"
"sync/atomic"
"testing"
"time"
"github.com/DoNewsCode/core/cron"
"github.com/DoNewsCode/core/di"
"github.com/DoNewsCode/core/logging"
"github.com/DoNewsCode/core/observability"
"github.com/gorilla/mux"
"github.com/go-kit/log"
"github.com/oklog/run"
"github.com/stretchr/testify/assert"
)
func TestServeIn_signalWatch(t *testing.T) {
var in serveIn
var buf bytes.Buffer
do, cancel, err := in.signalWatch(context.Background(), logging.WithLevel(log.NewLogfmtLogger(&buf)))
assert.NoError(t, err)
t.Run("stop when signal received", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("TestServeIn_signalWatch/stop_when_signal_received only works on unix")
}
var group run.Group
group.Add(do, cancel)
group.Add(func() error {
time.Sleep(time.Second)
p, err := os.FindProcess(os.Getpid())
if err != nil {
return err
}
if err := p.Signal(os.Interrupt); err != nil {
return err
}
// trigger the signal twice should be ok.
p.Signal(os.Interrupt)
return nil
}, func(err error) {})
err = group.Run()
assert.NoError(t, err)
assert.Contains(t, buf.String(), "signal received: interrupt")
})
t.Run("cancel when cancel func is called", func(t *testing.T) {
var group run.Group
group.Add(do, cancel)
group.Add(func() error {
return errors.New("some err")
}, func(err error) {
})
err = group.Run()
assert.Contains(t, buf.String(), "context canceled")
})
}
type NewCronModule struct {
CanRun uint32
}
func (module *NewCronModule) ProvideCron(crontab *cron.Cron) {
crontab.Add("* * * * * *", func(ctx context.Context) error {
atomic.StoreUint32(&module.CanRun, 1)
return nil
})
}
func TestServeIn_cron(t *testing.T) {
c := Default(
WithInline("grpc.disable", true),
WithInline("http.disable", true),
WithInline("log.level", "none"),
)
c.Provide(observability.Providers())
c.Provide(
di.Deps{func() *cron.Cron {
return cron.New(cron.Config{EnableSeconds: true})
}},
)
m := NewCronModule{}
c.AddModule(&m)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
c.Serve(ctx)
assert.True(t, m.CanRun == 1)
}
func TestServeIn_inject_HTTPRouter(t *testing.T) {
c := Default(
WithInline("grpc.disable", true),
WithInline("cron.disable", true),
WithInline("http.addr", ":8080"),
WithInline("log.level", "none"),
)
c.Provide(
di.Deps{func() *mux.Router {
r := mux.NewRouter()
r.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("test"))
}))
return r
}},
)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
go c.Serve(ctx)
time.Sleep(time.Second)
resp, err := http.Get("http://localhost:8080/")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
assert.True(t, string(bs) == "test")
}
type SimpleRunModule struct {
RunCount uint32
}
func (s *SimpleRunModule) Run(ctx context.Context) error {
atomic.AddUint32(&s.RunCount, 1)
return nil
}
func TestSimpleRun(t *testing.T) {
c := Default(
WithInline("grpc.disable", true),
WithInline("http.disable", true),
WithInline("cron.disable", true),
WithInline("log.level", "none"),
)
m := SimpleRunModule{}
c.AddModule(&m)
c.Serve(context.Background())
assert.Equal(t, uint32(1), m.RunCount)
}