Skip to content

Commit cb80fe4

Browse files
committed
better wait for state CI test
1 parent dab9bc0 commit cb80fe4

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

ci/test.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io"
78
"log/slog"
@@ -10,6 +11,7 @@ import (
1011
"net/http"
1112
"os"
1213
"os/exec"
14+
"path/filepath"
1315
"strings"
1416
"sync"
1517
"time"
@@ -60,8 +62,12 @@ func main() {
6062
fmt.Printf("Making sure %d attempt(s) pass\n", rateLimit)
6163
runParallelChecks(ips, rateLimit, "http://localhost")
6264

63-
time.Sleep(cp.StateSaveInterval + cp.StateSaveJitter + (1 * time.Second))
64-
runCommand("jq", ".", "tmp/state.json")
65+
statePath, err := waitForStateFile(30 * time.Second)
66+
if err != nil {
67+
slog.Error("State file was not created in time", "err", err)
68+
os.Exit(1)
69+
}
70+
runCommand("jq", ".", statePath)
6571

6672
fmt.Printf("Making sure attempt #%d causes a redirect to the challenge page\n", rateLimit+1)
6773
ensureRedirect(ips, "http://localhost")
@@ -81,7 +87,7 @@ func main() {
8187
time.Sleep(10 * time.Second)
8288
checkStateReload()
8389

84-
runCommand("rm", "tmp/state.json")
90+
runCommand("rm", "-f", statePath)
8591

8692
}
8793

@@ -421,3 +427,26 @@ func testGoogleBotGetsThrough(googleCIDRs []string) {
421427
// set things back to normal for other tests
422428
runCommand("docker", "compose", "down")
423429
}
430+
431+
func waitForStateFile(timeout time.Duration) (string, error) {
432+
paths := []string{
433+
filepath.Join("tmp", "state.json"),
434+
filepath.Join("ci", "tmp", "state.json"),
435+
}
436+
437+
deadline := time.Now().Add(timeout)
438+
for time.Now().Before(deadline) {
439+
for _, p := range paths {
440+
info, err := os.Stat(p)
441+
if err == nil && !info.IsDir() {
442+
return p, nil
443+
}
444+
if err != nil && !errors.Is(err, os.ErrNotExist) {
445+
return "", fmt.Errorf("failed to stat %s: %w", p, err)
446+
}
447+
}
448+
time.Sleep(500 * time.Millisecond)
449+
}
450+
451+
return "", fmt.Errorf("state file not found; checked: %s", strings.Join(paths, ", "))
452+
}

0 commit comments

Comments
 (0)