Skip to content

Commit e0a3553

Browse files
committed
added feature for extra delay for grpc apis
2 parents 1aed41b + 61620b8 commit e0a3553

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

stub/duration.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package stub
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"time"
7+
)
8+
9+
type Duration struct {
10+
time.Duration
11+
}
12+
13+
func (d Duration) MarshalJSON() ([]byte, error) {
14+
return json.Marshal(d.String())
15+
}
16+
17+
func (d *Duration) UnmarshalJSON(b []byte) error {
18+
var v interface{}
19+
if err := json.Unmarshal(b, &v); err != nil {
20+
return err
21+
}
22+
switch value := v.(type) {
23+
case float64:
24+
d.Duration = time.Duration(value)
25+
return nil
26+
case string:
27+
var err error
28+
d.Duration, err = time.ParseDuration(value)
29+
if err != nil {
30+
return err
31+
}
32+
return nil
33+
default:
34+
return errors.New("invalid duration")
35+
}
36+
}
37+

stub/storage.go

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"reflect"
99
"regexp"
1010
"sync"
11+
"time"
1112

1213
"github.com/lithammer/fuzzysearch/fuzzy"
1314
)
@@ -105,6 +106,7 @@ func findStub(stub *findStubPayload) (*Output, error) {
105106
closestMatch = append(closestMatch, closeMatch{"equals", expect})
106107
if equals(stub.Data, expect) {
107108
stubs[idx].timesCalled++
109+
time.Sleep(stubrange.Output.Delay.Duration)
108110
return &stubrange.Output, nil
109111
}
110112
}
@@ -113,6 +115,7 @@ func findStub(stub *findStubPayload) (*Output, error) {
113115
closestMatch = append(closestMatch, closeMatch{"contains", expect})
114116
if contains(stubrange.Input.Contains, stub.Data) {
115117
stubs[idx].timesCalled++
118+
time.Sleep(stubrange.Output.Delay.Duration)
116119
return &stubrange.Output, nil
117120
}
118121
}
@@ -121,6 +124,7 @@ func findStub(stub *findStubPayload) (*Output, error) {
121124
closestMatch = append(closestMatch, closeMatch{"matches", expect})
122125
if matches(stubrange.Input.Matches, stub.Data) {
123126
stubs[idx].timesCalled++
127+
time.Sleep(stubrange.Output.Delay.Duration)
124128
return &stubrange.Output, nil
125129
}
126130
}

stub/stub.go

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type Input struct {
6464
}
6565

6666
type Output struct {
67+
Delay Duration `json:"delay"`
6768
Data map[string]interface{} `json:"data"`
6869
Error string `json:"error"`
6970
Code *codes.Code `json:"code,omitempty"`

stub/stub_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ func TestStub(t *testing.T) {
3131
}
3232
},
3333
"output":{
34+
"delay": "1s",
3435
"data":{
3536
"Hello":"World"
3637
}
38+
3739
}
3840
}`
3941
read := bytes.NewReader([]byte(payload))
@@ -48,7 +50,7 @@ func TestStub(t *testing.T) {
4850
return httptest.NewRequest("GET", "/", nil)
4951
},
5052
handler: listStub,
51-
expect: "{\"Testing\":{\"TestMethod\":[{\"Input\":{\"equals\":{\"Hola\":\"Mundo\"},\"contains\":null,\"matches\":null},\"Output\":{\"data\":{\"Hello\":\"World\"},\"error\":\"\"}}]}}\n",
53+
expect: "{\"Testing\":{\"TestMethod\":[{\"Input\":{\"equals\":{\"Hola\":\"Mundo\"},\"contains\":null,\"matches\":null},\"Output\":{\"delay\":\"1s\",\"data\":{\"Hello\":\"World\"},\"error\":\"\"}}]}}\n",
5254
},
5355
{
5456
name: "find stub equals",
@@ -57,7 +59,7 @@ func TestStub(t *testing.T) {
5759
return httptest.NewRequest("POST", "/find", bytes.NewReader([]byte(payload)))
5860
},
5961
handler: handleFindStub,
60-
expect: "{\"data\":{\"Hello\":\"World\"},\"error\":\"\"}\n",
62+
expect: "{\"delay\":\"1s\",\"data\":{\"Hello\":\"World\"},\"error\":\"\"}\n",
6163
},
6264
{
6365
name: "add nested stub equals",
@@ -290,7 +292,7 @@ func TestStub(t *testing.T) {
290292
return httptest.NewRequest("GET", "/find", bytes.NewReader([]byte(payload)))
291293
},
292294
handler: handleFindStub,
293-
expect: "{\"data\":{\"hello\":\"world\"},\"error\":\"\"}\n",
295+
expect: "{\"delay\":\"0s\",\"data\":{\"hello\":\"world\"},\"error\":\"\"}\n",
294296
},
295297
{
296298
name: "add stub matches regex",
@@ -379,7 +381,7 @@ func TestStub(t *testing.T) {
379381
return httptest.NewRequest("GET", "/find", bytes.NewReader([]byte(payload)))
380382
},
381383
handler: handleFindStub,
382-
expect: "{\"data\":{\"reply\":\"OK\"},\"error\":\"\"}\n",
384+
expect: "{\"delay\":\"0s\",\"data\":{\"reply\":\"OK\"},\"error\":\"\"}\n",
383385
},
384386
{
385387
name: "error find stub contains",

0 commit comments

Comments
 (0)