From 9a72e63932f32ce7d7807a31dad000948c34ce3a Mon Sep 17 00:00:00 2001 From: Yann Esposito Date: Wed, 18 Mar 2015 17:47:00 +0100 Subject: [PATCH] more protective and more tests --- src/bigbrother/core.clj | 32 ++++++++++++++++++++++++++++---- test/bigbrother/core_test.clj | 22 +++++++++++++++++----- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/bigbrother/core.clj b/src/bigbrother/core.clj index 65cf428..76d26e5 100644 --- a/src/bigbrother/core.clj +++ b/src/bigbrother/core.clj @@ -1,5 +1,24 @@ (ns bigbrother.core - "This file provide helpers to manage time spend in functions" + "This file provide helpers to manage time spend in functions + +## Concepts + + You count the time between a start and an end time. +Typically: + +``` +start +action1 +log action1 finished +action2 +log action2 finished +action1 +log action1 finished +action3 +log action3 finished +end +``` +" (:require [clojure.tools.logging :as log] [clojure.data.json :as json] [overtone.at-at :refer [every mk-pool]] @@ -29,6 +48,7 @@ (def log-mmetric max-metrics/log-mmetric) (defn timer-loop-finished + "The loop is finished" ([] ;; increment the number of loop (swap! n inc) @@ -77,22 +97,26 @@ (defn ok [] (fn [_] "ok")) (defn warning [] (fn [_] "warning")) (defn critical [] (fn [_] "critical")) +(defn informational [] (fn [_] "informational")) (defn- to-riemann-event [[k v]] (when (number? v) (let [lvl-fn (get @level-by-key k) - level (if lvl-fn (lvl-fn v) "ok")] + level (if lvl-fn (lvl-fn v) "informational")] (into @riemann-conf {:service (str @riemann-service " " (name k)) - :state level + :state level :metric v})))) (defn send-to-riemann [m] (let [result-map (into @default-map m) events (remove nil? (map to-riemann-event result-map))] (when @riemann-conn - (r/send-events @riemann-conn events)))) + (try + (r/send-events @riemann-conn events) + (catch Exception e + (log/error e)))))) (defn reset-accumulators! [] diff --git a/test/bigbrother/core_test.clj b/test/bigbrother/core_test.clj index 980c071..fd8e5b3 100644 --- a/test/bigbrother/core_test.clj +++ b/test/bigbrother/core_test.clj @@ -27,11 +27,16 @@ (log-time session :end) (telescreen-off session) (end-session! session)) - (let [result (resume-map 1000)] + (let [result (resume-map 1000) + result2 (resume-map 2000) + ] (is (contains? result :nb)) (is (contains? result :x1)) (is (contains? result :x2)) - (is (>= (:total result) (:x2 result)))))) + (is (= (:x1 result) (:x1 result2))) + (is (= (:x2 result) (:x2 result2))) + (is (>= (:total result) (:x2 result))) + (is (= (/ (:nb result) 2) (:nb result2)))))) (deftest check-metrics (do @@ -46,13 +51,16 @@ (log-metric :foo 3) (log-metric :bar 1) (timer-loop-finished) - (let [result (resume-map 1000)] + (let [result (resume-map 1000) + result2 (resume-map 2000) + ] (is (contains? result :foo)) (is (contains? result :bar)) (is (contains? result :nb)) (is (= 2.0 (:foo result))) (is (= 2.0 (:bar result))) - (is (= 3.0 (:nb result)))))) + (is (= 3.0 (:nb result))) + (is (= (/ (:nb result) 2) (:nb result2)))))) (deftest check-mmetrics (do @@ -66,12 +74,16 @@ (log-mmetric :foo 60) (log-mmetric :bar 6) (timer-loop-finished) - (let [result (resume-map 1000)] + (let [result (resume-map 1000) + result2 (resume-map 2000) + ] (is (contains? result :foo)) (is (contains? result :bar)) (is (contains? result :nb)) (is (= 80 (:foo result))) + (is (= (:foo result) (:foo result2))) (is (= 6 (:bar result))) + (is (= (:bar result) (:bar result2))) (is (= 3.0 (:nb result)))))) (deftest check-counters