/
async-test.el
304 lines (261 loc) · 11.2 KB
/
async-test.el
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
;;; async-test --- async.el-related tests
;; Copyright (C) 2012 John Wiegley
;; Author: John Wiegley <jwiegley@gmail.com>
;; Created: 10 Jul 2012
;; Version: 1.0
;; Keywords: async
;; X-URL: https://github.com/jwiegley/emacs-async
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Contains tests for all the async modules.
;;; Code:
(add-to-list 'load-path (file-name-directory (or load-file-name (buffer-file-name))))
(require 'async)
(require 'async-file)
(eval-when-compile
(require 'cl))
(defun async-test-1 ()
(interactive)
(message "Starting async-test-1...")
(async-start
;; What to do in the child process
(lambda ()
(message "This is a test")
(sleep-for 3)
222)
;; What to do when it finishes
(lambda (result)
(message "Async process done, result should be 222: %s" result)))
(message "Starting async-test-1...done"))
(defun async-test-2 ()
(interactive)
(message "Starting async-test-2...")
(let ((proc (async-start
;; What to do in the child process
(lambda ()
(message "This is a test")
(sleep-for 3)
222))))
(message "I'm going to do some work here")
;; ....
(message "Async process done, result should be 222: %s"
(async-get proc))))
(defun async-test-3 ()
(interactive)
(message "Starting async-test-3...")
(async-start
;; What to do in the child process
(lambda ()
(message "This is a test")
(sleep-for 3)
(error "Error in child process")
222)
;; What to do when it finishes
(lambda (result)
(message "Async process done, result should be 222: %s" result)))
(message "Starting async-test-1...done"))
(defun async-test-4 ()
(interactive)
(message "Starting async-test-4...")
(async-start-process "sleep" "sleep"
;; What to do when it finishes
(lambda (proc)
(message "Sleep done, exit code was %d"
(process-exit-status proc)))
"3")
(message "Starting async-test-4...done"))
(defun async-test-5 ()
(interactive)
(message "Starting async-test-5...")
(let ((proc
(async-start
;; What to do in the child process
(lambda ()
(message "This is a test, sending message")
(async-send :hello "world")
;; wait for a message
(let ((msg (async-receive)))
(message "Child got message: %s"
(plist-get msg :goodbye)))
(sleep-for 3)
222)
;; What to do when it finishes
(lambda (result)
(if (async-message-p result)
(message "Got hello from child process: %s"
(plist-get result :hello))
(message "Async process done, result should be 222: %s"
result))))))
(async-send proc :goodbye "everyone"))
(message "Starting async-test-5...done"))
(defun async-test-6 ()
(interactive)
(message "Starting async-test-6...")
(async-start
;; What to do in the child process
`(lambda ()
,(async-inject-variables "\\`user-mail-address\\'")
(format "user-mail-address = %s" user-mail-address))
;; What to do when it finishes
(lambda (result)
(message "Async process done: %s" result))))
(defun async-test-7 ()
(interactive)
(message "Starting async-test-7...")
(eval
'(progn
(print
(mapcar #'async-get
(cl-loop repeat 2 collect
(async-start (lambda () t)))))
(print
(mapcar #'async-get
(cl-loop repeat 2 collect
(async-start '(lambda () t)))))
(print
(mapcar #'async-get
(cl-loop repeat 2 collect
(async-start `(lambda () ,(* 150 2)))))))
t)
(message "Finished async-test-7 successfully."))
(defsubst async-file-contents (file)
"Return the contents of FILE, as a string."
(with-temp-buffer
(insert-file-contents file)
(buffer-string)))
(defun* async-do-copy-file-test (ok-if-already-exists
keep-time preserve-uid-gid
preserve-selinux-context
&key use-native-commands
synchronously)
(let* ((temp-file (make-temp-file "async-do-copy-file-test"))
(temp-file2 (concat temp-file ".target")))
(unwind-protect
(progn
(with-temp-buffer
(insert "async-do-copy-file-test")
(write-region (point-min) (point-max) temp-file))
(let* ((async-file-use-native-commands use-native-commands)
(future (if synchronously
(copy-file temp-file temp-file2
ok-if-already-exists
keep-time
preserve-uid-gid
preserve-selinux-context)
(async-copy-file temp-file temp-file2
ok-if-already-exists
keep-time
preserve-uid-gid
preserve-selinux-context
:callback nil))))
(unless synchronously
(if use-native-commands
(let ((proc (async-get future)))
(should (processp proc))
(should (equal 'exit (process-status proc))))
(should (equal (async-get future) nil))))
(should (file-readable-p temp-file2))
(should (equal "async-do-copy-file-test"
(async-file-contents temp-file2)))))
(if (file-exists-p temp-file) (delete-file temp-file))
(if (file-exists-p temp-file2) (delete-file temp-file2)))))
(ert-deftest async-copy-file-lisp-sync-1 ()
(async-do-copy-file-test t t t nil :synchronously t))
(ert-deftest async-copy-file-lisp-1 ()
(async-do-copy-file-test t t t nil :use-native-commands nil))
(ert-deftest async-copy-file-native-1 ()
(async-do-copy-file-test t t t nil :use-native-commands t))
(defsubst async-file-make-temp-dir (prefix)
"Make a temporary directory using PREFIX.
Return the name of the directory."
(let ((dir (make-temp-name
(expand-file-name prefix temporary-file-directory))))
(make-directory dir)
dir))
(defsubst async-file-make-file (file contents)
"Create a new FILE with the given CONTENTS."
(with-temp-buffer
(insert contents)
(write-region (point-min) (point-max) file)))
(defun* async-do-copy-directory-test (keep-time parents copy-contents
&key use-native-commands
synchronously)
(let* ((temp-dir (async-file-make-temp-dir "async-do-copy-directory-test"))
(temp-dir2 (concat temp-dir ".target")))
(unwind-protect
(progn
(async-file-make-file (expand-file-name "foo" temp-dir) "foo")
(async-file-make-file (expand-file-name "bar" temp-dir) "bar")
;; Shouldn't the parents argument cause this to happen when needed?
;; But if the following is wrapped with "unless parents", even
;; `async-copy-directory-lisp-sync-2' fails.
(make-directory temp-dir2)
(let* ((async-file-use-native-commands use-native-commands)
(future (if synchronously
(copy-directory temp-dir temp-dir2
keep-time
parents
copy-contents)
(async-copy-directory temp-dir temp-dir2
keep-time
parents
copy-contents
:callback nil))))
(unless synchronously
(if use-native-commands
(let ((proc (async-get future)))
(should (processp proc))
(should (equal 'exit (process-status proc))))
;; Ignore the return value from `copy-directory'
(async-get future)))
(if (and parents copy-contents)
(should (file-directory-p temp-dir2)))
(let* ((target (if copy-contents
temp-dir2
(expand-file-name (file-name-nondirectory temp-dir)
temp-dir2)))
(foo-file (expand-file-name "foo" target))
(bar-file (expand-file-name "bar" target)))
(should (file-readable-p foo-file))
(should (file-readable-p bar-file))
(should (equal "foo" (async-file-contents foo-file)))
(should (equal "bar" (async-file-contents bar-file))))))
(if (file-directory-p temp-dir) (delete-directory temp-dir t))
(if (file-directory-p temp-dir2) (delete-directory temp-dir2 t)))))
(ert-deftest async-copy-directory-lisp-sync-1 ()
(async-do-copy-directory-test t nil nil :synchronously t))
(ert-deftest async-copy-directory-lisp-sync-2 ()
(async-do-copy-directory-test t t nil :synchronously t))
(ert-deftest async-copy-directory-lisp-sync-3 ()
(async-do-copy-directory-test t nil t :synchronously t))
(ert-deftest async-copy-directory-lisp-sync-4 ()
(async-do-copy-directory-test t t t :synchronously t))
(ert-deftest async-copy-directory-lisp-1 ()
(async-do-copy-directory-test t nil nil :use-native-commands nil))
(ert-deftest async-copy-directory-lisp-2 ()
(async-do-copy-directory-test t t nil :use-native-commands nil))
(ert-deftest async-copy-directory-lisp-3 ()
(async-do-copy-directory-test t nil t :use-native-commands nil))
(ert-deftest async-copy-directory-lisp-4 ()
(async-do-copy-directory-test t t t :use-native-commands nil))
(ert-deftest async-copy-directory-native-1 ()
(async-do-copy-directory-test t nil nil :use-native-commands t))
(ert-deftest async-copy-directory-native-2 ()
(async-do-copy-directory-test t t nil :use-native-commands t))
(ert-deftest async-copy-directory-native-3 ()
(async-do-copy-directory-test t nil t :use-native-commands t))
(ert-deftest async-copy-directory-native-4 ()
(async-do-copy-directory-test t t t :use-native-commands t))
(provide 'async-test)
;;; async-test.el ends here