From 7a4b5131912816fd383af60efee5fbeb56343970 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Fri, 19 Mar 2010 12:59:46 -0700 Subject: [PATCH 1/2] Add a test to blobstore_test.clj to detect for corrupt downloads. --- .../clojure/org/jclouds/blobstore_test.clj | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/blobstore/src/test/clojure/org/jclouds/blobstore_test.clj b/blobstore/src/test/clojure/org/jclouds/blobstore_test.clj index 3d0205156a..fe11de637d 100644 --- a/blobstore/src/test/clojure/org/jclouds/blobstore_test.clj +++ b/blobstore/src/test/clojure/org/jclouds/blobstore_test.clj @@ -91,3 +91,49 @@ ;; TODO: more tests involving blob-specific functions +(deftest corruption-hunt + (let [service "transient" + account "" + secret-key "" + container-name "test" + name "work-file" + upload-filename "/home/phil/work-file" + total-downloads 100 + threads 10 + blob-s (blobstore service account secret-key)] + + ;; upload + (create-container blob-s container-name) + (when-not (blob-exists? blob-s container-name name) + (create-blob blob-s container-name name + (java.io.File. upload-filename))) + + ;; download + (let [total (atom total-downloads)] + (defn new-agent [] + (agent name)) + + (defn dl-and-restart [file] + (when-not (<= @total 0) + (with-open [baos (java.io.ByteArrayOutputStream.)] + (try + (download-blob blob-s container-name file baos) + (catch Exception e + (with-open [of (java.io.FileOutputStream. + (java.io.File/createTempFile "jclouds" ".dl"))] + (.write of (.toByteArray baos))) + (throw e)))) + (swap! total dec) + (send *agent* dl-and-restart) + file)) + + (defn start-agents [] + (let [agents (map (fn [_] (new-agent)) + (range threads))] + (doseq [a agents] + (send-off a dl-and-restart)) + agents)) + + (let [agents (start-agents)] + (apply await agents) + (is (every? nil? (map agent-errors agents))))))) From 0d0b4e849f98362e6a2ba6658acc4fe57d9f15a8 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Fri, 19 Mar 2010 15:41:07 -0700 Subject: [PATCH 2/2] Factor out service args into a fixture so other modules can reuse tests. --- .../clojure/org/jclouds/blobstore_test.clj | 74 ++++++++----------- 1 file changed, 30 insertions(+), 44 deletions(-) diff --git a/blobstore/src/test/clojure/org/jclouds/blobstore_test.clj b/blobstore/src/test/clojure/org/jclouds/blobstore_test.clj index fe11de637d..913db4d843 100644 --- a/blobstore/src/test/clojure/org/jclouds/blobstore_test.clj +++ b/blobstore/src/test/clojure/org/jclouds/blobstore_test.clj @@ -4,49 +4,39 @@ (:import [org.jclouds.blobstore BlobStoreContextFactory] [java.io ByteArrayOutputStream])) -(def stub-context (.createContext (BlobStoreContextFactory.) "transient" "" "")) -(def stub-blobstore (.getBlobStore stub-context)) +(defn clean-stub-fixture + "This should allow basic tests to easily be run with another service." + [service account key & options] + (fn [f] + (with-blobstore [(apply blobstore service account key options)] + (doseq [container (containers)] + (delete-container (.getName container))) + (f)))) -(defn clean-stub-fixture [f] - (with-blobstore [stub-blobstore] - (doseq [container (containers)] - (delete-container (.getName container))) - (f))) - -(use-fixtures :each clean-stub-fixture) +(use-fixtures :each (clean-stub-fixture "transient" "" "")) (deftest blobstore?-test - (is (blobstore? stub-blobstore))) - -(deftest blobstore-context?-test - (is (blobstore-context? stub-context))) - -(deftest blobstore-context-test - (is (= stub-context (blobstore-context stub-blobstore)))) + (is (blobstore? *blobstore*))) (deftest as-blobstore-test (is (blobstore? (blobstore "transient" "user" "password"))) - (is (blobstore? (as-blobstore stub-blobstore))) - (is (blobstore? (as-blobstore stub-context)))) - -(deftest with-blobstore-test - (with-blobstore [stub-blobstore] - (is (= stub-blobstore *blobstore*)))) + (is (blobstore? (as-blobstore *blobstore*))) + (is (blobstore? (as-blobstore (blobstore-context *blobstore*))))) (deftest create-existing-container-test - (is (not (container-exists? stub-blobstore ""))) + (is (not (container-exists? *blobstore* ""))) (is (not (container-exists? ""))) - (is (create-container stub-blobstore "fred")) - (is (container-exists? stub-blobstore "fred"))) + (is (create-container *blobstore* "fred")) + (is (container-exists? *blobstore* "fred"))) (deftest create-container-test - (is (create-container stub-blobstore "fred")) - (is (container-exists? stub-blobstore "fred"))) + (is (create-container *blobstore* "fred")) + (is (container-exists? *blobstore* "fred"))) (deftest containers-test - (is (empty? (containers stub-blobstore))) - (is (create-container stub-blobstore "fred")) - (is (= 1 (count (containers stub-blobstore))))) + (is (empty? (containers *blobstore*))) + (is (create-container *blobstore* "fred")) + (is (= 1 (count (containers *blobstore*))))) (deftest list-container-test (is (create-container "container")) @@ -92,28 +82,24 @@ ;; TODO: more tests involving blob-specific functions (deftest corruption-hunt - (let [service "transient" - account "" - secret-key "" - container-name "test" + (let [container-name "test" name "work-file" - upload-filename "/home/phil/work-file" total-downloads 100 - threads 10 - blob-s (blobstore service account secret-key)] + threads 10] ;; upload - (create-container blob-s container-name) - (when-not (blob-exists? blob-s container-name name) - (create-blob blob-s container-name name - (java.io.File. upload-filename))) + (create-container container-name) + (when-not (blob-exists? container-name name) + (let [data-stream (java.io.ByteArrayOutputStream.)] + (dotimes [i 5000000] (.write data-stream i)) + (create-blob container-name name (.toByteArray data-stream)))) ;; download (let [total (atom total-downloads)] (defn new-agent [] (agent name)) - (defn dl-and-restart [file] + (defn dl-and-restart [blob-s file] (when-not (<= @total 0) (with-open [baos (java.io.ByteArrayOutputStream.)] (try @@ -124,14 +110,14 @@ (.write of (.toByteArray baos))) (throw e)))) (swap! total dec) - (send *agent* dl-and-restart) + (send *agent* (partial dl-and-restart blob-s)) file)) (defn start-agents [] (let [agents (map (fn [_] (new-agent)) (range threads))] (doseq [a agents] - (send-off a dl-and-restart)) + (send-off a (partial dl-and-restart *blobstore*))) agents)) (let [agents (start-agents)]