Skip to content

Commit 4b40955

Browse files
authored
Merge pull request #1619 from JoshVanL/fix-e2e-flakes-2
Fix e2e test flakes
2 parents 1c7e715 + fd76b46 commit 4b40955

19 files changed

Lines changed: 826 additions & 509 deletions

cmd/run_unix.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ func killProcessGroup(process *os.Process) error {
7474
if errors.Is(err, syscall.ESRCH) {
7575
return nil // process group gone
7676
}
77+
// EPERM can occur on macOS when the process group is in a
78+
// dying/zombie state. Keep polling rather than returning an
79+
// error so we can fall through to SIGKILL if needed.
80+
if errors.Is(err, syscall.EPERM) {
81+
time.Sleep(100 * time.Millisecond)
82+
continue
83+
}
7784
return fmt.Errorf("failed to check status of process group %d: %w", pgid, err)
7885
}
7986
// Grace period elapsed — force kill.

tests/e2e/spawn/spawn.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"bufio"
1818
"context"
1919
"os/exec"
20+
"time"
2021
)
2122

2223
// CommandWithContext runs a command with its arguments in background.
@@ -75,8 +76,14 @@ func Command(command string, arguments ...string) (string, error) {
7576

7677
// CommandExecWithContext runs a command with its arguments, kills the command after context is done
7778
// and returns the combined stdout, stderr or the error.
79+
//
80+
// WaitDelay is set so that if child processes (e.g. the compiled binary spawned
81+
// by `go run`) outlive the main process and keep its stdout/stderr pipes open,
82+
// CombinedOutput will still return after the delay instead of blocking forever.
83+
// This is critical on macOS where zombie process groups can hold pipes open.
7884
func CommandExecWithContext(ctx context.Context, command string, arguments ...string) (string, error) {
7985
cmd := exec.CommandContext(ctx, command, arguments...)
86+
cmd.WaitDelay = 10 * time.Second
8087
b, err := cmd.CombinedOutput()
8188
return string(b), err
8289
}

tests/e2e/standalone/init_negative_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import (
2727
func TestStandaloneInitNegatives(t *testing.T) {
2828
// Ensure a clean environment
2929
must(t, cmdUninstall, "failed to uninstall Dapr")
30+
// Reinstall Dapr when done so subsequent tests still work.
31+
t.Cleanup(func() {
32+
ensureDaprInstallation(t)
33+
})
3034

3135
homeDir, err := os.UserHomeDir()
3236
require.NoError(t, err, "expected no error on querying for os home dir")

tests/e2e/standalone/init_run_custom_path_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func echoTestAppArgs() []string {
3939
func TestStandaloneInitRunUninstallNonDefaultDaprPath(t *testing.T) {
4040
// Ensure a clean environment
4141
must(t, cmdUninstall, "failed to uninstall Dapr")
42+
// Reinstall Dapr when done so subsequent tests still work.
43+
t.Cleanup(func() {
44+
ensureDaprInstallation(t)
45+
})
4246
t.Run("run with --runtime-path flag", func(t *testing.T) {
4347
daprPath, err := os.MkdirTemp("", "dapr-e2e-run-with-flag-*")
4448
assert.NoError(t, err)

tests/e2e/standalone/init_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ func TestStandaloneInit(t *testing.T) {
4040
daprRuntimeVersion, daprDashboardVersion := common.GetVersionsFromEnv(t, false)
4141

4242
t.Cleanup(func() {
43-
// remove dapr installation after all tests in this function.
44-
must(t, cmdUninstall, "failed to uninstall Dapr")
43+
// Reinstall Dapr so subsequent tests still have a working installation.
44+
cmdUninstall()
45+
ensureDaprInstallation(t)
4546
})
4647

4748
t.Run("init with invalid private registry", func(t *testing.T) {

tests/e2e/standalone/invoke_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func StartTestService(t *testing.T, port int) common.Service {
5757

5858
func TestStandaloneInvoke(t *testing.T) {
5959
port := 9987
60-
ensureDaprInstallation(t)
6160
s := StartTestService(t, port)
6261
defer s.Stop()
6362

@@ -117,7 +116,6 @@ func TestStandaloneInvoke(t *testing.T) {
117116

118117
func TestStandaloneInvokeWithAppChannel(t *testing.T) {
119118
port := 9988
120-
ensureDaprInstallation(t)
121119
s := StartTestService(t, port)
122120
defer s.Stop()
123121

tests/e2e/standalone/list_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
)
3232

3333
func TestStandaloneList(t *testing.T) {
34-
ensureDaprInstallation(t)
3534
// Use a long-running app so we can test list and stop. Windows has no bash, so use cmd.
3635
runArgs := []string{"run", "--app-id", "dapr_e2e_list", "-H", "3555", "-G", "4555", "--"}
3736
if runtime.GOOS == "windows" {
@@ -86,9 +85,13 @@ func TestStandaloneList(t *testing.T) {
8685
cmd := exec.Command(daprdPath, "--app-id", "daprd_e2e_list", "--dapr-http-port", "3555", "--dapr-grpc-port", "4555", "--app-port", "0")
8786
cmd.Start()
8887

89-
output, err := cmdList("")
88+
// Wait for daprd to register and appear in the list.
89+
var output string
90+
require.Eventually(t, func() bool {
91+
output, err = cmdList("")
92+
return err == nil && !strings.Contains(output, "No Dapr instances found")
93+
}, 30*time.Second, time.Second, "daprd instance did not appear in list")
9094
t.Log(output)
91-
require.NoError(t, err, "dapr list failed with daprd instance")
9295
listOutputCheck(t, output, false)
9396

9497
// TODO: remove this condition when `dapr stop` starts working for Windows.

tests/e2e/standalone/main_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//go:build e2e || template
2+
3+
/*
4+
Copyright 2026 The Dapr Authors
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package standalone_test
19+
20+
import (
21+
"fmt"
22+
"net"
23+
"os"
24+
"path/filepath"
25+
"testing"
26+
"time"
27+
)
28+
29+
// TestMain installs Dapr once for the entire test binary, removing the
30+
// need for every test to call cmdUninstall/ensureDaprInstallation.
31+
// Tests that need to test the install/uninstall lifecycle itself must
32+
// reinstall Dapr in their t.Cleanup so subsequent tests still work.
33+
func TestMain(m *testing.M) {
34+
// Start from a clean slate.
35+
cmdUninstall()
36+
37+
if err := installDapr(); err != nil {
38+
fmt.Fprintf(os.Stderr, "TestMain: failed to install Dapr: %v\n", err)
39+
os.Exit(1)
40+
}
41+
42+
code := m.Run()
43+
44+
cmdUninstall()
45+
os.Exit(code)
46+
}
47+
48+
// installDapr performs a Dapr init for the test binary. This mirrors
49+
// ensureDaprInstallation but does not require a *testing.T.
50+
func installDapr() error {
51+
daprRuntimeVersion, ok := os.LookupEnv("DAPR_RUNTIME_PINNED_VERSION")
52+
if !ok {
53+
return fmt.Errorf("env var DAPR_RUNTIME_PINNED_VERSION not set")
54+
}
55+
daprDashboardVersion, ok := os.LookupEnv("DAPR_DASHBOARD_PINNED_VERSION")
56+
if !ok {
57+
return fmt.Errorf("env var DAPR_DASHBOARD_PINNED_VERSION not set")
58+
}
59+
60+
if !isSlimMode() {
61+
if err := waitForPortsFreeDirect(60*time.Second, 58080, 58081, 50005); err != nil {
62+
return fmt.Errorf("waiting for container ports: %w", err)
63+
}
64+
}
65+
66+
args := []string{
67+
"--runtime-version", daprRuntimeVersion,
68+
"--dashboard-version", daprDashboardVersion,
69+
}
70+
output, err := cmdInit(args...)
71+
if err != nil {
72+
return fmt.Errorf("dapr init: %s: %w", output, err)
73+
}
74+
75+
if isSlimMode() {
76+
homeDir, err := os.UserHomeDir()
77+
if err != nil {
78+
return fmt.Errorf("getting home dir: %w", err)
79+
}
80+
if err := createSlimComponents(filepath.Join(homeDir, ".dapr", "components")); err != nil {
81+
return fmt.Errorf("creating slim components: %w", err)
82+
}
83+
}
84+
85+
return nil
86+
}
87+
88+
// waitForPortsFreeDirect is a non-test variant of waitForPortsFree for
89+
// use in TestMain where *testing.T is not available.
90+
func waitForPortsFreeDirect(timeout time.Duration, ports ...int) error {
91+
deadline := time.Now().Add(timeout)
92+
for time.Now().Before(deadline) {
93+
allFree := true
94+
for _, port := range ports {
95+
ln, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
96+
if err != nil {
97+
allFree = false
98+
break
99+
}
100+
ln.Close()
101+
}
102+
if allFree {
103+
return nil
104+
}
105+
time.Sleep(time.Second)
106+
}
107+
return fmt.Errorf("ports %v not free within %v", ports, timeout)
108+
}

tests/e2e/standalone/publish_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
)
2929

3030
func TestStandalonePublish(t *testing.T) {
31-
ensureDaprInstallation(t)
3231
sub := &common.Subscription{
3332
PubsubName: "pubsub",
3433
Topic: "sample",

0 commit comments

Comments
 (0)