Skip to content

Commit

Permalink
bin data fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Oct 18, 2018
1 parent 43efde4 commit 2d13048
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -123,6 +123,12 @@ A simple unary call with metadata using template actions:
ghz -proto ./greeter.proto -call helloworld.Greeter.SayHello -d '{"name":"Joe"}' -m '{"trace_id":"{{.RequestNumber}}","timestamp":"{{.TimestampUnix}}"}' 0.0.0.0:50051
```

Using binary data file (see [writing a message](https://developers.google.com/protocol-buffers/docs/gotutorial#writing-a-message)):

```sh
ghz -proto ./greeter.proto -call helloworld.Greeter.SayHello -B ./hello_request_data.bin 0.0.0.0:50051
```

Custom number of requests and concurrency:

```sh
Expand Down
7 changes: 5 additions & 2 deletions config/config.go
Expand Up @@ -185,7 +185,7 @@ func (c *Config) Validate() error {
}

if strings.TrimSpace(c.DataPath) == "" {
if c.Data == nil {
if strings.TrimSpace(c.BinDataPath) == "" && len(c.BinData) == 0 && c.Data == nil {
return errors.New("data: is required")
}
}
Expand Down Expand Up @@ -253,12 +253,15 @@ func (c *Config) initData() error {
}

return json.Unmarshal(d, &c.Data)
} else if c.BinData != nil {
return nil
} else if strings.TrimSpace(c.BinDataPath) != "" {
d, err := ioutil.ReadFile(c.DataPath)
d, err := ioutil.ReadFile(c.BinDataPath)
if err != nil {
return err
}
c.BinData = d
return nil
}

return errors.New("No data specified")
Expand Down
24 changes: 24 additions & 0 deletions config/config_test.go
Expand Up @@ -388,6 +388,12 @@ func TestConfig_Validate(t *testing.T) {
err := c.Validate()
assert.NoError(t, err)
})

t.Run("BinDataPath", func(t *testing.T) {
c := &Config{Proto: "asdf.proto", Call: "call", Cert: "cert", BinDataPath: "asdf"}
err := c.Validate()
assert.NoError(t, err)
})
}

func TestConfig_initData(t *testing.T) {
Expand Down Expand Up @@ -418,6 +424,24 @@ func TestConfig_initData(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, c.Data, data)
})

t.Run("with bin data", func(t *testing.T) {
in, err := ioutil.ReadFile("../testdata/hello_request_data.bin")
assert.NoError(t, err)
c := &Config{BinData: in}
err = c.initData()
assert.NoError(t, err)
assert.Equal(t, c.BinData, in)
})

t.Run("with bin data file", func(t *testing.T) {
in, err := ioutil.ReadFile("../testdata/hello_request_data.bin")
assert.NoError(t, err)
c := &Config{BinDataPath: "../testdata/hello_request_data.bin"}
err = c.initData()
assert.NoError(t, err)
assert.Equal(t, c.BinData, in)
})
}

func TestConfig_initDurations(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions testdata/hello_request_data.bin
@@ -0,0 +1,2 @@

bob

0 comments on commit 2d13048

Please sign in to comment.