Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/platform/tenant/routing_test.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 9bde0f9a74b89d01980fa658e5253acda7528d0c |
| Branch | per-tenant-routing |
| Path or archive member | internal/platform/tenant/routing_test.go |
| Original SHA-256 | fc2b097d35a9ab119f446f0c1f585b138780b74824e4f938e8d73b8e3cc03e5a |
| Git blob SHA | dbd56a2abf8121555023708f1b2c95ee332d7641 |
| Reading-copy SHA-256 | fc2b097d35a9ab119f446f0c1f585b138780b74824e4f938e8d73b8e3cc03e5a |
Every recorded occurrence
These 3 records contain identical original source bytes. Their paths and revisions remain part of the archive.
| Original record | Branch or archive | Revision / identifier | Original location |
|---|---|---|---|
| Janady13/missionmatch / internal/platform/tenant/routing_test.go | per-tenant-routing | 9bde0f9a74b89d01980fa658e5253acda7528d0c | Open original |
| Janady13/missionmatch / internal/platform/tenant/routing_test.go | deploy-and-ops | bdacd9d0b4d5998d25fc74bf4007f4c3ec538c7a | Open original |
| Janady13/missionmatch / internal/platform/tenant/routing_test.go | per-tenant-routing | 9bde0f9a74b89d01980fa658e5253acda7528d0c | Open original |
Recovered source transcript
Download complete source text ↓87 lines · 2.9 KB
package tenant
import (
"context"
"errors"
"testing"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)
// fakeRouter records the org it was asked to resolve and returns a sentinel, so the routing dispatch is
// observable without a real database (the real open and verify path is proven on staging tenant DBs).
type fakeRouter struct {
called bool
gotOrg uuid.UUID
err error
}
func (f *fakeRouter) Acquire(_ context.Context, org uuid.UUID) (*DB, func(), error) {
f.called = true
f.gotOrg = org
return nil, func() {}, f.err
}
func TestRoutingDispatchesByValidatedOrg(t *testing.T) {
org := uuid.New()
fr := &fakeRouter{err: errors.New("sentinel unreachable")}
db := NewRouting(fr)
ctx := WithOrg(context.Background(), org.String())
err := db.RunInTenantTx(ctx, func(context.Context, pgx.Tx) error { return nil })
if err == nil || err.Error() != "sentinel unreachable" {
t.Fatalf("routing must dispatch to the router and surface its error, got %v", err)
}
if !fr.called || fr.gotOrg != org {
t.Fatalf("the router must be asked for exactly the validated ctx org, called=%v org=%v", fr.called, fr.gotOrg)
}
}
func TestRoutingFailsClosedWithoutOrg(t *testing.T) {
fr := &fakeRouter{}
db := NewRouting(fr)
err := db.RunInTenantTx(context.Background(), func(context.Context, pgx.Tx) error { return nil })
if !errors.Is(err, ErrNoTenant) {
t.Fatalf("a request with no validated org must fail closed with ErrNoTenant, got %v", err)
}
if fr.called {
t.Fatal("the router must NEVER be consulted without a validated org (no tenant pool is touched)")
}
}
func TestRoutingAcquireRoutesToo(t *testing.T) {
org := uuid.New()
fr := &fakeRouter{err: errors.New("sentinel")}
db := NewRouting(fr)
// Acquire (used by the direct pool readers and the skill matcher) routes by the same org.
_, _, err := db.Acquire(WithOrg(context.Background(), org.String()))
if err == nil || err.Error() != "sentinel" {
t.Fatalf("Acquire must route through the router, got %v", err)
}
if fr.gotOrg != org {
t.Fatalf("Acquire routed the wrong org: %v", fr.gotOrg)
}
// Without an org, Acquire fails closed and never touches the router.
fr2 := &fakeRouter{}
if _, _, err := NewRouting(fr2).Acquire(context.Background()); !errors.Is(err, ErrNoTenant) {
t.Fatalf("Acquire with no org must be ErrNoTenant, got %v", err)
}
if fr2.called {
t.Fatal("Acquire must not consult the router without a validated org")
}
}
func TestSharedModeIsANoOpWrapper(t *testing.T) {
// A shared mode DB (no router) resolves to itself with a no op release, so existing behavior is intact.
db := New(nil) // nil pool is fine; we only check the leaf identity and release, not a query
leaf, release, err := db.Acquire(context.Background())
if err != nil {
t.Fatalf("shared mode Acquire must not error: %v", err)
}
if leaf != db {
t.Fatal("shared mode must resolve to the same DB")
}
release() // must be safe to call
}