Mercurial > lasercutter
view 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 source
1 ; Copyright (c) Rich Hickey. All rights reserved.2 ; The use and distribution terms for this software are covered by the3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)4 ; which can be found in the file epl-v10.html at the root of this distribution.5 ; By using this software in any fashion, you are agreeing to be bound by6 ; the terms of this license.7 ; You must not remove this notice, or any other, from this software.9 ;; Author: Shawn Hoover11 (ns clojure.test-clojure.agents12 (:use clojure.test))14 (deftest handle-all-throwables-during-agent-actions15 ;; Bug fixed in r1198; previously hung Clojure or didn't report agent errors16 ;; after OutOfMemoryError, yet wouldn't execute new actions.17 (let [agt (agent nil)]18 (send agt (fn [state] (throw (Throwable. "just testing Throwables"))))19 (try20 ;; Let the action finish; eat the "agent has errors" error that bubbles up21 (await-for 100 agt)22 (catch RuntimeException _))23 (is (instance? Throwable (first (agent-errors agt))))24 (is (= 1 (count (agent-errors agt))))26 ;; And now send an action that should work27 (clear-agent-errors agt)28 (is (= nil @agt))29 (send agt nil?)30 (is (true? (await-for 100 agt)))31 (is (true? @agt))))33 (deftest default-modes34 (is (= :fail (error-mode (agent nil))))35 (is (= :continue (error-mode (agent nil :error-handler println)))))37 (deftest continue-handler38 (let [err (atom nil)39 agt (agent 0 :error-mode :continue :error-handler #(reset! err %&))]40 (send agt /)41 (is (true? (await-for 100 agt)))42 (is (= 0 @agt))43 (is (nil? (agent-error agt)))44 (is (= agt (first @err)))45 (is (true? (instance? ArithmeticException (second @err))))))47 (deftest fail-handler48 (let [err (atom nil)49 agt (agent 0 :error-mode :fail :error-handler #(reset! err %&))]50 (send agt /)51 (Thread/sleep 100)52 (is (true? (instance? ArithmeticException (agent-error agt))))53 (is (= 0 @agt))54 (is (= agt (first @err)))55 (is (true? (instance? ArithmeticException (second @err))))56 (is (thrown? RuntimeException (send agt inc)))))58 (deftest restart-no-clear59 (let [p (promise)60 agt (agent 1 :error-mode :fail)]61 (send agt (fn [v] @p))62 (send agt /)63 (send agt inc)64 (send agt inc)65 (deliver p 0)66 (Thread/sleep 100)67 (is (= 0 @agt))68 (is (= ArithmeticException (class (agent-error agt))))69 (restart-agent agt 10)70 (is (true? (await-for 100 agt)))71 (is (= 12 @agt))72 (is (nil? (agent-error agt)))))74 (deftest restart-clear75 (let [p (promise)76 agt (agent 1 :error-mode :fail)]77 (send agt (fn [v] @p))78 (send agt /)79 (send agt inc)80 (send agt inc)81 (deliver p 0)82 (Thread/sleep 100)83 (is (= 0 @agt))84 (is (= ArithmeticException (class (agent-error agt))))85 (restart-agent agt 10 :clear-actions true)86 (is (true? (await-for 100 agt)))87 (is (= 10 @agt))88 (is (nil? (agent-error agt)))89 (send agt inc)90 (is (true? (await-for 100 agt)))91 (is (= 11 @agt))92 (is (nil? (agent-error agt)))))94 (deftest invalid-restart95 (let [p (promise)96 agt (agent 2 :error-mode :fail :validator even?)]97 (is (thrown? RuntimeException (restart-agent agt 4)))98 (send agt (fn [v] @p))99 (send agt (partial + 2))100 (send agt (partial + 2))101 (deliver p 3)102 (Thread/sleep 100)103 (is (= 2 @agt))104 (is (= IllegalStateException (class (agent-error agt))))105 (is (thrown? RuntimeException (restart-agent agt 5)))106 (restart-agent agt 6)107 (is (true? (await-for 100 agt)))108 (is (= 10 @agt))109 (is (nil? (agent-error agt)))))111 ; http://clojure.org/agents113 ; agent114 ; deref, @-reader-macro, agent-errors115 ; send send-off clear-agent-errors116 ; await await-for117 ; set-validator get-validator118 ; add-watch remove-watch119 ; shutdown-agents