Permalink
Browse files

Closeable client.

  • Loading branch information...
1 parent a0154f5 commit cf970aff2b4470f70e5044bdd508be572ee80968 @mmcgrana committed Jan 9, 2010
Showing with 34 additions and 32 deletions.
  1. +13 −11 src/fleetdb/client.clj
  2. +21 −21 test/fleetdb/client_test.clj
View
@@ -2,7 +2,8 @@
(:require [clj-json :as json])
(:import (java.net Socket)
(java.io OutputStreamWriter BufferedWriter
- InputStreamReader BufferedReader)
+ InputStreamReader BufferedReader
+ Closeable)
(clojure.lang IFn ILookup)))
(defn- doquery [#^BufferedWriter writer #^BufferedReader reader q]
@@ -17,6 +18,14 @@
(throw (Exception. #^String result))))
(throw (Exception. "No response from server.")))))
+(defn query [client q]
+ (doquery (:writer client) (:reader client) q))
+
+(defn close [client]
+ (.close #^BufferedReader (:reader client))
+ (.close #^BufferedWriter (:writer client))
+ (.close #^Socket (:socket client)))
+
(defn connect [& [options]]
(let [host (get options :host "127.0.0.1")
port (get options :port 3400)
@@ -31,14 +40,7 @@
(.setSoTimeout socket (int (* timeout 1000))))
(when password
(doquery writer reader ["auth" password]))
- (proxy [IFn ILookup] []
+ (proxy [IFn ILookup Closeable] []
(invoke [q] (doquery writer reader q))
- (valAt [k] (attrs k)))))
-
-(defn query [client q]
- (doquery (:writer client) (:reader client) q))
-
-(defn close [client]
- (.close #^BufferedReader (:reader client))
- (.close #^BufferedWriter (:writer client))
- (.close #^Socket (:socket client)))
+ (valAt [k] (attrs k))
+ (close [] (close attrs)))))
@@ -1,61 +1,61 @@
(ns fleetdb.client-test
(:require (fleetdb [client :as client]))
+ (:import (java.io Closeable))
(:use (clj-unit core)))
-(defmacro with-client [name & body]
- `(let [~name (client/connect)]
- (try
- (~name ["delete" "elems"])
- ~@body
- (finally
- (client/close ~name)))))
+(defmacro with-client [[name opts] & body]
+ (let [tname (with-meta name {:tag 'Closeable})]
+ `(with-open [~tname (client/connect ~opts)]
+ (~name ["delete" "elems"]))))
-(deftest "nondefault initialization options"
- (let [c (client/connect {:host "localhost" :port 3400})]
- (client/close c)))
+(deftest "default options"
+ (with-open [#^Closeable client (client/connect)]))
+
+(deftest "nondefault options"
+ (with-open [#^Closeable client (client/connect {:port 3401})]))
(deftest "ping"
- (with-client client
+ (with-client [client nil]
(assert= "pong" (client ["ping"]))))
(deftest "malformed query"
- (with-client client
+ (with-client [client nil]
(assert-throws #"Malformed query"
(client ["foo"]))))
(deftest "valid query"
- (with-client client
+ (with-client [client nil]
(let [r1 (client ["insert" "elems" {"id" 1}])
r2 (client ["select" "elems"])]
(assert= r1 1)
(assert= r2 [{"id" 1}]))))
(deftest "timeout"
(assert-throws #"Read timed out"
- (let [client (client/connect {:port 3402 :timeout 1})]
+ (with-client [client {:port 3402 :timeout 1}]
(client ["ping"]))))
(deftest "missing server"
(assert-throws #"Connection refused"
- (client/connect {:port 3403})))
+ (with-client [client {:port 3403}])))
(deftest "auth success"
- (let [client (client/connect {:port 3401 :password "pass"})]
+ (with-client [client {:port 3401 :password "pass"}]
(assert= "pong" (client ["ping"]))))
(deftest "auth ommission"
- (let [client (client/connect {:port 3401})]
- (assert-throws #"auth needed"
+ (assert-throws #"auth needed"
+ (with-client [client {:port 3401}]
(client ["ping"]))))
(deftest "auth failure"
(assert-throws #"auth rejected"
- (client/connect {:port 3401 :password "wrong"})))
+ (with-client [client {:port 3401 :password "wrong"}])))
(deftest "client/query"
- (with-client client
+ (with-client [client nil]
(assert= "pong" (client/query client ["ping"]))))
(deftest "client attrs"
- (with-client client
+ (with-client [client nil]
(assert= 3400 (:port client))))

0 comments on commit cf970af

Please sign in to comment.