forked from mighty-gerbils/gerbil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactor-test.ss
251 lines (213 loc) · 7.72 KB
/
actor-test.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
;;; -*- Gerbil -*-
;;; (C) vyzo at hackzen.org
;;; :std/actor unit-test
(import :gerbil/gambit
:std/test
:std/event
:std/actor)
(export actor-rpc-test
actor-rpc-stream-test)
(defproto hello
call: (hello a)
stream: (hello-stream a))
(def (hello-server remoted)
(!!rpc.register remoted 'foo hello::proto)
(let lp ()
(<- ((!hello.hello val k)
(!!value @source val k)
(lp))
((!rpc.shutdown)
(void))
(content
(displayln "unexpected message " @message " " content)
(lp)))))
(def (hello-void-server remoted)
(!!rpc.register remoted 'foo hello::proto)
(let lp ()
(<- ((!rpc.shutdown)
(void))
(ignore
(lp)))))
(def rpc-server-address1 "127.0.0.1:9000")
(def rpc-server-address2 "127.0.0.1:9001")
(def rpc-server-address3 "127.0.0.1:9002")
(def rpc-server-address4 "127.0.0.1:9003")
(def rpc-server-address5 "127.0.0.1:9004")
(def rpc-server-address6 "127.0.0.1:9005")
(def rpc-server-address7 "127.0.0.1:9006")
(def rpc-server-address8 "127.0.0.1:9007")
(def rpc-server-address9 "127.0.0.1:9008")
(def rpc-server-address10 "127.0.0.1:9009")
(def rpc-cookie "/tmp/actor-test-cookie")
(rpc-generate-cookie! rpc-cookie)
(def actor-rpc-test
(test-suite "test :std/actor RPC"
(test-case "test RPC NULL proto"
(def remoted (start-rpc-server! rpc-server-address1))
(def hellod (spawn hello-server remoted))
(thread-sleep! 0.1)
(check (!!rpc.resolve remoted 'foo) => hellod)
(def locald (start-rpc-server!))
(def rfoo
(rpc-connect locald 'foo rpc-server-address1 hello::proto))
(check (!!hello.hello rfoo 'a timeout: 1) => 'a)
(stop-rpc-server! remoted)
(stop-rpc-server! locald))
(test-case "test RPC COOKIE proto"
(def remoted
(start-rpc-server! rpc-server-address2 proto: (rpc-cookie-proto rpc-cookie)))
(def hellod
(spawn hello-server remoted))
(thread-sleep! 0.1)
(check (!!rpc.resolve remoted 'foo) => hellod)
(def locald
(start-rpc-server! proto: (rpc-cookie-proto rpc-cookie)))
(def rfoo
(rpc-connect locald 'foo rpc-server-address2 hello::proto))
(check (!!hello.hello rfoo 'a timeout: 1) => 'a)
(stop-rpc-server! remoted)
(stop-rpc-server! locald))
(test-case "test RPC CIPHER proto"
(def remoted
(start-rpc-server! rpc-server-address3 proto: (rpc-cipher-proto)))
(def hellod
(spawn hello-server remoted))
(thread-sleep! 0.1)
(check (!!rpc.resolve remoted 'foo) => hellod)
(def locald
(start-rpc-server! proto: (rpc-cipher-proto)))
(def rfoo
(rpc-connect locald 'foo rpc-server-address3 hello::proto))
(check (!!hello.hello rfoo 'a timeout: 1) => 'a)
(stop-rpc-server! remoted)
(stop-rpc-server! locald))
(test-case "test RPC COOKIE-CIPHER proto"
(def remoted
(start-rpc-server! rpc-server-address4 proto: (rpc-cookie-cipher-proto rpc-cookie)))
(def hellod
(spawn hello-server remoted))
(thread-sleep! 0.1)
(check (!!rpc.resolve remoted 'foo) => hellod)
(def locald
(start-rpc-server! proto: (rpc-cookie-cipher-proto rpc-cookie)))
(def rfoo
(rpc-connect locald 'foo rpc-server-address4 hello::proto))
(check (!!hello.hello rfoo 'a timeout: 1) => 'a)
(stop-rpc-server! remoted)
(stop-rpc-server! locald))
(test-case "test RPC errors"
(def locald (start-rpc-server!))
(def rfoo
(make-remote locald 'foo rpc-server-address8 hello::proto))
(check (with-catch values (cut !!hello.hello rfoo 'a)) ? rpc-error?)
(def remoted (start-rpc-server! rpc-server-address8))
(thread-sleep! 0.1)
(check (with-catch values (cut !!hello.hello rfoo 'a)) ? remote-error?)
(def hellod
(spawn hello-void-server remoted))
(thread-sleep! 0.1)
(check (!!rpc.resolve remoted 'foo) => hellod)
(check (with-catch values (cut !!hello.hello rfoo 'a timeout: 1)) ? rpc-error?)
(stop-rpc-server! remoted)
(stop-rpc-server! locald))
(test-case "test RPC monitors"
(def remoted (start-rpc-server! rpc-server-address10))
(def hellod (spawn hello-server remoted))
(thread-sleep! 0.1)
(check (!!rpc.resolve remoted 'foo) => hellod)
(def locald (start-rpc-server!))
(def rfoo
(rpc-connect locald 'foo rpc-server-address10 hello::proto))
(check (!!hello.hello rfoo 'a timeout: 1) => 'a)
(!!rpc.monitor locald rfoo)
(thread-sleep! 1.0)
(stop-rpc-server! remoted)
(check (<- ((!rpc.disconnect r) r)) => rfoo)
(stop-rpc-server! locald))))
(def (hello-stream-server remoted N)
(!!rpc.register remoted 'foo hello::proto)
(let lp ()
(<- ((!hello.hello-stream _ k)
(let lp2 ((n 0))
(if (< n N)
(begin
(!!yield n k)
(<- ((!continue (eq? k))
(lp2 (1+ n)))))
(begin
(!!end k)
(lp)))))
((!rpc.shutdown)
(void)))))
(def (hello-stream-close-server remoted)
(!!rpc.register remoted 'foo hello::proto)
(let lp ()
(<- ((!hello.hello-stream _ k)
(let (source @source)
(let lp2 ((n 0))
(!!yield n k)
(<- ((!continue k)
(lp2 (1+ n)))
((!close k)
(!!end source k)
(lp))))))
((!rpc.shutdown)
(void)))))
(def actor-rpc-stream-test
(test-suite "test :std/actor RPC stream"
(test-case "test basic RPC stream"
(def N 5)
(def remoted (start-rpc-server! rpc-server-address5))
(def hellod (spawn hello-stream-server remoted N))
(thread-sleep! 0.1)
(check (!!rpc.resolve remoted 'foo) => hellod)
(def locald (start-rpc-server!))
(def rfoo
(rpc-connect locald 'foo rpc-server-address5 hello::proto))
(let (k (gensym 'stream))
(send-message/timeout rfoo (make-!stream (make-hello.hello-stream "stream") k) 1)
(let lp ((n 0))
(when (< n N)
(<- ((!yield x (eq? k))
(check x => n)
(!!continue k)
(lp (1+ n))))))
(let (end (thread-receive))
(check end ? message?)
(check (message-e end) ? !end?)))
(stop-rpc-server! remoted)
(stop-rpc-server! locald))
(test-case "test RPC stream ports"
(def N 5)
(def remoted (start-rpc-server! rpc-server-address6))
(def hellod (spawn hello-stream-server remoted N))
(thread-sleep! 0.1)
(check (!!rpc.resolve remoted 'foo) => hellod)
(def locald (start-rpc-server!))
(def rfoo
(rpc-connect locald 'foo rpc-server-address6 hello::proto))
(let ((values inp close) (!!hello.hello-stream rfoo "stream"))
(let lp ((n 0))
(when (< n N)
(check (read inp) => n)
(lp (1+ n))))
(check (read inp) ? eof-object?))
(stop-rpc-server! remoted)
(stop-rpc-server! locald))
(test-case "test RPC stream close"
(def remoted (start-rpc-server! rpc-server-address9))
(def hellod (spawn hello-stream-close-server remoted))
(thread-sleep! 0.1)
(check (!!rpc.resolve remoted 'foo) => hellod)
(def locald (start-rpc-server!))
(def rfoo
(rpc-connect locald 'foo rpc-server-address9 hello::proto))
(let ((values inp close) (!!hello.hello-stream rfoo "stream"))
(close)
(let lp ((n 0))
(let (next (read inp))
(unless (eof-object? next)
(lp (1+ n)))))
(check (read inp) ? eof-object?))
(stop-rpc-server! remoted)
(stop-rpc-server! locald))))