actors: Update self-destruct to run *clean-up* message handler.
authorChristopher Allan Webber <cwebber@dustycloud.org>
Mon, 2 Jan 2017 18:17:15 +0000 (12:17 -0600)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Mon, 2 Jan 2017 18:17:15 +0000 (12:17 -0600)
* 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.

8sync/actors.scm
tests/test-actors.scm

index 8aa38e325f5f2dfe6b79577d0d8ff5486dbcb666..1dab9d7918ea62b481722123aeee03513ec12965 100644 (file)
@@ -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)
index df98b86c3e41f9b4ede3b80b1bdc4870a9f0c9ad..aad93b5a03e23dc9e3ddef7568b0626416042b9f 100644 (file)
@@ -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 <foo>
+\f
+;;; Cleanup tests
+
+(define-simple-actor <cleanly>
   (*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 <foo>)
+   (hive-create-actor hive <cleanly>)
    (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 <cleanly>)
+   (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 <exploder>))
+   (run-hive hive (list (bootstrap-message hive exploder 'explode))
+             #:clean-up #f))
+ (get-spoken))
+
+
 (test-end "test-actors")
 (test-exit)