Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better timeout handling #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions goblin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package goblin
import (
"flag"
"fmt"
"os"
"regexp"
"runtime"
"sync"
Expand Down Expand Up @@ -44,7 +45,7 @@ func (g *G) Describe(name string, h func()) {
}

func (g *G) Timeout(time time.Duration) {
g.timeout = time
g.currentTimeout = time
g.timer.Reset(time)
}

Expand Down Expand Up @@ -130,7 +131,6 @@ type It struct {
failure *Failure
failureMu sync.RWMutex
reporter Reporter
isAsync bool
}

func (it *It) run(g *G) bool {
Expand Down Expand Up @@ -171,7 +171,6 @@ type Xit struct {
parent *Describe
failure *Failure
reporter Reporter
isAsync bool
}

func (xit *Xit) run(g *G) bool {
Expand All @@ -193,6 +192,13 @@ func parseFlags() {
} else {
runRegex = nil
}

if envTimeout := os.Getenv("GOBLIN_TIMEOUT"); envTimeout != "" {
var err error
if *timeout, err = time.ParseDuration(envTimeout); err != nil {
panic(err)
}
}
}

var doParseOnce sync.Once
Expand All @@ -202,11 +208,9 @@ var regexParam = flag.String("goblin.run", "", "Runs only tests which match the
var runRegex *regexp.Regexp

func Goblin(t *testing.T, arguments ...string) *G {
doParseOnce.Do(func() {
parseFlags()
})
doParseOnce.Do(parseFlags)

g := &G{t: t, timeout: *timeout}
g := &G{t: t, defaultTimeout: *timeout, currentTimeout: *timeout}
var fancy TextFancier
if *isTty {
fancy = &TerminalFancier{}
Expand All @@ -222,7 +226,7 @@ func runIt(g *G, it *It) {
g.mutex.Lock()
g.timedOut = false
g.mutex.Unlock()
g.timer = time.NewTimer(g.timeout)
g.timer = time.NewTimer(g.currentTimeout)
g.shouldContinue = make(chan bool)
if call, ok := it.h.(func()); ok {
// the test is synchronous
Expand Down Expand Up @@ -262,24 +266,30 @@ func runIt(g *G, it *It) {
//Set to nil as it shouldn't continue
g.shouldContinue = nil
g.timedOut = true
g.Fail("Test exceeded " + fmt.Sprintf("%s", g.timeout))
g.Fail("Test exceeded " + g.currentTimeout.String())
}
// Reset timeout value
g.timeout = *timeout
g.currentTimeout = g.defaultTimeout
}

type G struct {
t *testing.T
parent *Describe
currentIt Itable
timeout time.Duration
currentTimeout time.Duration
defaultTimeout time.Duration
reporter Reporter
timedOut bool
shouldContinue chan bool
mutex sync.Mutex
timer *time.Timer
}

func (g *G) SetDefaultTimeout(timeout time.Duration) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is never called / used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, this is user-facing API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, in that case this needs to be tested. However don't add anything until we decide if it makes sense to add this functionality or not. I still don't quite understand what this is improving / fixing.

g.defaultTimeout = timeout
g.currentTimeout = timeout
}

func (g *G) SetReporter(r Reporter) {
g.reporter = r
}
Expand Down
2 changes: 1 addition & 1 deletion goblin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ func TestItTimeout(t *testing.T) {
})

g.It("Should revert for different it", func() {
g.Assert(g.timeout).Equal(10 * time.Millisecond)
g.Assert(g.currentTimeout).Equal(10 * time.Millisecond)
})

})
Expand Down