begin adding some unit tests
This commit is contained in:
parent
d4c5784b4a
commit
19cfc50762
|
@ -4,6 +4,11 @@
|
|||
[cheshire "5.6.1"]
|
||||
[prismatic/dommy "1.1.0"]]
|
||||
:profiles {:provided
|
||||
{:dependencies
|
||||
[[org.clojure/clojure "1.8.0"]
|
||||
[org.clojure/clojurescript "1.8.51"]]}})
|
||||
{:dependencies
|
||||
[[org.clojure/clojure "1.8.0"]
|
||||
[org.clojure/clojurescript "1.8.51"]]}
|
||||
|
||||
:dev
|
||||
{:dependencies [[pjstadig/humane-test-output "0.8.0"]]
|
||||
:injections [(require 'pjstadig.humane-test-output)
|
||||
(pjstadig.humane-test-output/activate!)]}})
|
||||
|
|
|
@ -52,23 +52,8 @@
|
|||
(let [size (alength (.getBytes json "UTF-8"))]
|
||||
(str size "\n" json)))
|
||||
|
||||
;; make sure the root URI for channels starts with a / for route matching
|
||||
(defn- standard-base
|
||||
[s]
|
||||
(let [wofirst (if (= \/ (first s))
|
||||
(apply str (rest s))
|
||||
s)
|
||||
wolast (if (= \/ (last wofirst))
|
||||
(apply str (butlast wofirst))
|
||||
wofirst)]
|
||||
(str "/" wolast)))
|
||||
|
||||
;; @todo to test file
|
||||
#_(assert (= (repeat 4 "/foo")
|
||||
(map standard-base ["foo" "/foo" "foo/" "/foo"])))
|
||||
|
||||
;; type preserving drop upto for queueus
|
||||
(defn- drop-queue
|
||||
(defn drop-queue
|
||||
[queue id]
|
||||
(let [head (peek queue)]
|
||||
(if-not head
|
||||
|
@ -80,7 +65,7 @@
|
|||
|
||||
;; Key value pairs do not always come ordered by request number.
|
||||
;; E.g. {req0_key1 val01, req1_key1 val11, req0_key2 val02, req1_key2 val12}
|
||||
(defn- transform-url-data
|
||||
(defn transform-url-data
|
||||
[data]
|
||||
(let [ofs (get data "ofs" "0")
|
||||
pieces (dissoc data "count" "ofs")]
|
||||
|
@ -92,12 +77,6 @@
|
|||
(sort-by first) ;; order by request number so that messages are recieved on server in same order
|
||||
(map #(into {} (map second (val %)))))}))
|
||||
|
||||
#_(assert (= {:ofs 0 :maps [{"x" "3" "y" "10"} {"abc" "def"}]}
|
||||
(transform-url-data {"count" "2"
|
||||
"ofs" "0"
|
||||
"req0_x" "3"
|
||||
"req0_y" "10"
|
||||
"req1_abc" "def"})))
|
||||
;; maps are URL Encoded
|
||||
;;;; URL Encoded data:
|
||||
;;{ count: '2',
|
||||
|
@ -114,7 +93,7 @@
|
|||
;; "req1_abc" "def"}
|
||||
;; =>
|
||||
;;{:ofs 0 :maps [{"x" "3" "y" "10"},{"abc": "def"}]}
|
||||
(defn- get-maps
|
||||
(defn get-maps
|
||||
[req]
|
||||
(let [data (:form-params req)]
|
||||
(when-not (zero? (count data))
|
||||
|
@ -140,13 +119,13 @@
|
|||
[p]
|
||||
(str "[" (first p) "," (second p) "]"))
|
||||
|
||||
(defn- decode-map
|
||||
(defn decode-map
|
||||
[m]
|
||||
(if (contains? m "__edn")
|
||||
(edn/read-string (get m "__edn"))
|
||||
m))
|
||||
|
||||
(defn- encode-map
|
||||
(defn encode-map
|
||||
[data]
|
||||
{"__edn" (pr-str data)})
|
||||
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
(ns net.thegeez.browserchannel.common)
|
||||
|
||||
(defn ->queue
|
||||
[& x]
|
||||
(apply conj clojure.lang.PersistentQueue/EMPTY x))
|
|
@ -0,0 +1,105 @@
|
|||
(ns net.thegeez.browserchannel.server.array-buffer-tests
|
||||
(:use
|
||||
clojure.test
|
||||
net.thegeez.browserchannel.common
|
||||
net.thegeez.browserchannel.server)
|
||||
(:import (net.thegeez.browserchannel.server ArrayBuffer)))
|
||||
|
||||
(deftest basics-test
|
||||
(let [empty-array (ArrayBuffer. 0 0 (->queue) (->queue))]
|
||||
(is (= (to-flush empty-array)
|
||||
nil))
|
||||
(is (= (last-acknowledged-id empty-array)
|
||||
0))
|
||||
(is (= (outstanding-bytes empty-array)
|
||||
0))))
|
||||
|
||||
(deftest queue-tests
|
||||
(let [ab (-> (ArrayBuffer. 0 0 (->queue) (->queue))
|
||||
(queue "one"))]
|
||||
(is (= (first (to-flush ab))
|
||||
[[1 "one"]]))
|
||||
(is (= (last-acknowledged-id ab)
|
||||
0))
|
||||
(is (= (outstanding-bytes ab)
|
||||
3)))
|
||||
|
||||
(let [ab (-> (ArrayBuffer. 0 0 (->queue) (->queue))
|
||||
(queue nil))]
|
||||
(is (= (first (to-flush ab))
|
||||
[[1 nil]]))
|
||||
(is (= (last-acknowledged-id ab)
|
||||
0))
|
||||
(is (= (outstanding-bytes ab)
|
||||
0)))
|
||||
|
||||
(let [ab (-> (ArrayBuffer. 0 0 (->queue) (->queue))
|
||||
(queue "one")
|
||||
(queue "two")
|
||||
(queue "three"))]
|
||||
(is (= (first (to-flush ab))
|
||||
[[1 "one"]
|
||||
[2 "two"]
|
||||
[3 "three"]]))
|
||||
(is (= (last-acknowledged-id ab)
|
||||
0))
|
||||
(is (= (outstanding-bytes ab)
|
||||
11)))
|
||||
|
||||
(let [ab (-> (ArrayBuffer. 0 0 (->queue) (->queue))
|
||||
(queue "one")
|
||||
(queue "two")
|
||||
(queue "three"))
|
||||
flushed (second (to-flush ab))]
|
||||
(is (= (first (to-flush ab))
|
||||
[[1 "one"]
|
||||
[2 "two"]
|
||||
[3 "three"]]))
|
||||
(is (= (first (to-flush flushed))
|
||||
nil))))
|
||||
|
||||
(deftest acknowledge-tests
|
||||
(let [ab (-> (ArrayBuffer. 0 0 (->queue) (->queue))
|
||||
(queue "one")
|
||||
(queue "two")
|
||||
(queue "three")
|
||||
(queue "four")
|
||||
(queue "five"))]
|
||||
(is (= (first (to-flush ab))
|
||||
[[1 "one"]
|
||||
[2 "two"]
|
||||
[3 "three"]
|
||||
[4 "four"]
|
||||
[5 "five"]]))
|
||||
(is (= (last-acknowledged-id ab)
|
||||
0))
|
||||
|
||||
(let [ack-ab (acknowledge-id ab 2)]
|
||||
(is (= (first (to-flush ack-ab))
|
||||
[[3 "three"]
|
||||
[4 "four"]
|
||||
[5 "five"]]))
|
||||
(is (= (last-acknowledged-id ack-ab)
|
||||
2))
|
||||
(is (= (outstanding-bytes ack-ab)
|
||||
13)))
|
||||
|
||||
(let [ack-ab (acknowledge-id ab 0)]
|
||||
(is (= (first (to-flush ack-ab))
|
||||
[[1 "one"]
|
||||
[2 "two"]
|
||||
[3 "three"]
|
||||
[4 "four"]
|
||||
[5 "five"]]))
|
||||
(is (= (last-acknowledged-id ack-ab)
|
||||
0))
|
||||
(is (= (outstanding-bytes ack-ab)
|
||||
19)))
|
||||
|
||||
(let [ack-ab (acknowledge-id ab 6)]
|
||||
(is (= (first (to-flush ack-ab))
|
||||
nil))
|
||||
(is (= (last-acknowledged-id ack-ab)
|
||||
6))
|
||||
(is (= (outstanding-bytes ack-ab)
|
||||
0)))))
|
|
@ -0,0 +1,74 @@
|
|||
(ns net.thegeez.browserchannel.server.utils-test
|
||||
(:use
|
||||
clojure.test
|
||||
net.thegeez.browserchannel.common
|
||||
net.thegeez.browserchannel.server))
|
||||
|
||||
|
||||
(deftest request-param-parse-tests
|
||||
(is (= (transform-url-data
|
||||
{"count" "2"
|
||||
"ofs" "0"
|
||||
"req0_x" "3"
|
||||
"req0_y" "10"
|
||||
"req1_abc" "def"})
|
||||
{:ofs 0
|
||||
:maps [{"x" "3"
|
||||
"y" "10"}
|
||||
{"abc" "def"}]}))
|
||||
|
||||
(is (= (get-maps
|
||||
{:form-params {"count" "2"
|
||||
"ofs" "0"
|
||||
"req0_x" "3"
|
||||
"req0_y" "10"
|
||||
"req1_abc" "def"}})
|
||||
[{"x" "3"
|
||||
"y" "10"}
|
||||
{"abc" "def"}]))
|
||||
(is (= (get-maps
|
||||
{:form-params {"count" "0"
|
||||
"ofs" "0"}})
|
||||
[]))
|
||||
(is (= (get-maps
|
||||
{:form-params {}})
|
||||
nil)))
|
||||
|
||||
(deftest drop-queue-tests
|
||||
(let [q (->queue
|
||||
[1 "one"]
|
||||
[2 "two"]
|
||||
[3 "three"]
|
||||
[4 "four"]
|
||||
[5 "five"])]
|
||||
|
||||
(= (drop-queue q 2)
|
||||
(->queue
|
||||
[3 "three"]
|
||||
[4 "four"]
|
||||
[5 "five"]))
|
||||
|
||||
(= (drop-queue q 0)
|
||||
q)
|
||||
|
||||
(= (drop-queue q 5)
|
||||
(->queue))))
|
||||
|
||||
(deftest encoded-map-tests
|
||||
(= (encode-map "hello, world")
|
||||
{"__edn" "\"hello, world\""})
|
||||
(= (decode-map {"__edn" "\"hello, world\""})
|
||||
"hello, world")
|
||||
|
||||
(= (encode-map {:foo "bar"})
|
||||
{"__edn" "{:foo \"bar\"}"})
|
||||
(= (decode-map {"__edn" "{:foo \"bar\"}"})
|
||||
{:foo "bar"})
|
||||
|
||||
(= (encode-map nil)
|
||||
{"__edn" "nil"})
|
||||
(= (decode-map {"__edn" "nil"})
|
||||
nil)
|
||||
|
||||
(= (decode-map {:foo "bar"})
|
||||
{:foo "bar"}))
|
Reference in a new issue