Skip to content

Commit

Permalink
Moving away from the macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
Damon Snyder committed Dec 12, 2010
1 parent 948b073 commit 7b1b941
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 36 deletions.
69 changes: 52 additions & 17 deletions src/beanstalk/core.clj
Expand Up @@ -70,6 +70,27 @@
([request data response clauses] `(do ~request ~data (cmd-reply-case ~response ~clauses))))


; handler => (fn [beanstalk reply] {:payload (.read beanstalk)})
; handler => (fn [beanstalk reply] {:payload (.read beanstalk) :id (Integer. (:data reply))})
(defn protocol-response [beanstalk reply expected handler]
(condp = (:response reply)
expected (handler beanstalk reply)
(clojure.contrib.condition/raise
:message (str "Unexpected response from sever: " (:response reply)))))

(defn protocol-case
([beanstalk expected handle-response]
(let [reply (parse-reply (.read beanstalk))]
(beanstalk-debug (str "* <= " reply))
(protocol-response beanstalk reply expected handle-response)))
([beanstalk cmd-str data expected handle-response]
(do (.write beanstalk cmd-str)
(.write beanstalk data)
(protocol-case beanstalk expected handle-response)))
([beanstalk cmd-str expected handle-response]
(do (.write beanstalk cmd-str)
(protocol-case beanstalk expected handle-response))))

(defprotocol BeanstalkObject
(close [this] "Close the connection")
(read [this] "Read from beanstalk")
Expand All @@ -84,24 +105,38 @@
(close [this] (.close socket))
(read [this] (stream-read reader))
(write [this msg] (stream-write writer msg))
(stats [this] (cmd-reply-case
(.write this (beanstalk-cmd :stats))
(.read this)
(:ok (.read this))))
(stats [this]
(protocol-case
this
(beanstalk-cmd :stats)
:ok
(fn [b r] {:payload (.read b)})))
(put [this pri del ttr length data]
(Integer. (cmd-reply-case
(.write this (beanstalk-cmd :put pri del ttr length))
(.write this (beanstalk-data data))
(.read this)
(:inserted false))))
(use [this tube] (cmd-reply-case
(.write this (beanstalk-cmd :use tube))
(.read this)
(:using false)))
(reserve [this] (cmd-reply-case
(.write this (beanstalk-cmd :reserve))
(.read this)
(:reserved false))))
(protocol-case
this
(beanstalk-cmd :put pri del ttr length)
(beanstalk-data data)
:inserted
(fn [b r] {:id (Integer. (:data r))})))
(use [this tube]
(protocol-case
this
(beanstalk-cmd :use tube)
:using
(fn [b r] (let [tube (:data r)] {:payload tube :tube tube}))))
(reserve [this]
(protocol-case
this
(beanstalk-cmd :reserve)
:reserved
(fn [b r] {:payload (.read b)
; response is "<id> <length>"
:id (Integer. (first (clojure.string/split (:data r) #"\s+")) )}))))

;(cmd-reply-case
; (.write this (beanstalk-cmd :reserve))
; (.read this)
; (:reserved false))))

(defn new-beanstalk
([host port] (let [s (java.net.Socket. host port)]
Expand Down
50 changes: 31 additions & 19 deletions test/beanstalk/core_test.clj
Expand Up @@ -7,26 +7,38 @@
(instance? beanstalk.core.Beanstalk b)))))

(deftest test-stats
(binding [ beanstalk.core/*debug* true ]
(let [b (new-beanstalk)
result (.stats b)]
(is (not (nil? result)))
(is (> (.length result) 0)))))
(binding [ beanstalk.core/*debug* true ]
(let [b (new-beanstalk)
result (.stats b)]
(is (not (nil? (:payload result))))
(is (> (.length (:payload result)) 0)))))

(deftest test-put
(binding [ beanstalk.core/*debug* true ]
(let [b (new-beanstalk)
result (.put b 0 0 10 5 "hello")]
(println (str "result => " result))
(is (not (nil? result)))
; should be the job id
(is (> result 0)))))
(binding [ beanstalk.core/*debug* true ]
(let [b (new-beanstalk)
result (.put b 0 0 10 5 "hello")]
(println (str "result => " result))
(is (not (nil? result)))
(println (str "put returned id " (:id result)))
; should be the job id
(is (> (:id result) 0)))))

(deftest test-use
(binding [ beanstalk.core/*debug* true ]
(let [b (new-beanstalk)
result (.use b "test-tube")]
(println (str "result => " result))
(is (not (nil? result)))
; should be the job id
(is (= result "test-tube")))))
(binding [ beanstalk.core/*debug* true ]
(let [b (new-beanstalk)
result (.use b "test-tube")]
(println (str "result => " result))
(is (not (nil? result)))
(is (= (:payload result) "test-tube")))))

(deftest test-reserve
(binding [ beanstalk.core/*debug* true ]
(let [b (new-beanstalk)
use-r (.use b "test-tube")
put-r (.put b 0 0 10 5 "hello")
reserve-r (.reserve b)]
(println (str "result use => " use-r))
(println (str "result put => " put-r))
(println (str "result reserve => " reserve-r))
(is (not (nil? reserve-r)))
(is (= (:payload reserve-r) "hello")))))

0 comments on commit 7b1b941

Please sign in to comment.