diff --git a/goblin.go b/goblin.go index e029548..d237d81 100644 --- a/goblin.go +++ b/goblin.go @@ -37,6 +37,10 @@ func (g *G) Describe(name string, h func()) { g.reporter.end() } } +func (g *G) Timeout(time time.Duration) { + g.timeout = time + g.timer.Reset(time) +} type Describe struct { name string @@ -180,6 +184,7 @@ func runIt(g *G, h interface{}) { g.mutex.Lock() g.timedOut = false g.mutex.Unlock() + g.timer = time.NewTimer(g.timeout) g.shouldContinue = make(chan bool) if call, ok := h.(func()); ok { // the test is synchronous @@ -204,12 +209,14 @@ func runIt(g *G, h interface{}) { } select { case <-g.shouldContinue: - case <-time.After(g.timeout): + case <-g.timer.C: //Set to nil as it shouldn't continue g.shouldContinue = nil g.timedOut = true g.Fail("Test exceeded " + fmt.Sprintf("%s", g.timeout)) } + // Reset timeout value + g.timeout = *timeout } type G struct { @@ -221,6 +228,7 @@ type G struct { timedOut bool shouldContinue chan bool mutex sync.Mutex + timer *time.Timer } func (g *G) SetReporter(r Reporter) { @@ -278,7 +286,7 @@ func timeTrack(start time.Time, g *G) { func (g *G) Fail(error interface{}) { //Skips 7 stacks due to the functions between the stack and the test - stack := ResolveStack(4) + stack := ResolveStack(7) message := fmt.Sprintf("%v", error) g.currentIt.failed(message, stack) if g.shouldContinue != nil { diff --git a/goblin_test.go b/goblin_test.go index 04fe4f8..ca3f3f8 100644 --- a/goblin_test.go +++ b/goblin_test.go @@ -294,3 +294,25 @@ func TestTimeout(t *testing.T) { t.Fatal("Failed") } } + +func TestItTimeout(t *testing.T) { + fakeTest := testing.T{} + os.Args = append(os.Args, "-goblin.timeout=10ms") + parseFlags() + g := Goblin(&fakeTest) + + g.Describe("Test", func() { + g.It("Should override default timeout", func() { + g.Timeout(20 * time.Millisecond) + time.Sleep(15 * time.Millisecond) + }) + + g.It("Should revert for different it", func() { + g.Assert(g.timeout).Equal(10 * time.Millisecond) + }) + + }) + if fakeTest.Failed() { + t.Fatal("Failed") + } +} diff --git a/resolver_test.go b/resolver_test.go index 76f6ec1..4ba3c55 100644 --- a/resolver_test.go +++ b/resolver_test.go @@ -1,8 +1,6 @@ package goblin -import ( - "testing" -) +import "testing" func TestResolver(t *testing.T) { g := Goblin(t) @@ -15,6 +13,6 @@ func TestResolver(t *testing.T) { } func dummyFunc(g *G) { - stack := ResolveStack(1) - g.Assert(len(stack)).Equal(3) + stack := ResolveStack(3) + g.Assert(len(stack)).Equal(5) }