-
Notifications
You must be signed in to change notification settings - Fork 10
/
cube_example_test.go
68 lines (57 loc) · 1.9 KB
/
cube_example_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
package cube_test
import (
"os"
"time"
"github.com/anuvu/cube"
"github.com/anuvu/cube/component"
"github.com/anuvu/cube/config"
)
type dummy struct {
}
func newDummy(ctx component.Context) *dummy {
ctx.Log().Info().Msg("dummy object created")
return &dummy{}
}
func (d *dummy) Configure(ctx component.Context, store config.Store) error {
ctx.Log().Info().Msg("dummy object configured")
return nil
}
func (d *dummy) Start(ctx component.Context) error {
ctx.Log().Info().Msg("dummy object started")
return nil
}
func (d *dummy) Stop(ctx component.Context) error {
ctx.Log().Info().Msg("dummy object stopped")
return nil
}
func killer(ctx component.Context, k component.ServerShutdown) {
time.Sleep(time.Millisecond)
ctx.Log().Info().Msg("Killing the server")
k()
}
func ExampleMain() {
// Replace os.Args for test case
oldArgs := os.Args
os.Args = []string{"cube.test"}
defer func() { os.Args = oldArgs }()
cube.Main(func(g component.Group) (cube.Invoker, error) {
g.Add(newDummy)
return func() error {
g.Invoke(killer)
return nil
}, nil
})
// Output:
// {"level":"info","name":"cube.test-core","message":"creating group"}
// {"level":"info","name":"cube.test","message":"creating group"}
// {"level":"info","name":"cube.test","message":"dummy object created"}
// {"level":"info","name":"cube.test-core","message":"configuring group"}
// {"level":"info","name":"cube.test","message":"configuring group"}
// {"level":"info","name":"cube.test-core","message":"starting group"}
// {"level":"info","name":"cube.test","message":"starting group"}
// {"level":"info","name":"cube.test","message":"dummy object started"}
// {"level":"info","name":"cube.test","message":"Killing the server"}
// {"level":"info","name":"cube.test","message":"stopping group"}
// {"level":"info","name":"cube.test","message":"dummy object stopped"}
// {"level":"info","name":"cube.test-core","message":"stopping group"}
}