| index 5cd344a37..912c93ede 100644 |
| --- a/internal/console/requestctx_test.go |
| +++ b/internal/console/requestctx_test.go |
| @@ -10,6 +10,8 @@ import ( |
| "sync" |
| "testing" |
| "time" |
| + |
| + "github.com/carlosframework/platform/internal/svcrole" |
| ) |
| |
| // TestCancelledRequestLeavesTheConnectionQueue: the console store has one |
| @@ -20,9 +22,13 @@ import ( |
| // return while the connection is still held, without the page's queries |
| // running. |
| // |
| +// A case with pauseAt is held up further in: the connection is taken only |
| +// once the request reaches that query (pauseDB), so the request is past |
| +// its bearer lookup and queued at the later one when the client goes. |
| +// |
| // The wait is on the pool's own counter (the request is queued once |
| -// WaitCount moves) and on the handler returning; the timer only bounds a |
| -// failure. |
| +// WaitCount moves), on pauseDB's signal and on the handler returning; the |
| +// timer only bounds a failure. |
| func TestCancelledRequestLeavesTheConnectionQueue(t *testing.T) { |
| fx := newConsoleFixture(t) |
| const person = "paul@keymail.dev" |
| @@ -31,16 +37,22 @@ func TestCancelledRequestLeavesTheConnectionQueue(t *testing.T) { |
| t.Fatal(err) |
| } |
| token := mintHostClaimToken(t, fx.st, person) |
| + service := serviceToken(t, fx.st, OpsAccountID, "ci", "kass", svcrole.Publish) |
| cookie := func(r *http.Request) { r.AddCookie(&http.Cookie{Name: sessionCookie, Value: session}) } |
| bearer := func(r *http.Request) { r.Header.Set("Authorization", "Bearer "+token) } |
| + serviceBearer := func(r *http.Request) { r.Header.Set("Authorization", "Bearer "+service) } |
| for _, c := range []struct { |
| name, path string |
| auth func(*http.Request) |
| + pauseAt string |
| }{ |
| - {"app list", fx.path("/"), cookie}, |
| - {"app page", fx.path("/apps/kass"), cookie}, |
| - {"instances", fx.path("/apps/kass/instances"), cookie}, |
| - {"cli door", "/api/cli/apps", bearer}, |
| + {"app list", fx.path("/"), cookie, ""}, |
| + {"app page", fx.path("/apps/kass"), cookie, ""}, |
| + {"instances", fx.path("/apps/kass/instances"), cookie, ""}, |
| + {"cli door", "/api/cli/apps", bearer, ""}, |
| + {"cli door, service credential", "/api/cli/apps", serviceBearer, ""}, |
| + // serviceBinding's account read, after the bearer resolved. |
| + {"cli door, service binding", "/api/cli/apps", serviceBearer, "FROM accounts WHERE id = ?"}, |
| } { |
| t.Run(c.name, func(t *testing.T) { |
| // The door answers this caller when nothing is in its way. |
| @@ -48,22 +60,33 @@ func TestCancelledRequestLeavesTheConnectionQueue(t *testing.T) { |
| c.auth(req) |
| rec := httptest.NewRecorder() |
| fx.srv.ServeHTTP(rec, req) |
| - if rec.Code != http.StatusOK { |
| - t.Fatalf("GET %s = %d with the connection free, want 200:\n%s", c.path, rec.Code, rec.Body) |
| + if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "kass") { |
| + t.Fatalf("GET %s = %d with the connection free, want 200 naming kass:\n%s", c.path, rec.Code, rec.Body) |
| } |
| |
| - held, err := fx.st.rawPool.Conn(context.Background()) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| + var held *sql.Conn |
| released := false |
| release := func() { |
| - if !released { |
| + if held != nil && !released { |
| released = true |
| held.Close() |
| } |
| } |
| defer release() |
| + hold := func() { |
| + var err error |
| + if held, err = fx.st.rawPool.Conn(context.Background()); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + var pause *pauseDB |
| + if c.pauseAt != "" { |
| + pause = &pauseDB{sqlHandle: fx.st.db, at: c.pauseAt, reached: make(chan struct{}), proceed: make(chan struct{})} |
| + fx.st.db = pause |
| + defer func() { fx.st.db = pause.sqlHandle }() |
| + } else { |
| + hold() |
| + } |
| |
| ctx, cancel := context.WithCancel(context.Background()) |
| defer cancel() |
| @@ -77,6 +100,18 @@ func TestCancelledRequestLeavesTheConnectionQueue(t *testing.T) { |
| fx.srv.ServeHTTP(rec, req) |
| }() |
| bound := time.After(10 * time.Second) |
| + if pause != nil { |
| + select { |
| + case <-pause.reached: |
| + case <-done: |
| + t.Fatalf("GET %s returned %d before it reached %q", c.path, rec.Code, c.pauseAt) |
| + case <-bound: |
| + t.Fatalf("GET %s never reached %q", c.path, c.pauseAt) |
| + } |
| + hold() |
| + waits = fx.st.rawPool.Stats().WaitCount |
| + close(pause.proceed) |
| + } |
| for fx.st.rawPool.Stats().WaitCount == waits { |
| select { |
| case <-done: |
| @@ -98,11 +133,11 @@ func TestCancelledRequestLeavesTheConnectionQueue(t *testing.T) { |
| if released { |
| t.Fatal("the held connection was let go before the request returned") |
| } |
| - // It gave up at the first query, the session or bearer |
| - // lookup: the caller reads as signed out, and nothing of the |
| - // page was served. |
| - if rec.Code == http.StatusOK { |
| - t.Fatalf("GET %s = 200 after the client went away, want the signed-out answer", c.path) |
| + // It gave up at the query it was queued at: the session or |
| + // bearer lookup reads as signed out, the service binding as no |
| + // binding, and nothing of the page was served. |
| + if rec.Code == http.StatusOK && strings.Contains(rec.Body.String(), "kass") { |
| + t.Fatalf("GET %s served the page after the client went away:\n%s", c.path, rec.Body) |
| } |
| }) |
| } |
| @@ -123,6 +158,7 @@ func TestSignedInPagesQueryUnderTheRequestContext(t *testing.T) { |
| t.Fatal(err) |
| } |
| token := mintHostClaimToken(t, fx.st, person) |
| + service := serviceToken(t, fx.st, OpsAccountID, "ci", "kass", svcrole.Publish) |
| check := &requestCtxDB{sqlHandle: fx.st.db} |
| fx.st.db = check |
| t.Cleanup(func() { fx.st.db = check.sqlHandle }) |
| @@ -134,6 +170,7 @@ func TestSignedInPagesQueryUnderTheRequestContext(t *testing.T) { |
| {"app page", fx.path("/apps/kass"), session}, |
| {"instances", fx.path("/apps/kass/instances"), session}, |
| {"cli door", "/api/cli/apps", "Bearer " + token}, |
| + {"cli door, service credential", "/api/cli/apps", "Bearer " + service}, |
| } { |
| t.Run(c.name, func(t *testing.T) { |
| ctx := context.WithValue(context.Background(), requestCtxMark{}, c.path) |
| @@ -249,3 +286,31 @@ func (d *requestCtxDB) PrepareContext(ctx context.Context, query string) (*sql.S |
| d.note(ctx, "PrepareContext", query) |
| return d.sqlHandle.PrepareContext(ctx, query) |
| } |
| + |
| +// pauseDB holds up the first query containing at: it says so on reached |
| +// and goes on to the store only once proceed is closed. |
| +type pauseDB struct { |
| + sqlHandle |
| + at string |
| + once sync.Once |
| + reached, proceed chan struct{} |
| +} |
| + |
| +func (d *pauseDB) wait(query string) { |
| + if strings.Contains(query, d.at) { |
| + d.once.Do(func() { |
| + close(d.reached) |
| + <-d.proceed |
| + }) |
| + } |
| +} |
| + |
| +func (d *pauseDB) QueryRow(query string, args ...any) *sql.Row { |
| + d.wait(query) |
| + return d.sqlHandle.QueryRow(query, args...) |
| +} |
| + |
| +func (d *pauseDB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row { |
| + d.wait(query) |
| + return d.sqlHandle.QueryRowContext(ctx, query, args...) |
| +} |