@@ -7,7 +7,6 @@ package handlers
7
7
import (
8
8
"context"
9
9
"testing"
10
- "time"
11
10
12
11
"github.com/stretchr/testify/require"
13
12
@@ -26,7 +25,8 @@ import (
26
25
)
27
26
28
27
type mockUpgradeManager struct {
29
- msgChan chan string
28
+ msgChan chan string
29
+ completedChan chan struct {}
30
30
}
31
31
32
32
func (u * mockUpgradeManager ) Upgradeable () bool {
@@ -39,7 +39,7 @@ func (u *mockUpgradeManager) Reload(rawConfig *config.Config) error {
39
39
40
40
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 ) {
41
41
select {
42
- case <- time . After ( 2 * time . Second ) :
42
+ case <- u . completedChan :
43
43
u .msgChan <- "completed " + version
44
44
return nil , nil
45
45
case <- ctx .Done ():
@@ -66,6 +66,7 @@ func TestUpgradeHandler(t *testing.T) {
66
66
67
67
agentInfo := & info.AgentInfo {}
68
68
msgChan := make (chan string )
69
+ completedChan := make (chan struct {})
69
70
70
71
// Create and start the coordinator
71
72
c := coordinator .New (
@@ -75,7 +76,7 @@ func TestUpgradeHandler(t *testing.T) {
75
76
agentInfo ,
76
77
component.RuntimeSpecs {},
77
78
nil ,
78
- & mockUpgradeManager {msgChan : msgChan },
79
+ & mockUpgradeManager {msgChan : msgChan , completedChan : completedChan },
79
80
nil , nil , nil , nil , nil , false )
80
81
//nolint:errcheck // We don't need the termination state of the Coordinator
81
82
go c .Run (ctx )
@@ -85,6 +86,8 @@ func TestUpgradeHandler(t *testing.T) {
85
86
Version : "8.3.0" , SourceURI : "http://localhost" }}
86
87
ack := noopacker .New ()
87
88
err := u .Handle (ctx , & a , ack )
89
+ // indicate that upgrade is completed
90
+ close (completedChan )
88
91
require .NoError (t , err )
89
92
msg := <- msgChan
90
93
require .Equal (t , "completed 8.3.0" , msg )
@@ -100,6 +103,7 @@ func TestUpgradeHandlerSameVersion(t *testing.T) {
100
103
101
104
agentInfo := & info.AgentInfo {}
102
105
msgChan := make (chan string )
106
+ completedChan := make (chan struct {})
103
107
104
108
// Create and start the Coordinator
105
109
c := coordinator .New (
@@ -109,7 +113,7 @@ func TestUpgradeHandlerSameVersion(t *testing.T) {
109
113
agentInfo ,
110
114
component.RuntimeSpecs {},
111
115
nil ,
112
- & mockUpgradeManager {msgChan : msgChan },
116
+ & mockUpgradeManager {msgChan : msgChan , completedChan : completedChan },
113
117
nil , nil , nil , nil , nil , false )
114
118
//nolint:errcheck // We don't need the termination state of the Coordinator
115
119
go c .Run (ctx )
@@ -122,6 +126,8 @@ func TestUpgradeHandlerSameVersion(t *testing.T) {
122
126
err2 := u .Handle (ctx , & a , ack )
123
127
require .NoError (t , err1 )
124
128
require .NoError (t , err2 )
129
+ // indicate that upgrade is completed
130
+ close (completedChan )
125
131
msg := <- msgChan
126
132
require .Equal (t , "completed 8.3.0" , msg )
127
133
}
@@ -136,6 +142,7 @@ func TestUpgradeHandlerNewVersion(t *testing.T) {
136
142
137
143
agentInfo := & info.AgentInfo {}
138
144
msgChan := make (chan string )
145
+ completedChan := make (chan struct {})
139
146
140
147
// Create and start the Coordinator
141
148
c := coordinator .New (
@@ -145,7 +152,7 @@ func TestUpgradeHandlerNewVersion(t *testing.T) {
145
152
agentInfo ,
146
153
component.RuntimeSpecs {},
147
154
nil ,
148
- & mockUpgradeManager {msgChan : msgChan },
155
+ & mockUpgradeManager {msgChan : msgChan , completedChan : completedChan },
149
156
nil , nil , nil , nil , nil , false )
150
157
//nolint:errcheck // We don't need the termination state of the Coordinator
151
158
go c .Run (ctx )
@@ -158,11 +165,12 @@ func TestUpgradeHandlerNewVersion(t *testing.T) {
158
165
ack := noopacker .New ()
159
166
err1 := u .Handle (ctx , & a1 , ack )
160
167
require .NoError (t , err1 )
161
- time .Sleep (1 * time .Second )
162
168
err2 := u .Handle (ctx , & a2 , ack )
163
169
require .NoError (t , err2 )
164
170
msg1 := <- msgChan
165
171
require .Equal (t , "canceled 8.2.0" , msg1 )
172
+ // indicate that upgrade is completed
173
+ close (completedChan )
166
174
msg2 := <- msgChan
167
175
require .Equal (t , "completed 8.5.0" , msg2 )
168
176
}
0 commit comments