weavertest

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package weavertest provides a way to test Service Weaver components.

Use weavertest.Init as a drop-in replacement for weaver.Init in tests. 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) {
    ctx := context.Background()
    root := weavertest.Init(ctx, t, weavertest.Options{})
    reverser, err := weaver.Get[Reverser](root)
    if err != nil {
        t.Fatal(err)
    }
    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.Init receives a weavertest.Options, which you can use to configure the execution of the test. By default, weavertest.Init 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.Init will instead run every component in a single process, similar to what happens when you "go run" a Service Weaver application.

func TestReverse(t *testing.T) {
    ctx := context.Background()
    root := weavertest.Init(ctx, t, weavertest.Options{SingleProcess: true})
    reverser, err := weaver.Get[Reverser](root)
    // ...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(ctx context.Context, t testing.TB, opts Options) weaver.Instance

Init is a testing version of weaver.Init. Calling Init will create a brand new execution environment and return the main component. For example:

func TestFoo(t *testing.T) {
    root := weavertest.Init(context.Background(), t, weavertest.Options{})
    foo, err := weaver.Get[Foo](root)
    if err != nil {
        t.Fatal(err)
    }
    // Test the Foo component...
}

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
diverge
Package main implements a Service Weaver application that demonstrates where singleprocess and multiprocess execution diverge.
Package main implements a Service Weaver application that demonstrates where singleprocess and multiprocess execution diverge.
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