forked from github/multus-cni
vendor: add ginkgo and gomega
This commit is contained in:
committed by
Kuralamudhan Ramakrishnan
parent
1c8ad953d0
commit
5820658a5e
189
vendor/github.com/onsi/gomega/internal/asyncassertion/async_assertion.go
generated
vendored
Normal file
189
vendor/github.com/onsi/gomega/internal/asyncassertion/async_assertion.go
generated
vendored
Normal file
@@ -0,0 +1,189 @@
|
||||
package asyncassertion
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/onsi/gomega/internal/oraclematcher"
|
||||
"github.com/onsi/gomega/types"
|
||||
)
|
||||
|
||||
type AsyncAssertionType uint
|
||||
|
||||
const (
|
||||
AsyncAssertionTypeEventually AsyncAssertionType = iota
|
||||
AsyncAssertionTypeConsistently
|
||||
)
|
||||
|
||||
type AsyncAssertion struct {
|
||||
asyncType AsyncAssertionType
|
||||
actualInput interface{}
|
||||
timeoutInterval time.Duration
|
||||
pollingInterval time.Duration
|
||||
fail types.GomegaFailHandler
|
||||
offset int
|
||||
}
|
||||
|
||||
func New(asyncType AsyncAssertionType, actualInput interface{}, fail types.GomegaFailHandler, timeoutInterval time.Duration, pollingInterval time.Duration, offset int) *AsyncAssertion {
|
||||
actualType := reflect.TypeOf(actualInput)
|
||||
if actualType.Kind() == reflect.Func {
|
||||
if actualType.NumIn() != 0 || actualType.NumOut() == 0 {
|
||||
panic("Expected a function with no arguments and one or more return values.")
|
||||
}
|
||||
}
|
||||
|
||||
return &AsyncAssertion{
|
||||
asyncType: asyncType,
|
||||
actualInput: actualInput,
|
||||
fail: fail,
|
||||
timeoutInterval: timeoutInterval,
|
||||
pollingInterval: pollingInterval,
|
||||
offset: offset,
|
||||
}
|
||||
}
|
||||
|
||||
func (assertion *AsyncAssertion) Should(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool {
|
||||
return assertion.match(matcher, true, optionalDescription...)
|
||||
}
|
||||
|
||||
func (assertion *AsyncAssertion) ShouldNot(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool {
|
||||
return assertion.match(matcher, false, optionalDescription...)
|
||||
}
|
||||
|
||||
func (assertion *AsyncAssertion) buildDescription(optionalDescription ...interface{}) string {
|
||||
switch len(optionalDescription) {
|
||||
case 0:
|
||||
return ""
|
||||
default:
|
||||
return fmt.Sprintf(optionalDescription[0].(string), optionalDescription[1:]...) + "\n"
|
||||
}
|
||||
}
|
||||
|
||||
func (assertion *AsyncAssertion) actualInputIsAFunction() bool {
|
||||
actualType := reflect.TypeOf(assertion.actualInput)
|
||||
return actualType.Kind() == reflect.Func && actualType.NumIn() == 0 && actualType.NumOut() > 0
|
||||
}
|
||||
|
||||
func (assertion *AsyncAssertion) pollActual() (interface{}, error) {
|
||||
if assertion.actualInputIsAFunction() {
|
||||
values := reflect.ValueOf(assertion.actualInput).Call([]reflect.Value{})
|
||||
|
||||
extras := []interface{}{}
|
||||
for _, value := range values[1:] {
|
||||
extras = append(extras, value.Interface())
|
||||
}
|
||||
|
||||
success, message := vetExtras(extras)
|
||||
|
||||
if !success {
|
||||
return nil, errors.New(message)
|
||||
}
|
||||
|
||||
return values[0].Interface(), nil
|
||||
}
|
||||
|
||||
return assertion.actualInput, nil
|
||||
}
|
||||
|
||||
func (assertion *AsyncAssertion) matcherMayChange(matcher types.GomegaMatcher, value interface{}) bool {
|
||||
if assertion.actualInputIsAFunction() {
|
||||
return true
|
||||
}
|
||||
|
||||
return oraclematcher.MatchMayChangeInTheFuture(matcher, value)
|
||||
}
|
||||
|
||||
func (assertion *AsyncAssertion) match(matcher types.GomegaMatcher, desiredMatch bool, optionalDescription ...interface{}) bool {
|
||||
timer := time.Now()
|
||||
timeout := time.After(assertion.timeoutInterval)
|
||||
|
||||
description := assertion.buildDescription(optionalDescription...)
|
||||
|
||||
var matches bool
|
||||
var err error
|
||||
mayChange := true
|
||||
value, err := assertion.pollActual()
|
||||
if err == nil {
|
||||
mayChange = assertion.matcherMayChange(matcher, value)
|
||||
matches, err = matcher.Match(value)
|
||||
}
|
||||
|
||||
fail := func(preamble string) {
|
||||
errMsg := ""
|
||||
message := ""
|
||||
if err != nil {
|
||||
errMsg = "Error: " + err.Error()
|
||||
} else {
|
||||
if desiredMatch {
|
||||
message = matcher.FailureMessage(value)
|
||||
} else {
|
||||
message = matcher.NegatedFailureMessage(value)
|
||||
}
|
||||
}
|
||||
assertion.fail(fmt.Sprintf("%s after %.3fs.\n%s%s%s", preamble, time.Since(timer).Seconds(), description, message, errMsg), 3+assertion.offset)
|
||||
}
|
||||
|
||||
if assertion.asyncType == AsyncAssertionTypeEventually {
|
||||
for {
|
||||
if err == nil && matches == desiredMatch {
|
||||
return true
|
||||
}
|
||||
|
||||
if !mayChange {
|
||||
fail("No future change is possible. Bailing out early")
|
||||
return false
|
||||
}
|
||||
|
||||
select {
|
||||
case <-time.After(assertion.pollingInterval):
|
||||
value, err = assertion.pollActual()
|
||||
if err == nil {
|
||||
mayChange = assertion.matcherMayChange(matcher, value)
|
||||
matches, err = matcher.Match(value)
|
||||
}
|
||||
case <-timeout:
|
||||
fail("Timed out")
|
||||
return false
|
||||
}
|
||||
}
|
||||
} else if assertion.asyncType == AsyncAssertionTypeConsistently {
|
||||
for {
|
||||
if !(err == nil && matches == desiredMatch) {
|
||||
fail("Failed")
|
||||
return false
|
||||
}
|
||||
|
||||
if !mayChange {
|
||||
return true
|
||||
}
|
||||
|
||||
select {
|
||||
case <-time.After(assertion.pollingInterval):
|
||||
value, err = assertion.pollActual()
|
||||
if err == nil {
|
||||
mayChange = assertion.matcherMayChange(matcher, value)
|
||||
matches, err = matcher.Match(value)
|
||||
}
|
||||
case <-timeout:
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func vetExtras(extras []interface{}) (bool, string) {
|
||||
for i, extra := range extras {
|
||||
if extra != nil {
|
||||
zeroValue := reflect.Zero(reflect.TypeOf(extra)).Interface()
|
||||
if !reflect.DeepEqual(zeroValue, extra) {
|
||||
message := fmt.Sprintf("Unexpected non-nil/non-zero extra argument at index %d:\n\t<%T>: %#v", i+1, extra, extra)
|
||||
return false, message
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, ""
|
||||
}
|
13
vendor/github.com/onsi/gomega/internal/asyncassertion/async_assertion_suite_test.go
generated
vendored
Normal file
13
vendor/github.com/onsi/gomega/internal/asyncassertion/async_assertion_suite_test.go
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
package asyncassertion_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAsyncAssertion(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "AsyncAssertion Suite")
|
||||
}
|
345
vendor/github.com/onsi/gomega/internal/asyncassertion/async_assertion_test.go
generated
vendored
Normal file
345
vendor/github.com/onsi/gomega/internal/asyncassertion/async_assertion_test.go
generated
vendored
Normal file
@@ -0,0 +1,345 @@
|
||||
package asyncassertion_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
. "github.com/onsi/gomega/internal/asyncassertion"
|
||||
)
|
||||
|
||||
var _ = Describe("Async Assertion", func() {
|
||||
var (
|
||||
failureMessage string
|
||||
callerSkip int
|
||||
)
|
||||
|
||||
var fakeFailHandler = func(message string, skip ...int) {
|
||||
failureMessage = message
|
||||
callerSkip = skip[0]
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
failureMessage = ""
|
||||
callerSkip = 0
|
||||
})
|
||||
|
||||
Describe("Eventually", func() {
|
||||
Context("the positive case", func() {
|
||||
It("should poll the function and matcher", func() {
|
||||
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(BeNumerically("==", 5))
|
||||
Ω(failureMessage).Should(BeZero())
|
||||
})
|
||||
|
||||
It("should continue when the matcher errors", func() {
|
||||
counter := 0
|
||||
a := New(AsyncAssertionTypeEventually, func() interface{} {
|
||||
counter++
|
||||
if counter == 5 {
|
||||
return "not-a-number" //this should cause the matcher to error
|
||||
}
|
||||
return counter
|
||||
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
|
||||
|
||||
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() {
|
||||
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(BeNumerically(">", 100), "My description %d", 2)
|
||||
|
||||
Ω(counter).Should(BeNumerically(">", 8))
|
||||
Ω(counter).Should(BeNumerically("<=", 10))
|
||||
Ω(failureMessage).Should(ContainSubstring("Timed out after"))
|
||||
Ω(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))
|
||||
})
|
||||
})
|
||||
|
||||
Context("the negative case", func() {
|
||||
It("should poll the function and matcher", func() {
|
||||
counter := 0
|
||||
a := New(AsyncAssertionTypeEventually, func() int {
|
||||
counter += 1
|
||||
return counter
|
||||
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
|
||||
|
||||
a.ShouldNot(BeNumerically("<", 3))
|
||||
|
||||
Ω(counter).Should(Equal(3))
|
||||
Ω(failureMessage).Should(BeZero())
|
||||
})
|
||||
|
||||
It("should timeout when the matcher errors", func() {
|
||||
a := New(AsyncAssertionTypeEventually, func() interface{} {
|
||||
return 0 //this should cause the matcher to error
|
||||
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
|
||||
|
||||
a.ShouldNot(HaveLen(0), "My description %d", 2)
|
||||
|
||||
Ω(failureMessage).Should(ContainSubstring("Timed out after"))
|
||||
Ω(failureMessage).Should(ContainSubstring("Error:"))
|
||||
Ω(failureMessage).Should(ContainSubstring("My description 2"))
|
||||
Ω(callerSkip).Should(Equal(4))
|
||||
})
|
||||
|
||||
It("should be able to timeout", func() {
|
||||
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(Equal(0), "My description %d", 2)
|
||||
|
||||
Ω(failureMessage).Should(ContainSubstring("Timed out after"))
|
||||
Ω(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))
|
||||
})
|
||||
})
|
||||
|
||||
Context("with a function that returns multiple values", func() {
|
||||
It("should eventually succeed if the additional arguments are nil", func() {
|
||||
i := 0
|
||||
Eventually(func() (int, error) {
|
||||
i++
|
||||
return i, nil
|
||||
}).Should(Equal(10))
|
||||
})
|
||||
|
||||
It("should eventually timeout if the additional arguments are not nil", func() {
|
||||
i := 0
|
||||
a := New(AsyncAssertionTypeEventually, func() (int, error) {
|
||||
i++
|
||||
return i, errors.New("bam")
|
||||
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
|
||||
a.Should(Equal(2))
|
||||
|
||||
Ω(failureMessage).Should(ContainSubstring("Timed out after"))
|
||||
Ω(failureMessage).Should(ContainSubstring("Error:"))
|
||||
Ω(failureMessage).Should(ContainSubstring("bam"))
|
||||
Ω(callerSkip).Should(Equal(4))
|
||||
})
|
||||
})
|
||||
|
||||
Context("Making an assertion without a registered fail handler", func() {
|
||||
It("should panic", func() {
|
||||
defer func() {
|
||||
e := recover()
|
||||
RegisterFailHandler(Fail)
|
||||
if e == nil {
|
||||
Fail("expected a panic to have occurred")
|
||||
}
|
||||
}()
|
||||
|
||||
RegisterFailHandler(nil)
|
||||
c := make(chan bool, 1)
|
||||
c <- true
|
||||
Eventually(c).Should(Receive())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Consistently", func() {
|
||||
Describe("The positive case", func() {
|
||||
Context("when the matcher consistently passes for the duration", func() {
|
||||
It("should pass", func() {
|
||||
calls := 0
|
||||
a := New(AsyncAssertionTypeConsistently, func() string {
|
||||
calls++
|
||||
return "foo"
|
||||
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
|
||||
|
||||
a.Should(Equal("foo"))
|
||||
Ω(calls).Should(BeNumerically(">", 8))
|
||||
Ω(calls).Should(BeNumerically("<=", 10))
|
||||
Ω(failureMessage).Should(BeZero())
|
||||
})
|
||||
})
|
||||
|
||||
Context("when the matcher fails at some point", func() {
|
||||
It("should fail", func() {
|
||||
calls := 0
|
||||
a := New(AsyncAssertionTypeConsistently, func() interface{} {
|
||||
calls++
|
||||
if calls > 5 {
|
||||
return "bar"
|
||||
}
|
||||
return "foo"
|
||||
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
|
||||
|
||||
a.Should(Equal("foo"))
|
||||
Ω(failureMessage).Should(ContainSubstring("to equal"))
|
||||
Ω(callerSkip).Should(Equal(4))
|
||||
})
|
||||
})
|
||||
|
||||
Context("when the matcher errors at some point", func() {
|
||||
It("should fail", func() {
|
||||
calls := 0
|
||||
a := New(AsyncAssertionTypeConsistently, func() interface{} {
|
||||
calls++
|
||||
if calls > 5 {
|
||||
return 3
|
||||
}
|
||||
return []int{1, 2, 3}
|
||||
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
|
||||
|
||||
a.Should(HaveLen(3))
|
||||
Ω(failureMessage).Should(ContainSubstring("HaveLen matcher expects"))
|
||||
Ω(callerSkip).Should(Equal(4))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Describe("The negative case", func() {
|
||||
Context("when the matcher consistently passes for the duration", func() {
|
||||
It("should pass", func() {
|
||||
c := make(chan bool)
|
||||
a := New(AsyncAssertionTypeConsistently, c, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
|
||||
|
||||
a.ShouldNot(Receive())
|
||||
Ω(failureMessage).Should(BeZero())
|
||||
})
|
||||
})
|
||||
|
||||
Context("when the matcher fails at some point", func() {
|
||||
It("should fail", func() {
|
||||
c := make(chan bool)
|
||||
go func() {
|
||||
time.Sleep(time.Duration(100 * time.Millisecond))
|
||||
c <- true
|
||||
}()
|
||||
|
||||
a := New(AsyncAssertionTypeConsistently, c, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
|
||||
|
||||
a.ShouldNot(Receive())
|
||||
Ω(failureMessage).Should(ContainSubstring("not to receive anything"))
|
||||
})
|
||||
})
|
||||
|
||||
Context("when the matcher errors at some point", func() {
|
||||
It("should fail", func() {
|
||||
calls := 0
|
||||
a := New(AsyncAssertionTypeConsistently, func() interface{} {
|
||||
calls++
|
||||
return calls
|
||||
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
|
||||
|
||||
a.ShouldNot(BeNumerically(">", 5))
|
||||
Ω(failureMessage).Should(ContainSubstring("not to be >"))
|
||||
Ω(callerSkip).Should(Equal(4))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Context("with a function that returns multiple values", func() {
|
||||
It("should consistently succeed if the additional arguments are nil", func() {
|
||||
i := 2
|
||||
Consistently(func() (int, error) {
|
||||
i++
|
||||
return i, nil
|
||||
}).Should(BeNumerically(">=", 2))
|
||||
})
|
||||
|
||||
It("should eventually timeout if the additional arguments are not nil", func() {
|
||||
i := 2
|
||||
a := New(AsyncAssertionTypeEventually, func() (int, error) {
|
||||
i++
|
||||
return i, errors.New("bam")
|
||||
}, fakeFailHandler, time.Duration(0.2*float64(time.Second)), time.Duration(0.02*float64(time.Second)), 1)
|
||||
a.Should(BeNumerically(">=", 2))
|
||||
|
||||
Ω(failureMessage).Should(ContainSubstring("Error:"))
|
||||
Ω(failureMessage).Should(ContainSubstring("bam"))
|
||||
Ω(callerSkip).Should(Equal(4))
|
||||
})
|
||||
})
|
||||
|
||||
Context("Making an assertion without a registered fail handler", func() {
|
||||
It("should panic", func() {
|
||||
defer func() {
|
||||
e := recover()
|
||||
RegisterFailHandler(Fail)
|
||||
if e == nil {
|
||||
Fail("expected a panic to have occurred")
|
||||
}
|
||||
}()
|
||||
|
||||
RegisterFailHandler(nil)
|
||||
c := make(chan bool)
|
||||
Consistently(c).ShouldNot(Receive())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Context("when passed a function with the wrong # or arguments & returns", func() {
|
||||
It("should panic", func() {
|
||||
Ω(func() {
|
||||
New(AsyncAssertionTypeEventually, func() {}, fakeFailHandler, 0, 0, 1)
|
||||
}).Should(Panic())
|
||||
|
||||
Ω(func() {
|
||||
New(AsyncAssertionTypeEventually, func(a string) int { return 0 }, fakeFailHandler, 0, 0, 1)
|
||||
}).Should(Panic())
|
||||
|
||||
Ω(func() {
|
||||
New(AsyncAssertionTypeEventually, func() int { return 0 }, fakeFailHandler, 0, 0, 1)
|
||||
}).ShouldNot(Panic())
|
||||
|
||||
Ω(func() {
|
||||
New(AsyncAssertionTypeEventually, func() (int, error) { return 0, nil }, fakeFailHandler, 0, 0, 1)
|
||||
}).ShouldNot(Panic())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("bailing early", func() {
|
||||
Context("when actual is a value", func() {
|
||||
It("Eventually should bail out and fail early if the matcher says to", func() {
|
||||
c := make(chan bool)
|
||||
close(c)
|
||||
|
||||
t := time.Now()
|
||||
failures := InterceptGomegaFailures(func() {
|
||||
Eventually(c, 0.1).Should(Receive())
|
||||
})
|
||||
Ω(time.Since(t)).Should(BeNumerically("<", 90*time.Millisecond))
|
||||
|
||||
Ω(failures).Should(HaveLen(1))
|
||||
})
|
||||
})
|
||||
|
||||
Context("when actual is a function", func() {
|
||||
It("should never bail early", func() {
|
||||
c := make(chan bool)
|
||||
close(c)
|
||||
|
||||
t := time.Now()
|
||||
failures := InterceptGomegaFailures(func() {
|
||||
Eventually(func() chan bool {
|
||||
return c
|
||||
}, 0.1).Should(Receive())
|
||||
})
|
||||
Ω(time.Since(t)).Should(BeNumerically(">=", 90*time.Millisecond))
|
||||
|
||||
Ω(failures).Should(HaveLen(1))
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user