<commit_before>(ns riemann.test
  "Fast, end-to-end, repeatable testing for entire Riemann configs. Provides a
  `tap` macro which taps the event stream and (in testing mode) records all
  events that flow through that stream. Provides a variant of deftest that
  initiates controlled time and sets up a fresh result set for taps, and a
  function `inject!` to apply events to streams and see what each tap
  received."
  (:require [riemann.time.controlled :as time.controlled]
            [riemann.time :as time]
            [riemann.index :as index]
            [riemann.service :as service]
            [riemann.streams :as streams]
            [clojure.test :as test]))

; ugggggggh state is the worst
;  and yet necessary

(def ^:dynamic *testing*
  "Are we currently in test mode?"
  false)

(def ^:dynamic *results*
  "A map of tap names to atoms of vectors of captured events." nil)

(def ^:dynamic *taps*
  "An atom to a map of tap names to information about the taps; e.g. file and
  line number, for preventing collisions." nil)

(def ^:dynamic *core*
  "The core used in test mode"
  nil)

(defn tap-stream
  "Called by `tap` to construct a stream which records events in *results*
  before forwarding to child."
  [name child]
  (fn stream [event]
    ; Record event
    (-> *results*
        (get name)
        (swap! conj event))

    ; Forward downstream
    (child event)))

(defmacro tap
  "A stream which records inbound events in the *results* map. Takes a globally
  unique name which identifies that tap in *results*.

  When *testing* is false (at compile time!), tap has no effect; it compiles
  directly into (sdo child-streams).

  When *testing* is true at compile time, tap records any events that arrive in
  the results map, and passes those events on to its children.

  (rate 5 (tap :graph prod-graph-stream))"
  [name & children]
  (if *testing*
    (let [name (eval name)
          file *file*
          line (:line (meta &env))
          column (:column (meta &env))]
      (locking *taps*
        ; Make sure no other tap conflicts
        (when-let [extant (contains? @*taps* name)]
          (throw (RuntimeException.
                   (str "Tap " name " (" file ":" line ")"
                        " already defined at "
                        (:file extant) ":" (:line extant)))))
<commit_msg>Do not record events if *results* is nil. (#974)<commit_after>(ns riemann.test
  "Fast, end-to-end, repeatable testing for entire Riemann configs. Provides a
  `tap` macro which taps the event stream and (in testing mode) records all
  events that flow through that stream. Provides a variant of deftest that
  initiates controlled time and sets up a fresh result set for taps, and a
  function `inject!` to apply events to streams and see what each tap
  received."
  (:require [riemann.time.controlled :as time.controlled]
            [riemann.time :as time]
            [riemann.index :as index]
            [riemann.service :as service]
            [riemann.streams :as streams]
            [clojure.test :as test]))

; ugggggggh state is the worst
;  and yet necessary

(def ^:dynamic *testing*
  "Are we currently in test mode?"
  false)

(def ^:dynamic *results*
  "A map of tap names to atoms of vectors of captured events." nil)

(def ^:dynamic *taps*
  "An atom to a map of tap names to information about the taps; e.g. file and
  line number, for preventing collisions." nil)

(def ^:dynamic *core*
  "The core used in test mode"
  nil)

(defn tap-stream
  "Called by `tap` to construct a stream which records events in *results*
  before forwarding to child."
  [name child]
  (fn stream [event]
    ; Record event
    (some-> *results*
            (get name)
            (swap! conj event))

    ; Forward downstream
    (child event)))

(defmacro tap
  "A stream which records inbound events in the *results* map. Takes a globally
  unique name which identifies that tap in *results*.

  When *testing* is false (at compile time!), tap has no effect; it compiles
  directly into (sdo child-streams).

  When *testing* is true at compile time, tap records any events that arrive in
  the results map, and passes those events on to its children.

  (rate 5 (tap :graph prod-graph-stream))"
  [name & children]
  (if *testing*
    (let [name (eval name)
          file *file*
          line (:line (meta &env))
          column (:column (meta &env))]
      (locking *taps*
        ; Make sure no other tap conflicts
        (when-let [extant (contains? @*taps* name)]
          (throw (RuntimeException.
                   (str "Tap " name " (" file ":" line ")"
                        " already defined at "
                        (:file extant) ":" (:line extant)))))

{'fraction_non_alphanumeric': 0.09663064208518754, 'fraction_numerical': 0.0010595465140919686, 'mean_word_length': 2.6476043276661514, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '618660', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1348, 'n_words': 613}