Code Monkey home page Code Monkey logo

Comments (6)

h2non avatar h2non commented on May 9, 2024

You can try this:
https://pkg.go.dev/github.com/h2non/gock?tab=doc#Response.Delay

Note: delayed response is still experimental.

from gock.

jan4984 avatar jan4984 commented on May 9, 2024

@h2non Yes I do delay with Response.Delay, but client timeout error not return

from gock.

h2non avatar h2non commented on May 9, 2024

To make the delay error effective, you should run the request in a goroutine. Also, make sure the http.Transport of the http.Client has a reduced timeout tolerance.

from gock.

jan4984 avatar jan4984 commented on May 9, 2024

Yes maybe http.Transport problem with httptest.NewRecorder(). Must I run the testing in real tcp network?

the Testing function:

res := httptest.NewRecorder()
defer gock.Off()
//gock.New(aiEp).Post("/asr/v1/tasks").ReplyError(errors.New("simulate timeout error"))
gock.New(aiEp).Post("/asr/v1/tasks").Reply(200).Delay(time.Second * 3)
handleXXXXX(res, req)

the /asr/v1/tasks post handler

cxt, canF := context.WithTimeout(context.Background(), time.Second * 2)
defer canF()
...
l.Debug().Time("xlStart", time.Now()).Msg("start xxxx")
resp, err := http.DefaultClient.Do(taskReq.WithContext(cxt))
l.Debug().Time("xlEnd", time.Now()).Msg("end xxxx)

running the testing, I can not get timeout err return after 2 seconds, but get the norman reply after 3 seconds.

from gock.

h2non avatar h2non commented on May 9, 2024

Since you are not touching any network layer with this mocking/testing strategy, I would suggest you to simply return an artificially generated error in the mock simulating a native net/tcp timeout error.

timeoutErr := errors.New("dial tcp i/o timeout")
gock.New(aiEp).Post("/asr/v1/tasks").Reply(200).SetError(timeoutErr)

from gock.

jan4984 avatar jan4984 commented on May 9, 2024

OK. Since httptest is a official http testing package, please consider adding some support to it for network lantency testing.

from gock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.