Skip to content

Commit 9735c88

Browse files
authored
Merge pull request #677 from marle3003/develop
Develop
2 parents 2a0ebe6 + 64b3b15 commit 9735c88

File tree

5 files changed

+72
-11
lines changed

5 files changed

+72
-11
lines changed

config/dynamic/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ func AddRef(parent, ref *Config) {
7474
if !added {
7575
return
7676
}
77+
checksum := ref.Info.Checksum
7778
ref.Listeners.Add(parent.Info.Url.String(), func(e ConfigEvent) {
79+
// event Create is used for reading first time
80+
if bytes.Equal(e.Config.Info.Checksum, checksum) && e.Event == Create {
81+
return
82+
}
83+
e.Config.Info.Checksum = checksum
84+
7885
parent.Info.Time = ref.Info.Time
7986
parent.Listeners.Invoke(ConfigEvent{Event: Update, Config: parent, Name: parent.Info.Path()})
8087
if e.Event == Delete {

config/dynamic/config_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ func TestAddRef(t *testing.T) {
9797
name: "ref updates parent time",
9898
test: func(t *testing.T) {
9999
parent := &dynamic.Config{Info: dynamictest.NewConfigInfo(dynamictest.WithUrl("file://parent.yaml"))}
100-
ref := &dynamic.Config{Info: dynamictest.NewConfigInfo(dynamictest.WithUrl("file://ref.yaml"))}
100+
child := &dynamic.Config{Info: dynamictest.NewConfigInfo(dynamictest.WithUrl("file://ref.yaml"))}
101101
d, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
102-
ref.Info.Time = d
102+
child.Info.Time = d
103103

104-
dynamic.AddRef(parent, ref)
105-
ref.Listeners.Invoke(dynamic.ConfigEvent{Config: ref})
104+
dynamic.AddRef(parent, child)
105+
child.Info.Checksum = []byte{1}
106+
child.Listeners.Invoke(dynamic.ConfigEvent{Config: child})
106107

107108
require.Equal(t, d, parent.Info.Time)
108109
},

js/script_http_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,15 +424,17 @@ func TestMaxRedirects(t *testing.T) {
424424
testcases := []struct {
425425
name string
426426
code string
427-
}{{
428-
name: "max redirects 0",
429-
code: fmt.Sprintf(`import http from 'mokapi/http'
427+
}{
428+
{
429+
name: "max redirects 0",
430+
code: fmt.Sprintf(`import http from 'mokapi/http'
430431
export default function() {
431432
const res = http.get('%s', { maxRedirects: 0 });
432433
console.log(res.headers.Location[0]);
433434
}
434435
`, server.URL),
435-
}}
436+
},
437+
}
436438

437439
hook := test.NewGlobal()
438440
for _, tc := range testcases {

server/configwatcher.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (w *ConfigWatcher) Read(u *url.URL, v any) (*dynamic.Config, error) {
7373
parse = true
7474
} else {
7575
c = e.config
76-
parse = c.Data == nil
76+
parse = c.Data == nil && v != nil
7777
}
7878

7979
w.m.Unlock()
@@ -87,7 +87,7 @@ func (w *ConfigWatcher) Read(u *url.URL, v any) (*dynamic.Config, error) {
8787
c.Data = v
8888
}
8989

90-
log.Debugf("processing %v", c.Info.Path())
90+
log.Debugf("read processing %v", c.Info.Path())
9191
defer log.Debugf("processed %v", c.Info.Path())
9292

9393
// Currently, read does not validate config. Add Validate would break compatibility
@@ -245,6 +245,7 @@ func (w *ConfigWatcher) configChanged(evt dynamic.ConfigEvent) {
245245

246246
if c.Data == nil {
247247
e.m.Unlock()
248+
log.Debugf("processed %v", c.Info.Path())
248249
return
249250
}
250251

@@ -256,6 +257,8 @@ func (w *ConfigWatcher) configChanged(evt dynamic.ConfigEvent) {
256257

257258
e.m.Unlock()
258259

260+
log.Debugf("processed %v", c.Info.Path())
261+
259262
for _, l := range w.listener {
260263
go l(evt)
261264
}

server/configwatcher_test.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,54 @@ func TestConfigWatcher_Read(t *testing.T) {
389389
require.Equal(t, c, mainFile.Config)
390390
},
391391
},
392+
{
393+
name: "provider triggers main file and then referenced file",
394+
test: func(t *testing.T) {
395+
w := NewConfigWatcher(&static.Config{})
396+
mainPath := mustParse("foo://file.yml")
397+
mainConfig := &dynamic.Config{Info: dynamic.ConfigInfo{Url: mainPath, Checksum: []byte{1}}}
398+
refPath := mustParse("foo://ref.yml")
399+
refConfig := &dynamic.Config{Info: dynamic.ConfigInfo{Url: refPath, Checksum: []byte{1}}}
400+
401+
var ch chan dynamic.ConfigEvent
402+
w.providers["foo"] = &testprovider{
403+
read: func(u *url.URL) (*dynamic.Config, error) {
404+
c := &dynamic.Config{Info: dynamic.ConfigInfo{Url: u}}
405+
c.Info.Checksum = []byte{1}
406+
return c, nil
407+
},
408+
start: func(configs chan dynamic.ConfigEvent, pool *safe.Pool) error {
409+
ch = configs
410+
return nil
411+
},
412+
}
413+
pool := safe.NewPool(context.Background())
414+
w.Start(pool)
415+
defer pool.Stop()
416+
417+
var events []string
418+
w.AddListener(func(e dynamic.ConfigEvent) {
419+
events = append(events, e.Config.Info.Path())
420+
})
421+
422+
ch <- dynamic.ConfigEvent{Name: mainPath.String(), Event: dynamic.Create, Config: mainConfig}
423+
c, err := w.Read(refPath, nil)
424+
require.NoError(t, err)
425+
require.NotNil(t, c)
426+
dynamic.AddRef(mainConfig, c)
427+
428+
ch <- dynamic.ConfigEvent{Name: refPath.String(), Event: dynamic.Create, Config: refConfig}
429+
430+
time.Sleep(5 * time.Millisecond)
431+
require.Equal(t, []string{"foo://file.yml", "foo://ref.yml"}, events)
432+
433+
refConfig.Info.Checksum = []byte{2}
434+
ch <- dynamic.ConfigEvent{Name: refPath.String(), Event: dynamic.Create, Config: refConfig}
435+
436+
time.Sleep(5 * time.Millisecond)
437+
require.ElementsMatch(t, []string{"foo://file.yml", "foo://ref.yml", "foo://file.yml", "foo://ref.yml"}, events)
438+
},
439+
},
392440
}
393441

394442
for _, tc := range testcases {
@@ -465,8 +513,8 @@ func TestConfigWatcher_Start(t *testing.T) {
465513
},
466514
}
467515

516+
// no parallel git provider is not thread-safe
468517
for _, tc := range testcases {
469-
tc := tc
470518
t.Run(tc.name, func(t *testing.T) {
471519
tc.f(t)
472520
})

0 commit comments

Comments
 (0)