Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions cmd/apps/dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ func TestIsViteReady(t *testing.T) {
})

t.Run("vite is running", func(t *testing.T) {
// Start a mock server on the Vite port
listener, err := net.Listen("tcp", "localhost:5173")
// Use a free port so the check cannot collide with anything else bound to 5173.
listener, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)
defer listener.Close()

// Accept connections in the background
go func() {
for {
conn, err := listener.Accept()
Expand All @@ -36,11 +35,12 @@ func TestIsViteReady(t *testing.T) {
}
}()

// Give the listener a moment to start
time.Sleep(50 * time.Millisecond)

ready := isViteReady(5173)
assert.True(t, ready)
// A single dial has only viteReadyCheckInterval (100ms) to succeed. On loaded
// CI runners, and on Windows where dialing "localhost" may first try an
// unbound ::1 before 127.0.0.1, one attempt can time out. Poll like the
// production wait loop does.
port := listener.Addr().(*net.TCPAddr).Port
assert.Eventually(t, func() bool { return isViteReady(port) }, 10*time.Second, 50*time.Millisecond)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nice to have] assert.Eventually(..., 10*time.Second, 50*time.Millisecond) gives ~200 attempts, while the production loop in startViteDevServer budgets viteReadyMaxAttempts × viteReadyCheckInterval = 5 s with 50 attempts. If isViteReady regressed to succeeding only once every few seconds on a healthy local listener, this test would still pass. Consider capping at time.Duration(viteReadyMaxAttempts)*viteReadyCheckInterval (or even shorter — a handful of retries should be plenty for a loopback dial) so the test stays close to the production contract while still absorbing the dual-stack delay.

})
}

Expand Down
Loading