Skip to content

Commit

Permalink
Add tests for well known types and documentation (#180)
Browse files Browse the repository at this point in the history
* add tests for wrapped types

* remove fail in wrapped test and fix go mod files

* fix wrapped generated code and go mod files

* fix wrapped generated code and go mod files

* add wrapped file test and example
  • Loading branch information
bojand authored Apr 21, 2020
1 parent 207bb42 commit 3e5c8f2
Show file tree
Hide file tree
Showing 10 changed files with 459 additions and 6 deletions.
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,8 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.5.0 h1:OI5t8sDa1Or+q8AeE+yKeB/SDYioSHAgcVljj9JIETY=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/multierr v1.2.0 h1:6I+W7f5VwC5SV9dNrZ3qXrDB9mD0dyGOi/ZJmYw03T4=
go.uber.org/multierr v1.2.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.3.0 h1:sFPn2GLc3poCkfrpIXGhBD2X0CMIo4Q/zSULXrj/+uc=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4=
Expand Down
34 changes: 34 additions & 0 deletions internal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/bojand/ghz/internal/helloworld"
"github.com/bojand/ghz/internal/sleep"
"github.com/bojand/ghz/internal/wrapped"
)

// TestPort is the port.
Expand Down Expand Up @@ -95,3 +96,36 @@ func StartSleepServer(secure bool) (*sleep.SleepService, *grpc.Server, error) {

return &ss, s, err
}

// StartWrappedServer starts the wrapped test server
func StartWrappedServer(secure bool) (*wrapped.WrappedService, *grpc.Server, error) {
lis, err := net.Listen("tcp", ":0")
if err != nil {
return nil, nil, err
}

var opts []grpc.ServerOption

if secure {
creds, err := credentials.NewServerTLSFromFile("../testdata/localhost.crt", "../testdata/localhost.key")
if err != nil {
return nil, nil, err
}
opts = []grpc.ServerOption{grpc.Creds(creds)}
}

s := grpc.NewServer(opts...)

ws := wrapped.WrappedService{}
wrapped.RegisterWrappedServiceServer(s, &ws)
reflection.Register(s)

TestPort = strconv.Itoa(lis.Addr().(*net.TCPAddr).Port)
TestLocalhost = "localhost:" + TestPort

go func() {
_ = s.Serve(lis)
}()

return &ws, s, err
}
13 changes: 13 additions & 0 deletions internal/wrapped/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package wrapped

import (
"context"

wrappers "github.com/golang/protobuf/ptypes/wrappers"
)

type WrappedService struct{}

func (s *WrappedService) GetMessage(ctx context.Context, req *wrappers.StringValue) (*wrappers.StringValue, error) {
return &wrappers.StringValue{Value: "Hello: " + req.GetValue()}, nil
}
121 changes: 121 additions & 0 deletions internal/wrapped/wrapped.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions runner/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runner

import (
"encoding/json"
"fmt"
"testing"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -45,6 +46,14 @@ func TestData_createPayloads(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, mtdTestUnaryTwo)

mtdWrapped, err := protodesc.GetMethodDescFromProto(
"wrapped.WrappedService.GetMessage",
"../testdata/wrapped.proto",
[]string{"../testdata"})

assert.NoError(t, err)
assert.NotNil(t, mtdUnary)

t.Run("get empty when empty", func(t *testing.T) {
inputs, err := createPayloadsFromJSON("", mtdUnary)
assert.NoError(t, err)
Expand Down Expand Up @@ -204,6 +213,18 @@ func TestData_createPayloads(t *testing.T) {
assert.NotNil(t, inputs[0])
})

t.Run("create data with well-known wrapped type", func(t *testing.T) {
jsonData := `"foo"`

inputs, err := createPayloadsFromJSON(jsonData, mtdWrapped)
assert.NoError(t, err)
fmt.Println(err)
fmt.Println(inputs)
assert.NotNil(t, inputs)
assert.Len(t, inputs, 1)
assert.EqualValues(t, "foo", inputs[0].GetFieldByName("value"))
})

t.Run("create slice from single message binary data", func(t *testing.T) {
msg1 := &helloworld.HelloRequest{}
msg1.Name = "bob"
Expand Down
81 changes: 81 additions & 0 deletions runner/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,3 +1059,84 @@ func TestRunUnaryDurationStop(t *testing.T) {
assert.Equal(t, 6, report.StatusCodeDist["OK"])
})
}

func TestRunWrappedUnary(t *testing.T) {

_, s, err := internal.StartWrappedServer(false)

if err != nil {
assert.FailNow(t, err.Error())
}

defer s.Stop()

t.Run("json string data", func(t *testing.T) {
report, err := Run(
"wrapped.WrappedService.GetMessage",
internal.TestLocalhost,
WithProtoFile("../testdata/wrapped.proto", []string{"../testdata"}),
WithTotalRequests(1),
WithConcurrency(1),
WithTimeout(time.Duration(20*time.Second)),
WithDialTimeout(time.Duration(20*time.Second)),
WithDataFromJSON(`"foo"`),
WithInsecure(true),
)

assert.NoError(t, err)

assert.NotNil(t, report)

assert.Equal(t, 1, int(report.Count))
assert.NotZero(t, report.Average)
assert.NotZero(t, report.Fastest)
assert.NotZero(t, report.Slowest)
assert.NotZero(t, report.Rps)
assert.Empty(t, report.Name)
assert.NotEmpty(t, report.Date)
assert.NotEmpty(t, report.Options)
assert.NotEmpty(t, report.Details)
assert.Equal(t, true, report.Options.Insecure)
assert.NotEmpty(t, report.LatencyDistribution)
assert.Equal(t, ReasonNormalEnd, report.EndReason)
assert.Empty(t, report.ErrorDist)

assert.Equal(t, report.Average, report.Slowest)
assert.Equal(t, report.Average, report.Fastest)
})

t.Run("json string data from file", func(t *testing.T) {
report, err := Run(
"wrapped.WrappedService.GetMessage",
internal.TestLocalhost,
WithProtoFile("../testdata/wrapped.proto", []string{"../testdata"}),
WithTotalRequests(1),
WithConcurrency(1),
WithTimeout(time.Duration(20*time.Second)),
WithDialTimeout(time.Duration(20*time.Second)),
WithDataFromFile(`../testdata/wrapped_data.json`),
WithInsecure(true),
)

assert.NoError(t, err)

assert.NotNil(t, report)

assert.Equal(t, 1, int(report.Count))
assert.NotZero(t, report.Average)
assert.NotZero(t, report.Fastest)
assert.NotZero(t, report.Slowest)
assert.NotZero(t, report.Rps)
assert.Empty(t, report.Name)
assert.NotEmpty(t, report.Date)
assert.NotEmpty(t, report.Options)
assert.NotEmpty(t, report.Details)
assert.Equal(t, true, report.Options.Insecure)
assert.NotEmpty(t, report.LatencyDistribution)
assert.Equal(t, ReasonNormalEnd, report.EndReason)
assert.Empty(t, report.ErrorDist)

assert.Equal(t, report.Average, report.Slowest)
assert.Equal(t, report.Average, report.Fastest)
})
}
Loading

0 comments on commit 3e5c8f2

Please sign in to comment.