Skip to content

Commit

Permalink
Use await-cond instead of just sleep.
Browse files Browse the repository at this point in the history
  • Loading branch information
svetlyak40wt committed Jun 30, 2024
1 parent b300b7c commit cdfdedd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
17 changes: 16 additions & 1 deletion src/fcomputation.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#:make-future
#:futurep
#:complete-p
#:error-p
#:fcompleted
#:fawait
#:fresult
Expand Down Expand Up @@ -96,7 +97,21 @@ Create a future with:
(defun complete-p (future)
"Is `future` completed? Returns either `t` or `nil`."
(with-slots (promise) future
(promise-finished-p promise)))
(or (promise-finished-p promise)
;; QUESTION:
;; When queue is full and we are trying to ask
;; actor do more job, it will return a future
;; where promise is not finished but errored.
;; I think this state should be considered as
;; COMPLETED, because nothing will happen with
;; such future anymore.
(blackbird-base::promise-errored-p promise))))

(defun error-p (future)
"Is `future` errored? Returns either `t` or `nil`."
(with-slots (promise) future
;; For some reason, this function is not external in the blackbird :(
(blackbird-base::promise-errored-p promise)))

(defun %fcompleted (future completed-fun)
(with-slots (promise) future
Expand Down
32 changes: 19 additions & 13 deletions tests/actor-test.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -486,24 +486,30 @@
(let ((sys (asys:make-actor-system)))
(unwind-protect
(let* ((counter 0)
(queue-full-counter 0)
(actor (actor-of sys
:receive (lambda (msg)
(declare (ignore msg))
(sleep 1)
(incf counter))
:queue-size 1)))
(loop repeat 10
do (handler-case (tell actor "run")
(sento.queue:queue-full-error ()
(incf queue-full-counter))))

(sleep 2)
(sleep 0.1)
(incf counter)
(reply t))
:queue-size 1))
(futures
(loop repeat 10
collect (ask actor "run") into futures
finally
(is-true (await-cond 2
(every #'complete-p
futures)))
(return futures))))

(is (= 1 counter)
"Counter was incremented more then 1 time, it's value is ~A"
counter)
(is (= 9 queue-full-counter)
"Counter of unsuccessful attempts should be equal to 9, but it is ~A"
queue-full-counter))

(let ((queue-full-counter
(length (remove-if-not #'error-p
futures))))
(is (= 9 queue-full-counter)
"Counter of unsuccessful attempts should be equal to 9, but it is ~A"
queue-full-counter)))
(ac:shutdown sys))))

0 comments on commit cdfdedd

Please sign in to comment.