-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
ego_test.go
101 lines (87 loc) · 2.01 KB
/
ego_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
package ego
import (
"context"
"fmt"
"testing"
"time"
"github.com/gotomicro/ego/core/econf"
"github.com/gotomicro/ego/server"
"github.com/stretchr/testify/assert"
)
func TestEgoRun(t *testing.T) {
t.Run("ego run start error", func(t *testing.T) {
svc := &testServer{
ServeErr: fmt.Errorf("when server call start error"),
}
app := New()
app.Serve(svc)
go func() {
time.Sleep(time.Millisecond * 100)
err := app.Stop(context.Background(), false)
assert.Nil(t, err)
}()
err := app.Run()
assert.EqualError(t, err, "when server call start error")
})
t.Run("ego run stop error", func(t *testing.T) {
svc := &testServer{
StopErr: fmt.Errorf("when server call stop error"),
}
app := New()
app.Serve(svc)
go func() {
time.Sleep(time.Millisecond * 100)
err := app.Stop(context.Background(), false)
assert.Nil(t, err)
}()
err := app.Run()
assert.EqualError(t, err, "when server call stop error")
})
}
func TestEgoNew(t *testing.T) {
app := New()
assert.NotNil(t, app.logger)
assert.NotNil(t, app.servers)
assert.NotNil(t, app.jobs)
assert.NotNil(t, app.logger)
}
func TestEgoEconfRace(t *testing.T) {
New()
go func() {
econf.OnChange(func(c *econf.Configuration) {})
}()
econf.OnChange(func(c *econf.Configuration) {})
}
type testServer struct {
ServeBlockTime time.Duration
ServeErr error
StopBlockTime time.Duration
StopErr error
GstopBlockTime time.Duration
GstopErr error
}
func (s *testServer) Name() string {
return "test_server"
}
func (s *testServer) PackageName() string {
return "server"
}
func (s *testServer) Init() error {
time.Sleep(s.ServeBlockTime)
return s.ServeErr
}
func (s *testServer) Start() error {
time.Sleep(s.ServeBlockTime)
return s.ServeErr
}
func (s *testServer) Stop() error {
time.Sleep(s.StopBlockTime)
return s.StopErr
}
func (s *testServer) GracefulStop(ctx context.Context) error {
time.Sleep(s.GstopBlockTime)
return s.GstopErr
}
func (s *testServer) Info() *server.ServiceInfo {
return &server.ServiceInfo{}
}