Documentation ¶
Index ¶
- func Run(t cbtest.T, worker Worker)
- func RunE(t cbtest.T, worker Worker) bool
- func RunWithOutput(t cbtest.T, worker Worker, output io.Writer)
- func RunWithOutputE(t cbtest.T, worker Worker, output io.Writer) bool
- type Builder
- type BuilderFunc
- type Context
- type ContextBorrower
- type Memoizer
- type T
- type Worker
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RunE ¶
RunE runs the given workflow and waits until it is done. Returns boolean indicating success or failure.
func RunWithOutput ¶
RunWithOutput runs the given workflow and waits until it is done. All output is written to the given io.Writer. Panics on 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 (*Builder) Parallel ¶
func (b *Builder) Parallel(build BuilderFunc) Worker
Parallel builds a flow of parallel workers.
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.
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) Helper ¶
func (t *T) Helper()
Helper marks the calling function as a helper function, meaning its name will be skipped when printing messages.