1 ;;; async-test --- async.el-related tests
3 ;; Copyright (C) 2012 John Wiegley
5 ;; Author: John Wiegley <jwiegley@gmail.com>
6 ;; Created: 10 Jul 2012
9 ;; X-URL: https://github.com/jwiegley/emacs-async
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.
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.
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.
28 ;; Contains tests for all the async modules.
32 (add-to-list 'load-path (file-name-directory (or load-file-name (buffer-file-name))))
39 (defun async-test-1 ()
41 (message "Starting async-test-1...")
43 ;; What to do in the child process
45 (message "This is a test")
49 ;; What to do when it finishes
51 (message "Async process done, result should be 222: %s" result)))
52 (message "Starting async-test-1...done"))
54 (defun async-test-2 ()
56 (message "Starting async-test-2...")
57 (let ((proc (async-start
58 ;; What to do in the child process
60 (message "This is a test")
63 (message "I'm going to do some work here")
65 (message "Async process done, result should be 222: %s"
68 (defun async-test-3 ()
70 (message "Starting async-test-3...")
72 ;; What to do in the child process
74 (message "This is a test")
76 (error "Error in child process")
79 ;; What to do when it finishes
81 (message "Async process done, result should be 222: %s" result)))
82 (message "Starting async-test-1...done"))
84 (defun async-test-4 ()
86 (message "Starting async-test-4...")
87 (async-start-process "sleep" "sleep"
88 ;; What to do when it finishes
90 (message "Sleep done, exit code was %d"
91 (process-exit-status proc)))
93 (message "Starting async-test-4...done"))
95 (defun async-test-5 ()
97 (message "Starting async-test-5...")
100 ;; What to do in the child process
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)))
111 ;; What to do when it finishes
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"
118 (async-send proc :goodbye "everyone"))
119 (message "Starting async-test-5...done"))
121 (defun async-test-6 ()
123 (message "Starting async-test-6...")
125 ;; What to do in the child process
127 ,(async-inject-variables "\\`user-mail-address\\'")
128 (format "user-mail-address = %s" user-mail-address))
130 ;; What to do when it finishes
132 (message "Async process done: %s" result))))
134 (defun async-test-7 ()
136 (message "Starting async-test-7...")
141 (cl-loop repeat 2 collect
142 (async-start (lambda () t)))))
145 (cl-loop repeat 2 collect
146 (async-start '(lambda () t)))))
149 (cl-loop repeat 2 collect
150 (async-start `(lambda () ,(* 150 2)))))))
152 (message "Finished async-test-7 successfully."))
154 (defsubst async-file-contents (file)
155 "Return the contents of FILE, as a string."
157 (insert-file-contents file)
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
165 (let* ((temp-file (make-temp-file "async-do-copy-file-test"))
166 (temp-file2 (concat temp-file ".target")))
170 (insert "async-do-copy-file-test")
171 (write-region (point-min) (point-max) temp-file))
173 (let* ((async-file-use-native-commands use-native-commands)
174 (future (if synchronously
175 (copy-file temp-file temp-file2
179 preserve-selinux-context)
180 (async-copy-file temp-file temp-file2
184 preserve-selinux-context
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))))
193 (should (file-readable-p temp-file2))
195 (should (equal "async-do-copy-file-test"
196 (async-file-contents temp-file2)))))
198 (if (file-exists-p temp-file) (delete-file temp-file))
199 (if (file-exists-p temp-file2) (delete-file temp-file2)))))
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))
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))))
216 (defsubst async-file-make-file (file contents)
217 "Create a new FILE with the given CONTENTS."
220 (write-region (point-min) (point-max) file)))
222 (defun* async-do-copy-directory-test (keep-time parents copy-contents
223 &key use-native-commands
225 (let* ((temp-dir (async-file-make-temp-dir "async-do-copy-directory-test"))
226 (temp-dir2 (concat temp-dir ".target")))
229 (async-file-make-file (expand-file-name "foo" temp-dir) "foo")
230 (async-file-make-file (expand-file-name "bar" temp-dir) "bar")
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)
237 (let* ((async-file-use-native-commands use-native-commands)
238 (future (if synchronously
239 (copy-directory temp-dir temp-dir2
243 (async-copy-directory temp-dir temp-dir2
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'
256 (if (and parents copy-contents)
257 (should (file-directory-p temp-dir2)))
259 (let* ((target (if copy-contents
261 (expand-file-name (file-name-nondirectory temp-dir)
263 (foo-file (expand-file-name "foo" target))
264 (bar-file (expand-file-name "bar" target)))
266 (should (file-readable-p foo-file))
267 (should (file-readable-p bar-file))
269 (should (equal "foo" (async-file-contents foo-file)))
270 (should (equal "bar" (async-file-contents bar-file))))))
272 (if (file-directory-p temp-dir) (delete-directory temp-dir t))
273 (if (file-directory-p temp-dir2) (delete-directory temp-dir2 t)))))
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))
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))
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))
302 (provide 'async-test)
304 ;;; async-test.el ends here