-
Notifications
You must be signed in to change notification settings - Fork 70
/
service_test.go
77 lines (70 loc) · 1.66 KB
/
service_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
package patron
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/mantzas/patron/errors"
"github.com/mantzas/patron/sync/http"
)
func TestNewServer(t *testing.T) {
route := http.NewRoute("/", "GET", nil, true, nil)
type args struct {
name string
opt OptionFunc
}
tests := []struct {
name string
args args
wantErr bool
}{
{"success", args{name: "test", opt: Routes([]http.Route{route})}, false},
{"failed missing name", args{name: "", opt: Routes([]http.Route{route})}, true},
{"failed missing routes", args{name: "test", opt: Routes([]http.Route{})}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := New(tt.args.name, "", tt.args.opt)
if tt.wantErr {
assert.Error(t, err)
assert.Nil(t, got)
} else {
assert.NoError(t, err)
assert.NotNil(t, got)
}
})
}
}
func TestServer_Run_Shutdown(t *testing.T) {
tests := []struct {
name string
cp Component
wantRunErr bool
}{
{"success", &testComponent{}, false},
{"failed to run", &testComponent{errorRunning: true}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, err := New("test", "", Components(tt.cp, tt.cp, tt.cp))
assert.NoError(t, err)
err = s.Run()
if tt.wantRunErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
type testComponent struct {
errorRunning bool
}
func (ts testComponent) Run(ctx context.Context) error {
if ts.errorRunning {
return errors.New("failed to run component")
}
return nil
}
func (ts testComponent) Info() map[string]interface{} {
return map[string]interface{}{"type": "mock"}
}