Skip to content

Commit

Permalink
feat: gzip support (#1)
Browse files Browse the repository at this point in the history
* gzip support with example and integration test
  • Loading branch information
TheRTK committed Feb 25, 2023
1 parent 7a3c15c commit 5d6ce86
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ jobs:
uses: ./
with:
entrypoint: example/stub-subfolders/entrypoint.sh
- name: Run gzip example
uses: ./
with:
entrypoint: example/simple-with-gzip/entrypoint.sh
44 changes: 44 additions & 0 deletions example/simple-with-gzip/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"context"
"log"
"os"
"time"

pb "github.com/tokopedia/gripmock/protogen/example/simple-with-gzip"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding/gzip"
)

func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

// Set up a connection to the server.
conn, err := grpc.DialContext(ctx, "localhost:4770", grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()

c := pb.NewGripmockClient(conn)

// Contact the server and print out its response.
name := "tokopedia"
if len(os.Args) > 1 {
name = os.Args[1]
}
r, err := c.SayHello(context.Background(), &pb.Request{Name: name}, grpc.UseCompressor(gzip.Name))
if err != nil {
log.Fatalf("error from grpc: %v", err)
}
log.Printf("Greeting: %s (return code %d)", r.Message, r.ReturnCode)

name = "world"
r, err = c.SayHello(context.Background(), &pb.Request{Name: name}, grpc.UseCompressor(gzip.Name))
if err != nil {
log.Fatalf("error from grpc: %v", err)
}
log.Printf("Greeting: %s (return code %d)", r.Message, r.ReturnCode)
}
10 changes: 10 additions & 0 deletions example/simple-with-gzip/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env sh

# this file is used by .github/workflows/integration-test.yml

gripmock --stub=example/simple-with-gzip/stub example/simple-with-gzip/simple-with-gzip.proto &

# wait for generated files to be available and gripmock is up
sleep 2

go run example/simple-with-gzip/client/*.go
23 changes: 23 additions & 0 deletions example/simple-with-gzip/simple-with-gzip.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
syntax = "proto3";
option go_package = "github.com/tokopedia/gripmock/protogen/data/protogen/example/simple-with-gzip";

package simple_with_gzip;



// The Gripmock service definition.
service Gripmock {
// simple unary method
rpc SayHello (Request) returns (Reply);
}

// The request message containing the user's name.
message Request {
string name = 1;
}

// The response message containing the greetings
message Reply {
string message = 1;
int32 return_code = 2;
}
32 changes: 32 additions & 0 deletions example/simple-with-gzip/stub/simple-with-gzip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"service": "Gripmock",
"method": "SayHello",
"input": {
"equals": {
"name": "tokopedia"
}
},
"output": {
"data": {
"message": "Hello Tokopedia",
"return_code": 1
}
}
},
{
"service": "Gripmock",
"method": "SayHello",
"input": {
"equals": {
"name": "world"
}
},
"output": {
"data": {
"message": "Hello World",
"return_code": 1
}
}
}
]
1 change: 1 addition & 0 deletions protoc-gen-gripmock/server.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/golang/protobuf/jsonpb"
"golang.org/x/net/context"
"google.golang.org/grpc"
_ "google.golang.org/grpc/encoding/gzip"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/runtime/protoiface"
)
Expand Down
Loading

0 comments on commit 5d6ce86

Please sign in to comment.