feat(surf): AssembleRouter builds the router and handler once; the router stops the MCP endpoint it mounted (quick-261118-lbt)
- surf.AssembleRouter returns the router and the compiled handler from one build, so an mcp.enabled app is not built twice; Assemble keeps its signature and is implemented on top of it - serve uses AssembleRouter with a deferred Router.Close - pier.Pier.Close stops the session reaper and the Redis warm-up; the router that mounted the endpoint owns it - ExampleAssembleRouter, routing/testing docs and the surf/pier READMEs
This commit is contained in:
60
modules/surf/assemble_router_test.go
Normal file
60
modules/surf/assemble_router_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package surf
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/party"
|
||||
)
|
||||
|
||||
// TestAssembleRouterOneBuild: the router and the handler AssembleRouter
|
||||
// returns come from one build, so they share one limiter and one store, and
|
||||
// a failed build leaves no sweep goroutine behind.
|
||||
func TestAssembleRouterOneBuild(t *testing.T) {
|
||||
p := assemblePlugin{id: "golem15.demo", path: "/ping", use: []string{"throttle:2,1"}}
|
||||
r, h, err := AssembleRouter(backpack.New(nil), []party.Plugin{p})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found := false
|
||||
for _, ri := range r.Routes() {
|
||||
if ri.Method == http.MethodGet && ri.Pattern == "/api/ping" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("GET /api/ping not in %+v", r.Routes())
|
||||
}
|
||||
for i, want := range []int{http.StatusOK, http.StatusOK, http.StatusTooManyRequests} {
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/ping", nil))
|
||||
if rec.Code != want {
|
||||
t.Fatalf("request %d = %d, want %d", i+1, rec.Code, want)
|
||||
}
|
||||
}
|
||||
ms := routerStore(t, r)
|
||||
if err := r.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !waitClosed(ms.done, 2*time.Second) {
|
||||
t.Fatal("Router.Close left the store sweeping")
|
||||
}
|
||||
|
||||
t.Run("a failed build closes its store", func(t *testing.T) {
|
||||
bad := assemblePlugin{id: "golem15.demo", use: []string{"jwt.auth"}}
|
||||
baseline := runtime.NumGoroutine()
|
||||
for range 10 {
|
||||
r, h, err := AssembleRouter(backpack.New(nil), []party.Plugin{bad})
|
||||
if err == nil || r != nil || h != nil {
|
||||
t.Fatalf("AssembleRouter = %v, %v, %v; want nil, nil and an error", r, h, err)
|
||||
}
|
||||
}
|
||||
if n := waitGoroutines(baseline+2, 2*time.Second); n > baseline+2 {
|
||||
t.Fatalf("goroutines = %d after 10 failed builds, want at most %d", n, baseline+2)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user