Skip to content

Instantly share code, notes, and snippets.

View weissjeffm's full-sized avatar

Jeff Weiss weissjeffm

View GitHub Profile
@weissjeffm
weissjeffm / loop-with-timeout.clj
Created November 15, 2010 22:11
Clojure macro to insert timeouts into loops
;;Code rewriting macro
(defmacro loop-with-timeout [timeout bindings & forms]
`(let [starttime# (System/currentTimeMillis)]
(loop ~bindings
(if (> (- (System/currentTimeMillis) starttime#) ~timeout)
(throw (RuntimeException. (str "Hit timeout of " ~timeout "ms.")))
(do ~@forms)))))
;;example use of macro
(loop-with-timeout 60000 []
@weissjeffm
weissjeffm / genclassmacro.clj
Created November 23, 2010 17:20
trying to produce a call to gen-class with annotations put in place (pulled from the metadata of the fn)
(defn method-name [f]
(str (:name (meta f))))
(defn group-ann-val [t]
(->> (meta t) :groups (map name) (into [])))
(defn as-annotation [t]
{(if (configuration? t) (-> (configuration t) (testng-map)) Test)
{:groups (group-ann-val t)}})
@weissjeffm
weissjeffm / testng.clj
Created November 24, 2010 15:09
Example of running clojure code as testng tests
(ns test-clj.sample-tests
(:use [test-clj.meta :only [gen-class-testng]]))
;sample tests
;------------------------------
(defn ^{:test {:configuration :beforeSuite
:groups #{:group1 :group2}}}
config1 [_]
(do (println "running configuration1")
(println "configuration1 complete.")))
@weissjeffm
weissjeffm / try-clojure.sh
Created November 29, 2010 01:58
Shell script to install and run clojure labrepl (on rpm based linux)
#!/bin/bash
echo "Trying clojure!"
if ! sudo -S true < /dev/null &>/dev/null; then
echo "You need sudo permission to run this script."
exit 1
fi
LEIN=/usr/local/bin/lein
if ! which lein; then
/*
* Script that runs a testng suite or test within a suite
* Args = xml-suite-file [optional-test-name]
*/
ant = new AntBuilder();
workspace = System.getProperty("workspace.dir", System.getenv("WORKSPACE") ?: System.getProperty("user.dir")) //defaults to $WORKSPACE, or if that's null, pwd
println("workspace= "+workspace)
(ns test-clj.sample-tests
(:use [test-clj.testng :only [gen-class-testng]]
[test-clj.base-test :only [config-data]]))
;sample tests
;------------------------------
(defn ^{:test {:configuration :beforeSuite
:groups #{:group1 :group2}}}
config1 [_]
(do (println "running configuration1")
class ByValuePredicate implements Predicate<Object> {
Object value;
String fieldName;
public ByValuePredicate(String fieldName, Object value) {
this.value=value;
this.fieldName=fieldName;
}
public boolean apply(Object toTest) {
return toTest.class.getField(fieldName).get(toTest).equals(value);
}
(deferror myerror [] [s] {:msg (str "blerg! " s) :unhandled (throw-msg RuntimeException) })
(raise myerror "foo") ;; gives exception message "blerg! foo"
(def x myerror)
(raise x "foo") ;; gives exception message "blerg!"
@weissjeffm
weissjeffm / handler.clj
Created December 17, 2010 16:32
An error handler for Clojure
(ns com.redhat.qe.handler
(:import [javax.naming NamingException]))
(def *handlers* [])
(def *error* nil)
(defn- e-to-map [e]
{:msg (.getMessage e) :type (class e) :exception e})
(defn- wrapped? [e]
@weissjeffm
weissjeffm / errorhandle.clj
Created December 20, 2010 21:39
demo of error handling
;; in the task class
(defn register [username password & {:keys [system-name-input, autosubscribe]
:or {system-name-input nil, autosubscribe false}}]
(if (ui exists? :unregister-system)
(raise {:type :already-registered
:username username
:password password
:unregister-first (fn [] (unregister)
(register (:username *error*) (:password *error*)))}))
(ui click :register-system)