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

Waiting for predicate to be true #7

Open
canatella opened this issue Jan 12, 2016 · 2 comments
Open

Waiting for predicate to be true #7

canatella opened this issue Jan 12, 2016 · 2 comments

Comments

@canatella
Copy link

Would you accept a pull request with a function that takes a predicates and wait with a timeout for the predicate to be true ? I have a mode which is launching async commands to fill a buffer. In my test I need to wait for the buffer to be filled. I was thinking something along the lines of:

(defmacro ert-wait-for (timeout predicate &rest body)
  "Wait for maximum TIMEOUT second for PREDICATE to verify, than execute forms in BODY."
  `(with-timeout
       (,timeout (ert-fail (format "Timeout of %ds exceeded while waiting for predicate." ,timeout)))
     (while (not (funcall ,predicate))
       (accept-process-output nil 0.05))
     ,@body))

So one could use it like

(ert-deftest test-buffer-content ()
  (with-current-buffer my-buffer
    (ert-wait-for 10 (lambda ()
                       (goto-char (point-min))
                       (= (how-many "foobar" 2)))
      (goto-char (point-min))
      (should (re-search-forward "the foobar is in the foobar" nil t)))))

to wait for a buffer to contain twice foobar for example. I think this might be usefull for other users of async commands.

@rejeep
Copy link
Owner

rejeep commented Jan 13, 2016

Is the buffer contents inserted async?

Anyways, I don't think this is the appropriate library for such a function as it's not async, which is the purpose of this library?

@canatella
Copy link
Author

Yes the buffer content is the output of an async function started with start-process. But maybe I misunderstood this library purpose. If I understood correctly, with your lib, I could use a callback in my process sentinel and use ert-deftest-async to assert that the callback has been called before timeout. I found that, for me, going the way of the wait-for function was easier as it allows to also verify the side effect of the callback (like switching to a buffer or creating a new one). Anyway if you have a better suggestion, for my problem or for another library where this could be usefull, it's welcome.

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