Skip to content

Commit

Permalink
fix the hot-reloading example
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Apr 24, 2019
1 parent bf06343 commit 80500fe
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions config_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,28 @@ import (
)

func ExampleConfig_SetHotReload() {
filename := "test_set_hot_reload.ini"

// The flag and cli parser will ignore the hot-reloading automatically.
conf := NewDefault(nil).AddParser(NewEnvVarParser(10, ""))
conf := NewDefault(nil)
conf.SetHotReload(conf.Parsers()...)
conf.RegisterOpt(Str("reload_opt", "abc", "test reload"))
conf.Parse([]string{}...) // We disables the cli arguments only for test.
conf.Parse("--config-file", filename)

time.Sleep(time.Millisecond * 10)
fmt.Println(conf.String("reload_opt"))

// Only for test
os.Setenv("RELOAD_OPT", "xyz")
// Only for test: Write the test config data into the file.
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
fmt.Println(err)
return
}
file.WriteString("[DEFAULT]\nreload_opt=xyz")
file.Close()
defer os.Remove(filename)

// Send the signal SIGHUP.
cmd := exec.Command("kill", "-HUP", fmt.Sprintf("%d", os.Getpid()))
if err := cmd.Run(); err != nil {
fmt.Println(err)
Expand Down

0 comments on commit 80500fe

Please sign in to comment.