gomonkey Documentation

repository·master·Indexed 25 days ago

https://github.com/agiledragon/gomonkey

A Go library for monkey patching during unit testing, allowing developers to replace the implementation of functions, methods, and variables at runtime. It supports patching public and private member methods, interfaces, and global variables across amd64, arm64, 386, loong64, and riscv64 architectures on Linux, MAC OS X, and Windows. Note that gomonkey is not threadsafe and requires disabling compiler inlining via -gcflags to function correctly.

Tokens
926
Snippets
2
Records
14
Agent score
31%

What's inside gomonkey

  1. Patching capabilities of gomonkey

    master

    gomonkey provides extensive support for monkey patching in unit tests, including:

    • Targets: Functions, public member methods, private member methods, interfaces, function variables, and global variables.
    • Sequencing: You can specify a sequence of different patches for functions, member methods, interfaces, and function variables (allowing the patched function to return different values on consecutive calls).
  2. Disable inlining for successful patching

    master

    gomonkey cannot patch functions or member methods if the Go compiler has inlined them. To ensure patches work correctly, you must run your tests with inlining disabled using the -gcflags flag.

    • For Go 1.10 and above: use -gcflags=all=-l.
    • For Go versions below 1.10: use -gcflags=-l.
    go test . ./test -gcflags=all=-l
  3. Use Patches to manage monkey patching lifecycles

    master

    The Patches type is the central object used to track and revert monkey patches. You can either use the package-level functions (which use a hidden global Patches instance) or create your own instance using NewPatches().

    To ensure patches are automatically reverted after a test, use the Origin method. Origin will:

    1. Restore the original binary code for all patched targets.
    2. Execute the provided function fn().
    3. Re-apply the patches after fn() completes.

    Alternatively, you can call Reset() manually to restore all original values and code.

  4. Important limitations and thread safety

    master

    When using gomonkey, be aware of the following constraints:

    1. Inlining: As mentioned, you must disable inlining via -gcflags or patches will fail.
    2. Thread Safety: gomonkey is not threadsafe. A panic may occur if one goroutine is patching a function or member method while another goroutine is simultaneously visiting/executing that same function or method.
  5. Patch a function with a sequence of outputs using ApplyFuncSeq

    master

    Use ApplyFuncSeq to make a function return different values on successive calls. You provide a slice of OutputCell objects.

    An OutputCell contains:

    • Values: A Params ([]interface{}) representing the return values for that call.
    • Times: How many times to return these values. Use -1 for infinite times.
  6. Patch a function with ApplyFunc

    master
    Use ApplyFunc to replace a target function with a replacement (double) function. The double function must have a compatible signature (same number of arguments and return values) with the target function.