package testonly

import "github.com/google/trillian/util/election2/testonly"

Package testonly contains an Election implementation for testing.

Index

Variables

var Factory factory

Factory allows creating Election instances for testing.

var Tests = []NamedTest{
	{Name: "RunElectionAwait", Run: runElectionAwait},
	{Name: "RunElectionWithMastership", Run: runElectionWithMastership},
	{Name: "RunElectionResign", Run: runElectionResign},
	{Name: "RunElectionClose", Run: runElectionClose},
	{Name: "RunElectionLoop", Run: runElectionLoop},
}

Tests is the full list of available Election tests. TODO(pavelkalinnikov): Add tests for unexpected mastership loss.

Types

type Decorator

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

Decorator is an election2.Election decorator injecting errors, for testing.

func NewDecorator

func NewDecorator(e election2.Election) *Decorator

NewDecorator returns a Decorator wrapping the passed in Election object.

func (*Decorator) Await

func (d *Decorator) Await(ctx context.Context) error

Await blocks until the instance captures mastership.

func (*Decorator) BlockAwait

func (d *Decorator) BlockAwait(block bool)

BlockAwait enables or disables Await method blocking.

func (*Decorator) Close

func (d *Decorator) Close(ctx context.Context) error

Close permanently stops participating in election.

func (*Decorator) Resign

func (d *Decorator) Resign(ctx context.Context) error

Resign releases mastership for this instance.

func (*Decorator) Update

func (d *Decorator) Update(errs Errs)

Update updates errors returned by interface methods.

func (*Decorator) WithMastership

func (d *Decorator) WithMastership(ctx context.Context) (context.Context, error)

WithMastership returns a mastership context.

type Election

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

Election implements election2.Election interface for testing.

func NewElection

func NewElection() *Election

NewElection returns a new initialized Election for testing.

func (*Election) Await

func (e *Election) Await(ctx context.Context) error

Await sets this instance to be the master. It always succeeds. To imitate errors and/or blocking behavior use the Decorator type.

func (*Election) Close

func (e *Election) Close(ctx context.Context) error

Close resets mastership permanently.

func (*Election) Resign

func (e *Election) Resign(ctx context.Context) error

Resign resets mastership.

func (*Election) WithMastership

func (e *Election) WithMastership(ctx context.Context) (context.Context, error)

WithMastership returns mastership context, which gets canceled if / when this instance is not / stops being the master.

type Errs

type Errs struct {
	Await          error
	WithMastership error
	Resign         error
	Close          error
}

Errs contains errors to be returned by each of Election methods.

type NamedTest

type NamedTest struct {
	Name string
	Run  func(t *testing.T, f election2.Factory)
}

NamedTest is a test function paired with its string name.

Source Files

decorator.go election.go tests.go

Version
v1.7.1 (latest)
Published
Jan 9, 2025
Platform
linux/amd64
Imports
7 packages
Last checked
5 days ago

Tools for package owners.