faas/gateway/handlers/notifier_wrapper_test.go
Alex Ellis (OpenFaaS Ltd) 96812d2cd8 Record metrics for invocations when they start
* This experimental patch records metrics as invocations start
so that the metrics can be used to make better scale to zero
decisions in faas-idler.

Tested with Kubernetes on a single-node cluster, metrics
reported as expected. Existing metrics still report.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
2020-03-12 13:51:43 +00:00

90 lines
2.2 KiB
Go

// Copyright (c) OpenFaaS Author(s) 2018. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package handlers
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func Test_MakeNotifierWrapper_ReceivesHttpStatusInNotifier(t *testing.T) {
notifier := &testNotifier{}
handlerVisited := false
handlerWant := http.StatusAccepted
handler := MakeNotifierWrapper(func(w http.ResponseWriter, r *http.Request) {
handlerVisited = true
w.WriteHeader(handlerWant)
}, []HTTPNotifier{notifier})
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if handlerVisited != true {
t.Errorf("expected handler to have been visited")
}
if notifier.StatusReceived == 0 {
t.Errorf("notifier wanted a status, but got none")
t.Fail()
return
}
if rec.Result().StatusCode != handlerWant {
t.Errorf("recorder status want: %d, got %d", handlerWant, rec.Result().StatusCode)
}
if notifier.StatusReceived != handlerWant {
t.Errorf("notifier status want: %d, got %d", handlerWant, notifier.StatusReceived)
}
}
func Test_MakeNotifierWrapper_ReceivesDefaultHttpStatusWhenNotSet(t *testing.T) {
notifier := &testNotifier{}
handlerVisited := false
handlerWant := http.StatusOK
handler := MakeNotifierWrapper(func(w http.ResponseWriter, r *http.Request) {
handlerVisited = true
}, []HTTPNotifier{notifier})
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if handlerVisited != true {
t.Errorf("expected handler to have been visited")
}
if notifier.StatusReceived == 0 {
t.Errorf("notifier wanted a status, but got none")
t.Fail()
return
}
if rec.Result().StatusCode != handlerWant {
t.Errorf("recorder status want: %d, got %d", handlerWant, rec.Result().StatusCode)
}
if notifier.StatusReceived != handlerWant {
t.Errorf("notifier status want: %d, got %d", handlerWant, notifier.StatusReceived)
}
}
type testNotifier struct {
StatusReceived int
}
// Notify about service metrics
func (tf *testNotifier) Notify(method string, URL string, originalURL string, statusCode int, event string, duration time.Duration) {
tf.StatusReceived = statusCode
}