weavertest

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2023 License: Apache-2.0 Imports: 24 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.

The mode argument to weavertest.Run controls whether or not components are placed in different processes and whether or not RPCs are used for components in the same process.

  1. Local: all components are placed in a single process and method invocations are local procedure calls. This is similar to what happens when you run the Service Weaver binary directly or run the application using "go run".

  2. Multi: Every component will be placed in a separate process, similar to what happens when you "weaver multi deploy" a Service Weaver application.

  3. RPC: Every component will be placed in a same process, but method calls will use RPCs. This mode is most useful when profiling or collecting coverage information.

Example:

func TestReverseSingle(t *testing.T) {
  weavertest.Run(t, weavertest.Multi, weavetest.Options{}, 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

View Source
var (
	// Local is a Runner that places all components in the same process
	// and uses local procedure calls for method invocations.
	Local = Runner{/* contains filtered or unexported fields */}

	// RPC is a Runner that places all components in the same process
	// and uses RPCs for method invocations.
	RPC = Runner{/* contains filtered or unexported fields */}

	// Multi is a Runner that places all components in different
	// process (unless explicitly colocated) and uses RPCs for method
	// invocations on remote components and local procedure calls for
	// method invocations on colocated components.
	Multi = Runner{/* contains filtered or unexported fields */}
)

Functions

This section is empty.

Types

type Runner added in v0.11.0

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

Runner runs user-supplied testing code as a weaver application.

func AllRunners added in v0.11.0

func AllRunners() []Runner

AllRunners returns a slice of all builtin weavertest runners.

func (Runner) Name added in v0.11.0

func (r Runner) Name() string

Name returns the runner name. It is suitable for use as a sub-test or sub-benchmark name.

func (Runner) Run added in v0.11.0

func (r Runner) Run(t testing.TB, 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.Local.Run(t, 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)
...

func (Runner) WithConfig added in v0.11.0

func (r Runner) WithConfig(config string) Runner

WithConfig returns a new Runner with the specified Service Weaver configuration. The config value passed here is identical to what might be found in a Service Weaver config file. It can contain application level as well as component level configuration.

func (Runner) WithName added in v0.11.0

func (r Runner) WithName(name string) Runner

WithName returns a new Runner with the specified name. It is useful when the runner has been adjusted and is no longer identical to one of the predefined runners.

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