Skip to content

Repository files navigation

pester

forked from https://github.com/sethgrid/pester

add feature

  1. should input timeout explicitly for every request
  2. add asynPester, and add callbackFun when asyn finish

demo

import (
"encoding/json""fmt""io/ioutil""net/http""testing""time"
)
funcTestAsynGetCallBack(t*testing.T) {
c:=New()
c.Concurrency=3c.KeepLog=truec.Backoff=ExponentialBackoffc.MaxRetries=3url:="http://localhost/mock"stopChan:=make(chanstring)
callBack:=func (resp*http.Response, errerror, logsstring, transmissionParams...interface{} ) {
iferr!=nil {
t.Errorf("err:%v\nlogs:%v, \ntransmissionParams:%v", err, logs, transmissionParams)
stopChan<-""return
}
ifnil==resp||nil==resp.Body{
t.Errorf("response nil, logs:%s, transmissionParams:%v", logs, transmissionParams)
stopChan<-""return
}
body,er:=ioutil.ReadAll(resp.Body)
t.Logf("resp: %v, err:%v, logs:%s, transmissionParams:%v\n", string(body), er, logs, transmissionParams)
stopChan<-""
}
err:=c.GetAsyn(url, 50*time.Millisecond, callBack, 111)
iferr!=nil {
t.Errorf(c.LogString())
return
}
<-stopChan
}
varpostStopChan=make(chanstring)
funcasynPostCallBack(resp*http.Response, errerror, logsstring, transmissionParams...interface{} ) {
iferr!=nil {
fmt.Printf("err:%v\nlogs:%v, \ntransmissionParams:%v", err, logs, transmissionParams)
postStopChan<-""return
}
ifnil==resp||nil==resp.Body {
fmt.Printf("response nil, logs:%s, transmissionParams:%v", logs, transmissionParams)
postStopChan<-""return
}
body,er:=ioutil.ReadAll(resp.Body)
fmt.Printf("resp: %v, err:%v, logs:%s, transmissionParams:%v\n", string(body), er, logs, transmissionParams)
postStopChan<-""
}
funcTestAsynPostCallBack(t*testing.T) {
c:=New()
c.KeepLog=truec.Backoff=DefaultBackoffc.MaxRetries=5url:="http://localhost:8081/mock"bodyParam:=map[string]interface{}{"slotId":"1000"}
body, _:=json.Marshal(bodyParam)
err:=c.PostAsyn(url, body, 100*time.Millisecond, asynPostCallBack, 111, 112,"sss")
iferr!=nil {
t.Errorf(c.LogString())
return
}
t.Logf("waiting for callback")
<-postStopChan
}

About

Go (golang) http calls with retries and backoff. Added some new features

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages