mirror of
https://github.com/openfaas/faas.git
synced 2025-06-09 16:56:47 +00:00
Within MakeScalingHandler() there is a call to GetReplicas() which was not returning an error when a non-200 http response was received from /system/function/. The call would also return a populated struct, so the perception was that a function existed an had been scaled to zero. This meant that the function would be added to the function cache and the code would continue into SetReplicas() where an attempt would be made to scale up a non-existent function. This change amends GetReplicas() so that it will return an error if the gateway returns anything other than a 200 reponse code from the /system/function/ endpoint. This causes MakeScalingHandler() to return earlier with an error indicating that the function could not be found. The cache.Set call is also moved to after the error check so that the cache is only updated to include existent functions. During investigations as to the cause of #876 tests were added to function_cache to check that Get() is behaving as intended when function exists and when not. Tests are also added to plugin/external to test that GetReplicas() and SetReplicas() are following their intended modes of operation when 200 and non-200 responses are received from the gateway. Signed-off-by: Richard Gee <richard@technologee.co.uk>
116 lines
2.3 KiB
Go
116 lines
2.3 KiB
Go
// Copyright (c) OpenFaaS Author(s). All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
package handlers
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func Test_LastRefreshSet(t *testing.T) {
|
|
before := time.Now()
|
|
|
|
fnName := "echo"
|
|
|
|
cache := FunctionCache{
|
|
Cache: make(map[string]*FunctionMeta),
|
|
Expiry: time.Millisecond * 1,
|
|
}
|
|
|
|
if cache.Cache == nil {
|
|
t.Errorf("Expected cache map to be initialized")
|
|
t.Fail()
|
|
}
|
|
|
|
cache.Set(fnName, ServiceQueryResponse{AvailableReplicas: 1})
|
|
|
|
if _, exists := cache.Cache[fnName]; !exists {
|
|
t.Errorf("Expected entry to exist after setting %s", fnName)
|
|
t.Fail()
|
|
}
|
|
|
|
if cache.Cache[fnName].LastRefresh.Before(before) {
|
|
t.Errorf("Expected LastRefresh for function to have been after start of test")
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func Test_CacheExpiresIn1MS(t *testing.T) {
|
|
fnName := "echo"
|
|
|
|
cache := FunctionCache{
|
|
Cache: make(map[string]*FunctionMeta),
|
|
Expiry: time.Millisecond * 1,
|
|
}
|
|
|
|
cache.Set(fnName, ServiceQueryResponse{AvailableReplicas: 1})
|
|
time.Sleep(time.Millisecond * 2)
|
|
|
|
_, hit := cache.Get(fnName)
|
|
|
|
wantHit := false
|
|
|
|
if hit != wantHit {
|
|
t.Errorf("hit, want: %v, got %v", wantHit, hit)
|
|
}
|
|
}
|
|
|
|
func Test_CacheGivesHitWithLongExpiry(t *testing.T) {
|
|
fnName := "echo"
|
|
|
|
cache := FunctionCache{
|
|
Cache: make(map[string]*FunctionMeta),
|
|
Expiry: time.Millisecond * 500,
|
|
}
|
|
|
|
cache.Set(fnName, ServiceQueryResponse{AvailableReplicas: 1})
|
|
|
|
_, hit := cache.Get(fnName)
|
|
wantHit := true
|
|
|
|
if hit != wantHit {
|
|
t.Errorf("hit, want: %v, got %v", wantHit, hit)
|
|
}
|
|
}
|
|
|
|
func Test_CacheFunctionExists(t *testing.T) {
|
|
fnName := "echo"
|
|
|
|
cache := FunctionCache{
|
|
Cache: make(map[string]*FunctionMeta),
|
|
Expiry: time.Millisecond * 10,
|
|
}
|
|
|
|
cache.Set(fnName, ServiceQueryResponse{AvailableReplicas: 1})
|
|
time.Sleep(time.Millisecond * 2)
|
|
|
|
_, hit := cache.Get(fnName)
|
|
|
|
wantHit := true
|
|
|
|
if hit != wantHit {
|
|
t.Errorf("hit, want: %v, got %v", wantHit, hit)
|
|
}
|
|
}
|
|
func Test_CacheFunctionNotExist(t *testing.T) {
|
|
fnName := "echo"
|
|
testName := "burt"
|
|
|
|
cache := FunctionCache{
|
|
Cache: make(map[string]*FunctionMeta),
|
|
Expiry: time.Millisecond * 10,
|
|
}
|
|
|
|
cache.Set(fnName, ServiceQueryResponse{AvailableReplicas: 1})
|
|
time.Sleep(time.Millisecond * 2)
|
|
|
|
_, hit := cache.Get(testName)
|
|
|
|
wantHit := false
|
|
|
|
if hit != wantHit {
|
|
t.Errorf("hit, want: %v, got %v", wantHit, hit)
|
|
}
|
|
}
|