-
Notifications
You must be signed in to change notification settings - Fork 3
/
testing_utils.clj
70 lines (67 loc) · 2.85 KB
/
testing_utils.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(ns clj-honeycomb.testing-utils
"Functions to make it easier to test code that uses clj-honeycomb."
(:require [clj-honeycomb.core :as honeycomb])
(:import (clojure.lang Atom)
(io.honeycomb.libhoney HoneyClient
ResponseObserver)
(io.honeycomb.libhoney.responses ResponseObservable)
(io.honeycomb.libhoney.transport Transport)))
(defn recording-client
"Create a HoneyClient which records all events sent by conj'ing them onto the
atom-wrapped vector supplied."
[events client-options]
(when-not (and (instance? Atom events) (vector? @events))
(throw (IllegalArgumentException. "events must be a vector wrapped in an atom")))
(let [ro (ResponseObservable.)
transport (reify Transport
(close [_this]
nil)
(getResponseObservable [_this]
ro)
(submit [_this event]
(.markEnqueueTime event)
(.markStartOfHttpRequest event)
(.markEndOfHttpRequest event)
(swap! events conj event)
true))
ro (when (:response-observer client-options)
(#'honeycomb/response-observer (:response-observer client-options)))
client-options (#'honeycomb/client-options
(merge {:data-set "data-set"
:write-key "write-key"}
client-options))
client (HoneyClient. client-options transport)]
(when ro
(.addResponseObserver client ro))
client))
(defn- recording-response-observer
"A ResponseObserver that will record all the received errors in the supplied
atom-wrapped vector."
[errors]
(when-not (and (instance? Atom errors) (vector? @errors))
(throw (IllegalArgumentException. "errors must be a vector wrapped in an atom")))
(reify ResponseObserver
(onClientRejected [_this cr]
(swap! errors conj cr))
(onServerAccepted [_this _sa]
nil)
(onServerRejected [_this sr]
(swap! errors conj sr))
(onUnknown [_this u]
(swap! errors conj u))))
(defn validate-events
"Execute some code that uses the implicit client created by
clj-honeycomb.core/init but trap all events sent and pass them to a
validation function."
([fn-that-sends-events fn-to-validate-events]
(validate-events {}
fn-that-sends-events
fn-to-validate-events))
([client-options fn-that-sends-events fn-to-validate-events]
(let [events (atom [])
errors (atom [])]
(with-open [client (recording-client events client-options)]
(.addResponseObserver client (recording-response-observer errors))
(binding [honeycomb/*client* client]
(fn-that-sends-events)))
(fn-to-validate-events @events @errors))))