-
Notifications
You must be signed in to change notification settings - Fork 10
/
cube_test.go
113 lines (96 loc) · 2.6 KB
/
cube_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
package cube
import (
"errors"
"os"
"syscall"
"testing"
"github.com/anuvu/cube/component"
"github.com/anuvu/cube/config"
. "github.com/smartystreets/goconvey/convey"
)
type tester struct {
configError bool
}
func newtest(ctx component.Context) *tester {
return &tester{}
}
func newBadConfig() *tester {
return &tester{true}
}
func (d *tester) Config() config.Config {
return nil
}
func (d *tester) Configure(ctx component.Context) error {
if d.configError {
return errors.New("bad config")
}
return nil
}
func (d *tester) Start(ctx component.Context) error {
return errors.New("bad start")
}
type stoptester struct {
}
func (d *stoptester) Stop(ctx component.Context) error {
return errors.New("bad stop")
}
func TestCubePanics(t *testing.T) {
// Replace os.Args for test case
oldArgs := os.Args
os.Args = []string{"cube.test"}
defer func() { os.Args = oldArgs }()
Convey("cube main should panic on create error", t, func() {
initFunc := func(g component.Group) (Invoker, error) {
err := g.Add(func(bool) int { return 0 })
return nil, err
}
So(func() { Main(initFunc) }, ShouldPanic)
})
Convey("cube main should panic on config error", t, func() {
initFunc := func(g component.Group) (Invoker, error) {
err := g.Add(newBadConfig)
return nil, err
}
So(func() { Main(initFunc) }, ShouldPanic)
})
Convey("cube main should panic dependencies are not met", t, func() {
initFunc := func(g component.Group) (Invoker, error) {
err := g.Add(func(i *int) {})
return nil, err
}
So(func() { Main(initFunc) }, ShouldPanic)
})
Convey("cube main should panic on start errors", t, func() {
initFunc := func(g component.Group) (Invoker, error) {
g.Add(newtest)
return nil, nil
}
So(func() { Main(initFunc) }, ShouldPanic)
})
Convey("cube main should panic on invoke errors", t, func() {
initFunc := func(g component.Group) (Invoker, error) {
return func() error {
return errors.New("bad invoke")
}, nil
}
So(func() { Main(initFunc) }, ShouldPanic)
})
Convey("cube main should panic on stop errors", t, func() {
initFunc := func(g component.Group) (Invoker, error) {
g.Add(func() *stoptester { return &stoptester{} })
g.Add(func(s *stoptester, k component.ServerShutdown) int { k(); return 0 })
return nil, nil
}
So(func() { Main(initFunc) }, ShouldPanic)
})
Convey("calling shutdown handler should stop server", t, func() {
initFunc := func(g component.Group) (Invoker, error) {
g.Add(func(s *shutDownHandler) int {
s.shut(syscall.SIGTERM)
return 0
})
return nil, nil
}
So(func() { Main(initFunc) }, ShouldNotPanic)
})
}