Skip to content

Commit

Permalink
Add test for sending TestAllTypes and TestMap to Go server.
Browse files Browse the repository at this point in the history
This should hopefully guarantee some kind of conformance in our marshalling.
  • Loading branch information
Johan Brandhorst committed Aug 13, 2017
1 parent e3e1a60 commit ea6ab55
Show file tree
Hide file tree
Showing 7 changed files with 1,572 additions and 1 deletion.
6 changes: 6 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ regenerate:
protoc proto/test/test.proto \
--gopherjs_out=plugins=grpc,Mgoogle/protobuf/empty.proto=github.com/johanbrandhorst/protobuf/ptypes/empty:./client/ \
--go_out=plugins=grpc:./server/
protoc ../protoc-gen-gopherjs/test/multi/multi1.proto ../protoc-gen-gopherjs/test/multi/multi2.proto ../protoc-gen-gopherjs/test/multi/multi3.proto \
-I../protoc-gen-gopherjs/test \
--go_out=plugins=grpc,Mmulti/multi1.proto=github.com/johanbrandhorst/protobuf/protoc-gen-gopherjs/test/multi:./server/proto
protoc ../protoc-gen-gopherjs/test/types/types.proto \
-I../protoc-gen-gopherjs/test \
--go_out=plugins=grpc,Mmulti/multi1.proto=github.com/johanbrandhorst/protobuf/test/server/proto/multi:./server/proto

# Regenerate dependencies
(cd ../protoc-gen-gopherjs/test && make regenerate)
Expand Down
228 changes: 227 additions & 1 deletion test/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"io"
"net/url"
"reflect"
"strings"

"github.com/gopherjs/gopherjs/js"
Expand All @@ -17,6 +19,8 @@ import (
"github.com/johanbrandhorst/protobuf/grpcweb/status"
grpctest "github.com/johanbrandhorst/protobuf/grpcweb/test"
gentest "github.com/johanbrandhorst/protobuf/protoc-gen-gopherjs/test"
"github.com/johanbrandhorst/protobuf/protoc-gen-gopherjs/test/multi"
"github.com/johanbrandhorst/protobuf/protoc-gen-gopherjs/test/types"
"github.com/johanbrandhorst/protobuf/ptypes/empty"
"github.com/johanbrandhorst/protobuf/test/client/proto/test"
"github.com/johanbrandhorst/protobuf/test/recoverer"
Expand All @@ -25,7 +29,15 @@ import (

//go:generate gopherjs build main.go -o html/index.js

var uri = strings.TrimSuffix(dom.GetWindow().Document().BaseURI(), shared.GopherJSServer+"/")
var uri string

func init() {
u, err := url.Parse(dom.GetWindow().Document().BaseURI())
if err != nil {
panic(err)
}
uri = u.Scheme + "://" + u.Hostname()
}

func typeTests() {
qunit.Module("Integration Types tests")
Expand Down Expand Up @@ -829,6 +841,220 @@ func serverTests(label, serverAddr, emptyServerAddr string) {

return nil
})

qunit.AsyncTest("Unary call to echo server with many types", func() interface{} {
c := types.NewEchoServiceClient(uri + serverAddr)
req := &types.TestAllTypes{
SingleInt32: 1,
SingleInt64: 2,
SingleUint32: 3,
SingleUint64: 4,
SingleSint32: 5,
SingleSint64: 6,
SingleFixed32: 7,
SingleFixed64: 8,
SingleSfixed32: 9,
SingleSfixed64: 10,
SingleFloat: 10.5,
SingleDouble: 11.5,
SingleBool: true,
SingleString: "Alfred",
SingleBytes: []byte("Megan"),
SingleNestedEnum: types.TestAllTypes_BAR,
SingleForeignEnum: types.ForeignEnum_FOREIGN_BAR,
SingleImportedMessage: &multitest.Multi1{
Color: multitest.Multi2_GREEN,
HatType: multitest.Multi3_FEDORA,
},
SingleNestedMessage: &types.TestAllTypes_NestedMessage{
B: 12,
},
SingleForeignMessage: &types.ForeignMessage{
C: 13,
},
RepeatedInt32: []int32{14, 15},
RepeatedInt64: []int64{16, 17},
RepeatedUint32: []uint32{18, 19},
RepeatedUint64: []uint64{20, 21},
RepeatedSint32: []int32{22, 23},
RepeatedSint64: []int64{24, 25},
RepeatedFixed32: []uint32{26, 27},
RepeatedFixed64: []uint64{28, 29},
RepeatedSfixed32: []int32{30, 31},
RepeatedSfixed64: []int64{32, 33},
RepeatedFloat: []float32{34.33, 35.34},
RepeatedDouble: []float64{36.35, 37.36},
RepeatedBool: []bool{true, false, true},
RepeatedString: []string{"Alfred", "Robin", "Simon"},
RepeatedBytes: [][]byte{[]byte("David"), []byte("Henrik")},
RepeatedNestedEnum: []types.TestAllTypes_NestedEnum{types.TestAllTypes_BAR, types.TestAllTypes_BAZ},
RepeatedForeignEnum: []types.ForeignEnum{types.ForeignEnum_FOREIGN_BAR, types.ForeignEnum_FOREIGN_BAZ},
RepeatedImportedMessage: []*multitest.Multi1{
{
Color: multitest.Multi2_RED,
HatType: multitest.Multi3_FEZ,
},
{
Color: multitest.Multi2_GREEN,
HatType: multitest.Multi3_FEDORA,
},
},
RepeatedNestedMessage: []*types.TestAllTypes_NestedMessage{
{
B: 38,
},
{
B: 39,
},
},
RepeatedForeignMessage: []*types.ForeignMessage{
{
C: 40,
},
{
C: 41,
},
},
OneofField: &types.TestAllTypes_OneofImportedMessage{
OneofImportedMessage: &multitest.Multi1{
Multi2: &multitest.Multi2{
RequiredValue: 42,
Color: multitest.Multi2_BLUE,
},
Color: multitest.Multi2_RED,
HatType: multitest.Multi3_FEDORA,
},
},
}

go func() {
defer recoverer.Recover() // recovers any panics and fails tests
defer qunit.Start()

resp, err := c.EchoAllTypes(context.Background(), req)
if err != nil {
st := status.FromError(err)
qunit.Ok(false, "Unexpected error:"+st.Error())
return
}
if !reflect.DeepEqual(req, resp) {
qunit.Ok(false, fmt.Sprintf("response and request differed: Req:\n%v\nResp:\n%v", req, resp))
return
}

qunit.Ok(true, "Request and Response matched")
}()

return nil
})

qunit.AsyncTest("Unary call to echo server with many maps", func() interface{} {
c := types.NewEchoServiceClient(uri + serverAddr)
req := &types.TestMap{
MapInt32Int32: map[int32]int32{
1: 2,
3: 4,
},
MapInt64Int64: map[int64]int64{
5: 6,
7: 8,
},
MapUint32Uint32: map[uint32]uint32{
9: 10,
11: 12,
},
MapUint64Uint64: map[uint64]uint64{
13: 14,
15: 16,
},
MapSint32Sint32: map[int32]int32{
17: 18,
19: 20,
},
MapSint64Sint64: map[int64]int64{
21: 22,
23: 24,
},
MapFixed32Fixed32: map[uint32]uint32{
25: 26,
27: 28,
},
MapFixed64Fixed64: map[uint64]uint64{
29: 30,
31: 32,
},
MapSfixed32Sfixed32: map[int32]int32{
33: 34,
35: 36,
},
MapSfixed64Sfixed64: map[int64]int64{
37: 38,
39: 40,
},
MapInt32Float: map[int32]float32{
41: 42.41,
432: 44.43,
},
MapInt32Double: map[int32]float64{
45: 46.45,
47: 48.47,
},
MapBoolBool: map[bool]bool{
true: false,
false: false,
},
MapStringString: map[string]string{
"Henrik": "David",
"Simon": "Robin",
},
MapInt32Bytes: map[int32][]byte{
49: []byte("Astrid"),
50: []byte("Ebba"),
},
MapInt32Enum: map[int32]types.MapEnum{
51: types.MapEnum_MAP_ENUM_BAR,
52: types.MapEnum_MAP_ENUM_BAZ,
},
MapInt32ForeignMessage: map[int32]*types.ForeignMessage{
53: {C: 54},
55: {C: 56},
},
MapInt32ImportedMessage: map[int32]*multitest.Multi1{
57: {
Multi2: &multitest.Multi2{
RequiredValue: 58,
Color: multitest.Multi2_RED,
},
Color: multitest.Multi2_GREEN,
HatType: multitest.Multi3_FEZ,
},
59: {
Color: multitest.Multi2_BLUE,
HatType: multitest.Multi3_FEDORA,
},
},
}

go func() {
defer recoverer.Recover() // recovers any panics and fails tests
defer qunit.Start()

resp, err := c.EchoMaps(context.Background(), req)
if err != nil {
st := status.FromError(err)
qunit.Ok(false, "Unexpected error:"+st.Error())
return
}
if !reflect.DeepEqual(req, resp) {
qunit.Ok(false, fmt.Sprintf("response and request differed: Req:\n%v\nResp:\n%v", req, resp))
return
}

qunit.Ok(true, "Request and Response matched")
}()

return nil
})
}

func main() {
Expand Down
10 changes: 10 additions & 0 deletions test/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import (
"google.golang.org/grpc/transport"

testproto "github.com/johanbrandhorst/protobuf/test/server/proto/test"
"github.com/johanbrandhorst/protobuf/test/server/proto/types"
"github.com/johanbrandhorst/protobuf/test/shared"
)

func main() {
grpcServer := grpc.NewServer()
testproto.RegisterTestServiceServer(grpcServer, &testSrv{})
types.RegisterEchoServiceServer(grpcServer, &testSrv{})
grpclog.SetLogger(log.New(os.Stdout, "testserver: ", log.LstdFlags))

wrappedServer := grpcweb.WrapServer(grpcServer)
Expand Down Expand Up @@ -199,3 +201,11 @@ func (s *testSrv) PingList(ping *testproto.PingRequest, stream testproto.TestSer
}
return nil
}

func (s *testSrv) EchoAllTypes(ctx context.Context, in *types.TestAllTypes) (*types.TestAllTypes, error) {
return in, nil
}

func (s *testSrv) EchoMaps(ctx context.Context, in *types.TestMap) (*types.TestMap, error) {
return in, nil
}
84 changes: 84 additions & 0 deletions test/server/proto/multi/multi1.pb.go

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

Loading

0 comments on commit ea6ab55

Please sign in to comment.