inject-intervals! test helper
Closed this issue · 2 comments
cddr commented
I wanted to test a stream that uses the stable
operator to filter out temporary errors that would likely be fixed within a minute or so. Couldn't figure out how to do it with the currently available testing primitives so ended up writing a helper that behaves a bit like inject!
but combines the feature of being able to advance time by interposing intervals into the input events like run-stream-intervals
does. Is there any interest in merging this helper into the test ns?
(ns riemann.test.alpha
"Test helpers that could conceivably be added upstream"
(:require
[riemann.test :refer :all]
[riemann.time.controlled :as time.controlled]))
(defn inject-intervals!
"Like inject! but the input includes alternate events and intervals
to facilitate the testing of a composition of streams that depend
on the passage of time"
[streams inputs-and-intervals]
(binding [*results* (riemann.test/fresh-results @*taps*)]
(let [current-time (ref 0)
next-time (ref (deref current-time))]
(time.controlled/with-controlled-time!
(time.controlled/reset-time!)
;; Apply events
(doseq [[e interval] (partition-all 2 inputs-and-intervals)]
(doseq [stream streams]
(stream (assoc e :time @current-time)))
(when interval
(dosync (ref-set next-time (+ (deref next-time) interval)))
(dosync (ref-set current-time (+ (deref current-time) interval)))
(time.controlled/advance! (deref next-time))))
;; Return captured events
(->> *results*
(reduce (fn [results [tap-name results-atom]]
(assoc! results tap-name @results-atom))
(transient {}))
persistent!)))))
mcorbin commented
Hi,
Couldn't figure out how to do it with the currently available testing
Why you cannot use inject! to advance time ? For example :
(inject! [{:host "foo"
:service "test-stable"
:time 0}
{:time 10}
{:host "foo"
:service "test-stable"
:time 12}
{:time 15}
{:host "foo"
:service "test-stable"
:time 20}])
cddr commented
Ah that's a good trick. That didn't occur to me. Thanks!