Skip to content

Commit

Permalink
load-generator: create externalState file if it doesn't exist. (#4146)
Browse files Browse the repository at this point in the history
When using the `load-generator` with a config that
specifies an `"externalState"` file and `"dontSaveState: false` it's
clunky to have to manually create the file ahead of running the
`load-generator` (and put `{}` into it to avoid an unmarshalling error).
  • Loading branch information
cpu committed Apr 5, 2019
1 parent 9363101 commit de15c26
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions test/load-generator/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,24 @@ func (s *State) Snapshot(filename string) error {

// Restore previously generated accounts
func (s *State) Restore(filename string) error {
fmt.Printf("[+] Loading accounts from %s\n", filename)
content, err := ioutil.ReadFile(filename)
fmt.Printf("[+] Loading accounts from %q\n", filename)
// NOTE(@cpu): Using os.O_CREATE here explicitly to create the file if it does
// not exist.
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return err
}

content, err := ioutil.ReadAll(f)
if err != nil {
return err
}
// If the file's content is the empty string it was probably just created.
// Avoid an unmarshaling error by assuming an empty file is an empty snapshot.
if string(content) == "" {
content = []byte("{}")
}

snap := snapshot{}
err = json.Unmarshal(content, &snap)
if err != nil {
Expand Down

0 comments on commit de15c26

Please sign in to comment.