-
Notifications
You must be signed in to change notification settings - Fork 249
Fix flaky test #1587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix flaky test #1587
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,8 @@ | ||
| package e2e_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
|
|
@@ -15,8 +13,9 @@ import ( | |
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/docker/cagent/cmd/root" | ||
| "github.com/docker/cagent/pkg/config" | ||
| "github.com/docker/cagent/pkg/server" | ||
| "github.com/docker/cagent/pkg/session" | ||
| ) | ||
|
|
||
| type Session struct { | ||
|
|
@@ -38,10 +37,8 @@ func TestCagentAPI_ListSessions(t *testing.T) { | |
| {"desktop.db", 2}, | ||
| } { | ||
| t.Run(tc.db, func(t *testing.T) { | ||
| dbPath, err := filepath.Abs("testdata/db/" + tc.db) | ||
| require.NoError(t, err) | ||
| socketPath := startCagentAPI(t, filepath.Join("testdata", "db", tc.db)) | ||
|
|
||
| socketPath := startCagentAPI(t, dbPath) | ||
| client := &http.Client{ | ||
| Transport: &http.Transport{ | ||
| DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { | ||
|
|
@@ -66,12 +63,18 @@ func TestCagentAPI_ListSessions(t *testing.T) { | |
| func startCagentAPI(t *testing.T, db string) string { | ||
| t.Helper() | ||
|
|
||
| // Get absolute path to db before changing directory | ||
| absDB, err := filepath.Abs(db) | ||
| require.NoError(t, err) | ||
|
|
||
| tmpDir := t.TempDir() | ||
| t.Chdir(tmpDir) | ||
| t.Chdir(tmpDir) // Use relative socket path to avoid Unix socket path length limit | ||
|
|
||
| copyFile(t, "session.db", db) | ||
| if _, err := os.Stat(db + "-wal"); err == nil { | ||
| copyFile(t, "session.db-wal", db+"-wal") | ||
| // Copy database files to temp directory | ||
| dbCopy := tmpDir + "/session.db" | ||
| copyFile(t, dbCopy, absDB) | ||
| if _, err := os.Stat(absDB + "-wal"); err == nil { | ||
| copyFile(t, dbCopy+"-wal", absDB+"-wal") | ||
| } | ||
|
|
||
| ln, err := server.Listen(t.Context(), "unix://cagent.sock") | ||
|
|
@@ -80,12 +83,14 @@ func startCagentAPI(t *testing.T, db string) string { | |
| _ = ln.Close() | ||
| }) | ||
|
|
||
| file, err := ln.(*net.UnixListener).File() | ||
| sessionStore, err := session.NewSQLiteSessionStore(dbCopy) | ||
| require.NoError(t, err) | ||
|
|
||
| srv, err := server.New(t.Context(), sessionStore, &config.RuntimeConfig{}, 0, nil) | ||
| require.NoError(t, err) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Server goroutine lacks synchronization, creating potential race condition The function starts a goroutine with If the test's HTTP client (lines 43-49) attempts to connect during this window, the connection could fail intermittently. This is a classic race condition in test setup that could cause test flakiness—ironically, the issue this PR aims to fix. Recommendation: Add a readiness check before returning, such as:
|
||
| go func() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Server error is silently ignored in goroutine The goroutine ignores the error from Problems:
Recommendation: Either send the error through a channel and check it after the test, use |
||
| var stdout, stderr bytes.Buffer | ||
| _ = root.Execute(t.Context(), nil, &stdout, &stderr, "api", "-s", "session.db", "--listen", fmt.Sprintf("fd://%d", file.Fd()), "default") | ||
| _ = srv.Serve(t.Context(), ln) | ||
| }() | ||
|
|
||
| return "cagent.sock" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Path construction uses string concatenation instead of filepath.Join
The code uses
tmpDir + "/session.db"which hardcodes a forward slash, creating non-portable paths that may fail on Windows where backslashes are expected. This is confirmed as a bug because:Recommendation: Use
filepath.Join(tmpDir, "session.db")instead to ensure correct path separators on all platforms.