mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
functions: common concurrency stream for sync and async (#314)
* functions: add bounded concurrency * functions: plug runners to sync and async interfaces * functions: update documentation about the new env var * functions: fix test flakiness * functions: the runner is self-regulated, no need to set a number of runners * functions: push the execution to the background on incoming requests * functions: ensure async tasks are always on * functions: add prioritization to tasks consumption Ensure that Sync tasks are consumed before Async tasks. Also, fixes termination races problems for free. * functions: remove stale comments * functions: improve mem availability calculation * functions: parallel run for async tasks * functions: check for memory availability before pulling async task * functions: comment about rnr.hasAvailableMemory and sync.Cond * functions: implement memory check for async runners using Cond vars * functions: code grooming - remove unnecessary goroutines - fix stale docs - reorganize import group * Revert "functions: implement memory check for async runners using Cond vars" This reverts commit 922e64032201a177c03ce6a46240925e3d35430d. * Revert "functions: comment about rnr.hasAvailableMemory and sync.Cond" This reverts commit 49ad7d52d341f12da9603b1a1df9d145871f0e0a. * functions: set a minimum memory availability for sync * functions: simplify the implementation by removing the priority queue * functions: code grooming - code deduplication - review waitgroups Waits
This commit is contained in:
committed by
Seif Lotfy سيف لطفي
parent
c1f361dd0c
commit
9d06b6e687
@@ -10,7 +10,6 @@ import (
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -18,7 +17,6 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/functions/api/mqs"
|
||||
"github.com/iron-io/runner/drivers"
|
||||
)
|
||||
|
||||
func setLogBuffer() *bytes.Buffer {
|
||||
@@ -92,22 +90,6 @@ func getTestServer(mockTasks []*models.Task) *httptest.Server {
|
||||
return httptest.NewServer(r)
|
||||
}
|
||||
|
||||
var helloImage = "iron/hello"
|
||||
|
||||
func TestRunTask(t *testing.T) {
|
||||
mockTask := getMockTask()
|
||||
mockTask.Image = &helloImage
|
||||
|
||||
result, err := runTask(context.Background(), &mockTask)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if result.Status() != "success" {
|
||||
t.Errorf("TestRunTask failed to execute runTask")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTask(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
mockTask := getMockTask()
|
||||
@@ -206,19 +188,35 @@ func TestTasksrvURL(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func testRunner(t *testing.T) *Runner {
|
||||
r, err := New(NewMetricLogger())
|
||||
if err != nil {
|
||||
t.Fatal("Test: failed to create new runner")
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func TestAsyncRunnersGracefulShutdown(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
mockTask := getMockTask()
|
||||
ts := getTestServer([]*models.Task{&mockTask})
|
||||
defer ts.Close()
|
||||
|
||||
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go startAsyncRunners(ctx, &wg, 0, ts.URL+"/tasks", func(ctx context.Context, task *models.Task) (drivers.RunResult, error) {
|
||||
return nil, nil
|
||||
})
|
||||
wg.Wait()
|
||||
tasks := make(chan TaskRequest)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
defer close(tasks)
|
||||
go func() {
|
||||
for t := range tasks {
|
||||
t.Response <- TaskResponse{
|
||||
Result: nil,
|
||||
Err: nil,
|
||||
}
|
||||
|
||||
}
|
||||
}()
|
||||
|
||||
startAsyncRunners(ctx, ts.URL+"/tasks", tasks, testRunner(t))
|
||||
|
||||
if err := ctx.Err(); err != context.DeadlineExceeded {
|
||||
t.Log(buf.String())
|
||||
|
||||
Reference in New Issue
Block a user