fxutil

package module
v0.52.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: Apache-2.0 Imports: 18 Imported by: 9

Documentation

Overview

Package fxutil provides utilities for interacting with fx.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FxLoggingOption

func FxLoggingOption() fx.Option

FxLoggingOption creates an fx.Option to configure the Fx logger, either to do nothing (the default) or to log to the console (when TRACE_FX is set).

func GetAndFilterGroup added in v0.52.0

func GetAndFilterGroup[S ~[]E, E any](group S) S

GetAndFilterGroup filters 'zero' values from an FX group.

A 'zero' value, nil in most cases, can be injected into a group whem a component declares returning a element for that group but don't actually creates the element. This is common pattern with component that can be disabled or partially enabled.

This should be called in every component's constructor that requires an FX group as a dependency.

func OneShot

func OneShot(oneShotFunc interface{}, opts ...fx.Option) error

OneShot runs the given function in an fx.App using the supplied options. The function's arguments are supplied by Fx and can be any provided type. The function must return `error` or nothing.

The resulting app starts all components, then invokes the function, then immediately shuts down. This is typically used for command-line tools like `agent status`.

func Run

func Run(opts ...fx.Option) error

Run runs an fx.App using the supplied options, returning any errors.

This differs from fx.App#Run in that it returns errors instead of exiting the process.

func TemporaryAppTimeouts

func TemporaryAppTimeouts() fx.Option

TemporaryAppTimeouts returns new fx Start/Stop timeout options, defaulting to 5 minutes.

The start timeout can be overridden with the DD_FX_START_TIMEOUT_SECONDS environment variable. The stop timeout can be overridden with the DD_FX_STOP_TIMEOUT_SECONDS environment variable.

Before fx the Agent did not have any start/stop timeouts, it would hang indefinitely. As we have have been adding more fx.Hooks we began hitting flaky tests with expired fx timeouts. We use a large timeout value by default to minimize the chance that customers will be impacted by the timeout. However, note that most platforms service managers send SIGKILL after a timeout

  • upstart default is 5 seconds
  • see pkg/util/winutil/servicemain/servicemain.go:Service.HardStopTimeout

We can revisit this once we can better characterize the agent start/stop behavior and be intentional about timeout values

func Test

func Test[T any](t testing.TB, opts ...fx.Option) T

Test starts an app and returns fulfilled dependencies

The generic return type T must conform to fx.In such that it's dependencies can be fulfilled.

func TestApp added in v0.51.0

func TestApp[T any](opts ...fx.Option) (*fx.App, T, error)

TestApp starts an fx app and returns fulfilled dependencies

The generic return type T must conform to fx.In such that it's dependencies can be fulfilled.

func TestBundle

func TestBundle(t *testing.T, bundle BundleOptions, extraOptions ...fx.Option)

TestBundle is an helper to test Bundle.

This function checks that all components built with fx.Provide inside a bundle can be instanciated. To do so, it creates an `fx.Invoke(_ component1, _ component2, ...)` and call fx.ValidateApp

func TestOneShot

func TestOneShot(t *testing.T, fct func())

TestOneShot is a helper for testing there is no missing dependencies when calling fxutil.OneShot.

The function passed as the first argument of fx.OneShot is not called. It is validated with fx.ValidateApp, however.

func TestOneShotSubcommand

func TestOneShotSubcommand(
	t *testing.T,
	subcommands []*cobra.Command,
	commandline []string,
	expectedOneShotFunc interface{},
	verifyFn interface{})

TestOneShotSubcommand is a helper for testing commands implemented with fxutil.OneShot.

It takes an array of commands, and attaches all to a temporary top-level command, then executes the given command line (beginning with the subcommand name) against that top-level command.

The execution should eventually call fxutil.OneShot with the oneShotFunc given by expectedOneShotFunc. However, this function will not actually be called, as that would lead to the one-shot command actually running. It is validated with fx.ValidateApp, however.

The `fx.Option`s passed to fxutil.OneShot are used to create a new app containing only the final argument to this function. Be careful not to require any components, since nothing is mocked here. Typically, the function only requires static values such as `BundleParams` or `cliParams` and asserts they contain appropriate values.

func TestRun added in v0.51.0

func TestRun(t *testing.T, f func() error)

TestRun is a helper for testing code that uses fxutil.Run

It takes a anonymous function, and sets up fx so that no actual App will be constructed. Instead, it expects the given function to call fxutil.Run. Then, this test verifies that all Options given to that fxutil.Run call will satisfy fx's dependences by using fx.ValidateApp.

func TestStart

func TestStart(t testing.TB, opts fx.Option, appAssert appAssertFn, fn interface{})

TestStart runs an app fx.App.

This function does *not* leverage fxtest.App because we want to be able to test for App initialization errors and expected failures.

The given function is called after the app's startup has completed, with its arguments filled via Fx's dependency injection. The provided testing.TB argument will be used for the appAssertFn hook, but the test will not automatically fail if the application fails to start.

The supplied `fn` function will never be called, but is required to setup that arg appropriately

Use `fx.Options(..)` to bundle multiple fx.Option values into one.

func UnwrapIfErrArgumentsFailed

func UnwrapIfErrArgumentsFailed(err error) error

UnwrapIfErrArgumentsFailed unwrap the error if the error was returned by an FX invoke method otherwise return the error.

Types

type BundleOptions

type BundleOptions struct {
	fx.Option
	Options []fx.Option
}

BundleOptions is a fx.Module for Bundle with an exported field "Options" to list options

func Bundle

func Bundle(opts ...fx.Option) BundleOptions

Bundle is a simple wrapper around fx.Module that automatically determines the bundle name.

type Module

type Module struct {
	fx.Option
	Options []fx.Option
}

Module is a fx.Module for Component with an exported field "Options" to list options

func Component

func Component(opts ...fx.Option) Module

Component is a simple wrapper around fx.Module that automatically determines the component name.

type NoDependencies

type NoDependencies struct {
	fx.In
}

NoDependencies defines a component which doesn't have any dependencies

Jump to

Keyboard shortcuts

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