Ejemplo n.º 1
0
func TestSafeRetry(t *testing.T) {
	t.Parallel()
	port, err := freeport.Get()
	if err != nil {
		t.Fatal(err)
	}
	addr := fmt.Sprintf("127.0.0.1:%d", port)
	server := httptest.NewUnstartedServer(sleepHandler(time.Millisecond))
	transport := &httpcontrol.Transport{
		MaxTries: 2,
	}
	first := false
	second := false
	transport.Stats = func(stats *httpcontrol.Stats) {
		if !first {
			first = true
			if stats.Error == nil {
				t.Fatal("was expecting error")
			}
			if !stats.Retry.Pending {
				t.Fatal("was expecting pending retry", stats.Error)
			}
			server.Listener, err = net.Listen("tcp", addr)
			if err != nil {
				t.Fatal(err)
			}
			server.Start()
			return
		}

		if !second {
			second = true
			if stats.Error != nil {
				t.Fatal(stats.Error, server.URL)
			}
			return
		}
	}
	defer call(transport.Close, t)
	client := &http.Client{Transport: transport}
	res, err := client.Get(fmt.Sprintf("http://%s/", addr))
	if err != nil {
		t.Fatal(err)
	}
	assertResponse(res, t)
	if !first {
		t.Fatal("did not see first request")
	}
	if !second {
		t.Fatal("did not see second request")
	}
}
Ejemplo n.º 2
0
func TestSafeRetryAfterTimeout(t *testing.T) {
	t.Parallel()
	port, err := freeport.Get()
	if err != nil {
		t.Fatal(err)
	}
	addr := fmt.Sprintf("127.0.0.1:%d", port)
	server := httptest.NewUnstartedServer(sleepHandler(5 * time.Second))
	transport := &httpcontrol.Transport{
		MaxTries:          3,
		RequestTimeout:    5 * time.Millisecond,
		RetryAfterTimeout: true,
	}
	first := false
	second := false
	third := false
	transport.Stats = func(stats *httpcontrol.Stats) {
		if !first {
			first = true
			if stats.Error == nil {
				t.Fatal("was expecting error")
			}
			if !stats.Retry.Pending {
				t.Fatal("was expecting pending retry", stats.Error)
			}
			server.Listener, err = net.Listen("tcp", addr)
			if err != nil {
				t.Fatal(err)
			}
			server.Start()
			return
		}

		if !second {
			second = true
			if stats.Error == nil {
				t.Fatal("was expecting error")
			}
			if !stats.Retry.Pending {
				t.Fatal("was expecting pending retry", stats.Error)
			}
			return
		}

		if !third {
			third = true
			if stats.Error == nil {
				t.Fatal("was expecting error")
			}
			if !stats.Retry.Pending {
				t.Fatal("was expecting pending retry", stats.Error)
			}
		}
	}
	client := &http.Client{Transport: transport}
	_, err = client.Get(fmt.Sprintf("http://%s/", addr))

	// Expect this to fail
	if err == nil {
		t.Fatal(err)
	}

	if !first {
		t.Fatal("did not see first request")
	}
	if !second {
		t.Fatal("did not see second request")
	}

	if !third {
		t.Fatal("did not see third request")
	}
}