Skip to content

Commit 4f4b876

Browse files
committed
riemann test
1 parent 04b5be0 commit 4f4b876

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
(ns system.components.riemann-client
22
(:require [com.stuartsierra.component :as component]
3-
[riemann.client :as r]))
3+
[riemann.client :as r]
4+
[clojure.tools.logging :as log])
5+
(:import [io.riemann.riemann.client OverloadedException]))
46

57
(defrecord RiemannClient [host port transport]
68
component/Lifecycle
79
(start [component]
810
(let [client (case transport
911
:tcp (r/tcp-client {:host host :port port})
10-
:udp (r/udp-client {:host host :port port}))]
11-
(assoc component :client client)))
12+
:udp (r/udp-client {:host host :port port}))
13+
a (-> (agent {})
14+
(add-watch :key (fn [_k _r _os ns] (try (deref (:promise ns) 5000 ::timeout)
15+
(catch Exception e (log/error (.getMessage e)))))))
16+
f (fn [state client event]
17+
(let [v (try
18+
(r/send-event client event)
19+
(catch OverloadedException e (log/error (.getMessage e))))]
20+
(assoc state :promise v)))]
21+
(assoc component :client client :send-fn (partial send a f client))))
1222
(stop [component]
1323
(if-let [client (:client component)]
1424
(assoc component :client (r/close! client))
1525
component)))
1626

17-
(defn new-riemann-client [& {:keys [host port transport] :or {host "127.0.0.1" port 5555 transport :tcp}}]
27+
(defn new-riemann-client
28+
"Returns a Riemann client.
29+
30+
`send-fn` is a function that accepts a Riemann struct, which it will
31+
send in an agent threadpool (asynchronously). The promise that the
32+
Riemann `send-event` returns will be derefed in a watcher (also on
33+
the threadpool), and will log errors if any occur."
34+
[& {:keys [host port transport] :or {host "127.0.0.1" port 5555 transport :tcp}}]
1835
(map->RiemannClient {:host host :port port :transport transport}))

test/system/components/riemann_client_test.clj

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
(deftest ^:dependency riemann-client
1111
(let [client (component/start (new-riemann-client))]
1212
(is (started? client))
13-
(is (not (stopped? client)))
14-
(component/stop client)))
13+
(component/stop client)
14+
(is (stopped? client))))
1515

16+
(deftest ^:dependency riemann-send-fn
17+
(let [client (component/start (new-riemann-client))
18+
send-fn (:send-fn client)]
19+
(Thread/sleep 1000)
20+
(is (started? client))
21+
(is (= clojure.lang.Agent (type (send-fn {:service "foo" :state "ok"}))))
22+
(Thread/sleep 1000)
23+
(is (= io.riemann.riemann.client.MapPromise (type (:promise @(send-fn {:service "foo" :state "ok"})))))
24+
(is (= riemann.codec.Msg (type (deref (:promise @(send-fn {:service "foo" :state "ok"}))))))
25+
(Thread/sleep 1000)
26+
(component/stop client)))

0 commit comments

Comments
 (0)