Skip to content

Commit d3ba638

Browse files
authored
[testing/integration] Fix flaky test cases (#5359)
* fix: fix concurrency issue with the TestUpgrade* tests * fix: flaky long running test cases * chore: minor optimization
1 parent 0d83a61 commit d3ba638

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

internal/pkg/agent/application/actions/handlers/handler_action_upgrade_test.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package handlers
77
import (
88
"context"
99
"testing"
10-
"time"
1110

1211
"github.com/stretchr/testify/require"
1312

@@ -26,7 +25,8 @@ import (
2625
)
2726

2827
type mockUpgradeManager struct {
29-
msgChan chan string
28+
msgChan chan string
29+
completedChan chan struct{}
3030
}
3131

3232
func (u *mockUpgradeManager) Upgradeable() bool {
@@ -39,7 +39,7 @@ func (u *mockUpgradeManager) Reload(rawConfig *config.Config) error {
3939

4040
func (u *mockUpgradeManager) Upgrade(ctx context.Context, version string, sourceURI string, action *fleetapi.ActionUpgrade, details *details.Details, skipVerifyOverride bool, skipDefaultPgp bool, pgpBytes ...string) (_ reexec.ShutdownCallbackFn, err error) {
4141
select {
42-
case <-time.After(2 * time.Second):
42+
case <-u.completedChan:
4343
u.msgChan <- "completed " + version
4444
return nil, nil
4545
case <-ctx.Done():
@@ -66,6 +66,7 @@ func TestUpgradeHandler(t *testing.T) {
6666

6767
agentInfo := &info.AgentInfo{}
6868
msgChan := make(chan string)
69+
completedChan := make(chan struct{})
6970

7071
// Create and start the coordinator
7172
c := coordinator.New(
@@ -75,7 +76,7 @@ func TestUpgradeHandler(t *testing.T) {
7576
agentInfo,
7677
component.RuntimeSpecs{},
7778
nil,
78-
&mockUpgradeManager{msgChan: msgChan},
79+
&mockUpgradeManager{msgChan: msgChan, completedChan: completedChan},
7980
nil, nil, nil, nil, nil, false)
8081
//nolint:errcheck // We don't need the termination state of the Coordinator
8182
go c.Run(ctx)
@@ -85,6 +86,8 @@ func TestUpgradeHandler(t *testing.T) {
8586
Version: "8.3.0", SourceURI: "http://localhost"}}
8687
ack := noopacker.New()
8788
err := u.Handle(ctx, &a, ack)
89+
// indicate that upgrade is completed
90+
close(completedChan)
8891
require.NoError(t, err)
8992
msg := <-msgChan
9093
require.Equal(t, "completed 8.3.0", msg)
@@ -100,6 +103,7 @@ func TestUpgradeHandlerSameVersion(t *testing.T) {
100103

101104
agentInfo := &info.AgentInfo{}
102105
msgChan := make(chan string)
106+
completedChan := make(chan struct{})
103107

104108
// Create and start the Coordinator
105109
c := coordinator.New(
@@ -109,7 +113,7 @@ func TestUpgradeHandlerSameVersion(t *testing.T) {
109113
agentInfo,
110114
component.RuntimeSpecs{},
111115
nil,
112-
&mockUpgradeManager{msgChan: msgChan},
116+
&mockUpgradeManager{msgChan: msgChan, completedChan: completedChan},
113117
nil, nil, nil, nil, nil, false)
114118
//nolint:errcheck // We don't need the termination state of the Coordinator
115119
go c.Run(ctx)
@@ -122,6 +126,8 @@ func TestUpgradeHandlerSameVersion(t *testing.T) {
122126
err2 := u.Handle(ctx, &a, ack)
123127
require.NoError(t, err1)
124128
require.NoError(t, err2)
129+
// indicate that upgrade is completed
130+
close(completedChan)
125131
msg := <-msgChan
126132
require.Equal(t, "completed 8.3.0", msg)
127133
}
@@ -136,6 +142,7 @@ func TestUpgradeHandlerNewVersion(t *testing.T) {
136142

137143
agentInfo := &info.AgentInfo{}
138144
msgChan := make(chan string)
145+
completedChan := make(chan struct{})
139146

140147
// Create and start the Coordinator
141148
c := coordinator.New(
@@ -145,7 +152,7 @@ func TestUpgradeHandlerNewVersion(t *testing.T) {
145152
agentInfo,
146153
component.RuntimeSpecs{},
147154
nil,
148-
&mockUpgradeManager{msgChan: msgChan},
155+
&mockUpgradeManager{msgChan: msgChan, completedChan: completedChan},
149156
nil, nil, nil, nil, nil, false)
150157
//nolint:errcheck // We don't need the termination state of the Coordinator
151158
go c.Run(ctx)
@@ -158,11 +165,12 @@ func TestUpgradeHandlerNewVersion(t *testing.T) {
158165
ack := noopacker.New()
159166
err1 := u.Handle(ctx, &a1, ack)
160167
require.NoError(t, err1)
161-
time.Sleep(1 * time.Second)
162168
err2 := u.Handle(ctx, &a2, ack)
163169
require.NoError(t, err2)
164170
msg1 := <-msgChan
165171
require.Equal(t, "canceled 8.2.0", msg1)
172+
// indicate that upgrade is completed
173+
close(completedChan)
166174
msg2 := <-msgChan
167175
require.Equal(t, "completed 8.5.0", msg2)
168176
}

testing/integration/agent_long_running_leak_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (runner *ExtendedRunner) TestHandleLeak() {
151151
timer := time.NewTimer(testDuration)
152152
defer timer.Stop()
153153

154-
ticker := time.NewTicker(time.Second * 10)
154+
ticker := time.NewTicker(time.Second * 60)
155155
defer ticker.Stop()
156156

157157
done := false

0 commit comments

Comments
 (0)