Help us shape the future of Go! Take the 2026 Go Developer Survey

planfake

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package planfake provides a deterministic in-process REST scan-planning server for tests. It intentionally uses independent JSON responses instead of the production REST wire structs.

Index

Constants

View Source
const (
	PlanTableScanEndpoint       = "POST /v1/{prefix}/namespaces/{namespace}/tables/{table}/plan"
	FetchPlanningResultEndpoint = "GET /v1/{prefix}/namespaces/{namespace}/tables/{table}/plan/{plan-id}"
	CancelPlanningEndpoint      = "DELETE /v1/{prefix}/namespaces/{namespace}/tables/{table}/plan/{plan-id}"
	FetchScanTasksEndpoint      = "POST /v1/{prefix}/namespaces/{namespace}/tables/{table}/tasks"
)

Scan-planning endpoint strings as advertised by GET /v1/config. These are literal protocol fixtures, deliberately independent from catalog/rest's endpoint constants so a production wire-format regression cannot update both sides of a test unnoticed.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExpectedTarget

type ExpectedTarget struct {
	Prefix    string
	Namespace string
	Table     string
}

ExpectedTarget is the decoded REST planning target accepted by a Scenario. Namespace uses the REST namespace path representation (components joined by the unit separator), while Prefix and Table are decoded path values.

type Request

type Request struct {
	Method    string
	Path      string
	RawQuery  string
	Header    http.Header
	Body      []byte
	Prefix    string
	Namespace string
	Table     string
	PlanID    string
	PlanTask  string
}

Request is one request captured by the fake server. Path is the escaped wire path, which preserves opaque plan IDs containing slashes or dot segments. RawQuery is captured separately without decoding. Prefix, Namespace, Table, PlanID, and PlanTask are decoded routing values when applicable.

type Response

type Response struct {
	Status int
	Header http.Header
	Body   json.RawMessage
	Gate   <-chan struct{}
}

Response is one HTTP response in a scenario. A zero Status means 200 OK. Header and Body are cloned when the server is created and again before a response is written, so callers may safely reuse or mutate their fixtures. When Gate is non-nil, the server waits for it to close or receive a value before writing the response. Cancelling the request releases the waiter; closing the Server aborts the gated connection without manufacturing a successful response.

func ConfigResponse

func ConfigResponse(endpoints ...string) Response

ConfigResponse returns a successful catalog configuration response that advertises endpoints exactly as supplied.

type ResponseSequence

type ResponseSequence struct {
	Responses  []Response
	RepeatLast bool
}

ResponseSequence is an ordered sequence of responses for one scenario key. Each request consumes the next response. Once exhausted, a sequence returns a non-retryable scenario error unless RepeatLast is true, in which case its final response is returned for every later request.

type Scenario

type Scenario struct {
	ExpectedTarget  *ExpectedTarget
	ConfigResponse  Response
	PlanResponse    Response
	PollResponses   map[string]ResponseSequence
	TaskResponses   map[string]ResponseSequence
	CancelResponses map[string]ResponseSequence
}

Scenario declares the responses returned by a Server. ExpectedTarget is required for every planning operation; requests for another target, or a planning request made without an expected target, receive a non-retryable scenario error.

Poll responses are keyed by decoded plan ID, task responses by the plan-task value in the request body, and cancel responses by decoded plan ID. A missing key, empty sequence, or exhausted non-repeating sequence returns a clear, non-retryable scenario error.

An all-zero ConfigResponse or PlanResponse means that response was not configured. Use Response{Status: http.StatusOK} to model an intentional empty 200 response. Scenario errors fail the owning test during cleanup unless a test explicitly acknowledges their exact values with Server.AcknowledgeScenarioErrors.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a concurrency-safe in-process REST scan-planning server.

func New

func New(t testing.TB, scenario Scenario) *Server

New starts a fake server for scenario and registers cleanup with t.

func (*Server) AcknowledgeScenarioErrors

func (s *Server) AcknowledgeScenarioErrors(want ...string) error

AcknowledgeScenarioErrors marks the currently pending scenario errors as expected only when they exactly equal want in order. A mismatch leaves every error pending so cleanup still fails, as do any errors observed later.

func (*Server) CancelCount

func (s *Server) CancelCount(planID string) int

CancelCount returns the number of cancellation requests received for planID.

func (*Server) Close

func (s *Server) Close()

Close stops the fake server. It is safe to call more than once.

func (*Server) PollCount

func (s *Server) PollCount(planID string) int

PollCount returns the number of poll requests received for planID.

func (*Server) Requests

func (s *Server) Requests() []Request

Requests returns a defensive copy of the complete request history.

func (*Server) ScenarioErrors

func (s *Server) ScenarioErrors() []string

ScenarioErrors returns a defensive copy of unacknowledged scenario-contract errors observed by the server. Any errors left pending fail the owning test during cleanup.

func (*Server) TaskCount

func (s *Server) TaskCount(planTask string) int

TaskCount returns the number of task-fetch requests received for planTask.

func (*Server) URL

func (s *Server) URL() string

URL returns the fake server's base URL.

func (*Server) WaitForPollCount

func (s *Server) WaitForPollCount(ctx context.Context, planID string, atLeast int) error

WaitForPollCount waits until at least the requested number of poll requests have been received for planID. It returns ctx.Err if the context ends first, or http.ErrServerClosed if the server closes before the threshold is reached.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL