/
streams_test.clj
1745 lines (1514 loc) · 65.6 KB
/
streams_test.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns riemann.streams-test
(:use riemann.streams
[riemann.common :exclude [match]]
riemann.time.controlled
riemann.time
[riemann.test :refer [run-stream run-stream-intervals test-stream
with-test-stream test-stream-intervals]]
clojure.test)
(:require [riemann.index :as index]
[riemann.folds :as folds]
[riemann.logging :as logging])
(:import (java.util.concurrent Executor
CountDownLatch)))
(logging/init)
(use-fixtures :once control-time!)
(use-fixtures :each reset-time!)
(defn evs
"Generate events based on the given event, with given metrics"
[base-event & metrics]
(vec (map #(assoc base-event :metric %)
metrics)))
(defn em
"Generate events with given metrics"
[& metrics]
(vec (map (fn [m] {:metric m}) metrics)))
(deftest smap*-test
(testing "passes nil values"
(test-stream (smap* identity)
[1 nil false 3]
[1 nil false 3])))
(deftest smap-test
(testing "increment"
(test-stream (smap inc) [6 3 -1] [7 4 0]))
(testing "ignores nil values"
(test-stream (smap identity)
[1 nil false 3]
[1 false 3])))
(deftest smapcat-test
(testing "doubles"
(test-stream (smapcat #(vector % %)) [0 1 2 3]
[0 0 1 1 2 2 3 3])))
(deftest sdo-test
(let [vals1 (atom [])
vals2 (atom [])
add1 #(swap! vals1 conj %)
add2 #(swap! vals2 conj %)]
(run-stream (sdo add1 add2) [1 2 3])
(is (= @vals1 [1 2 3]))
(is (= @vals2 [1 2 3]))
(run-stream (sdo add1) [4 5 6])
(is (= @vals1 [1 2 3 4 5 6]))
(run-stream (sdo) [6 7 8])
(is (= @vals1 [1 2 3 4 5 6]))
(is (= @vals2 [1 2 3]))))
(deftest exception-stream-test
(testing "direct fn"
(logging/suppress
["riemann.streams" "riemann.streams-test"]
(let [out (atom [])
exceptions (atom [])
stream (exception-stream
#(swap! exceptions conj %)
(fn [e]
(swap! out conj e)
(throw
(RuntimeException. "foo"))))]
(stream :hi)
(is (= @out [:hi]))
(is (= 1 (count @exceptions)))
(let [e (first @exceptions)]
(is (= (:service e) "riemann exception"))
(is (= (:state e) "error"))
(is (= (:time e) 0))
(is (re-find #".*RuntimeException.*" (:description e)))
(is (= #{"exception" "java.lang.RuntimeException"}
(set (:tags e))))))))
(testing "scheduled fn"
(logging/suppress
"riemann.streams"
(reset-time!)
(let [e (atom nil)
s (exception-stream #(reset! e %)
(rate 1
(fn [e] (/ 1 0))))]
(s {:metric 2})
(is (nil? @e))
(advance! 1)
(is (= "error" (:state @e))))))
(testing "exception handling takes care of Throwables"
(logging/suppress
"riemann.streams"
(let [exceptions (atom [])]
(binding [riemann.streams/*exception-stream* #(swap! exceptions conj %)]
(run-stream (sdo #(throw (Throwable. %)))
["boo!"]))
(is (= 1 (count @exceptions)))))))
(deftest execute-on-test
(let [output (atom [])
n 100
events (range n)
latch (CountDownLatch. n)
stream (execute-on
(reify Executor
(execute [this task]
(future
(task)
(.countDown latch))))
(fn [event]
(swap! output conj event)))]
; Run streams against events.
(dorun (map stream events))
; Check that our event was applied to the conj stream.
(.await latch)
(is (= (set @output) (set events)))))
(deftest sreduce-test
(testing "explicit value"
(test-stream (sreduce + 1) [1 2 3] [2 4 7]))
(testing "implicit value"
(test-stream (sreduce +) [1 2 3 4] [3 6 10])))
(deftest counter-test
(testing "passes through events without metrics"
(test-stream (counter)
[{} {:state "expired"} {:service "foo"}]
[{} {:state "expired"} {:service "foo"}]))
(testing "counts"
(test-stream (counter)
[{:metric 2} {} {:metric 3}]
[{:metric 2} {} {:metric 5}])
(test-stream (counter 100)
[{:metric 2} {} {:metric 3}]
[{:metric 102} {} {:metric 105}]))
(testing "resets"
(test-stream (counter 100)
[{:metric 1}
{:metric 200 :tags ["reset"]}
{:metric 5}]
[{:metric 101}
{:metric 200 :tags ["reset"]}
{:metric 205}])))
(deftest match-test
; Regular strings.
(test-stream (match :service "foo")
[{}
{:service "bar"}
{:service "foo"}]
[{:service "foo"}])
; Sets
(test-stream (match :metric #{0 2})
[{}
{:metric 1}
{:metric 2}]
[{:metric 2}])
; Regexen
(test-stream (match :state #"^mi")
[{}
{:state "migas"}
{:state "other breakfast foods"}]
[{:state "migas"}])
; Functions
(test-stream (match identity 2)
[1 2 3]
[2])
; Maps
(test-stream (match identity {:state #"^mi", :host "other"})
[{}
{:state "migas"}
{:state "migas", :host "other"}]
[{:state "migas", :host "other"}])
; Nils
(test-stream (match :host nil)
[{}
{:host nil}
{:host :foo}]
[{}
{:host nil}]))
(deftest tag-test
; Single tag
(test-stream (tag "foo")
[{}
{:service :a :tags ["foo"]}
{:service :b :tags ["bar" "baz"]}]
[{:tags ["foo"]}
{:service :a :tags ["foo"]}
{:service :b :tags ["foo" "bar" "baz"]}])
; Multiple tags
(test-stream (tag ["foo" "bar"])
[{}
{:service :a :tags ["foo"]}
{:service :b :tags ["foo" "baz"]}]
[{:tags ["foo" "bar"]}
{:service :a :tags ["foo" "bar"]}
{:service :b :tags ["foo" "bar" "baz"]}]))
(deftest tagged-all-test
(test-stream (tagged-all ["kitten" "cat"])
[{:tags ["kitten" "cat"]}
{:tags ["kitten", "cat", "meow"]}
{:tags ["dog" "cat"]}
{:tags ["cat"]}
{:tags []}
{}]
[{:tags ["kitten" "cat"]}
{:tags ["kitten", "cat", "meow"]}])
(test-stream (tagged-all "meow")
[{:tags ["meow" "bark"]}
{:tags ["meow"]}
{:tags ["bark"]}
{}]
[{:tags ["meow" "bark"]}
{:tags ["meow"]}])
(testing "return values"
(is (true? ((tagged-all ["meow" "bark"])
{:tags ["meow" "bark" "grr"]})))
(is (nil? ((tagged-all ["meow" "bar"])
{:tags ["meow"]})))))
(deftest tagged-any-test
(test-stream (tagged-any ["kitten" "cat"])
[{:tags ["kitten" "cat"]}
{:tags ["cat", "dog"]}
{:tags ["kitten"]}
{:tags ["dog"]}
{:tags []}
{}]
[{:tags ["kitten" "cat"]}
{:tags ["cat", "dog"]}
{:tags ["kitten"]}])
(test-stream (tagged-any "meow")
[{:tags ["meow" "bark"]}
{:tags ["meow"]}
{:tags ["bark"]}
{}]
[{:tags ["meow" "bark"]}
{:tags ["meow"]}])
(testing "return values"
(is (true? ((tagged-any ["meow" "bark"])
{:tags ["meow" "moo"]})))
(is (nil? ((tagged-any ["meow" "bar"])
{:tags ["moo"]})))))
(deftest split*-test
(testing "with one branch"
(test-stream (split* identity)
[true false nil 2]
[true 2]))
(testing "with a default stream"
;; dispatch with default value
(let [sup (fn [threshold] #(< threshold (:metric %)))
res (atom [])
events [{:metric 15} {:metric 8} {:metric 2}]
expect [{:metric 15 :state :critical}
{:metric 8 :state :warn}
{:metric 2 :state :ok}]
stream (split* (sup 10) (with :state :critical
(partial swap! res conj))
(sup 5) (with :state :warn
(partial swap! res conj))
(with :state :ok
(partial swap! res conj)))]
(dorun (map stream events))
(is (= expect @res))))
(testing "with no default stream"
(let [sup (fn [threshold] #(< threshold (:metric %)))
res (atom [])
events [{:metric 15} {:metric 8} {:metric 2}]
expect [{:metric 15 :state :critical}
{:metric 8 :state :warn}]
stream (split*
(sup 10) (with :state :critical
(partial swap! res conj))
(sup 5) (with :state :warn
(partial swap! res conj)))]
(dorun (map stream events))
(is (= expect @res)))))
(deftest split-test
;; same test as above, using implicit rewrites
(testing "with a default stream"
(let [sup (fn [threshold] #(< threshold (:metric %)))
res (atom [])
events [{:metric 15} {:metric 8} {:metric 2}]
expect [{:metric 15 :state :critical}
{:metric 8 :state :warn}
{:metric 2 :state :ok}]
stream (split
(> metric 10) (with :state :critical
(partial swap! res conj))
(> metric 5) (with :state :warn
(partial swap! res conj))
(with :state :ok
(partial swap! res conj)))]
(dorun (map stream events))
(is (= expect @res))))
(testing "evaluates streams once"
(let [res (atom [])
stream (split
true
(let [state (atom 0)]
(fn [_]
(swap! res conj
(swap! state inc)))))]
(stream :x)
(stream :x)
(stream :x)
(is (= @res [1 2 3])))))
(deftest splitp-test
;; same test as above, using splitp
(testing "basics"
(let [res (atom [])
events [{:metric 15} {:metric 8} {:metric 2}]
expect [{:metric 15 :state :crit}
{:metric 8 :state :warn}
{:metric 2 :state :ok}]
stream (splitp <= metric
10 (with :state :crit
(partial swap! res conj))
5 (with :state :warn
(partial swap! res conj))
(with :state :ok
(partial swap! res conj)))]
(dorun (map stream events))
(is (= expect @res))))
(testing "Without a default"
(is (thrown? IllegalArgumentException
((splitp = true
false :doesnt-happen)
:foo))))
(testing "Evaluates child streams once at creation time"
(let [a (atom 0)
b (atom 0)
s (splitp = state
:foo (do (swap! a inc) identity)
(do (swap! b inc) identity))]
(is (= 1 @a))
(is (= 1 @b))
(s {:state :foo})
(s {:state :foo})
(s {:state :bar})
(s {:state :baz})
(is (= 1 @a))
(is (= 1 @b)))))
(deftest where*-test
(test-stream (where* identity)
[true false nil 2]
[true 2])
(test-stream (where* expired?)
[{:time -1 :ttl 0.5}
{:time 0 :ttl 1}]
[{:time -1 :ttl 0.5}])
; Complex closure with else clause
(let [good (atom [])
bad (atom [])
s (where* (fn [event]
(or (= "good" (:service event))
(< 2 (:metric event))))
(partial swap! good conj)
(else (partial swap! bad conj)))
events [{:service "good" :metric 0}
{:service "bad" :metric 0}
{:metric 1}
{:service "bad" :metric 1}
{:service "bad" :metric 3}]]
; Run stream
(doseq [e events] (s e))
(is (= @good
[{:service "good" :metric 0}
{:service "bad" :metric 3}]))
(is (= @bad
[{:service "bad" :metric 0}
{:metric 1}
{:service "bad" :metric 1}])))
(testing "evaluates children once"
; Where should evaluate its children exactly once.
(let [x (atom 0)
s (where* (fn [e] true) (do (swap! x inc) identity))]
(is (= @x 1))
(s {:service "test"})
(is (= @x 1))
(s {:service "test"})
(is (= @x 1)))))
(deftest where*-return-value
; Where*'s return value should be whether the predicate matched.
(is (= true ((where* expired? (fn [e] "hi"))
(expire {}))))
(is (= 2 ((where* (constantly 2)) :zoom))))
(deftest where-test
(testing "field"
(let [r (ref [])
s (where (or (state "ok" "good")
(= "weird" state))
(fn [e] (dosync (alter r conj e))))
events [{:state "ok"}
{:state "good"}
{:state "weird"}
{:state "error"}]
expect [{:state "ok"}
{:state "good"}
{:state "weird"}]]
(doseq [e events] (s e))
(is (= expect (deref r)))))
(testing "event binding"
(test-stream (where (= event 2))
[1 2 3 2]
[2 2]))
(testing "regex"
(test-stream (where (service #"^foo"))
[{}
{:service "foo"}
{:service "food"}]
[{:service "foo"}
{:service "food"}]))
(testing "functions"
(test-stream (where (and metric (even? metric)))
[{}
{:metric 1}
{:metric 2}]
[{:metric 2}]))
(testing "functions as values"
; May not support this later. I don't think it's all that
; useful given you can just use (even? metric).
(test-stream (where (and metric (metric even?)))
[{}
{:metric 1}
{:metric 2}]
[{:metric 2}]))
(testing "using sets as predicates"
(test-stream (where (service #{"service1" "service2"}))
[{:service "service1"}
{:service "service2"}
{:service "service-doesnt-match"}]
[{:service "service1"}
{:service "service2"}]))
(testing "variable"
; Verify that the macro allows variables to be used in
; predicates.
(let [regex #"cat"]
(test-stream (where (service regex))
[{:service "kitten"}
{:service "cats"}]
[{:service "cats"}])))
(testing "tagged (one tag)"
(let [r (atom [])
s (where (tagged "foo") (append r))
events [{}
{:tags []}
{:tags ["blah"]}
{:tags ["foo"]}
{:tags ["foo" "bar"]}]]
(doseq [e events] (s e))
(is (= (deref r)
[{:tags ["foo"]} {:tags ["foo" "bar"]}]))))
(testing "tagged-all (one tag)"
(let [r (atom [])
s (where (tagged-all "foo") (append r))
events [{}
{:tags []}
{:tags ["blah"]}
{:tags ["foo"]}
{:tags ["foo" "bar"]}]]
(doseq [e events] (s e))
(is (= (deref r)
[{:tags ["foo"]} {:tags ["foo" "bar"]}]))))
(testing "tagged-any (one tag)"
(let [r (atom [])
s (where (tagged-any "foo") (append r))
events [{}
{:tags []}
{:tags ["blah"]}
{:tags ["foo"]}
{:tags ["foo" "bar"]}]]
(doseq [e events] (s e))
(is (= (deref r)
[{:tags ["foo"]} {:tags ["foo" "bar"]}]))))
(testing "tagged (multiple tags)"
(let [r (atom [])
s (where (tagged ["foo" "bar"]) (append r))
events [{}
{:tags []}
{:tags ["blah"]}
{:tags ["foo"]}
{:tags ["foo" "bar"]}
{:tags ["foo" "bar" "baz"]}]]
(doseq [e events] (s e))
(is (= (deref r)
[{:tags ["foo" "bar"]} {:tags ["foo" "bar" "baz"]}]))))
(testing "tagged-all (multiple tags)"
(let [r (atom [])
s (where (tagged-all ["foo" "bar"]) (append r))
events [{}
{:tags []}
{:tags ["blah"]}
{:tags ["foo"]}
{:tags ["foo" "bar"]}
{:tags ["foo" "bar" "baz"]}]]
(doseq [e events] (s e))
(is (= (deref r)
[{:tags ["foo" "bar"]} {:tags ["foo" "bar" "baz"]}]))))
(testing "tagged-any (multiple tags)"
(let [r (atom [])
s (where (tagged-any ["foo" "bar"]) (append r))
events [{}
{:tags []}
{:tags ["blah"]}
{:tags ["foo"]}
{:tags ["foo" "bar"]}
{:tags ["baz" "bar"]}]]
(doseq [e events] (s e))
(is (= (deref r)
[{:tags ["foo"]} {:tags ["foo" "bar"]} {:tags ["baz" "bar"]}]))))
(testing "else"
; Where should take an else clause.
(let [a (atom [])
b (atom [])]
(run-stream
(where (service #"a")
#(swap! a conj (:service %))
(else #(swap! b conj (:service %))))
[{:service "cat"}
{:service "dog"}
{:service nil}
{:service "badger"}])
(is (= @a ["cat" "badger"]))
(is (= @b ["dog" nil]))))
(testing "evaluates children once"
; Where should evaluate its children exactly once.
(let [x (atom 0)
s (where true (do (swap! x inc) identity))]
(is (= @x 1))
(s {:service "test"})
(is (= @x 1))
(s {:service "test"})
(is (= @x 1))))
(testing "return value"
; Where's return value should be whether the predicate
; matched.
(is (= true ((where (service "foo")) {:service "foo"})))
(is (= false ((where (service "foo")) {:service "bar"})))
(is (= true ((where (tagged "foo")) {:tags ["foo"]})))
(is (= false ((where (tagged "foo")) {:tags ["bar"]})))
(is (= true ((where (tagged-all "foo")) {:tags ["foo"]})))
(is (= false ((where (tagged-all "foo")) {:tags ["bar"]})))
(is (= true ((where (tagged-any "foo")) {:tags ["foo"]})))
(is (= false ((where (tagged-any "foo")) {:tags ["bar"]})))
(is (= true ((where (tagged ["foo" "bar"])) {:tags ["foo" "bar"]})))
(is (= false ((where (tagged ["foo" "bar"])) {:tags ["bar"]})))
(is (= true ((where (tagged-all ["foo" "bar"])) {:tags ["foo" "bar"]})))
(is (= false ((where (tagged-all ["foo" "bar"])) {:tags ["bar"]})))
(is (= true ((where (tagged-any ["foo" "bar"])) {:tags ["foo" "bar"]})))
(is (= false ((where (tagged-any ["foo" "bar"])) {:tags ["blah"]})))
(is (= true ((where (service "foo")
(fn [event] 2))
{:service "foo"})))
(is (= false ((where (service "foo")
(else (fn [event] 2)))
{:service "bar"})))
(is (= 2 ((where 2) :wheeee!)))
(is (= nil ((where nil) :zoooom!)))))
(deftest default-kv
(let [r (atom nil)
s (default :service "foo" (register r))]
(s {:service nil})
(is (= {:service "foo"} (deref r)))
(s {:service "foo"})
(is (= {:service "foo"} (deref r)))
(s {:service "bar" :test "baz"})
(is (= {:service "bar" :test "baz"} (deref r)))))
(deftest default-map
(let [r (atom nil)
s (default {:service "foo" :state nil} (register r))]
(s (event {:service nil}))
(is (= "foo" (:service (deref r))))
(is (= nil (:state (deref r))))
(s (event {:service "foo"}))
(is (= "foo" (:service (deref r))))
(is (= nil (:state (deref r))))
(s (event {:service "bar" :host "baz" :state "evil"}))
(is (= "bar" (:service (deref r))))
(is (= "baz" (:host (deref r))))
(is (= "evil" (:state (deref r))))))
(deftest with-kv
(let [r (ref nil)
s (with :service "foo" (fn [e] (dosync (ref-set r e))))]
(s {:service nil})
(is (= {:service "foo"} (deref r)))
(s {:service "foo"})
(is (= {:service "foo"} (deref r)))
(s {:service "bar" :test "baz"})
(is (= {:service "foo" :test "baz"} (deref r)))
(s [{:service "bar" :test "baz"}])
(is (= [{:service "foo" :test "baz"}] (deref r)))))
(deftest with-map
(let [r (ref nil)
s (with {:service "foo" :state nil} (fn [e] (dosync (ref-set r e))))
empty-s (with {} (fn [e] (dosync (ref-set r e))))]
(s (event {:service nil}))
(is (= "foo" (:service (deref r))))
(is (= nil (:state (deref r))))
(s (event {:service "foo"}))
(is (= "foo" (:service (deref r))))
(is (= nil (:state (deref r))))
(s (event {:service "bar" :test "baz" :state "evil"}))
(is (= "foo" (:service (deref r))))
(is (= nil (:state (deref r))))
(empty-s (event {:service "bar" :test "baz"}))
(is (= "bar" (:service (deref r))))))
(deftest by-single
; Each test stream keeps track of the first host it sees, and confirms
; that each subsequent event matches that host.
(let [i (ref 0)
s (by :host
(let [host (ref nil)]
(fn [event]
(dosync
(alter i inc)
(when (nil? (deref host))
(ref-set host (event :host)))
(is (= (deref host) (event :host)))))))
events (map (fn [h] {:host h}) [:a :a :b :a :c :b])]
(doseq [event events]
(s event))
(is (= (count events) (deref i)))))
(deftest by-multiple
; Each test stream keeps track of the first host/service it sees, and
; confirms that each subsequent event matches that host.
(let [i (ref 0)
s (by [:host :service]
(let [host (ref nil)
service (ref nil)]
(fn [event]
(dosync
(alter i inc)
(when (nil? (deref host))
(ref-set host (event :host)))
(when (nil? (deref service))
(ref-set service (event :service)))
(is (= (deref host) (event :host)))
(is (= (deref service) (event :service)))))))
events (map (fn [h] {:host (first h)
:service (last h)})
[[1 :a]
[1 :b]
[1 :a]
[2 :a]
[2 :a]
[1 :a]
[2 :a]
[1 :b]])]
(doseq [event events]
(s event))
(is (= (count events) (deref i)))))
(deftest by-evaluates-children-once-per-branch
(let [i (atom 0)
s (by :metric (do (swap! i inc) identity))]
(is (= @i 0))
(s {:metric 1})
(is (= @i 1))
(s {:metric 2})
(is (= @i 2))
(s {:metric 1})
(is (= @i 2))))
(deftest pipe-test
(testing "One stage"
(test-stream
(pipe !)
[1 2 3]
[1 2 3]))
(testing "Chained stages"
(test-stream
(pipe ! (smap inc !) (smap (partial * 2) !))
[1 2 3]
[4 6 8]))
(testing "Multiple markers"
(test-stream
(pipe - (splitp < metric
0.9 (with :state :critical -)
0.5 (with :state :warning -)
(with :state :ok -))
(smap identity -))
[{:metric 0.1}
{:metric 0.7}
{:metric 1.0}]
[{:metric 0.1 :state :ok}
{:metric 0.7 :state :warning}
{:metric 1.0 :state :critical}]))
(testing "Rebinding a marker"
(test-stream
(pipe - (let [! -
- clojure.core/-]
(smap - !)))
[-1 0 1]
[1 0 -1])))
(deftest fill-in-test
(test-stream-intervals
(fill-in 0.01 {:metric 0})
[]
[])
; Quick succession
(is (= (map :metric (run-stream-intervals
(fill-in 0.01 {:metric 0})
(interpose nil (em 1 2 3))))
[1 2 3]))
; With a gap and expiry
(is (= (map :metric (run-stream-intervals
(fill-in 0.05 {:metric 0})
[{:metric 1} 0.06
{:metric 2} nil
{:metric 3} 0.08
{:metric 4 :state "expired"} 0.06
{:metric 5}]))
[1 0 2 3 0 4 5]))
)
(deftest fill-in-last-test
(test-stream-intervals
(fill-in-last 0.01 {:metric 0})
[]
[])
; Quick succession
(let [output (run-stream-intervals
(fill-in-last 0.01 {:metric 0})
(interpose nil (evs {:host "foo" :service "bar"}
1 2 3)))]
(is (= (map :metric output)
[1 2 3]))
(is (= (map :host output)
["foo" "foo" "foo"]))
(is (= (map :service output)
["bar" "bar" "bar"])))
; With a gap and expiry
(let [output (run-stream-intervals
(fill-in-last 0.05 {:metric 0})
[{:host "a" :metric 1} 0.06
{:host "b" :metric 2} nil
{:host "c" :metric 3} 0.08
{:host "d" :metric 4 :state "expired"} 0.06
{:host "e" :metric 5}])]
(is (= (map :metric output)
[1 0 2 3 0 4 5]))
(is (= (map :host output)
["a" "a" "b" "c" "c" "d" "e"]))))
(deftest interpolate-constant-test
(test-stream-intervals
(interpolate-constant 0.01)
[]
[])
; Should forward a single state
(is (= (map :metric (run-stream-intervals
(interpolate-constant 0.1)
[{:metric 1} 0.05]))
[1]))
; Should forward first state and ignore immediate successors
(is (= (map :metric (run-stream-intervals
(interpolate-constant 0.1)
[{:metric 1} 0.05
{:metric 2} nil
{:metric 3}]))
[1]))
; Should fill in missing states regularly
(is (= (map :metric (run-stream-intervals
(interpolate-constant 0.1)
(interpose 0.22 (em 1 2 3 4))))
[1 1 1 2 2 3 3]))
; Should forward final "expired" state.
(is (= (map :metric (run-stream-intervals
(interpolate-constant 0.1)
(interpose 0.22
[{:metric 1}
{:metric 2}
{:metric 3}
{:metric 4 :state "expired"}])))
[1 1 1 2 2 3 3 4]))
; Should not fill during expired times.
(is (= (map :metric (run-stream-intervals
(interpolate-constant 0.05)
[{:metric 1 :state "expired"}
0.12
{:metric 2}
0.12
{:metric 3 :state "expired"}
0.12]))
[1 2 2 2 3]))
)
(deftest ddt-immediate-test
; Empty -> empty
(test-stream (ddt) [] [])
; Ignore stream without metrics
(test-stream (ddt) [{} {} {} {}] [])
; Do nothing the first time
(test-stream (ddt) [{:metric 1 :time 0}] [])
; Differentiate
(test-stream (ddt)
[{:metric 0 :time 0}
{:metric 0 :time 1}
{:metric 2 :time 2}
{:metric -4 :time 4}]
[{:metric 0 :time 1}
{:metric 2 :time 2}
{:metric -3 :time 4}]))
(deftest ddt-interval-test
(testing "Quick burst without crossing interval"
(is (= (map :metric (run-stream-intervals
(ddt 0.1)
[{:metric 1} nil
{:metric 2} nil
{:metric 3}]))
[])))
(testing "1 event per interval"
(test-stream-intervals
(ddt 1)
; Note that the swap occurs just prior to events at time 1.
[{:time 0 :metric -1} 99/100
{:time 1 :metric 0} 1
{:time 2 :metric -5} 1]
[{:time 1 :metric 1}
{:time 2 :metric -5}]))
(testing "n events per interval"
(test-stream-intervals
(ddt 1)
[{:time 0 :metric -1} 1/100
{:time 1/2 :metric 100} 1/2 ; Ignored
{:time 2/2 :metric 1} 1/2
{:time 3/2 :metric nil} 1/2 ; Ignored
{:time 4/2 :metric -3} 1/2]
[{:time 2/2 :metric 2}
{:time 4/2 :metric -4}]))
(testing "emits zeroes when no events arrive in an interval"
(test-stream-intervals (ddt 2)
[{:time 0 :metric 0} 1
{:time 1 :metric 1} 2
{:time 3 :metric 2} 3]
[{:time 2 :metric 1}
{:time 4 :metric 1/2}
{:time 6 :metric 0}])))
(deftest rate-slow-even
(let [output (ref [])
interval 1
intervals 5
gen-rate 10
total (* gen-rate intervals)
gen-period (/ interval gen-rate)
r (rate interval
(fn [event] (dosync (alter output conj event))))]
; Generate events
(dotimes [_ intervals]
(dotimes [_ gen-rate]
(advance! (+ (unix-time) gen-period))
(r {:metric 1 :time (unix-time)})))
; Give all futures time to complete
(advance! (+ (unix-time) gen-period))
; Verify output states
(let [o (deref output)]
; All events recorded
(is (approx-equal total (reduce + (map :metric o))))
; Middle events should have the correct rate.
(is (every? (fn [measured-rate]
(approx-equal gen-rate measured-rate))
(map :metric (drop 1 (drop-last o)))))
; First and last events should be complementary
(let [first-last (+ (:metric (first o))
(:metric (last o)))]
(is (or (approx-equal (* 2 gen-rate) first-last)
(approx-equal gen-rate first-last))))
)))
(deftest rate-threaded
(let [output (atom nil)
interval 5/2
total 10000
threads 4
r (rate interval
(fn [event] (dosync (reset! output event))))
; Generate events
workers (map (fn [t] (future
(dotimes [i (/ total threads)]
(r {:metric 1 :time (unix-time)}))))
(range threads))]
; Wait for workers
(dorun (map deref workers))
(advance! interval)