Skip to content

Commit 9107bc3

Browse files
committed
Use aio-wait-for with ERT for testing
1 parent c8b2476 commit 9107bc3

File tree

2 files changed

+83
-122
lines changed

2 files changed

+83
-122
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ clean:
1010
rm -f aio.elc aio-test.elc
1111

1212
check: aio-test.elc
13-
$(EMACS) -Q -batch -L . -l aio-test.elc -f aio-run-tests
13+
$(EMACS) -batch -Q -L . -l aio-test.elc -f ert-run-tests-batch
1414

1515
.SUFFIXES: .el .elc
1616
.el.elc:

aio-test.el

+82-121
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
;;; Commentary:
44

5-
;; emacs -Q -nw -L . -l aio-test.elc -f aio-run-tests
5+
;; $ emacs -batch -Q -l aio-test.elc -f ert-run-tests-batch
66

77
;; Because the tests run as async functions, the test suite cannot be
88
;; run in batch mode. The results will be written into a buffer and
@@ -12,87 +12,45 @@
1212

1313
(require 'aio)
1414
(require 'cl-lib)
15-
16-
(defvar aio-tests ())
17-
18-
(defvar aio-test-total nil)
19-
(defvar aio-test-failures nil)
20-
21-
(defun aio-run-tests ()
22-
(setf aio-test-total 0
23-
aio-test-failures 0)
24-
(let* ((buffer (get-buffer-create "*aio-results*"))
25-
(promises
26-
(cl-loop for (name . test) in (reverse aio-tests)
27-
for promise = (funcall test)
28-
for cb =
29-
(let ((test-name name))
30-
(lambda (value)
31-
(princ (format "%S: %S\n" test-name (funcall value)))))
32-
collect promise
33-
do (aio-listen (aio-catch promise) cb)))
34-
(done (aio-all promises)))
35-
(switch-to-buffer buffer)
36-
(erase-buffer)
37-
(aio-listen done (lambda (_)
38-
(with-current-buffer buffer
39-
(princ "*** aio-run-tests complete ***\n")
40-
(princ (format "%d / %d PASS\n"
41-
(- aio-test-total aio-test-failures)
42-
aio-test-total))
43-
(kill-emacs))))
44-
(while t
45-
(accept-process-output))))
46-
47-
(defun aio--should (result value-a value-b expr-a expr-b)
48-
(cl-incf aio-test-total)
49-
(unless result
50-
(cl-incf aio-test-failures)
51-
(princ (format "FAIL:\n%S\n= %S\n%S\n= %S\n"
52-
expr-a value-a expr-b value-b))))
53-
54-
(defmacro aio-should (cmp expr-a expr-b)
55-
(let ((value-a (make-symbol "a"))
56-
(value-b (make-symbol "b")))
57-
`(let ((,value-a ,expr-a)
58-
(,value-b ,expr-b))
59-
(aio--should (,cmp ,value-a ,value-b)
60-
,value-a ,value-b ',expr-a ',expr-b))))
61-
62-
(defmacro aio-deftest (name _ &rest body)
63-
(declare (indent defun))
64-
`(push (cons ',name (aio-lambda () ,@body)) aio-tests))
15+
(require 'ert)
6516

6617
;; Tests:
6718

68-
(aio-deftest sleep ()
69-
(let ((start (float-time)))
70-
(dotimes (i 3)
71-
(aio-should eql i (aio-await (aio-sleep 0.5 i))))
72-
(aio-should > (- (float-time) start) 1.4)))
73-
74-
(aio-deftest chain ()
75-
(let ((sub (aio-lambda (result) (aio-await (aio-sleep .1 result)))))
76-
(aio-should eq :a (aio-await (funcall sub :a)))
77-
(aio-should eq :b (aio-await (funcall sub :b)))))
78-
79-
(aio-deftest timeout ()
80-
(let ((sleep (aio-sleep 1.0 t))
81-
(timeout (aio-timeout 0.5))
82-
(select (aio-make-select)))
83-
(aio-select-add select sleep)
84-
(aio-select-add select timeout)
85-
(aio-should equal
86-
'(:error aio-timeout . 0.5)
87-
(aio-await (aio-catch (aio-await (aio-select select))))))
88-
(let ((sleep (aio-sleep 0.1 t))
89-
(timeout (aio-timeout 0.5))
90-
(select (aio-make-select)))
91-
(aio-select-add select sleep)
92-
(aio-select-add select timeout)
93-
(aio-should equal
94-
'(:success . t)
95-
(aio-await (aio-catch (aio-await (aio-select select)))))))
19+
(ert-deftest sleep ()
20+
(aio-wait-for
21+
(aio-with-async
22+
(let ((start (float-time)))
23+
(dotimes (i 3)
24+
(should (eql i(aio-await (aio-sleep 0.5 i)))))
25+
(should (> (- (float-time) start)
26+
1.4))))))
27+
28+
(ert-deftest chain ()
29+
(aio-wait-for
30+
(aio-with-async
31+
(let ((sub (aio-lambda (result) (aio-await (aio-sleep .1 result)))))
32+
(should (eq :a (aio-await (funcall sub :a))))
33+
(should (eq :b (aio-await (funcall sub :b))))))))
34+
35+
(ert-deftest timeout ()
36+
(aio-wait-for
37+
(aio-with-async
38+
(let ((sleep (aio-sleep 1.0 t))
39+
(timeout (aio-timeout 0.5))
40+
(select (aio-make-select)))
41+
(aio-select-add select sleep)
42+
(aio-select-add select timeout)
43+
(let ((winner (aio-await (aio-select select))))
44+
(should (equal '(:error aio-timeout . 0.5)
45+
(aio-await (aio-catch winner))))))
46+
(let ((sleep (aio-sleep 0.1 t))
47+
(timeout (aio-timeout 0.5))
48+
(select (aio-make-select)))
49+
(aio-select-add select sleep)
50+
(aio-select-add select timeout)
51+
(let ((winner (aio-await (aio-select select))))
52+
(should (equal '(:success . t)
53+
(aio-await (aio-catch winner)))))))))
9654

9755
(defun aio-test--shuffle (values)
9856
"Return a shuffled copy of VALUES."
@@ -102,45 +60,48 @@
10260
do (cl-rotatef (aref v i) (aref v j))
10361
finally return (append v nil))))
10462

105-
(aio-deftest sleep-sort ()
106-
(let* ((values (cl-loop for i from 5 to 60
107-
collect (/ i 20.0) into values
108-
finally return (aio-test--shuffle values)))
109-
(count (length values))
110-
(select (aio-make-select))
111-
(promises (dolist (value values)
112-
(aio-select-add select (aio-sleep value value))))
113-
(last 0.0))
114-
(dotimes (_ count)
115-
(let ((promise (aio-await (aio-select select))))
116-
(let ((result (aio-await promise)))
117-
(aio-should > result last)
118-
(setf last result))))))
119-
120-
(aio-deftest process-sentinel ()
121-
(let ((process (start-process-shell-command "test" nil "exit 0"))
122-
(sentinel (aio-make-callback)))
123-
(setf (process-sentinel process) (car sentinel))
124-
(aio-should equal
125-
"finished\n"
126-
(nth 1 (aio-chain (cdr sentinel))))))
127-
128-
(aio-deftest process-filter ()
129-
(let* ((command
130-
(if (eq system-type 'windows-nt)
131-
(mapconcat #'identity
132-
'("echo a b c"
133-
"waitfor /t 1 x 2>nul"
134-
"echo 1 2 3"
135-
"waitfor /t 1 x 2>nul")
136-
"&")
137-
"echo a b c; sleep 1; echo 1 2 3; sleep 1"))
138-
(process (start-process-shell-command "test" nil command))
139-
(filter (aio-make-callback)))
140-
(setf (process-filter process) (car filter))
141-
(aio-should equal
142-
"a b c\n"
143-
(nth 1 (aio-chain (cdr filter))))
144-
(aio-should equal
145-
"1 2 3\n"
146-
(nth 1 (aio-chain (cdr filter))))))
63+
(ert-deftest sleep-sort ()
64+
(aio-wait-for
65+
(aio-with-async
66+
(let* ((values (cl-loop for i from 5 to 60
67+
collect (/ i 20.0) into values
68+
finally return (aio-test--shuffle values)))
69+
(count (length values))
70+
(select (aio-make-select))
71+
(promises (dolist (value values)
72+
(aio-select-add select (aio-sleep value value))))
73+
(last 0.0))
74+
(dotimes (_ count :done)
75+
(let ((promise (aio-await (aio-select select))))
76+
(let ((result (aio-await promise)))
77+
(should (> result last))
78+
(setf last result))))))))
79+
80+
(ert-deftest process-sentinel ()
81+
(aio-wait-for
82+
(aio-with-async
83+
(let ((process (start-process-shell-command "test" nil "exit 0"))
84+
(sentinel (aio-make-callback)))
85+
(setf (process-sentinel process) (car sentinel))
86+
(should (equal "finished\n"
87+
(nth 1 (aio-chain (cdr sentinel)))))))))
88+
89+
(ert-deftest process-filter ()
90+
(aio-wait-for
91+
(aio-with-async
92+
(let* ((command
93+
(if (eq system-type 'windows-nt)
94+
(mapconcat #'identity
95+
'("echo a b c"
96+
"waitfor /t 1 x 2>nul"
97+
"echo 1 2 3"
98+
"waitfor /t 1 x 2>nul")
99+
"&")
100+
"echo a b c; sleep 1; echo 1 2 3; sleep 1"))
101+
(process (start-process-shell-command "test" nil command))
102+
(filter (aio-make-callback)))
103+
(setf (process-filter process) (car filter))
104+
(should (equal "a b c\n"
105+
(nth 1 (aio-chain (cdr filter)))))
106+
(should (equal "1 2 3\n"
107+
(nth 1 (aio-chain (cdr filter)))))))))

0 commit comments

Comments
 (0)