prometheus/scrape
Russ Cox 28f5502828 scrape: fix two loop variable scoping bugs in test
Consider code like:

	for i := 0; i < numTargets; i++ {
		stopFuncs = append(stopFuncs, func() {
			time.Sleep(i*20*time.Millisecond)
		})
	}

Because the loop variable i is shared by all closures,
all the stopFuncs sleep for numTargets*20 ms.

If the i were made per-iteration, as we are considering
for a future Go release, the stopFuncs would have sleep
durations ranging from 0 to (numTargets-1)*20 ms.

Two tests had code like this and were checking that the
aggregate sleep was at least numTargets*20 ms
("at least as long as the last target slept"). This is only true
today because i == numTarget during all the sleeps.

To keep the code working even if the semantics of this loop
change, this PR computes

	d := time.Duration((i+1)*20) * time.Millisecond

outside the closure (but inside the loop body), and then each
closure has its own d. Now the sleeps range from 20 ms
to numTargets*20 ms, keeping the test passing
(and probably behaving closer to the intent of the test author).

The failure being fixed can be reproduced by using the current
Go development branch with

	GOEXPERIMENT=loopvar go test

Signed-off-by: Russ Cox <rsc@golang.org>
2023-04-26 10:33:10 -04:00
..
testdata Re-generate test cert to fix test_windows test failures 2022-03-17 19:37:18 +01:00
helpers_test.go Support FloatHistogram in TSDB (#11522) 2022-12-28 14:25:07 +05:30
manager_test.go Merge pull request #12048 from bboreham/faster-targets 2023-03-09 11:10:01 +00:00
manager.go style: Replace else if cascades with switch 2023-04-19 17:22:31 +02:00
scrape_test.go scrape: fix two loop variable scoping bugs in test 2023-04-26 10:33:10 -04:00
scrape.go Merge branch 'main' into linter/nilerr 2023-04-19 19:56:39 +02:00
target_test.go lint: Adjust to the lint warnings raised by current versions of golint-ci 2023-04-19 17:10:10 +02:00
target.go enable gocritic, unconvert and unused linters 2023-04-13 19:20:22 +00:00