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

allow users to wrap the Gomega interface #521

Merged
merged 1 commit into from Mar 25, 2022

Conversation

tzneal
Copy link
Contributor

@tzneal tzneal commented Feb 18, 2022

gomega internally casts to *internal.Gomega which makes wrapping
it impossible. To allow this, add an interface that if implemented
will be used to get the actual *internal.Gomega. This allows users
to replace gomega.Default with their own wrapper as long as it
implements provides an 'Inner() Gomega' method.

gomega internally casts to *internal.Gomega which makes wrapping
it impossible.  To allow this, add an interface that if implemented
will be used to get the actual *internal.Gomega.  This allows users
to replace gomega.Default with their own wrapper as long as it
implements provides an 'Inner() Gomega' method.
@tzneal
Copy link
Contributor Author

tzneal commented Feb 18, 2022

Just wanted to check if there was interest in something like this. I used it to add some random delays to all of the Expect calls to look for any timing issues with tests.

@onsi
Copy link
Owner

onsi commented Feb 22, 2022

hey this is a great idea. i'm a bit behind but will try to take a look soon.

@onsi
Copy link
Owner

onsi commented Mar 25, 2022

Hey so sorry for the crazy long delay. This looks good to me and I'm merging it in. I wonder if you'd be up for submitting a PR to the documentation at https://github.com/onsi/gomega/blob/master/docs/index.md to explain to folks how to wrap their own Gomega? Even just sharing your example usecase in the docs could be helpful to others looking to do the same.

@onsi onsi merged commit 1f2e714 into onsi:master Mar 25, 2022
@tzneal
Copy link
Contributor Author

tzneal commented Apr 4, 2022

Sure, not sure where the docs should go, but this is some example code that uses this feature:

//go:build random_test_delay

package test

package test

import (
	"github.com/onsi/ginkgo"
	"github.com/onsi/gomega"
	"github.com/onsi/gomega/types"
	"math/rand"
	"sync"
	"time"
)

type gomegaWrapper struct {
	inner gomega.Gomega
	mu    sync.Mutex
	r     *rand.Rand
}

func (g *gomegaWrapper) randomDelay() {
	g.mu.Lock()
	delay := time.Duration(g.r.Intn(100)) * time.Millisecond
	g.mu.Unlock()
	time.Sleep(delay)
}

func (g *gomegaWrapper) Ω(actual interface{}, extra ...interface{}) types.Assertion {
	g.randomDelay()
	return g.inner.Ω(actual, extra...)
}

func (g *gomegaWrapper) Expect(actual interface{}, extra ...interface{}) types.Assertion {
	g.randomDelay()
	return g.inner.Expect(actual, extra...)
}

func (g *gomegaWrapper) ExpectWithOffset(offset int, actual interface{}, extra ...interface{}) types.Assertion {
	g.randomDelay()
	return g.inner.ExpectWithOffset(offset, actual, extra...)
}

func (g *gomegaWrapper) Eventually(actual interface{}, intervals ...interface{}) types.AsyncAssertion {
	g.randomDelay()
	return g.inner.Eventually(actual, intervals...)
}

func (g *gomegaWrapper) EventuallyWithOffset(offset int, actual interface{}, intervals ...interface{}) types.AsyncAssertion {
	g.randomDelay()
	return g.inner.EventuallyWithOffset(offset, actual, intervals...)
}

func (g *gomegaWrapper) Consistently(actual interface{}, intervals ...interface{}) types.AsyncAssertion {
	g.randomDelay()
	return g.inner.Consistently(actual, intervals...)
}

func (g *gomegaWrapper) ConsistentlyWithOffset(offset int, actual interface{}, intervals ...interface{}) types.AsyncAssertion {
	g.randomDelay()
	return g.inner.ConsistentlyWithOffset(offset, actual, intervals...)
}

func (g *gomegaWrapper) SetDefaultEventuallyTimeout(duration time.Duration) {
	g.inner.SetDefaultEventuallyTimeout(duration)
}

func (g *gomegaWrapper) SetDefaultEventuallyPollingInterval(duration time.Duration) {
	g.inner.SetDefaultEventuallyPollingInterval(duration)
}

func (g *gomegaWrapper) SetDefaultConsistentlyDuration(duration time.Duration) {
	g.inner.SetDefaultConsistentlyDuration(duration)
}

func (g *gomegaWrapper) SetDefaultConsistentlyPollingInterval(duration time.Duration) {
	g.inner.SetDefaultConsistentlyPollingInterval(duration)
}

func (g *gomegaWrapper) Inner() gomega.Gomega {
	return g.inner
}

func init() {
	gomega.Default = &gomegaWrapper{
		inner: gomega.Default,
		r:     rand.New(rand.NewSource(ginkgo.GinkgoRandomSeed())),
	}
}

@tzneal tzneal deleted the allow-users-to-wrap-gomega-default branch Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants