Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the go timeout flag on "Go test"

Tags:

testing

go

go test -timeout 99999 

throws this non sense error

invalid value "99999" for flag -test.timeout:  time: missing unit in duration 99999 

Is it a bug ? I'm using go version go1.3

The "help" cli is useless too. It says -test.timeout=0: if positive, sets an aggregate time limit for all tests. However if you do go test -test.timeout 99999 you get the same error

 -test.timeout=0: if positive, sets an aggregate time limit for all tests 
like image 788
hey Avatar asked Jul 24 '14 09:07

hey


People also ask

How do I test my go code?

At the command line in the greetings directory, run the go test command to execute the test. The go test command executes test functions (whose names begin with Test ) in test files (whose names end with _test.go). You can add the -v flag to get verbose output that lists all of the tests and their results.

Does go test run go vet?

As part of building a test binary, go test runs go vet on the package and its test source files to identify significant problems. If go vet finds any problems, go test reports those and does not run the test binary.

Does go test run go build?

It will have two functions: a constructor NewPerson and a function older , which will take a *Person and return if one *Person is more senior than another *Person . This way, the go test locates the package, builds it, and runs its tests.


2 Answers

Use a valid time.ParseDuration input. For example,

$ go test -timeout 300ms  $ go test -timeout 99999s 

Command go

Testing flags

-timeout t 

If a test runs longer than t, panic.

Package flag

Duration flags accept any input valid for time.ParseDuration.

Package time

func ParseDuration

func ParseDuration(s string) (Duration, error) 

ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".

like image 113
peterSO Avatar answered Sep 18 '22 13:09

peterSO


If you need this for just one test that you want to easily fail when it times out there is a neat way of just using timeout channels.

If I suspect a test will time out and I still want it to fail is to work with a timeout channel.

So imagine you have code where you suspect that some goroutines will deadlock and you want to make sure your test fails on that.

For that I run the actual test in a goroutine and the main goroutine then sits around waiting for either the done channel to finish or the timeout to finish.

func TestWithTimeOut(t *testing.T) {     timeout := time.After(3 * time.Second)     done := make(chan bool)     go func() {         // do your testing         time.Sleep(5 * time.Second)         done <- true     }()      select {     case <-timeout:         t.Fatal("Test didn't finish in time")     case <-done:     } } 
like image 20
Tigraine Avatar answered Sep 18 '22 13:09

Tigraine