Skip to content

Commit

Permalink
Added unit test.
Browse files Browse the repository at this point in the history
Signed-off-by: Du Zheng <zsuzhengdu@gmail.com>
  • Loading branch information
zsuzhengdu committed Apr 2, 2021
1 parent 29e2804 commit 5a4a378
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pkg/reloader/reloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ func (r *Reloader) Watch(ctx context.Context) error {
}
}

if r.watchInterval == 0 {
// Skip watching the file-system
return nil
}

for _, dir := range r.watchedDirs {
if err := r.watcher.addDirectory(dir); err != nil {
return errors.Wrapf(err, "add directory %s to watcher", dir)
Expand Down Expand Up @@ -291,11 +296,6 @@ func (r *Reloader) apply(ctx context.Context) error {
if err := os.Rename(tmpFile, r.cfgOutputFile); err != nil {
return errors.Wrap(err, "rename file")
}

if r.watchInterval == 0 {
<-ctx.Done()
return nil
}
}
}

Expand Down
90 changes: 90 additions & 0 deletions pkg/reloader/reloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,93 @@ func TestReloaderDirectoriesApplyBasedOnWatchInterval(t *testing.T) {
testutil.Ok(t, err)
testutil.Equals(t, 2, reloads.Load().(int))
}

func TestReloader_ConfigApplyWithWatchIntervalEqualsZero(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()

l, err := net.Listen("tcp", "localhost:0")
testutil.Ok(t, err)

reloads := &atomic.Value{}
reloads.Store(0)
i := 0
srv := &http.Server{}
srv.Handler = http.HandlerFunc(func(resp http.ResponseWriter, r *http.Request) {
i++
if i%2 == 0 {
// Every second request, fail to ensure that retry works.
resp.WriteHeader(http.StatusServiceUnavailable)
return
}

reloads.Store(reloads.Load().(int) + 1) // The only writer.
resp.WriteHeader(http.StatusOK)
})
go func() { _ = srv.Serve(l) }()
defer func() { testutil.Ok(t, srv.Close()) }()

reloadURL, err := url.Parse(fmt.Sprintf("http://%s", l.Addr().String()))
testutil.Ok(t, err)

dir, err := ioutil.TempDir("", "reloader-cfg-test")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()

testutil.Ok(t, os.Mkdir(filepath.Join(dir, "in"), os.ModePerm))
testutil.Ok(t, os.Mkdir(filepath.Join(dir, "out"), os.ModePerm))

var (
input = filepath.Join(dir, "in", "cfg.yaml.tmpl")
output = filepath.Join(dir, "out", "cfg.yaml")
)
reloader := New(nil, nil, &Options{
ReloadURL: reloadURL,
CfgFile: input,
CfgOutputFile: output,
WatchedDirs: nil,
WatchInterval: 0, // Set WatchInterval equals to 0
RetryInterval: 100 * time.Millisecond,
DelayInterval: 1 * time.Millisecond,
})

testutil.Ok(t, ioutil.WriteFile(input, []byte(`
config:
a: 1
b: 2
c: 3
`), os.ModePerm))

rctx, cancel2 := context.WithCancel(ctx)
g := sync.WaitGroup{}
g.Add(1)
go func() {
defer g.Done()
testutil.Ok(t, reloader.Watch(rctx))
}()

for {
select {
case <-ctx.Done():
fmt.Println("cancelled")
break
case <-time.After(300 * time.Millisecond):
fmt.Println("timeout")
}

if reloads.Load().(int) == 1 {
// Initial apply seen (without doing nothing).
f, err := ioutil.ReadFile(output)
testutil.Ok(t, err)
testutil.Equals(t, `
config:
a: 1
b: 2
c: 3
`, string(f))
break
}
}
cancel2()
g.Wait()
}

0 comments on commit 5a4a378

Please sign in to comment.