-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader_test.go
141 lines (109 loc) · 3.27 KB
/
loader_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
package conf_test
import (
"context"
"errors"
"fmt"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/hsqds/conf"
"github.com/hsqds/conf/test/mocks"
)
// TestConfigsLoader
func TestConfigsLoader(t *testing.T) {
t.Parallel()
const (
configsCount = 3
svc1, svc2, svc3 = "service1", "service2", "service3"
)
var (
cfg = conf.NewMapConfig(map[string]string{})
srcID = fmt.Sprint(1)
newSourceMock = func() (*mocks.MockSource, func()) {
ctrl := gomock.NewController(t)
src := mocks.NewMockSource(ctrl)
finish := func() {
ctrl.Finish()
}
return src, finish
}
)
t.Run("should load config", func(t *testing.T) {
t.Parallel()
const (
priority = 50
)
services := []string{svc1, svc2, svc3}
src, finish := newSourceMock()
defer finish()
loader := new(conf.ConfigsLoader)
src.EXPECT().ID().Return(srcID).Times(1)
src.EXPECT().Load(gomock.Any(), services).Return(nil).Times(1)
src.EXPECT().ServiceConfig(gomock.Any()).Return(cfg, nil).Times(len(services))
src.EXPECT().Close(gomock.Any()).Times(1)
src.EXPECT().Priority().Return(priority).Times(len(services))
mockSources := []conf.Source{src}
res := loader.Load(context.Background(), mockSources, services)
assert.Equal(t, 3, len(res))
assert.Equal(t, []conf.LoadResult{
{SourceID: srcID, Config: cfg, Err: nil, Service: svc1, Priority: priority},
{SourceID: srcID, Config: cfg, Err: nil, Service: svc2, Priority: priority},
{SourceID: srcID, Config: cfg, Err: nil, Service: svc3, Priority: priority},
}, res)
})
t.Run("LoadResult should contain error when loading from source failed", func(t *testing.T) {
t.Parallel()
var (
err = errors.New("fail")
loadErr = conf.LoadError{
Err: err,
Service: svc1,
SourceID: srcID,
}
services = []string{svc1}
loader = new(conf.ConfigsLoader)
)
src, finish := newSourceMock()
defer finish()
src.EXPECT().ID().Return(srcID).Times(2)
src.EXPECT().Load(gomock.Any(), services).Return(err).Times(1)
src.EXPECT().Close(gomock.Any()).Times(1)
mockSources := []conf.Source{src}
res := loader.Load(context.Background(), mockSources, services)
assert.Equal(t, []conf.LoadResult{
{SourceID: srcID, Config: nil, Err: loadErr},
}, res)
})
t.Run("LoadResult should contain error when source has no config for service", func(t *testing.T) {
t.Parallel()
var (
err = errors.New("fail")
loadErr1 = conf.LoadError{
SourceID: srcID,
Service: svc1,
Err: err,
}
loadErr3 = conf.LoadError{
SourceID: srcID,
Service: svc3,
Err: err,
}
services = []string{svc1, svc3}
loader = new(conf.ConfigsLoader)
)
src, finish := newSourceMock()
defer finish()
src.EXPECT().ID().Return(srcID).Times(3)
src.EXPECT().Load(gomock.Any(), services).Return(nil).Times(1)
src.EXPECT().ServiceConfig(gomock.Any()).Return(nil, err).Times(len(services))
src.EXPECT().Close(gomock.Any()).Times(1)
mockSources := []conf.Source{
conf.Source(src),
}
res := loader.Load(context.Background(), mockSources, services)
assert.Equal(t, []conf.LoadResult{
{SourceID: srcID, Config: nil, Err: loadErr1, Service: svc1},
{SourceID: srcID, Config: nil, Err: loadErr3, Service: svc3},
}, res)
})
}