Skip to content

Commit

Permalink
fix flakey async tests
Browse files Browse the repository at this point in the history
  • Loading branch information
onsi committed Oct 22, 2014
1 parent 835b5e4 commit 94e521f
Showing 1 changed file with 31 additions and 36 deletions.
67 changes: 31 additions & 36 deletions internal/asyncassertion/async_assertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,48 +28,46 @@ var _ = Describe("Async Assertion", func() {
Describe("Eventually", func() {
Context("the positive case", func() {
It("should poll the function and matcher", func() {
arr := []int{}
a := New(AsyncAssertionTypeEventually, func() []int {
arr = append(arr, 1)
return arr
counter := 0
a := New(AsyncAssertionTypeEventually, func() int {
counter++
return counter
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)

a.Should(HaveLen(10))

Ω(arr).Should(HaveLen(10))
a.Should(BeNumerically("==", 5))
Ω(failureMessage).Should(BeZero())
})

It("should continue when the matcher errors", func() {
var arr = []int{}
counter := 0
a := New(AsyncAssertionTypeEventually, func() interface{} {
arr = append(arr, 1)
if len(arr) == 4 {
return 0 //this should cause the matcher to error
counter++
if counter == 5 {
return "not-a-number" //this should cause the matcher to error
}
return arr
return counter
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)

a.Should(HaveLen(4), "My description %d", 2)
a.Should(BeNumerically("==", 5), "My description %d", 2)

Ω(failureMessage).Should(ContainSubstring("Timed out after"))
Ω(failureMessage).Should(ContainSubstring("My description 2"))
Ω(callerSkip).Should(Equal(4))
})

It("should be able to timeout", func() {
arr := []int{}
a := New(AsyncAssertionTypeEventually, func() []int {
arr = append(arr, 1)
return arr
counter := 0
a := New(AsyncAssertionTypeEventually, func() int {
counter++
return counter
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)

a.Should(HaveLen(11), "My description %d", 2)
a.Should(BeNumerically(">", 100), "My description %d", 2)

Ω(len(arr)).Should(BeNumerically(">", 8))
Ω(len(arr)).Should(BeNumerically("<=", 10))
Ω(counter).Should(BeNumerically(">", 8))
Ω(counter).Should(BeNumerically("<=", 10))
Ω(failureMessage).Should(ContainSubstring("Timed out after"))
Ω(failureMessage).Should(ContainSubstring("<[]int | len:10"), "Should pass the correct value to the matcher message formatter.")
Ω(failureMessage).Should(MatchRegexp(`\<int\>: \d`), "Should pass the correct value to the matcher message formatter.")
Ω(failureMessage).Should(ContainSubstring("My description 2"))
Ω(callerSkip).Should(Equal(4))
})
Expand All @@ -78,18 +76,14 @@ var _ = Describe("Async Assertion", func() {
Context("the negative case", func() {
It("should poll the function and matcher", func() {
counter := 0
arr := []int{}
a := New(AsyncAssertionTypeEventually, func() []int {
a := New(AsyncAssertionTypeEventually, func() int {
counter += 1
if counter >= 10 {
arr = append(arr, 1)
}
return arr
return counter
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)

a.ShouldNot(HaveLen(0))
a.ShouldNot(BeNumerically("<", 3))

Ω(arr).Should(HaveLen(1))
Ω(counter).Should(Equal(3))
Ω(failureMessage).Should(BeZero())
})

Expand All @@ -107,14 +101,14 @@ var _ = Describe("Async Assertion", func() {
})

It("should be able to timeout", func() {
a := New(AsyncAssertionTypeEventually, func() []int {
return []int{}
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
a := New(AsyncAssertionTypeEventually, func() int {
return 0
}, fakeFailHandler, time.Duration(0.1*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)

a.ShouldNot(HaveLen(0), "My description %d", 2)
a.ShouldNot(Equal(0), "My description %d", 2)

Ω(failureMessage).Should(ContainSubstring("Timed out after"))
Ω(failureMessage).Should(ContainSubstring("<[]int | len:0"), "Should pass the correct value to the matcher message formatter.")
Ω(failureMessage).Should(ContainSubstring("<int>: 0"), "Should pass the correct value to the matcher message formatter.")
Ω(failureMessage).Should(ContainSubstring("My description 2"))
Ω(callerSkip).Should(Equal(4))
})
Expand Down Expand Up @@ -173,7 +167,8 @@ var _ = Describe("Async Assertion", func() {
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)

a.Should(Equal("foo"))
Ω(calls).Should(Equal(10))
Ω(calls).Should(BeNumerically(">", 8))
Ω(calls).Should(BeNumerically("<=", 10))
Ω(failureMessage).Should(BeZero())
})
})
Expand All @@ -183,7 +178,7 @@ var _ = Describe("Async Assertion", func() {
calls := 0
a := New(AsyncAssertionTypeConsistently, func() interface{} {
calls++
if calls > 9 {
if calls > 5 {
return "bar"
}
return "foo"
Expand Down

0 comments on commit 94e521f

Please sign in to comment.