public
Created

  • Download Gist
event_bus_test.clj
Clojure
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
(ns chatter-box.event-bus-test
#+cljs
(:require-macros [cemerick.cljs.test :refer [deftest testing is]]
[cljs.core.async.macros :refer [go]])
(:require
[chatter-box.event-bus :as bus :refer [create-bus Component init accept-message? get-channel]]
#+clj [clojure.test :as test :refer [deftest testing is]]
#+cljs [cemerick.cljs.test :as t]
#+clj [clojure.core.async :as a :refer [alts! <! >! chan timeout go]]
#+cljs [cljs.core.async :as a :refer [alts! <! >! chan timeout]]))
 
 
(defrecord TestComponent [channel filt]
Component
(init [_ ch] ch)
(accept-message? [_ msg] (filt msg))
(get-channel [_] channel))
 
 
(defn basic-test [filt check message]
(let [my-co (TestComponent. (chan) filt)
bus-chan (create-bus my-co)]
(go (>! bus-chan message)
(let [[msg _] (alts! [(timeout 100) (:channel my-co)])]
(is (check msg message))))))
 
 
(deftest bus-echo-test
(testing "bus basic echo test"
(basic-test (fn [msg] true) = "testing")))
 
 
(deftest bus-filter-test
(testing "negative filter"
(basic-test :good-key
(fn [in out] (nil? in))
{:bad-key true}))
(testing "positive filter"
(basic-test :good-key
=
{:good-key true})))
 
 
(deftest bus-multi-test
(testing "multiple component test"
(let [co1 (TestComponent. (chan) :good-key)
co2 (TestComponent. (chan) :good-key)
co3 (TestComponent. (chan) :bad-key)
bus-chan (create-bus co1 co2 co2)
message {:good-key true}]
(go
(>! bus-chan message)
(let [[msg _] (alts! [(timeout 100) (:channel co1)])]
(is (= msg message)))
(let [[msg _] (alts! [(timeout 100) (:channel co2)])]
(is (= msg message)))
(let [[msg _] (alts! [(timeout 100) (:channel co3)])]
(is (nil? msg)))))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.