Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/dynamic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ func AddRef(parent, ref *Config) {
if !added {
return
}
checksum := ref.Info.Checksum
ref.Listeners.Add(parent.Info.Url.String(), func(e ConfigEvent) {
// event Create is used for reading first time
if bytes.Equal(e.Config.Info.Checksum, checksum) && e.Event == Create {
return
}
e.Config.Info.Checksum = checksum

parent.Info.Time = ref.Info.Time
parent.Listeners.Invoke(ConfigEvent{Event: Update, Config: parent, Name: parent.Info.Path()})
if e.Event == Delete {
Expand Down
9 changes: 5 additions & 4 deletions config/dynamic/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ func TestAddRef(t *testing.T) {
name: "ref updates parent time",
test: func(t *testing.T) {
parent := &dynamic.Config{Info: dynamictest.NewConfigInfo(dynamictest.WithUrl("file://parent.yaml"))}
ref := &dynamic.Config{Info: dynamictest.NewConfigInfo(dynamictest.WithUrl("file://ref.yaml"))}
child := &dynamic.Config{Info: dynamictest.NewConfigInfo(dynamictest.WithUrl("file://ref.yaml"))}
d, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
ref.Info.Time = d
child.Info.Time = d

dynamic.AddRef(parent, ref)
ref.Listeners.Invoke(dynamic.ConfigEvent{Config: ref})
dynamic.AddRef(parent, child)
child.Info.Checksum = []byte{1}
child.Listeners.Invoke(dynamic.ConfigEvent{Config: child})

require.Equal(t, d, parent.Info.Time)
},
Expand Down
10 changes: 6 additions & 4 deletions js/script_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,17 @@ func TestMaxRedirects(t *testing.T) {
testcases := []struct {
name string
code string
}{{
name: "max redirects 0",
code: fmt.Sprintf(`import http from 'mokapi/http'
}{
{
name: "max redirects 0",
code: fmt.Sprintf(`import http from 'mokapi/http'
export default function() {
const res = http.get('%s', { maxRedirects: 0 });
console.log(res.headers.Location[0]);
}
`, server.URL),
}}
},
}

hook := test.NewGlobal()
for _, tc := range testcases {
Expand Down
7 changes: 5 additions & 2 deletions server/configwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (w *ConfigWatcher) Read(u *url.URL, v any) (*dynamic.Config, error) {
parse = true
} else {
c = e.config
parse = c.Data == nil
parse = c.Data == nil && v != nil
}

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

log.Debugf("processing %v", c.Info.Path())
log.Debugf("read processing %v", c.Info.Path())
defer log.Debugf("processed %v", c.Info.Path())

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

if c.Data == nil {
e.m.Unlock()
log.Debugf("processed %v", c.Info.Path())
return
}

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

e.m.Unlock()

log.Debugf("processed %v", c.Info.Path())

for _, l := range w.listener {
go l(evt)
}
Expand Down
50 changes: 49 additions & 1 deletion server/configwatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,54 @@ func TestConfigWatcher_Read(t *testing.T) {
require.Equal(t, c, mainFile.Config)
},
},
{
name: "provider triggers main file and then referenced file",
test: func(t *testing.T) {
w := NewConfigWatcher(&static.Config{})
mainPath := mustParse("foo://file.yml")
mainConfig := &dynamic.Config{Info: dynamic.ConfigInfo{Url: mainPath, Checksum: []byte{1}}}
refPath := mustParse("foo://ref.yml")
refConfig := &dynamic.Config{Info: dynamic.ConfigInfo{Url: refPath, Checksum: []byte{1}}}

var ch chan dynamic.ConfigEvent
w.providers["foo"] = &testprovider{
read: func(u *url.URL) (*dynamic.Config, error) {
c := &dynamic.Config{Info: dynamic.ConfigInfo{Url: u}}
c.Info.Checksum = []byte{1}
return c, nil
},
start: func(configs chan dynamic.ConfigEvent, pool *safe.Pool) error {
ch = configs
return nil
},
}
pool := safe.NewPool(context.Background())
w.Start(pool)
defer pool.Stop()

var events []string
w.AddListener(func(e dynamic.ConfigEvent) {
events = append(events, e.Config.Info.Path())
})

ch <- dynamic.ConfigEvent{Name: mainPath.String(), Event: dynamic.Create, Config: mainConfig}
c, err := w.Read(refPath, nil)
require.NoError(t, err)
require.NotNil(t, c)
dynamic.AddRef(mainConfig, c)

ch <- dynamic.ConfigEvent{Name: refPath.String(), Event: dynamic.Create, Config: refConfig}

time.Sleep(5 * time.Millisecond)
require.Equal(t, []string{"foo://file.yml", "foo://ref.yml"}, events)

refConfig.Info.Checksum = []byte{2}
ch <- dynamic.ConfigEvent{Name: refPath.String(), Event: dynamic.Create, Config: refConfig}

time.Sleep(5 * time.Millisecond)
require.ElementsMatch(t, []string{"foo://file.yml", "foo://ref.yml", "foo://file.yml", "foo://ref.yml"}, events)
},
},
}

for _, tc := range testcases {
Expand Down Expand Up @@ -465,8 +513,8 @@ func TestConfigWatcher_Start(t *testing.T) {
},
}

// no parallel git provider is not thread-safe
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
tc.f(t)
})
Expand Down
Loading