1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright (C) 2016 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of 8sync.
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.
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.
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/>.
19 (define-module (tests test-actors)
20 #:use-module (srfi srfi-64)
21 #:use-module (8sync systems actors)
22 #:use-module (8sync agenda)
23 #:use-module (oop goops)
24 #:use-module (tests utils))
26 (test-begin "test-actors")
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 ...))
36 ;;; Some test dummy values
37 ;;; ======================
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))
46 (make-message ((simple-message-id-generator))
48 %fake-hive-actor-id ; Bootstrap messages come from the hive
49 'greet-proog `(#:target ,%fake-proog-id)))
57 ;; Make sure our test message serializes and deserializes okay
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
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 systems actors) message-replied))))
74 ;;; Test reply / autoreply
75 ;;; ======================
77 (define-simple-actor <antsy-caller>
78 (pester-rep (wrap-apply antsy-caller-pester-rep)))
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")
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...")))))))
95 (define-simple-actor <diligent-rep>
96 (field-call (wrap-apply rep-field-call)))
98 (define (rep-field-call actor message)
99 (~display "good-rep> Hm, another call from a customer...\n")
100 (msg-receive (reply #:key msg)
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")))
107 (define-simple-actor <lazy-rep>
109 (lambda (actor message)
110 (~display "lazy-rep> I'm not answering that.\n"))))
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 (ez-run-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"
130 ;; * Playing a tape of a lazy service rep *
131 (parameterize ((%record-out (open-output-string)))
132 (let* ((result (ez-run-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"
142 (test-end "test-actors")