flow

package
v0.0.0-...-b93a6bd Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2020 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(t cbtest.T, worker Worker)

Run runs the given workflow and waits until it is done. Panics on failure.

func RunE

func RunE(t cbtest.T, worker Worker) bool

RunE runs the given workflow and waits until it is done. Returns boolean indicating success or failure.

func RunWithOutput

func RunWithOutput(t cbtest.T, worker Worker, output io.Writer)

RunWithOutput runs the given workflow and waits until it is done. All output is written to the given io.Writer. Panics on failure.

func RunWithOutputE

func RunWithOutputE(t cbtest.T, worker Worker, output io.Writer) bool

RunWithOutputE runs the given workflow and waits until it is done. All output is written to the given io.Writer. Returns boolean indicating success or failure.

Types

type Builder

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

Builder lets you create flows programatically. It works by constructing a tree of flows that can be executed later using Run.

Builder life cycle

Everything is evaluated eagerly, with the exception of the Run function, which is executed when the flow is run.

func NewBuilder

func NewBuilder() *Builder

NewBuilder returns a new *Builder instance.

func (*Builder) Parallel

func (b *Builder) Parallel(build BuilderFunc) Worker

Parallel builds a flow of parallel workers.

func (*Builder) Run

func (b *Builder) Run(worker Worker) Worker

Run adds the given worker to the current flow.

func (*Builder) Sequence

func (b *Builder) Sequence(build BuilderFunc) Worker

Sequence builds a flow of sequential workers.

func (*Builder) WithContext

func (b *Builder) WithContext(borrower ContextBorrower) *Builder

WithContext sets the context to use for the next builder call.

func (*Builder) WithName

func (b *Builder) WithName(name string) *Builder

WithName sets the name to use for the next builder call.

type BuilderFunc

type BuilderFunc func(*Builder)

BuilderFunc is a function that receives a *Builder instance.

type Context

type Context interface {

	// Deadline returns the time when work on behalf of this context should
	// be cancalled.
	Deadline() (deadline time.Time, ok bool)

	// Done returs a channel that is closed when work on behalf of this context
	// should be cancelled.
	Done() <-chan struct{}

	// Err returns nil if Done is not closed. Otherwise, it returns an error
	// explaining why.
	Err() error

	// Indentifier returns an integer that uniquely identifies this context
	// life-cycle in a group (sequence, parallel, etc).
	Identifier() int

	// Stash saves a value for later use down the pipeline.
	Stash(key interface{}, value interface{})

	// Untash loads a previously stashed value.
	Unstash(key interface{}) interface{}

	// Unwrap returns the underlying context.Context.
	Unwrap() context.Context
}

Context simulates the interfaces exposed by context.Context but with storing (stash) and loading (unstash) of values. Note that users will never be able to create Context instances directly, and will need to go through the borrower instead.

type ContextBorrower

type ContextBorrower interface {

	// Borrow returns a context and a release function for releasing the borrow.
	// If the context was already borrowed, an error will be returned instead.
	Borrow() (Context, func(), error)
}

ContextBorrower defines an interfaces for "borrowing" context(s). This is what we use for ensuring no workers are using the same context at the same time.

type Memoizer

type Memoizer interface {

	// Get returns the same context borrower for multiple invocations with the same key.
	Get(key interface{}) ContextBorrower
}

Memoizer defines an interface for caching multiple Borrower(s). Useful for running workers with shared context(s).

func NewMemoizer

func NewMemoizer() Memoizer

NewMemoizer returns a new Memoizer instance that can be used for obtaining context borrower(s).

Memoization

The first time you call Get, a new borrower will be returned:

a := memo.Get("some-key")

When you call it again with the same key, the *same* borrower will be returned:

b := memo.Get("some-key")
Example
package main

import (
	"fmt"

	"github.com/clearblade/cbtest/contrib/flow"
	"github.com/clearblade/cbtest/mocks"
)

func main() {

	mockT := &mocks.T{}
	mockT.On("Helper")

	// ignore above this line

	memo := flow.NewMemoizer()

	workflow := flow.NewBuilder().Sequence(func(b *flow.Builder) {

		b.WithContext(memo.Get(0)).Run(func(t *flow.T, ctx flow.Context) {
			ctx.Stash("message", "Hello, world!")
		})

		b.WithContext(memo.Get(0)).Run(func(t *flow.T, ctx flow.Context) {
			fmt.Println(ctx.Unstash("message"))
		})
	})

	flow.Run(mockT, workflow)

}
Output:

Hello, world!

type T

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

T serves as flow controller and test handler (akin to testing.T). A new instance is passed to each of the workers in a given flow.

func (*T) Error

func (t *T) Error(args ...interface{})

Error outputs the given args and marks the flow as failed.

func (*T) Errorf

func (t *T) Errorf(format string, args ...interface{})

Errorf outputs the given format, args, and marks the flow as failed.

func (*T) Fail

func (t *T) Fail()

Fail fails the current flow but continues execution.

func (*T) FailNow

func (t *T) FailNow()

FailNow fails the current flow, and halts execution.

func (*T) Failed

func (t *T) Failed() bool

Failed returns whenever the flow has failed.

func (*T) Helper

func (t *T) Helper()

Helper marks the calling function as a helper function, meaning its name will be skipped when printing messages.

func (*T) Log

func (t *T) Log(args ...interface{})

Log outputs the given args.

func (*T) Logf

func (t *T) Logf(format string, args ...interface{})

Logf outputs the given format and args.

func (*T) Name

func (t *T) Name() string

Name returns the name of the current flow.

type Worker

type Worker func(t *T, ctx Context)

Worker is a function that represents a worker with a context.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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