]> code.delx.au - gnu-emacs-elpa/blob - async-test.el
Add more comprehensive testing for anti-closure feature
[gnu-emacs-elpa] / async-test.el
1 ;;; async-test --- async.el-related tests
2
3 ;; Copyright (C) 2012 John Wiegley
4
5 ;; Author: John Wiegley <jwiegley@gmail.com>
6 ;; Created: 10 Jul 2012
7 ;; Version: 1.0
8 ;; Keywords: async
9 ;; X-URL: https://github.com/jwiegley/emacs-async
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25 \f
26 ;;; Commentary:
27
28 ;; Contains tests for all the async modules.
29 \f
30 ;;; Code:
31
32 (add-to-list 'load-path (file-name-directory (or load-file-name (buffer-file-name))))
33 (require 'async)
34 (require 'async-file)
35
36 (eval-when-compile
37 (require 'cl))
38
39 (defun async-test-1 ()
40 (interactive)
41 (message "Starting async-test-1...")
42 (async-start
43 ;; What to do in the child process
44 (lambda ()
45 (message "This is a test")
46 (sleep-for 3)
47 222)
48
49 ;; What to do when it finishes
50 (lambda (result)
51 (message "Async process done, result should be 222: %s" result)))
52 (message "Starting async-test-1...done"))
53
54 (defun async-test-2 ()
55 (interactive)
56 (message "Starting async-test-2...")
57 (let ((proc (async-start
58 ;; What to do in the child process
59 (lambda ()
60 (message "This is a test")
61 (sleep-for 3)
62 222))))
63 (message "I'm going to do some work here")
64 ;; ....
65 (message "Async process done, result should be 222: %s"
66 (async-get proc))))
67
68 (defun async-test-3 ()
69 (interactive)
70 (message "Starting async-test-3...")
71 (async-start
72 ;; What to do in the child process
73 (lambda ()
74 (message "This is a test")
75 (sleep-for 3)
76 (error "Error in child process")
77 222)
78
79 ;; What to do when it finishes
80 (lambda (result)
81 (message "Async process done, result should be 222: %s" result)))
82 (message "Starting async-test-1...done"))
83
84 (defun async-test-4 ()
85 (interactive)
86 (message "Starting async-test-4...")
87 (async-start-process "sleep" "sleep"
88 ;; What to do when it finishes
89 (lambda (proc)
90 (message "Sleep done, exit code was %d"
91 (process-exit-status proc)))
92 "3")
93 (message "Starting async-test-4...done"))
94
95 (defun async-test-5 ()
96 (interactive)
97 (message "Starting async-test-5...")
98 (let ((proc
99 (async-start
100 ;; What to do in the child process
101 (lambda ()
102 (message "This is a test, sending message")
103 (async-send :hello "world")
104 ;; wait for a message
105 (let ((msg (async-receive)))
106 (message "Child got message: %s"
107 (plist-get msg :goodbye)))
108 (sleep-for 3)
109 222)
110
111 ;; What to do when it finishes
112 (lambda (result)
113 (if (async-message-p result)
114 (message "Got hello from child process: %s"
115 (plist-get result :hello))
116 (message "Async process done, result should be 222: %s"
117 result))))))
118 (async-send proc :goodbye "everyone"))
119 (message "Starting async-test-5...done"))
120
121 (defun async-test-6 ()
122 (interactive)
123 (message "Starting async-test-6...")
124 (async-start
125 ;; What to do in the child process
126 `(lambda ()
127 ,(async-inject-variables "\\`user-mail-address\\'")
128 (format "user-mail-address = %s" user-mail-address))
129
130 ;; What to do when it finishes
131 (lambda (result)
132 (message "Async process done: %s" result))))
133
134 (defun async-test-7 ()
135 (interactive)
136 (message "Starting async-test-7...")
137 (eval
138 '(progn
139 (print
140 (mapcar #'async-get
141 (cl-loop repeat 2 collect
142 (async-start (lambda () t)))))
143 (print
144 (mapcar #'async-get
145 (cl-loop repeat 2 collect
146 (async-start '(lambda () t)))))
147 (print
148 (mapcar #'async-get
149 (cl-loop repeat 2 collect
150 (async-start `(lambda () ,(* 150 2)))))))
151 t)
152 (message "Finished async-test-7 successfully."))
153
154 (defsubst async-file-contents (file)
155 "Return the contents of FILE, as a string."
156 (with-temp-buffer
157 (insert-file-contents file)
158 (buffer-string)))
159
160 (defun* async-do-copy-file-test (ok-if-already-exists
161 keep-time preserve-uid-gid
162 preserve-selinux-context
163 &key use-native-commands
164 synchronously)
165 (let* ((temp-file (make-temp-file "async-do-copy-file-test"))
166 (temp-file2 (concat temp-file ".target")))
167 (unwind-protect
168 (progn
169 (with-temp-buffer
170 (insert "async-do-copy-file-test")
171 (write-region (point-min) (point-max) temp-file))
172
173 (let* ((async-file-use-native-commands use-native-commands)
174 (future (if synchronously
175 (copy-file temp-file temp-file2
176 ok-if-already-exists
177 keep-time
178 preserve-uid-gid
179 preserve-selinux-context)
180 (async-copy-file temp-file temp-file2
181 ok-if-already-exists
182 keep-time
183 preserve-uid-gid
184 preserve-selinux-context
185 :callback nil))))
186 (unless synchronously
187 (if use-native-commands
188 (let ((proc (async-get future)))
189 (should (processp proc))
190 (should (equal 'exit (process-status proc))))
191 (should (equal (async-get future) nil))))
192
193 (should (file-readable-p temp-file2))
194
195 (should (equal "async-do-copy-file-test"
196 (async-file-contents temp-file2)))))
197
198 (if (file-exists-p temp-file) (delete-file temp-file))
199 (if (file-exists-p temp-file2) (delete-file temp-file2)))))
200
201 (ert-deftest async-copy-file-lisp-sync-1 ()
202 (async-do-copy-file-test t t t nil :synchronously t))
203 (ert-deftest async-copy-file-lisp-1 ()
204 (async-do-copy-file-test t t t nil :use-native-commands nil))
205 (ert-deftest async-copy-file-native-1 ()
206 (async-do-copy-file-test t t t nil :use-native-commands t))
207
208 (defsubst async-file-make-temp-dir (prefix)
209 "Make a temporary directory using PREFIX.
210 Return the name of the directory."
211 (let ((dir (make-temp-name
212 (expand-file-name prefix temporary-file-directory))))
213 (make-directory dir)
214 dir))
215
216 (defsubst async-file-make-file (file contents)
217 "Create a new FILE with the given CONTENTS."
218 (with-temp-buffer
219 (insert contents)
220 (write-region (point-min) (point-max) file)))
221
222 (defun* async-do-copy-directory-test (keep-time parents copy-contents
223 &key use-native-commands
224 synchronously)
225 (let* ((temp-dir (async-file-make-temp-dir "async-do-copy-directory-test"))
226 (temp-dir2 (concat temp-dir ".target")))
227 (unwind-protect
228 (progn
229 (async-file-make-file (expand-file-name "foo" temp-dir) "foo")
230 (async-file-make-file (expand-file-name "bar" temp-dir) "bar")
231
232 ;; Shouldn't the parents argument cause this to happen when needed?
233 ;; But if the following is wrapped with "unless parents", even
234 ;; `async-copy-directory-lisp-sync-2' fails.
235 (make-directory temp-dir2)
236
237 (let* ((async-file-use-native-commands use-native-commands)
238 (future (if synchronously
239 (copy-directory temp-dir temp-dir2
240 keep-time
241 parents
242 copy-contents)
243 (async-copy-directory temp-dir temp-dir2
244 keep-time
245 parents
246 copy-contents
247 :callback nil))))
248 (unless synchronously
249 (if use-native-commands
250 (let ((proc (async-get future)))
251 (should (processp proc))
252 (should (equal 'exit (process-status proc))))
253 ;; Ignore the return value from `copy-directory'
254 (async-get future)))
255
256 (if (and parents copy-contents)
257 (should (file-directory-p temp-dir2)))
258
259 (let* ((target (if copy-contents
260 temp-dir2
261 (expand-file-name (file-name-nondirectory temp-dir)
262 temp-dir2)))
263 (foo-file (expand-file-name "foo" target))
264 (bar-file (expand-file-name "bar" target)))
265
266 (should (file-readable-p foo-file))
267 (should (file-readable-p bar-file))
268
269 (should (equal "foo" (async-file-contents foo-file)))
270 (should (equal "bar" (async-file-contents bar-file))))))
271
272 (if (file-directory-p temp-dir) (delete-directory temp-dir t))
273 (if (file-directory-p temp-dir2) (delete-directory temp-dir2 t)))))
274
275 (ert-deftest async-copy-directory-lisp-sync-1 ()
276 (async-do-copy-directory-test t nil nil :synchronously t))
277 (ert-deftest async-copy-directory-lisp-sync-2 ()
278 (async-do-copy-directory-test t t nil :synchronously t))
279 (ert-deftest async-copy-directory-lisp-sync-3 ()
280 (async-do-copy-directory-test t nil t :synchronously t))
281 (ert-deftest async-copy-directory-lisp-sync-4 ()
282 (async-do-copy-directory-test t t t :synchronously t))
283
284 (ert-deftest async-copy-directory-lisp-1 ()
285 (async-do-copy-directory-test t nil nil :use-native-commands nil))
286 (ert-deftest async-copy-directory-lisp-2 ()
287 (async-do-copy-directory-test t t nil :use-native-commands nil))
288 (ert-deftest async-copy-directory-lisp-3 ()
289 (async-do-copy-directory-test t nil t :use-native-commands nil))
290 (ert-deftest async-copy-directory-lisp-4 ()
291 (async-do-copy-directory-test t t t :use-native-commands nil))
292
293 (ert-deftest async-copy-directory-native-1 ()
294 (async-do-copy-directory-test t nil nil :use-native-commands t))
295 (ert-deftest async-copy-directory-native-2 ()
296 (async-do-copy-directory-test t t nil :use-native-commands t))
297 (ert-deftest async-copy-directory-native-3 ()
298 (async-do-copy-directory-test t nil t :use-native-commands t))
299 (ert-deftest async-copy-directory-native-4 ()
300 (async-do-copy-directory-test t t t :use-native-commands t))
301
302 (provide 'async-test)
303
304 ;;; async-test.el ends here