aad93b5a03e23dc9e3ddef7568b0626416042b9f
[8sync.git] / tests / test-actors.scm
1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright (C) 2016 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;;;
4 ;;; This file is part of 8sync.
5 ;;;
6 ;;; 8sync is free software: you can redistribute it and/or modify it
7 ;;; under the terms of the GNU Lesser General Public License as
8 ;;; published by the Free Software Foundation, either version 3 of the
9 ;;; License, or (at your option) any later version.
10 ;;;
11 ;;; 8sync is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;; GNU Lesser General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with 8sync.  If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (tests test-actors)
20   #:use-module (srfi srfi-64)
21   #:use-module (8sync actors)
22   #:use-module (8sync agenda)
23   #:use-module (oop goops)
24   #:use-module (tests utils))
25
26 (test-begin "test-actors")
27
28
29 ;;; Test writing things to here
30 (define %record-out (make-parameter (open-output-string)))
31 (define (~display str)
32   (display str (%record-out)))
33 (define-syntax-rule (~format args ...)
34   (format (%record-out) args ...))
35
36 ;;; Some test dummy values
37 ;;; ======================
38
39 (define %fake-hive-id "the-main-hive")
40 ;; Some fake ids for actors
41 (define %fake-emo-id (make-address "emo" %fake-hive-id))
42 (define %fake-proog-id (make-address "proog" %fake-hive-id))
43 (define %fake-hive-actor-id (make-address "hive" %fake-hive-id))
44
45 (define test-message
46   (make-message ((simple-message-id-generator))
47                 %fake-emo-id
48                 %fake-hive-actor-id ; Bootstrap messages come from the hive
49                 'greet-proog `(#:target ,%fake-proog-id)))
50
51 ;;; Actor utilities
52 ;;; ===============
53
54 ;;; Message tests
55 ;;; =============
56
57 ;; Make sure our test message serializes and deserializes okay
58
59 (let ((reread-message
60        (read-message-from-string
61         (with-output-to-string
62           (lambda () (write-message test-message))))))
63   (test-assert (message? reread-message))
64   ;; Make sure that all the properties are the same from
65   ;; the original message to the re-read message
66   (for-each
67    (lambda (getter)
68      (test-equal (getter test-message) (getter reread-message)))
69    (list message-id message-to message-from message-action message-body
70          message-in-reply-to message-wants-reply
71          (@@ (8sync actors) message-replied))))
72
73
74 ;;; Test reply / autoreply
75 ;;; ======================
76
77 (define-simple-actor <antsy-caller>
78   (pester-rep (wrap-apply antsy-caller-pester-rep)))
79
80 (define* (antsy-caller-pester-rep actor message #:key who-to-call)
81   (~display "customer> I'm calling customer service about this!\n")
82   (msg-receive (first-reply #:key msg)
83       (<-wait actor who-to-call 'field-call)
84     (if (message-auto-reply? first-reply)
85         (~display "customer> Whaaaaat?  I can't believe I got voice mail!\n")
86         (begin
87           (~format "*customer hears*: ~a\n" msg)
88           (msg-receive (second-reply #:key *auto-reply*)
89               (<-reply-wait actor first-reply
90                             #:msg "Yes, it didn't work, I'm VERY ANGRY!")
91             (if (message-auto-reply? second-reply)
92                 (~display "customer> Well then!  Harumph.\n")
93                 (error "Not an autoreply?  What's going on here...")))))))
94
95 (define-simple-actor <diligent-rep>
96   (field-call (wrap-apply rep-field-call)))
97
98 (define (rep-field-call actor message)
99   (~display "good-rep> Hm, another call from a customer...\n")
100   (msg-receive (reply #:key msg)
101       (<-reply-wait
102        actor message
103        #:msg "Have you tried turning it off and on?")
104     (~format "*rep hears*: ~a\n" msg)
105     (~display "good-rep> I'm sorry, that's all I can do for you.\n")))
106
107 (define-simple-actor <lazy-rep>
108   (field-call
109    (lambda (actor message)
110      (~display "lazy-rep> I'm not answering that.\n"))))
111
112 (let* ((hive (make-hive))
113        (customer (hive-create-actor* hive <antsy-caller> "antsy-caller"))
114        (diligent-rep (hive-create-actor* hive <diligent-rep> "diligent-rep"))
115        (lazy-rep (hive-create-actor* hive <lazy-rep> "lazy-rep")))
116   ;; * Playing a tape of a diligent service rep *
117   (parameterize ((%record-out (open-output-string)))
118     (let* ((result (run-hive
119                     hive
120                     (list (bootstrap-message hive customer 'pester-rep
121                                              #:who-to-call diligent-rep))))
122            (displayed-text (get-output-string (%record-out))))
123       (test-equal "customer> I'm calling customer service about this!
124 good-rep> Hm, another call from a customer...
125 *customer hears*: Have you tried turning it off and on?
126 *rep hears*: Yes, it didn't work, I'm VERY ANGRY!
127 good-rep> I'm sorry, that's all I can do for you.
128 customer> Well then!  Harumph.\n"
129         displayed-text)))
130   ;; * Playing a tape of a lazy service rep *
131   (parameterize ((%record-out (open-output-string)))
132     (let* ((result (run-hive
133                     hive
134                     (list (bootstrap-message hive customer 'pester-rep
135                                                   #:who-to-call lazy-rep))))
136            (displayed-text (get-output-string (%record-out))))
137       (test-equal "customer> I'm calling customer service about this!
138 lazy-rep> I'm not answering that.
139 customer> Whaaaaat?  I can't believe I got voice mail!\n"
140           displayed-text))))
141
142 \f
143 ;;; Cleanup tests
144
145 (define-simple-actor <cleanly>
146   (*clean-up* test-call-clean-up))
147
148 (define (test-call-clean-up actor message)
149   (speak "Hey, I'm cleanin' up here!\n"))
150
151 (with-fresh-speaker
152  (let ((hive (make-hive)))
153    (hive-create-actor hive <cleanly>)
154    (run-hive hive '()))
155  (test-equal '("Hey, I'm cleanin' up here!\n")
156    (get-spoken)))
157
158 ;; won't work if we turn off #:clean-up though
159
160 (with-fresh-speaker
161  (let ((hive (make-hive)))
162    (hive-create-actor hive <cleanly>)
163    (run-hive hive '() #:clean-up #f))
164  (test-equal '("Hey, I'm cleanin' up here!\n")
165    (get-spoken)))
166
167 ;; The exploder self-destructs, even though run-hive has clean-up
168 ;; disabled, because it cleans up on self-destruct.
169
170 (with-fresh-speaker
171  (let ((hive (make-hive)))
172    (define exploder (hive-create-actor hive <exploder>))
173    (run-hive hive (list (bootstrap-message hive exploder 'explode))
174              #:clean-up #f))
175  (get-spoken))
176
177
178 (test-end "test-actors")
179 (test-exit)