weavertest

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package weavertest provides a way to test Service Weaver components.

Use weavertest.Test 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.Local.Test(t, func(t *testing.T, 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)
	}
    })
}

Multiple Runners are provided by weavertest. The runners differ in 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.Multi.Test(t, func(t *testing.T, reverser Reverser) {
    // ...
  })
}

Index

Constants

View Source
const DefaultReplication = 2

DefaultReplication is 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{Name: "Local"}

	// RPC is a Runner that places all components in the same process
	// and uses RPCs for method invocations. We also add an extra retry to
	// every retriable RPC to discover methods that should have been
	// marked non-retriable.
	RPC = Runner{Name: "RPC", /* 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{Name: "Multi", /* contains filtered or unexported fields */}
)

Functions

This section is empty.

Types

type FakeComponent added in v0.13.0

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

FakeComponent records the implementation to use for a specific component type.

func Fake added in v0.13.0

func Fake[T any](impl any) FakeComponent

Fake arranges to use impl as the implementation for the component type T. The result is typically placed in Runner.Fakes. REQUIRES: impl must implement T.

type Runner added in v0.11.0

type Runner struct {

	// Name is used as the name of the sub-test created by
	// Runner.Test (or the sub-benchmark created by
	// Runner.Bench). The default value is fine unless the user
	// has adjusted some properties of Runner like Config.
	Name string

	// Config is used as the Service Weaver configuration. The
	// string is interpreted as the contents of a Service Weaver
	// config file. It can contain application level as well as
	// component level configuration.
	Config string

	// Fakes holds a list of component implementations that should
	// be used instead of the implementations registered in the binary.
	// The typical use is to override some subset of the application
	// code being tested with test-specific component implementations.
	Fakes []FakeComponent
	// 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) Bench added in v0.13.0

func (r Runner) Bench(b *testing.B, testBody any)

Bench runs a sub-benchmark of b that benchmarks the supplied Service Weaver application code. It fails at runtime if body is not a function whose signature looks like:

func(*testing.B, ComponentType1)
func(*testing.B, ComponentType, ComponentType2)
...

body is called in the context of a brand-new Service Weaver application and is passed the *testing.B for the sub-benchmark, followed by a the list of components.

func BenchmarkFoo(b *testing.B) {
	weavertest.Local.Bench(b, func(b *testing.B, foo Foo, bar *bar) {
		for i := 0; i < b.N; i++ {
			// Benchmark foo and bar ...
		}
	})
}

Component arguments can either be component interface types (e.g., Foo) or component implementation pointer types (e.g., *bar).

In contrast with weaver.Run, the Bench method does not run the Main method of any registered weaver.Main component.

func (Runner) Test added in v0.13.0

func (r Runner) Test(t *testing.T, body any)

Test runs a sub-test of t that tests the supplied Service Weaver application code. It fails at runtime if body is not a function whose signature looks like:

func(*testing.T, ComponentType1)
func(*testing.T, ComponentType, ComponentType2)
...

body is called in the context of a brand-new Service Weaver application and is passed the *testing.T for the sub-test, followed by a the list of components.

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

Component arguments can either be component interface types (e.g., Foo) or component implementation pointer types (e.g., *bar).

In contrast with weaver.Run, the Test method does not run the Main method of any registered weaver.Main component.

Directories

Path Synopsis
internal
chain
Package chain defines a chain of components A -> B -> C with methods to propagate a value from the head of the chain to the tail of the chain.
Package chain defines a chain of components A -> B -> C with methods to propagate a value from the head of the chain to the tail of the chain.
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