tempo

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2025 License: MIT Imports: 14 Imported by: 0

README

Go Report Card codecov

Tempo

Tempo is a utility that runs Go-like tests in a distributed manner using Temporal.

Usage

Add the dependency to your go.mod file:

go get github.com/cito-oss/tempo

[!TIP] For more examples, check the _example directory.

Then create your first test app:

package main

import (
	"context"

	"github.com/cito-oss/tempo"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.temporal.io/sdk/client"
	"go.temporal.io/sdk/worker"
)

func Greeter(ctx context.Context, name string) (string, error) {
	if name == "" {
		name = "World"
	}

	return "Hello " + name, nil
}

func SayHello(t *tempo.T) {
	var greetings string

	err := t.Task(Greeter, "John Doe", &greetings)
	require.NoError(t, err)

	assert.Equal(t, "Hello John Doe", greetings)
}

func main() {
	cli, err := client.Dial(client.Options{})
	if err != nil {
		panic(err)
	}
	defer cli.Close()

	queue := "default"

	// WORKER
	myworker := worker.New(cli, queue, worker.Options{})

	tempo.Worker(myworker, tempo.Registry{
		Tests: []tempo.Test{
			tempo.NewTest(SayHello),
		},
		Tasks: []tempo.Task{
			Greeter,
		},
	})

	err = myworker.Start()
	if err != nil {
		panic(err)
	}
	defer myworker.Stop()
	// /WORKER

	// RUNNER
	myrunner := tempo.NewRunner(cli, queue,
		tempo.NewPlan(SayHello, "John Doe"),
	)

	err = myrunner.Run("v1.0.0")
	if err != nil {
		panic(err)
	}
	// /RUNNER
}

Now run it and check your Temporal:

Temporal Screenshot

Documentation

Index

Constants

View Source
const TestFailedErrorType = "TestFailedError"

Variables

View Source
var (
	ErrWorkflowExecute = errors.New("execute workflow")
	ErrWorkflowAwait   = errors.New("await workflow")
	ErrWorkflow        = errors.New("workflow")
	ErrFuture          = errors.New("future")
	ErrTest            = errors.New("test")
)
View Source
var DefaultActivityOptions = workflow.ActivityOptions{
	StartToCloseTimeout: time.Minute,
	RetryPolicy: &temporal.RetryPolicy{
		MaximumAttempts: 1,
	},
}

Functions

func NewTestFailedError

func NewTestFailedError(msgs []string) error

func Run

func Run(cli client.Client, queue string, id string, plan Plan, output any) error

func Worker

func Worker(w worker.Worker, reg Registry)

Types

type Plan

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

func NewPlan

func NewPlan(fn any, input any) Plan

func (Plan) Input

func (p Plan) Input() any

func (Plan) Name

func (p Plan) Name() string

type Registry

type Registry struct {
	Tests []Test
	Tasks []Task
}

type Runner

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

func NewRunner

func NewRunner(client client.Client, queue string, plans ...Plan) *Runner

func (*Runner) Run

func (r *Runner) Run(prefix string) error

func (*Runner) SetLimit

func (r *Runner) SetLimit(limit int)

type T

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

func (*T) Errorf

func (t *T) Errorf(format string, args ...any)

func (*T) FailNow

func (t *T) FailNow()

func (*T) Go

func (t *T) Go(fn func(t *T))

func (*T) Run

func (t *T) Run(name string, fn func(*T))

func (*T) RunAsChild

func (t *T) RunAsChild(fn any, input any, output any)

func (*T) SetActivityOptions

func (t *T) SetActivityOptions(options workflow.ActivityOptions)

func (*T) Task

func (t *T) Task(task any, input any, output any) error

func (*T) WaitGroup

func (t *T) WaitGroup() *WaitGroup

func (*T) Warnf

func (t *T) Warnf(format string, args ...any)

type Task

type Task any

type Test

type Test interface {
	Name() string
	Function() any
}

func NewTest

func NewTest(fn func(*T)) Test

NewTest wraps fn in a workflowWrapper, that can be passed to temporal Worker:

mytest := tempo.NewTest(func(*T))

w.RegisterWorkflowWithOptions(mytest.Function(), workflow.RegisterOptions{
	Name: mytest.Name(),
})

func NewTestWithInput

func NewTestWithInput[INPUT any](fn func(*T, INPUT)) Test

NewTestWithInput wraps fn in a workflowWrapper, that can be passed to temporal Worker:

mytest := tempo.NewTestWithInput(func(*T, string))

w.RegisterWorkflowWithOptions(mytest.Function(), workflow.RegisterOptions{
	Name: mytest.Name(),
})

func NewTestWithInputAndOutput

func NewTestWithInputAndOutput[INPUT any, OUTPUT any](fn func(*T, INPUT) OUTPUT) Test

NewTestWithInputAndOutput wraps fn in a workflowWrapper, that can be passed to temporal Worker:

mytest := tempo.NewTestWithInputAndOutput(func(*T, string) string)

w.RegisterWorkflowWithOptions(mytest.Function(), workflow.RegisterOptions{
	Name: mytest.Name(),
})

func NewTestWithOutput

func NewTestWithOutput[OUTPUT any](fn func(*T) OUTPUT) Test

NewTestWithOutput wraps fn in a workflowWrapper, that can be passed to temporal Worker:

mytest := tempo.NewTestWithOutput(func(*T) string)

w.RegisterWorkflowWithOptions(mytest.Function(), workflow.RegisterOptions{
	Name: mytest.Name(),
})

type TestFailedError

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

func (*TestFailedError) Error

func (e *TestFailedError) Error() string

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
	FailNow()
}

type WaitGroup

type WaitGroup struct {
	workflow.WaitGroup
	// contains filtered or unexported fields
}

func (*WaitGroup) Wait

func (wg *WaitGroup) Wait()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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