riverinternaltest

package
v0.0.0-...-862f905 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: MPL-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package riverinternaltest contains shared testing utilities for tests throughout the rest of the project.

Index

Constants

View Source
const SchedulerShortInterval = 500 * time.Millisecond

SchedulerShortInterval is an artificially short interval for the scheduler that's used in the tests of various components to make sure that errored jobs always end up in a `retryable` state rather than `available`. Normally, the job executor sets `available` if the retry delay is smaller than the scheduler's interval. To simplify things so errors are always `retryable`, this time is picked to be smaller than the any retry delay that the default retry policy will ever produce. It's shared so we can document/explain it all in one place.

Variables

This section is empty.

Functions

func BaseServiceArchetype

func BaseServiceArchetype(tb testing.TB) *baseservice.Archetype

BaseServiceArchetype returns a new base service suitable for use in tests. Returns a new instance so that it's not possible to accidentally taint a shared object.

func DatabaseConfig

func DatabaseConfig(databaseName string) *pgxpool.Config

func DatabaseURL

func DatabaseURL(databaseName string) string

DatabaseURL gets a test database URL from TEST_DATABASE_URL or falls back on a default pointing to `river_testdb`. If databaseName is set, it replaces the database in the URL, although the host and other parameters are preserved.

Most of the time DatabaseConfig should be used instead of this function, but it may be useful in non-pgx situations like for examples showing the use of `database/sql`.

func DiscardContinuously

func DiscardContinuously[T any](drainChan <-chan T) func()

DiscardContinuously drains continuously out of the given channel and discards anything that comes out of it. Returns a stop function that should be invoked to stop draining. Stop must be invoked before tests finish to stop an internal goroutine.

func DrainContinuously

func DrainContinuously[T any](drainChan <-chan T) func() []T

DrainContinuously drains continuously out of the given channel and accumulates items that are received from it. Returns a get function that can be called to retrieve the current set of received items, and which will also cause the function to shut down and stop draining. This function must be invoked before tests finish to stop an internal goroutine. It's safe to call it multiple times.

func Logger

func Logger(tb testing.TB) *wlog.WLogger

Logger returns a logger suitable for use in tests.

func LoggerWarn

func LoggerWarn(tb testing.TB) *wlog.WLogger

Logger returns a logger suitable for use in tests which outputs only at warn or above. Useful in tests where particularly noisy log output is expected.

func StubTime

func StubTime(archetype *baseservice.Archetype, t time.Time) time.Time

StubTime is a shortcut for stubbing time for a the given archetype at the given time. It returns the time given as argument for convenience.

func TestDB

func TestDB(ctx context.Context, tb testing.TB) *pgxpool.Pool

TestDB acquires a dedicated test database for the duration of the test. If an error occurs, the test fails. The test database will be automatically returned to the pool at the end of the test and the pgxpool will be closed.

func TestTx

func TestTx(ctx context.Context, tb testing.TB) pgx.Tx

TestTx starts a test transaction that's rolled back automatically as the test case is cleaning itself up. This can be used as a lighter weight alternative to `testdb.Manager` in components where it's not necessary to have many connections open simultaneously.

func TruncateRiverTables

func TruncateRiverTables(ctx context.Context, pool *pgxpool.Pool) error

TruncateRiverTables truncates River tables in the target database. This is for test cleanup and should obviously only be used in tests.

func WaitOrTimeout

func WaitOrTimeout[T any](t *testing.T, waitChan <-chan T) T

WaitOrTimeout tries to wait on the given channel for a value to come through, and returns it if one does, but times out after a reasonable amount of time. Useful to guarantee that test cases don't hang forever, even in the event of something wrong.

func WaitOrTimeoutN

func WaitOrTimeoutN[T any](t *testing.T, waitChan <-chan T, numValues int) []T

WaitOrTimeoutN tries to wait on the given channel for N values to come through, and returns it if they do, but times out after a reasonable amount of time. Useful to guarantee that test cases don't hang forever, even in the event of something wrong.

func WrapTestMain

func WrapTestMain(m *testing.M)

WrapTestMain performs some common setup and teardown that should be shared amongst all packages. e.g. Configures a manager for test databases on setup, and checks for no goroutine leaks on teardown.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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