mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
server: Add global LRU for routes with keys being the appname + path (#472)
* Add global lru for routes with keys being the appname + path * minor comment fixes * remove duplicate entires from THIRD_PARTY * Make sure that we lock and unlock on get, refresh and delete on the cache
This commit is contained in:
committed by
C Cirello
parent
b8d01817b8
commit
6f8e94629f
@@ -17,11 +17,12 @@ import (
|
||||
"github.com/iron-io/functions/api/mqs"
|
||||
"github.com/iron-io/functions/api/runner"
|
||||
"github.com/iron-io/functions/api/runner/task"
|
||||
"github.com/iron-io/functions/api/server/internal/routecache"
|
||||
)
|
||||
|
||||
var tmpBolt = "/tmp/func_test_bolt.db"
|
||||
|
||||
func testRouter(ds models.Datastore, mq models.MessageQueue, rnr *runner.Runner, tasks chan task.Request) *gin.Engine {
|
||||
func testServer(ds models.Datastore, mq models.MessageQueue, rnr *runner.Runner, tasks chan task.Request) *Server {
|
||||
ctx := context.Background()
|
||||
|
||||
s := &Server{
|
||||
@@ -31,14 +32,15 @@ func testRouter(ds models.Datastore, mq models.MessageQueue, rnr *runner.Runner,
|
||||
MQ: mq,
|
||||
tasks: tasks,
|
||||
Enqueue: DefaultEnqueue,
|
||||
hotroutes: routecache.New(2),
|
||||
}
|
||||
|
||||
r := s.Router
|
||||
r.Use(gin.Logger())
|
||||
|
||||
r.Use(prepareMiddleware(ctx))
|
||||
s.Router.Use(prepareMiddleware(ctx))
|
||||
s.bindHandlers()
|
||||
return r
|
||||
return s
|
||||
}
|
||||
|
||||
func routerRequest(t *testing.T, router *gin.Engine, method, path string, body io.Reader) (*http.Request, *httptest.ResponseRecorder) {
|
||||
@@ -104,38 +106,45 @@ func TestFullStack(t *testing.T) {
|
||||
|
||||
go runner.StartWorkers(ctx, rnr, tasks)
|
||||
|
||||
router := testRouter(ds, &mqs.Mock{}, rnr, tasks)
|
||||
srv := testServer(ds, &mqs.Mock{}, rnr, tasks)
|
||||
srv.hotroutes = routecache.New(2)
|
||||
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
method string
|
||||
path string
|
||||
body string
|
||||
expectedCode int
|
||||
name string
|
||||
method string
|
||||
path string
|
||||
body string
|
||||
expectedCode int
|
||||
expectedCacheSize int
|
||||
}{
|
||||
{"create my app", "POST", "/v1/apps", `{ "app": { "name": "myapp" } }`, http.StatusOK},
|
||||
{"list apps", "GET", "/v1/apps", ``, http.StatusOK},
|
||||
{"get app", "GET", "/v1/apps/myapp", ``, http.StatusOK},
|
||||
{"add myroute", "POST", "/v1/apps/myapp/routes", `{ "route": { "name": "myroute", "path": "/myroute", "image": "iron/hello" } }`, http.StatusOK},
|
||||
{"add myroute2", "POST", "/v1/apps/myapp/routes", `{ "route": { "name": "myroute2", "path": "/myroute2", "image": "iron/error" } }`, http.StatusOK},
|
||||
{"get myroute", "GET", "/v1/apps/myapp/routes/myroute", ``, http.StatusOK},
|
||||
{"get myroute2", "GET", "/v1/apps/myapp/routes/myroute2", ``, http.StatusOK},
|
||||
{"get all routes", "GET", "/v1/apps/myapp/routes", ``, http.StatusOK},
|
||||
{"execute myroute", "POST", "/r/myapp/myroute", `{ "name": "Teste" }`, http.StatusOK},
|
||||
{"execute myroute2", "POST", "/r/myapp/myroute2", `{ "name": "Teste" }`, http.StatusInternalServerError},
|
||||
{"delete myroute", "DELETE", "/v1/apps/myapp/routes/myroute", ``, http.StatusOK},
|
||||
{"delete app (fail)", "DELETE", "/v1/apps/myapp", ``, http.StatusBadRequest},
|
||||
{"delete myroute2", "DELETE", "/v1/apps/myapp/routes/myroute2", ``, http.StatusOK},
|
||||
{"delete app (success)", "DELETE", "/v1/apps/myapp", ``, http.StatusOK},
|
||||
{"get deleted app", "GET", "/v1/apps/myapp", ``, http.StatusNotFound},
|
||||
{"get delete route on deleted app", "GET", "/v1/apps/myapp/routes/myroute", ``, http.StatusInternalServerError},
|
||||
{"create my app", "POST", "/v1/apps", `{ "app": { "name": "myapp" } }`, http.StatusOK, 0},
|
||||
{"list apps", "GET", "/v1/apps", ``, http.StatusOK, 0},
|
||||
{"get app", "GET", "/v1/apps/myapp", ``, http.StatusOK, 0},
|
||||
{"add myroute", "POST", "/v1/apps/myapp/routes", `{ "route": { "name": "myroute", "path": "/myroute", "image": "iron/hello" } }`, http.StatusOK, 1},
|
||||
{"add myroute2", "POST", "/v1/apps/myapp/routes", `{ "route": { "name": "myroute2", "path": "/myroute2", "image": "iron/error" } }`, http.StatusOK, 2},
|
||||
{"get myroute", "GET", "/v1/apps/myapp/routes/myroute", ``, http.StatusOK, 2},
|
||||
{"get myroute2", "GET", "/v1/apps/myapp/routes/myroute2", ``, http.StatusOK, 2},
|
||||
{"get all routes", "GET", "/v1/apps/myapp/routes", ``, http.StatusOK, 2},
|
||||
{"execute myroute", "POST", "/r/myapp/myroute", `{ "name": "Teste" }`, http.StatusOK, 2},
|
||||
{"execute myroute2", "POST", "/r/myapp/myroute2", `{ "name": "Teste" }`, http.StatusInternalServerError, 2},
|
||||
{"delete myroute", "DELETE", "/v1/apps/myapp/routes/myroute", ``, http.StatusOK, 1},
|
||||
{"delete app (fail)", "DELETE", "/v1/apps/myapp", ``, http.StatusBadRequest, 1},
|
||||
{"delete myroute2", "DELETE", "/v1/apps/myapp/routes/myroute2", ``, http.StatusOK, 0},
|
||||
{"delete app (success)", "DELETE", "/v1/apps/myapp", ``, http.StatusOK, 0},
|
||||
{"get deleted app", "GET", "/v1/apps/myapp", ``, http.StatusNotFound, 0},
|
||||
{"get delete route on deleted app", "GET", "/v1/apps/myapp/routes/myroute", ``, http.StatusInternalServerError, 0},
|
||||
} {
|
||||
_, rec := routerRequest(t, router, test.method, test.path, bytes.NewBuffer([]byte(test.body)))
|
||||
_, rec := routerRequest(t, srv.Router, test.method, test.path, bytes.NewBuffer([]byte(test.body)))
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test \"%s\": Expected status code to be %d but was %d",
|
||||
test.name, test.expectedCode, rec.Code)
|
||||
}
|
||||
if srv.hotroutes.Len() != test.expectedCacheSize {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test \"%s\": Expected cache size to be %d but was %d",
|
||||
test.name, test.expectedCacheSize, srv.hotroutes.Len())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user