fix flakey async tests
diff --git a/internal/asyncassertion/async_assertion_test.go b/internal/asyncassertion/async_assertion_test.go index 95e4e82..4288d4d 100644 --- a/internal/asyncassertion/async_assertion_test.go +++ b/internal/asyncassertion/async_assertion_test.go
@@ -28,29 +28,27 @@ 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")) @@ -58,18 +56,18 @@ }) 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)) }) @@ -78,18 +76,14 @@ 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()) }) @@ -107,14 +101,14 @@ }) 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)) }) @@ -173,7 +167,8 @@ }, 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()) }) }) @@ -183,7 +178,7 @@ calls := 0 a := New(AsyncAssertionTypeConsistently, func() interface{} { calls++ - if calls > 9 { + if calls > 5 { return "bar" } return "foo"