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 feature for extra delay for grpc apis #71

Open
wants to merge 2 commits 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
37 changes: 37 additions & 0 deletions stub/duration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package stub

import (
"encoding/json"
"errors"
"time"
)

type Duration struct {
time.Duration
}

func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}

func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
d.Duration = time.Duration(value)
return nil
case string:
var err error
d.Duration, err = time.ParseDuration(value)
if err != nil {
return err
}
return nil
default:
return errors.New("invalid duration")
}
}

4 changes: 4 additions & 0 deletions stub/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"reflect"
"regexp"
"sync"
"time"

"github.com/lithammer/fuzzysearch/fuzzy"
)
Expand Down Expand Up @@ -71,20 +72,23 @@ func findStub(stub *findStubPayload) (*Output, error) {
if expect := stubrange.Input.Equals; expect != nil {
closestMatch = append(closestMatch, closeMatch{"equals", expect})
if equals(stub.Data, expect) {
time.Sleep(stubrange.Output.Delay.Duration)
return &stubrange.Output, nil
}
}

if expect := stubrange.Input.Contains; expect != nil {
closestMatch = append(closestMatch, closeMatch{"contains", expect})
if contains(stubrange.Input.Contains, stub.Data) {
time.Sleep(stubrange.Output.Delay.Duration)
return &stubrange.Output, nil
}
}

if expect := stubrange.Input.Matches; expect != nil {
closestMatch = append(closestMatch, closeMatch{"matches", expect})
if matches(stubrange.Input.Matches, stub.Data) {
time.Sleep(stubrange.Output.Delay.Duration)
return &stubrange.Output, nil
}
}
Expand Down
4 changes: 2 additions & 2 deletions stub/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package stub
import (
"encoding/json"
"fmt"
"github.com/go-chi/chi"
"io/ioutil"
"log"
"net/http"
"strings"

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

type Options struct {
Expand Down Expand Up @@ -60,6 +59,7 @@ type Input struct {
}

type Output struct {
Delay Duration `json:"delay"`
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 can't do it directly under Output struct, can you please create a Meta field and put duration under it?

Copy link
Author

Choose a reason for hiding this comment

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

Hey @jekiapp Duration is meta field defined in file stub/duration.go, such that
type Duration struct {
time.Duration
}

Copy link
Contributor

Choose a reason for hiding this comment

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

@shubhjha123 I mean like this

type Output struct{
  Meta Meta
}

type Meta struct{
  Delay Duration
}

Data map[string]interface{} `json:"data"`
Error string `json:"error"`
}
Expand Down
10 changes: 6 additions & 4 deletions stub/stub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ func TestStub(t *testing.T) {
}
},
"output":{
"delay": "1s",
"data":{
"Hello":"World"
}

}
}`
read := bytes.NewReader([]byte(payload))
Expand All @@ -48,7 +50,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\":{\"delay\":\"1s\",\"data\":{\"Hello\":\"World\"},\"error\":\"\"}}]}}\n",
},
{
name: "find stub equals",
Expand All @@ -57,7 +59,7 @@ func TestStub(t *testing.T) {
return httptest.NewRequest("POST", "/find", bytes.NewReader([]byte(payload)))
},
handler: handleFindStub,
expect: "{\"data\":{\"Hello\":\"World\"},\"error\":\"\"}\n",
expect: "{\"delay\":\"1s\",\"data\":{\"Hello\":\"World\"},\"error\":\"\"}\n",
},
{
name: "add stub contains",
Expand Down Expand Up @@ -97,7 +99,7 @@ func TestStub(t *testing.T) {
return httptest.NewRequest("GET", "/find", bytes.NewReader([]byte(payload)))
},
handler: handleFindStub,
expect: "{\"data\":{\"hello\":\"world\"},\"error\":\"\"}\n",
expect: "{\"delay\":\"0s\",\"data\":{\"hello\":\"world\"},\"error\":\"\"}\n",
}, {
name: "add stub matches regex",
mock: func() *http.Request {
Expand Down Expand Up @@ -132,7 +134,7 @@ func TestStub(t *testing.T) {
return httptest.NewRequest("GET", "/find", bytes.NewReader([]byte(payload)))
},
handler: handleFindStub,
expect: "{\"data\":{\"reply\":\"OK\"},\"error\":\"\"}\n",
expect: "{\"delay\":\"0s\",\"data\":{\"reply\":\"OK\"},\"error\":\"\"}\n",
}, {
name: "error find stub contains",
mock: func() *http.Request {
Expand Down