From 3837382b80a579ac7de469fe680bfda9a00c3c95 Mon Sep 17 00:00:00 2001 From: cyprien Date: Sat, 10 Oct 2020 21:19:43 +0200 Subject: [PATCH] Added Timeout() for Describe and Top Level --- goblin.go | 15 ++++++++--- goblin_test.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/goblin.go b/goblin.go index 96a1fd9..59e9dff 100644 --- a/goblin.go +++ b/goblin.go @@ -22,7 +22,7 @@ type Itable interface { } func (g *G) Describe(name string, h func()) { - d := &Describe{name: name, h: h, parent: g.parent} + d := &Describe{name: name, h: h, parent: g.parent, timeout: g.timeout} if d.parent != nil { d.parent.children = append(d.parent.children, Runnable(d)) @@ -44,8 +44,14 @@ func (g *G) Describe(name string, h func()) { } func (g *G) Timeout(time time.Duration) { - g.timeout = time - g.timer.Reset(time) + if g.currentIt != nil { + g.timeout = time + g.timer.Reset(time) + } else if g.parent != nil { + g.parent.timeout = time + } else { + g.timeout = time + } } type Describe struct { @@ -58,6 +64,7 @@ type Describe struct { beforeEach []func() justBeforeEach []func() hasTests bool + timeout time.Duration parent *Describe } @@ -218,7 +225,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(it.parent.timeout) g.shouldContinue = make(chan bool) if call, ok := it.h.(func()); ok { // the test is synchronous diff --git a/goblin_test.go b/goblin_test.go index 2d7ae81..831b446 100644 --- a/goblin_test.go +++ b/goblin_test.go @@ -465,7 +465,7 @@ func TestTimeout(t *testing.T) { parseFlags() g := Goblin(&fakeTest) - g.Describe("Test", func() { + g.Describe("Test arg", func() { g.It("Should fail if test exceeds the specified timeout with sync test", func() { time.Sleep(100 * time.Millisecond) }) @@ -481,6 +481,71 @@ func TestTimeout(t *testing.T) { } } +func TestTopLevelTimeout(t *testing.T) { + fakeTest := testing.T{} + os.Args = append(os.Args, "-goblin.timeout=10ms") + parseFlags() + g := Goblin(&fakeTest) + + g.Timeout(20 * time.Millisecond) + + g.Describe("Test", func() { + g.It("Should override default timeout with sync test", func() { + time.Sleep(15 * time.Millisecond) + }) + + g.It("Should override default timeout with async test", func(done Done) { + time.Sleep(15 * time.Millisecond) + done() + }) + }) + if fakeTest.Failed() { + t.Fatal("Failed") + } +} + +func TestDescribeTimeout(t *testing.T) { + fakeTest := testing.T{} + os.Args = append(os.Args, "-goblin.timeout=10ms") + parseFlags() + g := Goblin(&fakeTest) + + g.Describe("Test 0", func() { + g.Timeout(20 * time.Millisecond) + g.It("Should override default timeout with sync test", func() { + time.Sleep(15 * time.Millisecond) + }) + + g.It("Should override default timeout with async test", func() { + time.Sleep(15 * time.Millisecond) + }) + + }) + if fakeTest.Failed() { + t.Fatal("Failed") + } + + g.Describe("Test 1", func() { + g.It("Should revert for different Describe with sync test", func() { + time.Sleep(15 * time.Millisecond) + }) + + }) + if !fakeTest.Failed() { + t.Fatal("Failed") + } + + g.Describe("Test 2", func() { + g.It("Should revert for different Describe with async test", func() { + time.Sleep(15 * time.Millisecond) + }) + + }) + if !fakeTest.Failed() { + t.Fatal("Failed") + } +} + func TestItTimeout(t *testing.T) { fakeTest := testing.T{} os.Args = append(os.Args, "-goblin.timeout=10ms")