Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added output parameter to keep server stream open #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
FROM golang:alpine

FROM golang:1.15.3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what requires this PR to use this golang version?

RUN mkdir /proto

RUN mkdir /stubs

RUN apk -U --no-cache add git protobuf

RUN go get -u -v github.com/golang/protobuf/protoc-gen-go \
google.golang.org/grpc \
google.golang.org/grpc/reflection \
golang.org/x/net/context \
github.com/go-chi/chi \
github.com/lithammer/fuzzysearch/fuzzy \
golang.org/x/tools/imports
# System setup
RUN apt-get update && apt-get install -y unzip git curl

# Install protoc
ENV PATH $PATH:$(go env GOPATH)/bin
ENV PROTOBUF_URL https://github.com/protocolbuffers/protobuf/releases/download/v3.12.2/protoc-3.12.2-linux-x86_64.zip
RUN curl -L -o /tmp/protobuf.tar.gz $PROTOBUF_URL
WORKDIR /tmp/
RUN unzip protobuf.tar.gz
RUN mv /tmp/bin/protoc /usr/bin

RUN git clone --depth 1 --branch v1.3.5 https://github.com/golang/protobuf
RUN cd /tmp/protobuf/protoc-gen-go && go build -o protoc-gen-go
RUN cp /tmp/protobuf/protoc-gen-go/protoc-gen-go $(go env GOPATH)/bin
RUN rm -rf /tmp/protobuf/

RUN go get github.com/markbates/pkger/cmd/pkger

Expand Down
22 changes: 14 additions & 8 deletions protoc-gen-gripmock/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,30 @@ func main() {
}

file := plugin.NewGeneratedFile("server.go", ".")
file.Write(buf.Bytes())

_, err = file.Write(buf.Bytes())
if err != nil {
log.Fatalf("Unable to write to file %v", err)
}
// Generate a response from our plugin and marshall as protobuf
out, err := proto.Marshal(plugin.Response())
if err != nil {
log.Fatalf("error marshalling plugin response: %v", err)
}

// Write the response to stdout, to be picked up by protoc
os.Stdout.Write(out)
_, err = os.Stdout.Write(out)
if err != nil {
log.Printf("Error %v writing to stdout", err)
}
}

type generatorParam struct {
Services []Service
Dependencies map[string]string
GrpcAddr string
AdminPort string
PbPath string
Services []Service
Dependencies map[string]string
GrpcAddr string
AdminPort string
PbPath string
KeepServerStreamOpen bool
}

type Service struct {
Expand Down
40 changes: 24 additions & 16 deletions protoc-gen-gripmock/server.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,27 @@ type {{.Name}} struct{}
{{ define "standard_method" }}
func (s *{{.ServiceName}}) {{.Name}}(ctx context.Context, in *{{.Input}}) (*{{.Output}},error){
out := &{{.Output}}{}
err := findStub("{{.ServiceName}}", "{{.Name}}", in, out)
_, err := findStub("{{.ServiceName}}", "{{.Name}}", in, out)
return out, err
}
{{ end }}

{{ define "server_stream_method" }}
func (s *{{.ServiceName}}) {{.Name}}(in *{{.Input}},stream {{.ServiceName}}_{{.Name}}Server) error {
out := &{{.Output}}{}
err := findStub("{{.ServiceName}}", "{{.Name}}", in, out)
if err!=nil {
return err
for {
out := &{{.Output}}{}
KeepServerStreamOpen, err := findStub("{{.ServiceName}}", "{{.Name}}", in, out)
if err!=nil {
return err
}
if err := stream.Send(out); err != nil {
return err
}
if !KeepServerStreamOpen {
return nil
}
time.Sleep(25 * time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it 25 seconds?

}

return stream.Send(out)
}
{{ end }}

Expand Down Expand Up @@ -142,11 +149,12 @@ type payload struct {
}

type response struct {
Data interface{} `json:"data"`
Error string `json:"error"`
Data interface{} `json:"data"`
Error string `json:"error"`
KeepServerStreamOpen bool `json:"keepServerStreamOpen"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to put it under Meta field, and instead of boolean, it should receive duration.

}

func findStub(service, method string, in, out protoiface.MessageV1) error {
func findStub(service, method string, in, out protoiface.MessageV1) (bool, error) {
url := fmt.Sprintf("http://localhost%s/find", HTTP_PORT)
pyl := payload{
Service: service,
Expand All @@ -155,30 +163,30 @@ func findStub(service, method string, in, out protoiface.MessageV1) error {
}
byt, err := json.Marshal(pyl)
if err != nil {
return err
return false, err
}
reader := bytes.NewReader(byt)
resp, err := http.DefaultClient.Post(url, "application/json", reader)
if err != nil {
return fmt.Errorf("Error request to stub server %v",err)
return false, fmt.Errorf("Error request to stub server %v",err)
}

if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf(string(body))
return false, fmt.Errorf(string(body))
}

respRPC := new(response)
err = json.NewDecoder(resp.Body).Decode(respRPC)
if err != nil {
return fmt.Errorf("decoding json response %v",err)
return false, fmt.Errorf("decoding json response %v",err)
}

if respRPC.Error != "" {
return fmt.Errorf(respRPC.Error)
return false, fmt.Errorf(respRPC.Error)
}

data, _ := json.Marshal(respRPC.Data)
return jsonpb.Unmarshal(bytes.NewReader(data), out)
return respRPC.KeepServerStreamOpen, jsonpb.Unmarshal(bytes.NewReader(data), out)
}
{{ end }}
13 changes: 7 additions & 6 deletions stub/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"log"
"net/http"
"strings"

"github.com/go-chi/chi"
)

Expand Down Expand Up @@ -60,8 +60,9 @@ type Input struct {
}

type Output struct {
Data map[string]interface{} `json:"data"`
Error string `json:"error"`
Data map[string]interface{} `json:"data"`
Error string `json:"error"`
KeepServerStreamOpen bool `json:"keepServerStreamOpen"`
}

func addStub(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -106,7 +107,7 @@ func validateStub(stub *Stub) error {
if stub.Method == "" {
return fmt.Errorf("Method name can't be emtpy")
}

// due to golang implementation
// method name must capital
stub.Method = strings.Title(stub.Method)
Expand Down Expand Up @@ -143,11 +144,11 @@ func handleFindStub(w http.ResponseWriter, r *http.Request) {
responseError(err, w)
return
}

// due to golang implementation
// method name must capital
stub.Method = strings.Title(stub.Method)

output, err := findStub(stub)
if err != nil {
log.Println(err)
Expand Down
35 changes: 20 additions & 15 deletions stub/stub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestStub(t *testing.T) {
return httptest.NewRequest("GET", "/", nil)
},
handler: listStub,
expect: "{\"Testing\":{\"TestMethod\":[{\"Input\":{\"equals\":{\"Hola\":\"Mundo\"},\"contains\":null,\"matches\":null},\"Output\":{\"data\":{\"Hello\":\"World\"},\"error\":\"\"}}]}}\n",
expect: "{\"Testing\":{\"TestMethod\":[{\"Input\":{\"equals\":{\"Hola\":\"Mundo\"},\"contains\":null,\"matches\":null},\"Output\":{\"data\":{\"Hello\":\"World\"},\"error\":\"\",\"keepServerStreamOpen\":false}}]}}\n",
},
{
name: "find stub equals",
Expand All @@ -57,7 +57,7 @@ func TestStub(t *testing.T) {
return httptest.NewRequest("POST", "/find", bytes.NewReader([]byte(payload)))
},
handler: handleFindStub,
expect: "{\"data\":{\"Hello\":\"World\"},\"error\":\"\"}\n",
expect: "{\"data\":{\"Hello\":\"World\"},\"error\":\"\",\"keepServerStreamOpen\":false}\n",
},
{
name: "add stub contains",
Expand All @@ -74,7 +74,8 @@ func TestStub(t *testing.T) {
"output":{
"data":{
"hello":"world"
}
},
"keepServerStreamOpen": true
}
}`
return httptest.NewRequest("POST", "/add", bytes.NewReader([]byte(payload)))
Expand All @@ -97,8 +98,9 @@ func TestStub(t *testing.T) {
return httptest.NewRequest("GET", "/find", bytes.NewReader([]byte(payload)))
},
handler: handleFindStub,
expect: "{\"data\":{\"hello\":\"world\"},\"error\":\"\"}\n",
}, {
expect: "{\"data\":{\"hello\":\"world\"},\"error\":\"\",\"keepServerStreamOpen\":true}\n",
},
{
name: "add stub matches regex",
mock: func() *http.Request {
payload := `{
Expand All @@ -119,21 +121,23 @@ func TestStub(t *testing.T) {
},
handler: addStub,
expect: "Success add stub",
}, {
},
{
name: "find stub matches regex",
mock: func() *http.Request {
payload := `{
"service":"Testing2",
"method":"TestMethod",
"data":{
"field1":"hello"
}
}`
"service":"Testing2",
"method":"TestMethod",
"data":{
"field1":"hello"
}
}`
return httptest.NewRequest("GET", "/find", bytes.NewReader([]byte(payload)))
},
handler: handleFindStub,
expect: "{\"data\":{\"reply\":\"OK\"},\"error\":\"\"}\n",
}, {
expect: "{\"data\":{\"reply\":\"OK\"},\"error\":\"\",\"keepServerStreamOpen\":false}\n",
},
{
name: "error find stub contains",
mock: func() *http.Request {
payload := `{
Expand All @@ -149,7 +153,8 @@ func TestStub(t *testing.T) {
},
handler: handleFindStub,
expect: "Can't find stub \n\nService: Testing \n\nMethod: TestMethod \n\nInput\n\n{\n\tfield1: hello field1\n\tfield2: hello field2\n\tfield3: hello field4\n}\n\nClosest Match \n\ncontains:{\n\tfield1: hello field1\n\tfield3: hello field3\n}",
}, {
},
{
name: "error find stub equals",
mock: func() *http.Request {
payload := `{"service":"Testing","method":"TestMethod","data":{"Hola":"Dunia"}}`
Expand Down