mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
this works by having every request from the functions server kick back a FXLB-WAIT header on every request with the wait time for that function to start. the lb then keeps track on a per node+function basis an ewma of the last 10 request's wait times (to reduce jitter). now that we don't have max concurrency it's actually pretty challenging to get the wait time stuff to tick. i expect in the near future we will be throttling functions on a given node in order to induce this, but that is for another day as that code needs a lot of reworking. i tested this by introducing some arbitrary throttling (not checked in) and load spreads over nodes correctly (see images). we will also need to play with the intervals we want to use, as if you have a func with 50ms run time then basically 10 of those will rev up another node (this was before removing max_c, with max_c=1) but in any event this wires in the basic plumbing. * make docs great again. renamed lb dir to fnlb * added wait time to dashboard * wires in a ready channel to await the first pull for hot images to count in the wait time (should be otherwise useful) future: TODO rework lb code api to be pluggable + wire in data store TODO toss out first data point containing pull to not jump onto another node immediately (maybe this is actually a good thing?)
122 lines
3.4 KiB
Go
122 lines
3.4 KiB
Go
package runner
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitlab-odx.oracle.com/odx/functions/api/models"
|
|
"gitlab-odx.oracle.com/odx/functions/api/runner/task"
|
|
)
|
|
|
|
func TestRunnerHello(t *testing.T) {
|
|
buf := setLogBuffer()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
runner, err := New(ctx, NewFuncLogger(), NewMetricLogger())
|
|
if err != nil {
|
|
t.Fatalf("Test error during New() - %s", err)
|
|
}
|
|
|
|
for i, test := range []struct {
|
|
route *models.Route
|
|
payload string
|
|
expectedStatus string
|
|
expectedOut string
|
|
expectedErr string
|
|
}{
|
|
{&models.Route{Image: "funcy/hello"}, ``, "success", "Hello World!", ""},
|
|
{&models.Route{Image: "funcy/hello"}, `{"name": "test"}`, "success", "Hello test!", ""},
|
|
} {
|
|
var stdout, stderr bytes.Buffer
|
|
cfg := &task.Config{
|
|
ID: fmt.Sprintf("hello-%d-%d", i, time.Now().Unix()),
|
|
Image: test.route.Image,
|
|
Timeout: 10 * time.Second,
|
|
Ready: make(chan struct{}),
|
|
Stdin: strings.NewReader(test.payload),
|
|
Stdout: &stdout,
|
|
Stderr: &stderr,
|
|
}
|
|
|
|
result, err := runner.run(ctx, cfg)
|
|
if err != nil {
|
|
t.Log(buf.String())
|
|
t.Fatalf("Test %d: error during Run() - %s", i, err)
|
|
}
|
|
|
|
if test.expectedStatus != result.Status() {
|
|
t.Log(buf.String())
|
|
t.Fatalf("Test %d: expected result status to be `%s` but it was `%s`", i, test.expectedStatus, result.Status())
|
|
}
|
|
|
|
if !bytes.Contains(stdout.Bytes(), []byte(test.expectedOut)) {
|
|
t.Log(buf.String())
|
|
t.Fatalf("Test %d: expected output log to contain `%s` in `%s`", i, test.expectedOut, stdout.String())
|
|
}
|
|
|
|
if !bytes.Contains(stderr.Bytes(), []byte(test.expectedErr)) {
|
|
t.Log(buf.String())
|
|
t.Fatalf("Test %d: expected error log to contain `%s` in `%s`", i, test.expectedErr, stderr.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRunnerError(t *testing.T) {
|
|
buf := setLogBuffer()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
runner, err := New(ctx, NewFuncLogger(), NewMetricLogger())
|
|
if err != nil {
|
|
t.Fatalf("Test error during New() - %s", err)
|
|
}
|
|
|
|
for i, test := range []struct {
|
|
route *models.Route
|
|
payload string
|
|
expectedStatus string
|
|
expectedOut string
|
|
expectedErr string
|
|
}{
|
|
{&models.Route{Image: "funcy/error"}, ``, "error", "", ""},
|
|
{&models.Route{Image: "funcy/error"}, `{"name": "test"}`, "error", "", ""},
|
|
} {
|
|
var stdout, stderr bytes.Buffer
|
|
cfg := &task.Config{
|
|
ID: fmt.Sprintf("err-%d-%d", i, time.Now().Unix()),
|
|
Image: test.route.Image,
|
|
Timeout: 10 * time.Second,
|
|
Ready: make(chan struct{}),
|
|
Stdin: strings.NewReader(test.payload),
|
|
Stdout: &stdout,
|
|
Stderr: &stderr,
|
|
}
|
|
|
|
result, err := runner.run(ctx, cfg)
|
|
if err != nil {
|
|
t.Log(buf.String())
|
|
t.Fatalf("Test %d: error during Run() - %s", i, err)
|
|
}
|
|
|
|
if test.expectedStatus != result.Status() {
|
|
t.Log(buf.String())
|
|
t.Fatalf("Test %d: expected result status to be `%s` but it was `%s`", i, test.expectedStatus, result.Status())
|
|
}
|
|
|
|
if !bytes.Contains(stdout.Bytes(), []byte(test.expectedOut)) {
|
|
t.Log(buf.String())
|
|
t.Fatalf("Test %d: expected output log to contain `%s` in `%s`", i, test.expectedOut, stdout.String())
|
|
}
|
|
|
|
if !bytes.Contains(stderr.Bytes(), []byte(test.expectedErr)) {
|
|
t.Log(buf.String())
|
|
t.Fatalf("Test %d: expected error log to contain `%s` in `%s`", i, test.expectedErr, stderr.String())
|
|
}
|
|
}
|
|
}
|