pgfault

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

internal/testutil/pgfault

Overview

Test helper that relays PostgreSQL connections through a local TCP proxy and injects one fault into the next COMMIT. Tests use it to lose a COMMIT's reply after the server committed, to drop the COMMIT so the server aborts, to hold the transaction open, or to commit it late, and then check how a store resolves the outcome it never heard [pgfault.go:Proxy].

Interface

  • Start — listens on 127.0.0.1, relays to the host of a database URL, and returns the proxy and the URL rewritten to go through it [pgfault.go:Start]
  • Proxy.Arm — arms one Fault for the next COMMIT on any relayed connection, with an optional callback that runs once, before the proxy closes any connection [pgfault.go:Arm]
  • Proxy.Fired — reports whether the armed fault took effect [pgfault.go:Fired]
  • Fault values DropReply, DropCommit, HoldCommit, CutAll, DelayCommit [pgfault.go:Fault]
  • CommitDelay — how long DelayCommit holds the COMMIT before it relays it (300 ms) [pgfault.go:CommitDelay]

Invariants

  • Only a client read that is exactly pgx's simple-protocol commit Query message triggers the fault, and one arm triggers at most one fault [pgfault.go:commitMsg] [pgfault.go:take]
  • Every fault marks itself fired, then runs the callback, before the proxy closes any connection, so Fired is true once the client sees the fault, also when the callback ends the client's context. DropReply runs the callback after the server's reply arrives [pgfault.go:relay] [pgfault.go:fire]
  • Arm panics on an unknown fault [pgfault.go:Arm]

Side Effects

  • Opens a TCP listener and one server connection per client connection; the test's cleanup closes all of them [pgfault.go:Start] [pgfault.go:closeAll]

Verification

  • The package has no tests of its own. The PostgreSQL tests that use it exercise it: go test ./internal/ref/... ./cmd/openstorage/... -tags=openstorage_viewcheck -count=1 -race -timeout=30m (needs Docker)

Documentation

Overview

Package pgfault relays PostgreSQL connections through a local TCP proxy and injects one fault into the next COMMIT, for tests of a COMMIT whose outcome the client does not learn.

Index

Constants

View Source
const CommitDelay = 300 * time.Millisecond

CommitDelay is how long DelayCommit holds the COMMIT.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fault

type Fault int

Fault is what the proxy does to the next COMMIT.

const (
	// DropReply relays the COMMIT, swallows the server's reply, and closes
	// both sides: the transaction commits, and the client never learns it.
	DropReply Fault = iota + 1
	// DropCommit closes both sides instead of relaying the COMMIT: the
	// server aborts the transaction.
	DropCommit
	// HoldCommit closes the client side and keeps the server side open
	// without relaying the COMMIT: the transaction stays in progress until
	// the test ends.
	HoldCommit
	// CutAll drops the COMMIT like DropCommit, then closes every relayed
	// connection and the listener: no later connection reaches the server.
	CutAll
	// DelayCommit closes the client side, then relays the COMMIT after
	// CommitDelay: the transaction stays in progress for CommitDelay, then
	// commits, and the client never learns it.
	DelayCommit
)

type Proxy

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

Proxy relays TCP connections to one PostgreSQL server.

func Start

func Start(t testing.TB, dbURL string) (*Proxy, string)

Start relays to the host of dbURL and returns the proxy and dbURL rewritten to connect through it. t's cleanup closes the listener and every relayed connection.

func (*Proxy) Arm

func (p *Proxy) Arm(f Fault, onFault func())

Arm makes the next COMMIT that any relayed connection sends suffer f. onFault, when not nil, runs once, after Fired turns true and before the proxy closes any connection, so Fired is true by the time the client sees the fault. For DropReply it runs after the server's reply arrived: the transaction committed. For the other faults it runs when the proxy holds the COMMIT.

func (*Proxy) Fired

func (p *Proxy) Fired() bool

Fired reports whether the armed fault took effect.

Jump to

Keyboard shortcuts

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