~subsetpark/bagatto

ref: HEAD bagatto/test/threads.janet -rw-r--r-- 870 bytes
dabc238c — Zach Smith Update docs to reflect template syntax 3 days ago
                                                                                
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
(import testament :prefix "" :exit true)
(import /src/threads)

(defn thread-init [f]
  (thread/new (fn [parent] (f parent))))

(defn dist-job [i]
  (fn [parent] (:send parent [:res i true])))

(deftest distribute
  (let [jobs (seq [x :range [0 10]] (dist-job x))
        out (threads/distribute jobs thread-init)]
    (is (== @{0 true 2 true 1 true 3 true 5 true 6 true 4 true 8 true 7 true 9 true} out))))

(defn pool-init [] ())
(defn do-work
  [parent self msg _state]
  (match msg
    :die (do (:send parent self)
           (thread/exit))
    [:consume n] (spit (string "test/support/pool/" n) "ok")))

(deftest pool
  (os/shell "rm test/support/pool/*")

  (threads/demand-pipeline (range 0 10)
                           pool-init
                           do-work)
  # 10 + 1 placeholder file
  (is (= (length (os/dir "test/support/pool")) 11)))

(run-tests!)