Documentation
¶
Overview ¶
Package test implements a testing framwork, and provides default tests.
Deprecated: This package is deprecated in favor of go-micro.dev/v5/testing. Use the testing.Harness for a cleaner, more maintainable approach. See test/DEPRECATED.md for migration guide.
Package test provides utilities for testing micro services.
Due to go-micro's global defaults, running multiple services in one process requires careful isolation. This package provides helpers for the common case of testing a single service.
Basic usage:
func TestUserService(t *testing.T) {
h := test.NewHarness(t)
defer h.Stop()
// Register your service handler
h.Register(new(UsersHandler))
// Start the harness
h.Start()
// Call the service
var rsp UserResponse
err := h.Call("Users.Create", &CreateRequest{Name: "Alice"}, &rsp)
if err != nil {
t.Fatal(err)
}
}
Index ¶
- Variables
- func RunBenchmark(b *testing.B, name string, service micro.Service, test testFunc, ...)
- type Harness
- func (h *Harness) AssertCallFails(endpoint string, req, rsp interface{})
- func (h *Harness) AssertCallSucceeds(endpoint string, req, rsp interface{})
- func (h *Harness) AssertServiceRunning()
- func (h *Harness) Call(endpoint string, req, rsp interface{}) error
- func (h *Harness) CallContext(ctx context.Context, endpoint string, req, rsp interface{}) error
- func (h *Harness) Client() client.Client
- func (h *Harness) Name(name string) *Harness
- func (h *Harness) Register(handler interface{}) *Harness
- func (h *Harness) Registry() registry.Registry
- func (h *Harness) Server() server.Server
- func (h *Harness) Start()
- func (h *Harness) Stop()
- type ServiceTestConfig
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoTests returns no test params are set. ErrNoTests = errors.New("No tests to run, all values set to 0") )
Functions ¶
func RunBenchmark ¶
func RunBenchmark(b *testing.B, name string, service micro.Service, test testFunc, cancel context.CancelFunc, started chan struct{})
RunBenchmark will run benchmarks on a provided service.
A test function can be provided that will be fun b.N times.
Types ¶
type Harness ¶ added in v5.15.0
type Harness struct {
// contains filtered or unexported fields
}
Harness provides an in-process test environment for a micro service
func NewHarness ¶ added in v5.15.0
NewHarness creates a new test harness
func (*Harness) AssertCallFails ¶ added in v5.15.0
AssertCallFails checks that a call fails
func (*Harness) AssertCallSucceeds ¶ added in v5.15.0
AssertCallSucceeds checks that a call succeeds
func (*Harness) AssertServiceRunning ¶ added in v5.15.0
func (h *Harness) AssertServiceRunning()
AssertServiceRunning checks that the service is registered
func (*Harness) CallContext ¶ added in v5.15.0
CallContext invokes a service method with context
type ServiceTestConfig ¶
type ServiceTestConfig struct {
// Service name to use for the tests
Name string
// NewService function will be called to setup the new service.
// It takes in a list of options, which by default will Context and an
// AfterStart with channel to signal when the service has been started.
NewService func(name string, opts ...micro.Option) (micro.Service, error)
// Parallel is the number of prallell routines to use for the tests.
Parallel []int
// Sequential is the number of sequential requests to send per parallel process.
Sequential []int
// Streams is the nummber of streaming messages to send over the stream per routine.
Streams []int
// PubSub is the number of times to publish messages to the broker per routine.
PubSub []int
// contains filtered or unexported fields
}
ServiceTestConfig allows you to easily test a service configuration by running predefined tests against your custom service. You only need to provide a function to create the service, and how many of which test you want to run.
The default tests provided, all running with separate parallel routines are:
- Sequential Call requests
- Bi-directional streaming
- Pub/Sub events brokering
You can provide an array of parallel routines to run for the request and stream tests. They will be run as matrix tests, so with each possible combination. Thus, in total (p * seq) + (p * streams) tests will be run.
func (*ServiceTestConfig) Handle ¶
func (stc *ServiceTestConfig) Handle(ctx context.Context, msg *pb.HealthRequest) error
Handle is used as a test handler.
func (*ServiceTestConfig) HandleError ¶
func (stc *ServiceTestConfig) HandleError(ctx context.Context, msg *pb.HealthRequest) error
HandleError is used as a test handler.
func (*ServiceTestConfig) Run ¶
func (stc *ServiceTestConfig) Run(b *testing.B)
Run will start the benchmark tests.