got

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2020 License: MIT Imports: 6 Imported by: 8

README

Overview

This lib provides useful functions to handle runtime goroutine stack. Such as wait for goroutines to exit.

Check the example to use it with go testing.

Check API doc for usage.

Example

import (
	"testing"
	"time"

	"github.com/ysmood/got/pkg/testleak"
	"github.com/ysmood/gotest"
)

func TestSuite(t *testing.T) {
	gotest.Each(t, S{}, func(t *testing.T) {
		testleak.Check(t, 0) // check each test
	})
}

type S struct{}

func (S) Test01() {
	go func() {
		time.Sleep(1000)
	}()
}

func (S) Test02() {
	go func() {
		time.Sleep(1000)
	}()
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Timeout

func Timeout(d time.Duration) context.Context

Timeout shortcut for context.WithTimeout(context.Background(), d)

Types

type Ignore

type Ignore func(Trace) bool

Ignore filter

func CombineIgnores

func CombineIgnores(list ...Ignore) Ignore

CombineIgnores into one

Example
package main

import (
	"context"
	"fmt"
	"strings"
	"time"

	"github.com/ysmood/got"
)

func main() {
	ignore := got.CombineIgnores(
		got.IgnoreCurrent(),
		func(t got.Trace) bool {
			return strings.Contains(t.Raw, "ExampleCombineIgnores.func2")
		},
	)

	go func() {
		time.Sleep(2 * time.Second)
	}()

	go func() {
		time.Sleep(time.Second)
	}()

	start := time.Now()
	got.Wait(context.TODO(), ignore)
	end := time.Since(start)

	if time.Second < end && end < 2*time.Second {
		fmt.Println("only waits for the second goroutine")
	}

}
Output:
only waits for the second goroutine

func IgnoreCurrent

func IgnoreCurrent() Ignore

IgnoreCurrent Trace list

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/ysmood/got"
)

func main() {
	ignore := got.IgnoreCurrent()

	go func() {
		time.Sleep(time.Second)
	}()

	start := time.Now()
	got.Wait(context.TODO(), ignore)
	end := time.Since(start)

	if end > time.Second {
		fmt.Println("waited for 1 second")
	}

}
Output:
waited for 1 second

func IgnoreFuncs

func IgnoreFuncs(names ...string) Ignore

IgnoreFuncs ignores a Trace if it's first Stack's Func equals one of the names.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/ysmood/got"
)

func main() {
	ignore := got.IgnoreFuncs("internal/poll.runtime_pollWait")

	go func() {
		time.Sleep(time.Second)
	}()

	start := time.Now()
	got.Wait(context.TODO(), ignore)
	end := time.Since(start)

	if end > time.Second {
		fmt.Println("waited for 1 second")
	}

}
Output:
waited for 1 second

func IgnoreList

func IgnoreList(list Traces) Ignore

IgnoreList of Trace

type Stack

type Stack struct {
	Func string
	Loc  string
}

Stack info

type Trace

Trace of one goroutine

type Traces

type Traces []Trace

Traces of goroutines

func Get

func Get(all bool) Traces

Get the Trace of the calling goroutine. If all is true, all other goroutines' Traces will be appended into the result too.

Example
package main

import (
	"fmt"

	"github.com/ysmood/got"
)

func main() {
	list := got.Get(true)

	fmt.Println("goroutine count:", len(list))
	fmt.Println("id of current:", list[0].GoroutineID)
	fmt.Println("caller of current:", list[0].Stacks[2].Func)

}
Output:

goroutine count: 2
id of current: 1
caller of current: github.com/ysmood/got_test.ExampleGet()

func Wait

func Wait(ctx context.Context, ignores ...Ignore) (remain Traces)

Wait for other goroutines that are not ignored to exit. It returns the ones that are still active. It keeps counting the active goroutines that are not ignored, if the number is zero return.

func (Traces) Any

func (list Traces) Any() bool

Any item exists in the list

func (Traces) Format

func (list Traces) Format() string

Format list into a human readable string

Example
package main

import (
	"fmt"
	"strings"
	"time"

	"github.com/ysmood/got"
)

func main() {
	go func() {
		time.Sleep(time.Second)
	}()

	str := got.Wait(got.Timeout(0)).Format()

	fmt.Println(strings.Contains(str, "got_test.ExampleTraces_Format"))

}
Output:
true

Directories

Path Synopsis
lib
benchmark module
example module
pkg

Jump to

Keyboard shortcuts

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