-
Notifications
You must be signed in to change notification settings - Fork 27
/
harvester_test.go
184 lines (173 loc) · 5.36 KB
/
harvester_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package harvester
import (
"context"
"testing"
"time"
"github.com/beatlabs/harvester/config"
"github.com/beatlabs/harvester/sync"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
addr = "127.0.0.1:8500"
)
func TestCreateWithConsulAndRedis(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{})
type args struct {
cfg interface{}
consulAddress string
seedRedisClient redis.UniversalClient
monitorRedisClient redis.UniversalClient
monitoringPollInterval time.Duration
}
tests := map[string]struct {
args args
expectedErr string
}{
"invalid config": {
args: args{
cfg: "test",
consulAddress: addr,
seedRedisClient: redisClient,
monitorRedisClient: redisClient,
monitoringPollInterval: 10 * time.Millisecond,
}, expectedErr: "configuration should be a pointer type",
},
"invalid consul address": {
args: args{
cfg: &testConfig{},
consulAddress: "",
seedRedisClient: redisClient,
monitorRedisClient: redisClient,
monitoringPollInterval: 10 * time.Millisecond,
}, expectedErr: "address is empty",
},
"invalid redis seed client": {
args: args{
cfg: &testConfig{},
consulAddress: addr,
seedRedisClient: nil,
monitorRedisClient: redisClient,
monitoringPollInterval: 10 * time.Millisecond,
}, expectedErr: "client is nil",
},
"invalid redis monitor client": {
args: args{
cfg: &testConfig{},
consulAddress: addr,
seedRedisClient: redisClient,
monitorRedisClient: nil,
monitoringPollInterval: 10 * time.Millisecond,
}, expectedErr: "client is nil",
},
"invalid redis monitor poll interval": {
args: args{
cfg: &testConfig{},
consulAddress: addr,
seedRedisClient: redisClient,
monitorRedisClient: redisClient,
monitoringPollInterval: -1,
}, expectedErr: "redis monitor poll interval should be a positive number",
},
"success": {
args: args{
cfg: &testConfig{},
consulAddress: addr,
seedRedisClient: redisClient,
monitorRedisClient: redisClient,
monitoringPollInterval: 10 * time.Millisecond,
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got, err := New(tt.args.cfg, nil,
WithConsulSeed(tt.args.consulAddress, "", "", 0),
WithConsulMonitor(tt.args.consulAddress, "", "", 0),
WithConsulFolderPrefixMonitor(tt.args.consulAddress, "", "", "", 0),
WithRedisSeed(tt.args.seedRedisClient),
WithRedisMonitor(tt.args.monitorRedisClient, tt.args.monitoringPollInterval))
if tt.expectedErr != "" {
require.EqualError(t, err, tt.expectedErr)
assert.Nil(t, got)
} else {
require.NoError(t, err)
assert.NotNil(t, got)
}
})
}
}
func TestWithNotification(t *testing.T) {
type args struct {
cfg interface{}
chNotify chan<- config.ChangeNotification
}
tests := map[string]struct {
args args
}{
"nil notify channel": {args: args{cfg: &testConfig{}, chNotify: nil}},
"success": {args: args{cfg: &testConfig{}, chNotify: make(chan config.ChangeNotification)}},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got, err := New(tt.args.cfg, tt.args.chNotify)
require.NoError(t, err)
assert.NotNil(t, got)
})
}
}
func TestCreate_NoConsulOrRedis(t *testing.T) {
cfg := &testConfigNoConsul{}
got, err := New(cfg, nil)
require.NoError(t, err)
assert.NotNil(t, got)
ctx, cnl := context.WithCancel(context.Background())
defer cnl()
require.NoError(t, got.Harvest(ctx))
assert.Equal(t, "John Doe", cfg.Name.Get())
assert.Equal(t, int64(18), cfg.Age.Get())
assert.InDelta(t, 99.9, cfg.Balance.Get(), 0.01)
assert.True(t, cfg.HasJob.Get())
assert.Equal(t, int64(8000), cfg.Position.Salary.Get())
assert.Equal(t, int64(24), cfg.Position.Place.RoomNumber.Get())
}
func TestCreate_SeedError(t *testing.T) {
cfg := &testConfigSeedError{}
got, err := New(cfg, nil)
require.NoError(t, err)
assert.NotNil(t, got)
ctx, cnl := context.WithCancel(context.Background())
defer cnl()
err = got.Harvest(ctx)
require.Error(t, err)
}
type testConfig struct {
Name sync.String `seed:"John Doe" consul:"harvester1/name"`
Age sync.Int64 `seed:"18" consul:"harvester/age"`
Balance sync.Float64 `seed:"99.9" consul:"harvester/balance"`
HasJob sync.Bool `seed:"true" consul:"harvester/has-job"`
FunTime sync.TimeDuration `seed:"1s" consul:"harvester/fun-time"`
IsAdult sync.Bool `seed:"false" redis:"is-adult"`
}
type testConfigNoConsul struct {
Name sync.String `seed:"John Doe"`
Age sync.Int64 `seed:"18"`
Balance sync.Float64 `seed:"99.9"`
HasJob sync.Bool `seed:"true"`
FunTime sync.TimeDuration `seed:"3s"`
Position struct {
Salary sync.Int64 `seed:"8000"`
Place struct {
RoomNumber sync.Int64 `seed:"24"`
SignTime sync.TimeDuration `seed:"8s"`
}
}
}
type testConfigSeedError struct {
Name sync.String `seed:"John Doe"`
Age sync.Int64 `seed:"XXX"`
Balance sync.Float64 `seed:"99.9"`
HasJob sync.Bool `seed:"true"`
FunTime sync.TimeDuration `seed:"1s"`
}