Repository: prometheus/prometheus
File: cmd/prometheus/reload_test.go (Go)

Code before:
	lines := string(body)
	var actualValue float64
	found := false

	for _, line := range strings.Split(lines, "\n") {
		if strings.HasPrefix(line, configReloadMetric) {
			parts := strings.Fields(line)
			if len(parts) >= 2 {
				actualValue, err = strconv.ParseFloat(parts[1], 64)
				require.NoError(t, err)
				found = true
				break
			}
		}
	}

	require.True(t, found, "Expected metric %s not found in Prometheus metrics", configReloadMetric)
	require.Equal(t, expectedValue, actualValue)
}

func captureLogsToTLog(t *testing.T, r io.Reader) {
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		t.Log(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		t.Logf("Error reading logs: %v", err)
	}
}

func runPrometheusWithLogging(t *testing.T, configFilePath string, port int) *exec.Cmd {
	stdoutPipe, stdoutWriter := io.Pipe()
	stderrPipe, stderrWriter := io.Pipe()

	prom := exec.Command(promPath, "-test.main", "--enable-feature=auto-reload-config", "--config.file="+configFilePath, "--config.auto-reload-interval=1s", "--web.listen-address=0.0.0.0:"+strconv.Itoa(port))
	prom.Stdout = stdoutWriter
	prom.Stderr = stderrWriter

	go func() {
		defer stdoutWriter.Close()
		captureLogsToTLog(t, stdoutPipe)
	}()

	go func() {
		defer stderrWriter.Close()
		captureLogsToTLog(t, stderrPipe)
	}()

	require.NoError(t, prom.Start())
	return prom
}

Review comment (suggestion):
Rewriting `verifyConfigReloadMetric` to work with `require.EventuallyWithT`:

```suggestion
func verifyConfigReloadMetric(t *testing.T, c *assert.CollectT, baseURL string, expectedValue float64) {
	resp, err := http.Get(baseURL + "/metrics")
	require.NoError(t, err)
	defer resp.Body.Close()
	require.Equal(t, 2, resp.StatusCode/100)

	body, err := io.ReadAll(resp.Body)
	require.NoError(t, err)

	lines := string(body)
	var actualValue float64
	found := false

	for _, line := range strings.Split(lines, "\n") {
		if strings.HasPrefix(line, configReloadMetric) {
			parts := strings.Fields(line)
			if len(parts) >= 2 {
				actualValue, err = strconv.ParseFloat(parts[1], 64)
				require.NoError(t, err)
				found = true
				break
			}
		}
	}

	require.True(t, found, "Expected metric %s not found in Prometheus metrics", configReloadMetric)
	assert.Equal(c, expectedValue, actualValue)
}
```

Code after:
func verifyConfigReloadMetric(t *testing.T, baseURL string, expectedValue float64) bool {
	resp, err := http.Get(baseURL + "/metrics")
	require.NoError(t, err)
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	require.NoError(t, err)

	lines := string(body)
	var actualValue float64
	found := false

	for _, line := range strings.Split(lines, "\n") {
		if strings.HasPrefix(line, configReloadMetric) {
			parts := strings.Fields(line)
			if len(parts) >= 2 {
				actualValue, err = strconv.ParseFloat(parts[1], 64)
				require.NoError(t, err)
				found = true
				break
			}
		}
	}

	return found && actualValue == expectedValue
}

func captureLogsToTLog(t *testing.T, r io.Reader) {
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		t.Log(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		t.Logf("Error reading logs: %v", err)
	}
}

func runPrometheusWithLogging(t *testing.T, configFilePath string, port int) {
	stdoutPipe, stdoutWriter := io.Pipe()
	stderrPipe, stderrWriter := io.Pipe()

	var wg sync.WaitGroup
	wg.Add(2)

	prom := exec.Command(promPath, "-test.main", "--enable-feature=auto-reload-config", "--config.file="+configFilePath, "--config.auto-reload-interval=1s", "--web.listen-address=0.0.0.0:"+strconv.Itoa(port))
	prom.Stdout = stdoutWriter
	prom.Stderr = stderrWriter

	go func() {
		defer wg.Done()
		captureLogsToTLog(t, stdoutPipe)