Skip to content

Commit 5c86a24

Browse files
author
shugenniu
committed
bugfix: fix prometheus server post process callback
1 parent 5c78d22 commit 5c86a24

File tree

3 files changed

+60
-42
lines changed

3 files changed

+60
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ coverage.txt
1919
*.bolt.lock
2020
/vendor
2121
.codecc
22+
*.log

plugin/discoverstat/discoverlocal/local_test.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,33 @@ func TestDiscoverStatisWorker_AddDiscoverCall(t *testing.T) {
6363
},
6464
}
6565

66-
go worker.Run()
66+
workerStarted := make(chan struct{}, 1)
67+
go func() {
68+
workerStarted <- struct{}{}
69+
worker.Run()
70+
}()
71+
<-workerStarted // 等待 worker 启动
6772

6873
namespace := "Test"
6974
timeout := time.After(time.Minute * 3)
75+
stop := make(chan struct{})
7076
for i := 0; i < 1000; i++ {
71-
go func(index int) {
77+
go func(stop chan struct{}, index int) {
78+
trigger := time.NewTimer(time.Millisecond * 10)
7279
for {
73-
name := fmt.Sprintf("test-service-%d", index)
74-
if err := worker.AddDiscoverCall(name, namespace, time.Now()); err != nil {
75-
t.Errorf("err: %s", err.Error())
80+
select {
81+
case <-trigger.C:
82+
name := fmt.Sprintf("test-service-%d", index)
83+
if err := worker.AddDiscoverCall(name, namespace, time.Now()); err != nil {
84+
t.Errorf("err: %s", err.Error())
85+
}
86+
case <-stop:
87+
return
7688
}
77-
time.Sleep(time.Millisecond * 10)
7889
}
79-
}(i)
90+
}(stop, i)
8091
}
8192
<-timeout
93+
stop <- struct{}{}
8294
t.Log("pass")
8395
}

test/config_center/client_test.go

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ func TestClientSetupAndFileNotExisted(t *testing.T) {
4545
}
4646

4747
rsp := configService.GetConfigFileForClient(defaultCtx, fileInfo)
48-
assert.Equal(t, uint32(api.NotFoundResource), rsp.Code.GetValue(), "GetConfigFileForClient must notfound")
48+
assert.Equal(t, api.NotFoundResource, rsp.Code.GetValue(), "GetConfigFileForClient must notfound")
4949

5050
rsp2 := configService.CheckClientConfigFileByVersion(defaultCtx, assembleDefaultClientConfigFile(0))
51-
assert.Equal(t, uint32(api.DataNoChange), rsp2.Code.GetValue(), "CheckClientConfigFileByVersion must nochange")
51+
assert.Equal(t, api.DataNoChange, rsp2.Code.GetValue(), "CheckClientConfigFileByVersion must nochange")
5252
assert.Nil(t, rsp2.ConfigFile)
5353

5454
rsp3 := configService.CheckClientConfigFileByMd5(defaultCtx, assembleDefaultClientConfigFile(0))
55-
assert.Equal(t, uint32(api.DataNoChange), rsp3.Code.GetValue())
55+
assert.Equal(t, api.DataNoChange, rsp3.Code.GetValue())
5656
assert.Nil(t, rsp3.ConfigFile)
5757
}
5858

@@ -78,7 +78,8 @@ func TestClientSetupAndFileExisted(t *testing.T) {
7878

7979
// 拉取配置接口
8080
rsp3 := configService.GetConfigFileForClient(defaultCtx, fileInfo)
81-
assert.Equalf(t, api.ExecuteSuccess, rsp3.Code.GetValue(), "GetConfigFileForClient must success, acutal code : %d", rsp3.Code.GetValue())
81+
assert.Equalf(t, api.ExecuteSuccess, rsp3.Code.GetValue(),
82+
"GetConfigFileForClient must success, acutal code : %d", rsp3.Code.GetValue())
8283
assert.NotNil(t, rsp3.ConfigFile)
8384
assert.Equal(t, uint64(1), rsp3.ConfigFile.Version.GetValue())
8485
assert.Equal(t, configFile.Content.GetValue(), rsp3.ConfigFile.Content.GetValue())
@@ -164,17 +165,18 @@ func TestWatchConfigFileAtFirstPublish(t *testing.T) {
164165
received := make(chan uint64)
165166

166167
watchConfigFiles := assembleDefaultClientConfigFile(0)
167-
clientId := "TestWatchConfigFileAtFirstPublish-first"
168+
clientID := "TestWatchConfigFileAtFirstPublish-first"
168169

169170
defer func() {
170-
configService.WatchCenter().RemoveWatcher(clientId, watchConfigFiles)
171+
configService.WatchCenter().RemoveWatcher(clientID, watchConfigFiles)
171172
}()
172173

173-
configService.WatchCenter().AddWatcher(clientId, watchConfigFiles, func(clientId string, rsp *api.ConfigClientResponse) bool {
174-
t.Logf("clientId=[%s] receive config publish msg", clientId)
175-
received <- rsp.ConfigFile.Version.GetValue()
176-
return true
177-
})
174+
configService.WatchCenter().AddWatcher(clientID,
175+
watchConfigFiles, func(clientId string, rsp *api.ConfigClientResponse) bool {
176+
t.Logf("clientId=[%s] receive config publish msg", clientId)
177+
received <- rsp.ConfigFile.Version.GetValue()
178+
return true
179+
})
178180

179181
rsp := configService.CreateConfigFile(defaultCtx, configFile)
180182
assert.Equal(t, api.ExecuteSuccess, rsp.Code.GetValue())
@@ -193,13 +195,14 @@ func TestWatchConfigFileAtFirstPublish(t *testing.T) {
193195
// 版本号由于发布过一次,所以是1
194196
watchConfigFiles := assembleDefaultClientConfigFile(1)
195197

196-
clientId := "TestWatchConfigFileAtFirstPublish-second"
198+
clientID := "TestWatchConfigFileAtFirstPublish-second"
197199

198-
configService.WatchCenter().AddWatcher(clientId, watchConfigFiles, func(clientId string, rsp *api.ConfigClientResponse) bool {
199-
t.Logf("clientId=[%s] receive config publish msg", clientId)
200-
received <- rsp.ConfigFile.Version.GetValue()
201-
return true
202-
})
200+
configService.WatchCenter().AddWatcher(clientID,
201+
watchConfigFiles, func(clientId string, rsp *api.ConfigClientResponse) bool {
202+
t.Logf("clientId=[%s] receive config publish msg", clientId)
203+
received <- rsp.ConfigFile.Version.GetValue()
204+
return true
205+
})
203206

204207
rsp3 := configService.PublishConfigFile(defaultCtx, assembleConfigFileRelease(configFile))
205208
assert.Equal(t, api.ExecuteSuccess, rsp3.Code.GetValue())
@@ -209,7 +212,7 @@ func TestWatchConfigFileAtFirstPublish(t *testing.T) {
209212
assert.Equal(t, uint64(2), receivedVersion)
210213

211214
// 为了避免影响其它 case,删除订阅
212-
configService.WatchCenter().RemoveWatcher(clientId, watchConfigFiles)
215+
configService.WatchCenter().RemoveWatcher(clientID, watchConfigFiles)
213216
})
214217
}
215218

@@ -224,14 +227,15 @@ func Test10000ClientWatchConfigFile(t *testing.T) {
224227
receivedVersion := make(map[string]uint64)
225228
watchConfigFiles := assembleDefaultClientConfigFile(0)
226229
for i := 0; i < clientSize; i++ {
227-
clientId := fmt.Sprintf("Test10000ClientWatchConfigFile-client-id=%d", i)
228-
received[clientId] = false
229-
receivedVersion[clientId] = uint64(0)
230-
configService.WatchCenter().AddWatcher(clientId, watchConfigFiles, func(clientId string, rsp *api.ConfigClientResponse) bool {
231-
received[clientId] = true
232-
receivedVersion[clientId] = rsp.ConfigFile.Version.GetValue()
233-
return true
234-
})
230+
clientID := fmt.Sprintf("Test10000ClientWatchConfigFile-client-id=%d", i)
231+
received[clientID] = false
232+
receivedVersion[clientID] = uint64(0)
233+
configService.WatchCenter().AddWatcher(clientID,
234+
watchConfigFiles, func(clientId string, rsp *api.ConfigClientResponse) bool {
235+
received[clientId] = true
236+
receivedVersion[clientId] = rsp.ConfigFile.Version.GetValue()
237+
return true
238+
})
235239
}
236240

237241
// 创建并发布配置文件
@@ -258,11 +262,11 @@ func Test10000ClientWatchConfigFile(t *testing.T) {
258262
for _, v := range receivedVersion {
259263
receivedVerCnt += v
260264
}
261-
assert.Equal(t, uint64(len(receivedVersion)), uint64(receivedVerCnt))
265+
assert.Equal(t, uint64(len(receivedVersion)), receivedVerCnt)
262266

263267
// 为了避免影响其它case,删除订阅
264-
for clientId := range received {
265-
configService.WatchCenter().RemoveWatcher(clientId, watchConfigFiles)
268+
for clientID := range received {
269+
configService.WatchCenter().RemoveWatcher(clientID, watchConfigFiles)
266270
}
267271
}
268272

@@ -282,16 +286,17 @@ func TestDeleteConfigFile(t *testing.T) {
282286
time.Sleep(1200 * time.Millisecond)
283287

284288
// 客户端订阅
285-
clientId := randomStr()
289+
clientID := randomStr()
286290
received := make(chan uint64)
287291
watchConfigFiles := assembleDefaultClientConfigFile(0)
288292

289293
t.Log("add config watcher")
290294

291-
configService.WatchCenter().AddWatcher(clientId, watchConfigFiles, func(clientId string, rsp *api.ConfigClientResponse) bool {
292-
received <- rsp.ConfigFile.Version.GetValue()
293-
return true
294-
})
295+
configService.WatchCenter().AddWatcher(clientID,
296+
watchConfigFiles, func(clientId string, rsp *api.ConfigClientResponse) bool {
297+
received <- rsp.ConfigFile.Version.GetValue()
298+
return true
299+
})
295300

296301
// 删除配置文件
297302
t.Log("remove config file")
@@ -312,5 +317,5 @@ func TestDeleteConfigFile(t *testing.T) {
312317

313318
// 重新拉取配置,获取不到配置文件
314319
rsp4 := configService.GetConfigFileForClient(defaultCtx, fileInfo)
315-
assert.Equal(t, uint32(api.NotFoundResource), rsp4.Code.GetValue())
320+
assert.Equal(t, api.NotFoundResource, rsp4.Code.GetValue())
316321
}

0 commit comments

Comments
 (0)