weavertest

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2023 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package weavertest provides a way to test Service Weaver components.

Use weavertest.Run to exercise a set of components. For example, imagine we have a Reverser component with a Reverse method that reverses strings. To test the Reverser component, we can create a reverser_test.go file with the following contents.

func TestReverse(t *testing.T) {
    weavertest.Run(t, weavetest.Options{}, func(reverser Reverser) {
	got, err := reverser.Reverse(ctx, "diaper drawer")
	if err != nil {
	    t.Fatal(err)
	}
	if want := "reward repaid"; got != want {
	    t.Fatalf("got %q, want %q", got, want)
	}
    })
}

weavertest.Run is passed a weavertest.Options, which you can use to configure the execution of the test. By default, weavertest.Run will run every component in a different process. This is similar to what happens when you run weaver multi deploy. If you set the SingleProcess option, weavertest.Run will instead run every component in a single process, similar to what happens when you "go run" a Service Weaver application.

func TestReverseSingle(t *testing.T) {
    weavertest.Run(t, weavetest.Options{SingleProcess: true}, func(reverser Reverser) {
	// ...
    })
}

Index

Constants

View Source
const DefaultReplication = 2

The default number of times a component is replicated.

TODO(mwhittaker): Include this in the Options struct?

Variables

This section is empty.

Functions

func Run added in v0.6.0

func Run(t testing.TB, opts Options, testBody any)

Run is a testing version of weaver.Run. Run is passed a function that accepts a list of components. Run will create a brand new weaver application execution environment, create the components whose types are arguments to testBody, and callTestBody with these components.

func TestFoo(t *testing.T) {
    weavertest.Run(t, weavertest.Options{}, func(foo Foo, bar Bar) {
	// Test foo and bar ...
    })
}

The test fails at runtime if testBody is not a function whose signature looks like:

func(ComponentType1)
func(ComponentType, ComponentType2)
...

Types

type Options

type Options struct {
	// If true, every component is colocated in a single process. Otherwise,
	// every component is run in a separate OS process.
	SingleProcess bool

	// Config contains configuration identical to what might be found in a
	// Service Weaver config file. It can contain application level as well as component
	// level configuration. Config is allowed to be empty.
	Config string
}

Options configure weavertest.Init.

Directories

Path Synopsis
internal
generate
Package generate tests that "weaver generate"-generated code *executes* correctly.
Package generate tests that "weaver generate"-generated code *executes* correctly.
simple
Package simple is used for a trivial test of code that uses Service Weaver.
Package simple is used for a trivial test of code that uses Service Weaver.

Jump to

Keyboard shortcuts

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