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

Stop Eventually/Consistently with success #786

Closed
grosser opened this issue Oct 26, 2024 · 8 comments
Closed

Stop Eventually/Consistently with success #786

grosser opened this issue Oct 26, 2024 · 8 comments

Comments

@grosser
Copy link
Contributor

grosser commented Oct 26, 2024

as per https://onsi.github.io/gomega/#bailing-out-early
StopTrying is the only way to stop iteration without having to implement a custom matcher
but I'd like to also stop with a success, for example

	errChan := make(chan error)
	go func() {
		time.Sleep(10 * time.Second)
		errChan <- fmt.Errorf("error")
	}()
	Consistently(func() (bool) {
		select {
		case err := <-errChan:
			StopTrying("background task finished").Now()
			return true
		default:
			return true // actual check goes here
		}
	}, timeout, interval).Should(BeTrue())

to not fail, for example with StopTrying("background task stopped").Success()
or return return true, StopTrying("background task stopped").Success()

@onsi
Copy link
Owner

onsi commented Oct 29, 2024

hey - this makes sense, though really only for Consistently. I'll implement StopTrying(...).Success() but will have it error if you use it with Eventually but do what you're describing fro Consistently.

@grosser
Copy link
Contributor Author

grosser commented Oct 29, 2024

thx! :D
yeah it would be weird for eventually

@onsi
Copy link
Owner

onsi commented Oct 29, 2024

do you mind if I use your example in the docs?

@grosser
Copy link
Contributor Author

grosser commented Oct 29, 2024

go ahead 👍

@onsi onsi closed this as completed in eeca931 Oct 29, 2024
@onsi
Copy link
Owner

onsi commented Oct 29, 2024

alrighty, this is pushed now. will cut a release oon

@grosser
Copy link
Contributor Author

grosser commented Oct 29, 2024

thank you 🎉

@grosser
Copy link
Contributor Author

grosser commented Oct 31, 2024

FYI using this wrapper

func ConsistentlyUntil[T any](fn func() T, timeout any, interval any, errChan chan error) AsyncAssertion {
	return Consistently(func() T {
		select {
		case err := <-errChan:
			if err == nil {
				StopTrying("success!").Successfully().Now()
			}
			StopTrying(fmt.Sprintf("background task faild: %v", err)).Now()
			return fn() // unreachable since StopTrying will panic
		default:
			return fn()
		}
	}, timeout, interval)
}

@onsi
Copy link
Owner

onsi commented Oct 31, 2024

ooh that's nice. glad it's proven useful :)

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

No branches or pull requests

2 participants