@@ -45,8 +45,8 @@ func TestImmediatelySuccessfulProxy(t *testing.T) {
45
45
46
46
timeouts := defaultTimeouts ()
47
47
dialCtxFunc := retryDialContextFunc (timeouts , timeouts .DefaultBackoff ())
48
- waitFunc := func (context.Context , string , string ) error {
49
- return nil
48
+ waitFunc := func (context.Context , string , string ) ( int , error ) {
49
+ return 1 , nil
50
50
}
51
51
hdl := newForwardingHandler (
52
52
logr .Discard (),
@@ -68,6 +68,7 @@ func TestImmediatelySuccessfulProxy(t *testing.T) {
68
68
69
69
hdl .ServeHTTP (res , req )
70
70
71
+ r .Equal ("false" , res .Header ().Get ("X-KEDA-HTTP-Cold-Start" ), "expected X-KEDA-HTTP-Cold-Start false" )
71
72
r .Equal (200 , res .Code , "expected response code 200" )
72
73
r .Equal ("test response" , res .Body .String ())
73
74
}
@@ -85,8 +86,8 @@ func TestWaitFailedConnection(t *testing.T) {
85
86
timeouts ,
86
87
backoff ,
87
88
)
88
- waitFunc := func (context.Context , string , string ) error {
89
- return nil
89
+ waitFunc := func (context.Context , string , string ) ( int , error ) {
90
+ return 1 , nil
90
91
}
91
92
routingTable := routing .NewTable ()
92
93
routingTable .AddTarget (host , routing .NewTarget (
@@ -117,6 +118,7 @@ func TestWaitFailedConnection(t *testing.T) {
117
118
118
119
hdl .ServeHTTP (res , req )
119
120
121
+ r .Equal ("false" , res .Header ().Get ("X-KEDA-HTTP-Cold-Start" ), "expected X-KEDA-HTTP-Cold-Start false" )
120
122
r .Equal (502 , res .Code , "response code was unexpected" )
121
123
}
122
124
@@ -166,13 +168,19 @@ func TestTimesOutOnWaitFunc(t *testing.T) {
166
168
167
169
t .Logf ("elapsed time was %s" , elapsed )
168
170
// serving should take at least timeouts.DeploymentReplicas, but no more than
169
- // timeouts.DeploymentReplicas*2
170
- // elapsed time should be more than the deployment replicas wait time
171
- // but not an amount that is much greater than that
171
+ // timeouts.DeploymentReplicas*4
172
172
r .GreaterOrEqual (elapsed , timeouts .DeploymentReplicas )
173
173
r .LessOrEqual (elapsed , timeouts .DeploymentReplicas * 4 )
174
174
r .Equal (502 , res .Code , "response code was unexpected" )
175
175
176
+ // we will always return the X-KEDA-HTTP-Cold-Start header
177
+ // when we are able to forward the
178
+ // request to the backend but not if we have failed due
179
+ // to a timeout from a waitFunc or earlier in the pipeline,
180
+ // for example, if we cannot reach the Kubernetes control
181
+ // plane.
182
+ r .Equal ("" , res .Header ().Get ("X-KEDA-HTTP-Cold-Start" ), "expected X-KEDA-HTTP-Cold-Start to be empty" )
183
+
176
184
// waitFunc should have been called, even though it timed out
177
185
waitFuncCalled := false
178
186
select {
@@ -277,8 +285,8 @@ func TestWaitHeaderTimeout(t *testing.T) {
277
285
278
286
timeouts := defaultTimeouts ()
279
287
dialCtxFunc := retryDialContextFunc (timeouts , timeouts .DefaultBackoff ())
280
- waitFunc := func (context.Context , string , string ) error {
281
- return nil
288
+ waitFunc := func (context.Context , string , string ) ( int , error ) {
289
+ return 1 , nil
282
290
}
283
291
routingTable := routing .NewTable ()
284
292
target := routing .NewTarget (
@@ -309,6 +317,7 @@ func TestWaitHeaderTimeout(t *testing.T) {
309
317
310
318
hdl .ServeHTTP (res , req )
311
319
320
+ r .Equal ("false" , res .Header ().Get ("X-KEDA-HTTP-Cold-Start" ), "expected X-KEDA-HTTP-Cold-Start false" )
312
321
r .Equal (502 , res .Code , "response code was unexpected" )
313
322
close (originHdlCh )
314
323
}
@@ -346,19 +355,19 @@ func waitForSignal(sig <-chan struct{}, waitDur time.Duration) error {
346
355
// is called, or the context that is passed to it is done (e.g. cancelled, timed out,
347
356
// etc...). in the former case, the returned func itself returns nil. in the latter,
348
357
// it returns ctx.Err()
349
- func notifyingFunc () (func (context. Context , string , string ) error , <- chan struct {}, func ()) {
358
+ func notifyingFunc () (forwardWaitFunc , <- chan struct {}, func ()) {
350
359
calledCh := make (chan struct {})
351
360
finishCh := make (chan struct {})
352
361
finishFunc := func () {
353
362
close (finishCh )
354
363
}
355
- return func (ctx context.Context , _ , _ string ) error {
364
+ return func (ctx context.Context , _ , _ string ) ( int , error ) {
356
365
close (calledCh )
357
366
select {
358
367
case <- finishCh :
359
- return nil
368
+ return 0 , nil
360
369
case <- ctx .Done ():
361
- return fmt .Errorf ("TEST FUNCTION CONTEXT ERROR: %w" , ctx .Err ())
370
+ return 0 , fmt .Errorf ("TEST FUNCTION CONTEXT ERROR: %w" , ctx .Err ())
362
371
}
363
372
}, calledCh , finishFunc
364
373
}
0 commit comments