Skip to content

Commit

Permalink
feat: grpc consumer example
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Jun 28, 2022
1 parent 2580b6f commit d373fdc
Show file tree
Hide file tree
Showing 4 changed files with 1,093 additions and 0 deletions.
90 changes: 90 additions & 0 deletions examples/grpc/grpc_consumer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package grpc

import (
"context"
"fmt"
"os"
"testing"
"time"

"github.com/pact-foundation/pact-go/v2/log"
message "github.com/pact-foundation/pact-go/v2/message/v4"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/pact-foundation/pact-go/v2/examples/grpc/routeguide"
)

func TestGrpcInteraction(t *testing.T) {
p, _ := message.NewSynchronousPact(message.Config{
Consumer: "consumer",
Provider: "provider",
PactDir: "/tmp/",
})
log.SetLogLevel("INFO")

dir, _ := os.Getwd()
path := fmt.Sprintf("%s/routeguide/route_guide.proto", dir)

grpcInteraction := `{
"pact:proto": "` + path + `",
"pact:proto-service": "RouteGuide/GetFeature",
"pact:content-type": "application/protobuf",
"request": {
"latitude": "matching(number, 180)",
"longitude": "matching(number, 200)"
},
"response": {
"name": "matching(type, 'Big Tree')",
"location": {
"latitude": "matching(number, 180)",
"longitude": "matching(number, 200)"
}
}
}`

err := p.AddSynchronousMessage("Route guide - GetFeature").
Given("feature 'Big Tree' exists").
UsingPlugin(message.PluginConfig{
Plugin: "protobuf",
Version: "0.1.7",
}).
WithContents(grpcInteraction, "application/protobuf").
StartTransport("grpc", "127.0.0.1", nil). // For plugin tests, we can't assume if a transport is needed, so this is optional
ExecuteTest(t, func(transport message.TransportConfig, m message.SynchronousMessage) error {
fmt.Println("gRPC transport running on", transport)

// Establish the gRPC connection
conn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", transport.Port), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal("unable to communicate to grpc server", err)
}
defer conn.Close()

// Create the gRPC client
c := routeguide.NewRouteGuideClient(conn)

point := &routeguide.Point{
Latitude: 180,
Longitude: 200,
}

// Now we can make a normal gRPC request
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
feature, err := c.GetFeature(ctx, point)

if err != nil {
t.Fatal(err.Error())
}

feature.GetLocation()
assert.Equal(t, "Big Tree", feature.GetName())
assert.Equal(t, int32(180), feature.GetLocation().GetLatitude())

return nil
})

assert.NoError(t, err)
}

0 comments on commit d373fdc

Please sign in to comment.