Skip to content

Commit 0efab75

Browse files
Fix flaky test Test_runDispatcher/gateway_actions_passed (#5405)
* refctor: make context timeout adjustable per test case A short context timeout makes some tests flaky, but making it longer across the board would unnecessarily make test runs longer. Let's make it customizable per test case. * Fix flaky test `Test_runDispatcher/gateway_actions_passed` The flakiness is a result of a 100 miliseconds not always being enough time to dispatch the action in the test environment. The correct way to fix this issue is to run the assertions in an `assert.Eventually` with a longer timeout. Unfortunately, a deficiency in the `testify` testing framework makes it impossible to put the `AssertExpectations` methods in an `assert.Eventually` or `assert.EventuallyWithT` function: stretchr/testify#1414. The workaround is to increase the context timeout for the `gateway actions passed` test case. I'm increasing it from 100ms to 200ms.
1 parent a172e8e commit 0efab75

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

internal/pkg/agent/application/managed_mode_test.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ func Test_runDispatcher(t *testing.T) {
7474
name string
7575
mockGateway func(chan []fleetapi.Action) *mockGateway
7676
mockDispatcher func() *mockDispatcher
77-
interval time.Duration
77+
flushInterval time.Duration
78+
contextTimeout time.Duration
7879
skipOnWindowsReason string
7980
}{{
8081
name: "dispatcher not called",
@@ -87,7 +88,8 @@ func Test_runDispatcher(t *testing.T) {
8788
dispatcher := &mockDispatcher{}
8889
return dispatcher
8990
},
90-
interval: time.Second,
91+
flushInterval: time.Second,
92+
contextTimeout: time.Millisecond * 100,
9193
}, {
9294
name: "gateway actions passed",
9395
mockGateway: func(ch chan []fleetapi.Action) *mockGateway {
@@ -101,7 +103,8 @@ func Test_runDispatcher(t *testing.T) {
101103
dispatcher.On("Dispatch", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Once()
102104
return dispatcher
103105
},
104-
interval: time.Second,
106+
flushInterval: time.Second,
107+
contextTimeout: time.Millisecond * 200,
105108
}, {
106109
name: "no gateway actions, dispatcher is flushed",
107110
mockGateway: func(ch chan []fleetapi.Action) *mockGateway {
@@ -115,7 +118,8 @@ func Test_runDispatcher(t *testing.T) {
115118
dispatcher.On("Dispatch", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Maybe() // allow a second call in case there are timing issues in the CI pipeline
116119
return dispatcher
117120
},
118-
interval: time.Millisecond * 60,
121+
flushInterval: time.Millisecond * 60,
122+
contextTimeout: time.Millisecond * 100,
119123
}}
120124

121125
for _, tc := range tests {
@@ -130,9 +134,9 @@ func Test_runDispatcher(t *testing.T) {
130134
detailsSetter := func(upgradeDetails *details.Details) {}
131135
acker := &mockAcker{}
132136

133-
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
137+
ctx, cancel := context.WithTimeout(context.Background(), tc.contextTimeout)
134138
defer cancel()
135-
runDispatcher(ctx, dispatcher, gateway, detailsSetter, acker, tc.interval)
139+
runDispatcher(ctx, dispatcher, gateway, detailsSetter, acker, tc.flushInterval)
136140
assert.Empty(t, ch)
137141

138142
gateway.AssertExpectations(t)

0 commit comments

Comments
 (0)