Giter VIP home page Giter VIP logo

gofail's Introduction

gofail

Build Status

An implementation of failpoints for golang. Please read design.md for a deeper understanding.

Add a failpoint

Failpoints are special comments that include a failpoint variable declaration and some trigger code,

func someFunc() string {
	// gofail: var SomeFuncString string
	// // this is called when the failpoint is triggered
	// return SomeFuncString
	return "default"
}

Build with failpoints

Building with failpoints will translate gofail comments in place to code that accesses the gofail runtime.

Call gofail in the directory with failpoints to generate gofail runtime bindings, then build as usual,

gofail enable
go build cmd/

The translated code looks something like,

func someFunc() string {
        if vSomeFuncString, __fpErr := __fp_SomeFuncString.Acquire(); __fpErr == nil { SomeFuncString, __fpTypeOK := vSomeFuncString.(string); if !__fpTypeOK { goto __badTypeSomeFuncString}
		// this is called when the failpoint is triggered
		return SomeFuncString; __badTypeSomeFuncString: __fp_SomeFuncString.BadType(vSomeFuncString, "string"); };
        return "default"
}

To disable failpoints and revert to the original code,

gofail disable

Triggering a failpoint

After building with failpoints enabled, the program's failpoints can be activated so they may trigger when evaluated.

Command line

From the command line, trigger the failpoint to set SomeFuncString to hello,

GOFAIL_FAILPOINTS='my/package/path/SomeFuncString=return("hello")' ./cmd

Multiple failpoints are set by using ';' for a delimiter,

GOFAIL_FAILPOINTS='failpoint1=return("hello");failpoint2=sleep(10)' ./cmd

HTTP endpoint

First, enable the HTTP server from the command line,

GOFAIL_HTTP="127.0.0.1:1234" ./cmd

Activate a failpoint with curl,

$ curl http://127.0.0.1:1234/my/package/path/SomeFuncString -XPUT -d'return("hello")'

List the failpoints,

$ curl http://127.0.0.1:1234/
my/package/path/SomeFuncString=return("hello")

Deactivate a failpoint,

$ curl http://127.0.0.1:1234/my/package/path/SomeFuncString -XDELETE

Unit tests

From a unit test,

import (
	"testing"

	gofail "go.etcd.io/gofail/runtime"
)

func TestWhatever(t *testing.T) {
	gofail.Enable("my/package/path/SomeFuncString", `return("hello")`)
	defer gofail.Disable("my/package/path/SomeFuncString")
	...
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.