fate

package module
v0.0.0-...-0e719e9 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2022 License: MIT Imports: 7 Imported by: 16

README

Fate

Build Go Report Card Go Doc

Fate is a simple library that allows "designing for failure" by introducing errors inside application logic by "tempting fate".

See GoDoc for details.

Documentation

Overview

Package fate allows implementing "designing for failure" by introducing errors inside application logic by "tempting fate".

type Fate interface {
   // Tempt sometimes returns ErrTempt.
   Tempt() error
}

Fate supports deterministic behavior for tests and probabilistic behavior for production.

The canonical usage pattern:

function multiStepLogic(state state.State, fate fate.Fate, arg int) error {
  r1, err := step1(state.Step1DB(), arg)
  if err != nil {
    return err
  }

  if err := fate.Tempt(); err != nil {
    return err
  }

  err := step2(state.Step2DB(), r1)
  if err != nil {
    return err
  }

  return fate.Tempt()
}

Increasing the tangibility and probability of errors by regularly tempting fate both in production and tests, re-enforces the need for idempotent robust failure proof code.

Index

Constants

This section is empty.

Variables

View Source
var ErrTempt = errors.New("tempt fate error", j.C("ERR_9f3adc780288ce11"))

ErrTempt is the error returned when tempting fate and losing.

Functions

func SetConfig

func SetConfig(opts ...option) func()

SetConfig sets the global config defined by the functional options. It returns an undo function mostly used for testing. Note configuring and using Fate instances is advised over global config.

func Tempt

func Tempt() error

Tempt sometimes returns ErrTempt. Note this uses the global config, configuring and using Fate instances is advised.

func WithDefaultP

func WithDefaultP(p float64) option

WithDefaultP allows defining the default error probability when tempting fate.

func WithOfficeHours

func WithOfficeHours(loc *time.Location, hourStart, hourEnd int) option

WithOfficeHours allows limiting errors to office hours.

func WithPackageP

func WithPackageP(p map[string]float64) option

WithPackageP allows defining the per package error probability when tempting fate.

func WithoutOfficeHours

func WithoutOfficeHours() option

WithoutOfficeHours allows disabling limiting of errors to office hours.

Types

type Fate

type Fate interface {
	Tempt() error
}

Fate is an interface that wraps the tempt method.

The main use case of the interface is to support deterministic behaviour for tests. See mockfate package.

func New

func New(opts ...option) Fate

New returns a new fate instance configured with the options provided.

Directories

Path Synopsis
Package mockfate provides a mock implementation for the fate.Fate interface useful for testing.
Package mockfate provides a mock implementation for the fate.Fate interface useful for testing.

Jump to

Keyboard shortcuts

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