From 71e592e6771839cc54efe46a6a61c171ef580610 Mon Sep 17 00:00:00 2001
From: Roman Khavronenko <roman@victoriametrics.com>
Date: Fri, 30 Aug 2024 13:23:32 +0200
Subject: [PATCH] attempt to fix flaky TestClientProxyReadOk (#6899)

Signed-off-by: hagen1778 <roman@victoriametrics.com>
(cherry picked from commit f586082520dda509d5398a777c2fce83ed39e214)
---
 lib/promscrape/client_test.go | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/lib/promscrape/client_test.go b/lib/promscrape/client_test.go
index 5798243f24..bafd8cd342 100644
--- a/lib/promscrape/client_test.go
+++ b/lib/promscrape/client_test.go
@@ -3,6 +3,7 @@ package promscrape
 import (
 	"context"
 	"encoding/base64"
+	"errors"
 	"fmt"
 	"io"
 	"net"
@@ -133,18 +134,29 @@ func TestClientProxyReadOk(t *testing.T) {
 		defer ps.Close()
 
 		c, err := newClient(ctx, &ScrapeWork{
-			ScrapeURL:       backend.URL,
-			ProxyURL:        proxy.MustNewURL(ps.URL),
-			ScrapeTimeout:   2 * time.Second,
-			AuthConfig:      newTestAuthConfig(t, isBackendTLS, backendAuth),
-			ProxyAuthConfig: newTestAuthConfig(t, isProxyTLS, proxyAuth),
-			MaxScrapeSize:   16000,
+			ScrapeURL: backend.URL,
+			ProxyURL:  proxy.MustNewURL(ps.URL),
+			// bump timeout for slow CIs
+			ScrapeTimeout: 5 * time.Second,
+			// force connection re-creating to avoid broken conns in slow CIs
+			DisableKeepAlive: true,
+			AuthConfig:       newTestAuthConfig(t, isBackendTLS, backendAuth),
+			ProxyAuthConfig:  newTestAuthConfig(t, isProxyTLS, proxyAuth),
+			MaxScrapeSize:    16000,
 		})
 		if err != nil {
 			t.Fatalf("failed to create client: %s", err)
 		}
+
 		var bb bytesutil.ByteBuffer
-		if err := c.ReadData(&bb); err != nil {
+		err = c.ReadData(&bb)
+		if errors.Is(err, io.EOF) {
+			bb.Reset()
+			// EOF could occur in slow envs, like CI
+			err = c.ReadData(&bb)
+		}
+
+		if err != nil {
 			t.Fatalf("unexpected error at ReadData: %s", err)
 		}
 		got, err := io.ReadAll(bb.NewReader())