Mercurial > lasercutter
diff src/clojure/test_clojure/agents.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/test_clojure/agents.clj Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,120 @@ 1.4 +; Copyright (c) Rich Hickey. All rights reserved. 1.5 +; The use and distribution terms for this software are covered by the 1.6 +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.7 +; which can be found in the file epl-v10.html at the root of this distribution. 1.8 +; By using this software in any fashion, you are agreeing to be bound by 1.9 +; the terms of this license. 1.10 +; You must not remove this notice, or any other, from this software. 1.11 + 1.12 +;; Author: Shawn Hoover 1.13 + 1.14 +(ns clojure.test-clojure.agents 1.15 + (:use clojure.test)) 1.16 + 1.17 +(deftest handle-all-throwables-during-agent-actions 1.18 + ;; Bug fixed in r1198; previously hung Clojure or didn't report agent errors 1.19 + ;; after OutOfMemoryError, yet wouldn't execute new actions. 1.20 + (let [agt (agent nil)] 1.21 + (send agt (fn [state] (throw (Throwable. "just testing Throwables")))) 1.22 + (try 1.23 + ;; Let the action finish; eat the "agent has errors" error that bubbles up 1.24 + (await-for 100 agt) 1.25 + (catch RuntimeException _)) 1.26 + (is (instance? Throwable (first (agent-errors agt)))) 1.27 + (is (= 1 (count (agent-errors agt)))) 1.28 + 1.29 + ;; And now send an action that should work 1.30 + (clear-agent-errors agt) 1.31 + (is (= nil @agt)) 1.32 + (send agt nil?) 1.33 + (is (true? (await-for 100 agt))) 1.34 + (is (true? @agt)))) 1.35 + 1.36 +(deftest default-modes 1.37 + (is (= :fail (error-mode (agent nil)))) 1.38 + (is (= :continue (error-mode (agent nil :error-handler println))))) 1.39 + 1.40 +(deftest continue-handler 1.41 + (let [err (atom nil) 1.42 + agt (agent 0 :error-mode :continue :error-handler #(reset! err %&))] 1.43 + (send agt /) 1.44 + (is (true? (await-for 100 agt))) 1.45 + (is (= 0 @agt)) 1.46 + (is (nil? (agent-error agt))) 1.47 + (is (= agt (first @err))) 1.48 + (is (true? (instance? ArithmeticException (second @err)))))) 1.49 + 1.50 +(deftest fail-handler 1.51 + (let [err (atom nil) 1.52 + agt (agent 0 :error-mode :fail :error-handler #(reset! err %&))] 1.53 + (send agt /) 1.54 + (Thread/sleep 100) 1.55 + (is (true? (instance? ArithmeticException (agent-error agt)))) 1.56 + (is (= 0 @agt)) 1.57 + (is (= agt (first @err))) 1.58 + (is (true? (instance? ArithmeticException (second @err)))) 1.59 + (is (thrown? RuntimeException (send agt inc))))) 1.60 + 1.61 +(deftest restart-no-clear 1.62 + (let [p (promise) 1.63 + agt (agent 1 :error-mode :fail)] 1.64 + (send agt (fn [v] @p)) 1.65 + (send agt /) 1.66 + (send agt inc) 1.67 + (send agt inc) 1.68 + (deliver p 0) 1.69 + (Thread/sleep 100) 1.70 + (is (= 0 @agt)) 1.71 + (is (= ArithmeticException (class (agent-error agt)))) 1.72 + (restart-agent agt 10) 1.73 + (is (true? (await-for 100 agt))) 1.74 + (is (= 12 @agt)) 1.75 + (is (nil? (agent-error agt))))) 1.76 + 1.77 +(deftest restart-clear 1.78 + (let [p (promise) 1.79 + agt (agent 1 :error-mode :fail)] 1.80 + (send agt (fn [v] @p)) 1.81 + (send agt /) 1.82 + (send agt inc) 1.83 + (send agt inc) 1.84 + (deliver p 0) 1.85 + (Thread/sleep 100) 1.86 + (is (= 0 @agt)) 1.87 + (is (= ArithmeticException (class (agent-error agt)))) 1.88 + (restart-agent agt 10 :clear-actions true) 1.89 + (is (true? (await-for 100 agt))) 1.90 + (is (= 10 @agt)) 1.91 + (is (nil? (agent-error agt))) 1.92 + (send agt inc) 1.93 + (is (true? (await-for 100 agt))) 1.94 + (is (= 11 @agt)) 1.95 + (is (nil? (agent-error agt))))) 1.96 + 1.97 +(deftest invalid-restart 1.98 + (let [p (promise) 1.99 + agt (agent 2 :error-mode :fail :validator even?)] 1.100 + (is (thrown? RuntimeException (restart-agent agt 4))) 1.101 + (send agt (fn [v] @p)) 1.102 + (send agt (partial + 2)) 1.103 + (send agt (partial + 2)) 1.104 + (deliver p 3) 1.105 + (Thread/sleep 100) 1.106 + (is (= 2 @agt)) 1.107 + (is (= IllegalStateException (class (agent-error agt)))) 1.108 + (is (thrown? RuntimeException (restart-agent agt 5))) 1.109 + (restart-agent agt 6) 1.110 + (is (true? (await-for 100 agt))) 1.111 + (is (= 10 @agt)) 1.112 + (is (nil? (agent-error agt))))) 1.113 + 1.114 +; http://clojure.org/agents 1.115 + 1.116 +; agent 1.117 +; deref, @-reader-macro, agent-errors 1.118 +; send send-off clear-agent-errors 1.119 +; await await-for 1.120 +; set-validator get-validator 1.121 +; add-watch remove-watch 1.122 +; shutdown-agents 1.123 +