From: Christopher Allan Webber Date: Mon, 2 Jan 2017 18:17:15 +0000 (-0600) Subject: actors: Update self-destruct to run *clean-up* message handler. X-Git-Tag: v0.4.0~35 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=17830fd9912894b6a30a5c4a4a83722a74c01ccd;p=8sync.git actors: Update self-destruct to run *clean-up* message handler. * 8sync/actors.scm (self-destruct): Wait on *clean-up* action before self-destructing by default. (run-hive): Add parameter to specify whether or not to clean-up after the hive finishes its tasks. * tests/test-actors.scm: Add tests for disabling #:clean-up on run-tasks and ensuring that *clean-up* runs on self-destruct. --- diff --git a/8sync/actors.scm b/8sync/actors.scm index 8aa38e3..1dab9d7 100644 --- a/8sync/actors.scm +++ b/8sync/actors.scm @@ -705,8 +705,13 @@ Like create-actor, but permits supplying an id-cookie." init id-cookie)) -(define (self-destruct actor) - "Remove an actor from the hive." +(define* (self-destruct actor #:key (clean-up #t)) + "Remove an actor from the hive. + +Unless #:clean-up is set to #f, this will first have the actor handle +its '*clean-up* action handler." + (when clean-up + (<-wait actor (actor-id actor) '*clean-up*)) (hash-remove! (hive-actor-registry (actor-hive actor)) (actor-id actor))) @@ -715,7 +720,8 @@ Like create-actor, but permits supplying an id-cookie." ;;; 8sync bootstrap utilities ;;; ========================= -(define* (run-hive hive initial-tasks) +(define* (run-hive hive initial-tasks + #:key (clean-up #t)) "Start up an agenda and run HIVE in it with INITIAL-TASKS." (dynamic-wind (const #f) @@ -726,7 +732,8 @@ Like create-actor, but permits supplying an id-cookie." (start-agenda agenda))) ;; Run clean-up (lambda () - (run-hive-clean-up hive)))) + (when clean-up + (run-hive-clean-up hive))))) (define (run-hive-clean-up hive) (let ((queue (list->q (list (bootstrap-message hive (actor-id hive) diff --git a/tests/test-actors.scm b/tests/test-actors.scm index df98b86..aad93b5 100644 --- a/tests/test-actors.scm +++ b/tests/test-actors.scm @@ -139,7 +139,10 @@ lazy-rep> I'm not answering that. customer> Whaaaaat? I can't believe I got voice mail!\n" displayed-text)))) -(define-simple-actor + +;;; Cleanup tests + +(define-simple-actor (*clean-up* test-call-clean-up)) (define (test-call-clean-up actor message) @@ -147,10 +150,30 @@ customer> Whaaaaat? I can't believe I got voice mail!\n" (with-fresh-speaker (let ((hive (make-hive))) - (hive-create-actor hive ) + (hive-create-actor hive ) (run-hive hive '())) (test-equal '("Hey, I'm cleanin' up here!\n") (get-spoken))) +;; won't work if we turn off #:clean-up though + +(with-fresh-speaker + (let ((hive (make-hive))) + (hive-create-actor hive ) + (run-hive hive '() #:clean-up #f)) + (test-equal '("Hey, I'm cleanin' up here!\n") + (get-spoken))) + +;; The exploder self-destructs, even though run-hive has clean-up +;; disabled, because it cleans up on self-destruct. + +(with-fresh-speaker + (let ((hive (make-hive))) + (define exploder (hive-create-actor hive )) + (run-hive hive (list (bootstrap-message hive exploder 'explode)) + #:clean-up #f)) + (get-spoken)) + + (test-end "test-actors") (test-exit)