-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.cljs
93 lines (86 loc) · 2.79 KB
/
tests.cljs
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
(ns ^:figwheel-always pull2chan.tests
(:require [cljs.core.async :refer (chan >! <! put! close! timeout)]
[figwheel.client :as fw]
cljs.test
[pull2chan.core :refer (pull->chan chan->pull)])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]
[cljs.test :refer [deftest is testing run-tests async]]))
(def pull (js/require "pull-stream"))
(deftest test-pull->chan
(async done
(let [source (. pull (values #js [1 2 3]))
ch (pull->chan source)]
(go (is (= (<! ch) 1))
(is (= (<! ch) 2))
(is (= (<! ch) 3))
(is (= (<! ch) nil)) ; channel is closed after being drained
(done)
))))
(deftest test-chan->pull
(async done
(let [ch (chan)
source (chan->pull ch)
vals [1 2 3 4 5 6 7 8 9]
sink (. pull (collect (fn [err actual]
(is (= (js->clj actual) vals))
(done))))
]
(go
(>! ch 1)
(>! ch 2)
(>! ch 3)
(>! ch 4)
(>! ch 5)
(>! ch 6)
(>! ch 7)
(>! ch 8)
(>! ch 9)
(close! ch)
(is (not (>! ch "won't happen")))
)
(pull source sink)
)))
(deftest test-timing-chan->pull
(async done
(let [ch (chan)
source (chan->pull ch)
sink (. pull (collect (fn [err vs]
(is (= (js->clj vs) [1 2 3 4]))
(done))))]
(pull source sink)
(go (>! ch 1)
(<! (timeout 100))
(>! ch 4)
(close! ch))
(go (>! ch 2)
(>! ch 3)))))
(deftest test-chan->pull->chan
(async done
(let [ch1 (chan)
source (chan->pull ch1)
ch2 (pull->chan source)]
(go (>! ch1 1)
(>! ch1 2)
(close! ch1)
)
(go (is (= (<! ch2) 1))
(is (= (<! ch2) 2))
(is (= (<! ch2) nil))
(done)
))))
(deftest test-pull->chan->pull
(async done
(let [take-count (atom 0)
nums (vec (range 10000))
source (-> (. pull (values (clj->js nums)))
pull->chan
chan->pull)
sink (. pull (collect
(fn [err v]
(is (= nums (js->clj v)))
(done)
)))]
(pull source sink)
)))
(enable-console-print!)
(run-tests)