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 1 commit
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
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(time.Duration(stubrange.Output.Delay)*time.Millisecond)
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(time.Duration(stubrange.Output.Delay)*time.Millisecond)
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(time.Duration(stubrange.Output.Delay)*time.Millisecond)
return &stubrange.Output, nil
}
}
Expand Down
1 change: 1 addition & 0 deletions stub/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Input struct {
}

type Output struct {
Delay int `json:"delay"`
Ardiannn08 marked this conversation as resolved.
Show resolved Hide resolved
Data map[string]interface{} `json:"data"`
Error string `json:"error"`
}
Expand Down
8 changes: 4 additions & 4 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\":{\"delay\":0,\"data\":{\"Hello\":\"World\"},\"error\":\"\"}}]}}\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: "{\"delay\":0,\"data\":{\"Hello\":\"World\"},\"error\":\"\"}\n",
},
{
name: "add stub contains",
Expand Down Expand Up @@ -97,7 +97,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\":0,\"data\":{\"hello\":\"world\"},\"error\":\"\"}\n",
}, {
name: "add stub matches regex",
mock: func() *http.Request {
Expand Down Expand Up @@ -132,7 +132,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\":0,\"data\":{\"reply\":\"OK\"},\"error\":\"\"}\n",
}, {
name: "error find stub contains",
mock: func() *http.Request {
Expand Down