Exemplo n.º 1
0
func (c *FakeClient) Do(req *http.Request) (*http.Response, error) {
	c.CallCount++

	if req.Body != nil {
		content, err := boshhttp.ReadAndClose(req.Body)
		if err != nil {
			return nil, err
		}
		c.RequestBodies = append(c.RequestBodies, string(content))
	}
	c.Requests = append(c.Requests, req)

	if len(c.doBehavior) > 0 {
		output := c.doBehavior[0]
		c.doBehavior = c.doBehavior[1:]
		return output.resp, output.err
	}

	var resp *http.Response
	if !c.returnNilResponse {
		resp = &http.Response{
			Body:       boshhttp.NewStringReadCloser(c.responseMessage),
			StatusCode: c.StatusCode,
		}
	}
	err := c.Error

	return resp, err
}
			uint(maxOtherAttempts),
			delay,
			timeService,
		)
	})

	Describe("Try", func() {
		var (
			lastError   error
			unavailable *http.Response
			notFound    *http.Response
		)

		BeforeEach(func() {
			lastError = errors.New("last-error")
			unavailable = &http.Response{StatusCode: 503, Body: boshhttp.NewStringReadCloser("")}
			notFound = &http.Response{StatusCode: 404, Body: boshhttp.NewStringReadCloser("")}
		})

		Context("when all responses are only 503s", func() {
			It("retries until maxUnavailableAttempts + maxOtherAttempts are exhausted", func() {
				for i := 0; i < maxUnavailableAttempts+maxOtherAttempts-1; i++ {
					retryable.AddAttemptBehavior(unavailable, true, errors.New("fake-error"))
				}
				retryable.AddAttemptBehavior(unavailable, true, lastError)

				errChan := tryInBackground(monitRetryStrategy)

				sleepForIncrements(timeService, maxUnavailableAttempts+maxOtherAttempts, delay)

				Eventually(errChan).Should(Receive(Equal(lastError)))