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

Added Timeout() for Describe and Top Level #95

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
15 changes: 11 additions & 4 deletions goblin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 {
Expand All @@ -58,6 +64,7 @@ type Describe struct {
beforeEach []func()
justBeforeEach []func()
hasTests bool
timeout time.Duration
parent *Describe
}

Expand Down Expand Up @@ -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
Expand Down
67 changes: 66 additions & 1 deletion goblin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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")
Expand Down