|
7 | 7 |
|
8 | 8 | (deftest test-dispatcher-balanced ()
|
9 | 9 | "Make sure all the workers share equally for similarly timed jobs."
|
10 |
| - (define-actor counter ((c 0)) (increment) |
11 |
| - (sleep 0.025) |
12 |
| - (incf c increment)) |
13 |
| - |
14 |
| - (let ((actor (counter :workers 2))) |
15 |
| - (dotimes (i 40) (send actor 1)) |
16 |
| - (close-actor actor) |
17 |
| - (let ((stores (join-actor actor))) |
18 |
| - (is (~= (first stores) (second stores)))))) |
| 10 | + (let ((lock (bt2:make-lock))) |
| 11 | + (define-actor counter ((c 0)) (increment) |
| 12 | + (bt2:with-lock-held (lock) |
| 13 | + (incf c increment))) |
| 14 | + |
| 15 | + (let ((actor (counter :workers 2))) |
| 16 | + (bt2:with-lock-held (lock) |
| 17 | + (dotimes (i 40) (send actor 1))) |
| 18 | + (close-actor actor) |
| 19 | + (let ((stores (join-actor actor))) |
| 20 | + (is (~= (first stores) (second stores))))))) |
19 | 21 |
|
20 | 22 | (deftest test-dispatcher-unbalanced ()
|
21 | 23 | "Make sure a worker with slow jobs doesn't fill up."
|
22 |
| - (define-actor sleeper ((c 0)) (time) |
23 |
| - (sleep time) |
24 |
| - (incf c)) |
25 |
| - |
26 |
| - (let ((actor (sleeper :workers 2))) |
27 |
| - (dotimes (i 5) |
28 |
| - (send (first (thespis::dispatcher-workers actor)) 5)) |
29 |
| - (dotimes (i 19) (sleep 0.001) (send actor 0)) |
30 |
| - (is (= 20 (ask actor 0))) |
31 |
| - (destroy-actor actor))) |
| 24 | + (define-actor waiter ((c 0)) (lock) |
| 25 | + (if (bt2:lockp lock) |
| 26 | + (bt2:with-lock-held (lock) |
| 27 | + (incf c)) |
| 28 | + (incf c))) |
| 29 | + |
| 30 | + (let ((actor (waiter :workers 2)) |
| 31 | + (lock (bt2:make-lock))) |
| 32 | + (bt2:with-lock-held (lock) |
| 33 | + (dotimes (i 10) |
| 34 | + (send (first (thespis::dispatcher-workers actor)) lock)) |
| 35 | + (dotimes (i 5) |
| 36 | + (send actor nil))) |
| 37 | + (is (equal '((10 5)) (close-and-join-actors actor))))) |
32 | 38 |
|
33 | 39 | (deftest test-dispatcher-registry ()
|
34 | 40 | "Test registering a dispatcher with a global name."
|
|
53 | 59 |
|
54 | 60 | (send :my-counter 1)
|
55 | 61 | (send :my-counter 1)
|
56 |
| - (is (= 6 (reduce #'+ (join-actor (close-actor :my-counter)))))) |
| 62 | + |
| 63 | + (is (= 6 (reduce #'+ |
| 64 | + (mapcar (lambda (x) (or x 0)) |
| 65 | + (join-actor (close-actor :my-counter))))))) |
57 | 66 |
|
58 | 67 | (deftest test-dispatcher-close-and-join ()
|
59 | 68 | (define-actor counter ((c 0)) (increment)
|
|
65 | 74 |
|
66 | 75 | (deftest test-closed-actor ()
|
67 | 76 | "Sending messages to a closed dispatcher signals an error."
|
68 |
| - (define-actor closer () (x) |
69 |
| - x) |
| 77 | + (define-actor closer () (x) x) |
70 | 78 |
|
71 | 79 | (let ((actor (closer :workers 2)))
|
72 | 80 | (close-actor actor)
|
|
0 commit comments