File: filesystem_watch_service_test.clj

package info (click to toggle)
trapperkeeper-filesystem-watcher-clojure 1.2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 240 kB
  • sloc: java: 51; makefile: 24; sh: 15; xml: 10
file content (800 lines) | stat: -rw-r--r-- 36,645 bytes parent folder | download
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
(ns puppetlabs.trapperkeeper.services.watcher.filesystem-watch-service-test
  (:require [clojure.set :as set]
            [clojure.test :refer [deftest is testing use-fixtures]]
            [me.raynes.fs :as fs]
            [puppetlabs.trapperkeeper.app :as tk-app]
            [puppetlabs.trapperkeeper.core :as tk]
            [puppetlabs.trapperkeeper.internal :as tk-internal]
            [puppetlabs.trapperkeeper.services.protocols.filesystem-watch-service :refer [add-callback! add-file-callback! add-watch-dir! create-watcher]]
            [puppetlabs.trapperkeeper.services.watcher.filesystem-watch-core :as watch-core]
            [puppetlabs.trapperkeeper.services.watcher.filesystem-watch-service :refer [filesystem-watch-service]]
            [puppetlabs.trapperkeeper.testutils.bootstrap :refer [with-app-with-config]]
            [puppetlabs.trapperkeeper.testutils.logging :refer [with-test-logging]]
            [schema.test :as schema-test])
  (:import (com.puppetlabs DirWatchUtils)
           (java.io File)
           (java.net URI)
           (java.nio.file FileSystems Paths StandardWatchEventKinds WatchEvent)))

(use-fixtures :once schema-test/validate-schemas)

(defn make-callback
  [dest]
  (fn [events]
    (swap! dest concat events)))

(defn format-events
  [dest]
  (set (map #(select-keys % [:changed-path :type]) @dest)))

(defn contains-events?
  [dest events]
  (let [select-keys-set (format-events dest)]
    (set/subset? events select-keys-set)))

(defn exactly-matches-event?
  [dest expected-event]
  (let [select-keys-set (set (map #(select-keys % [:changed-path :type]) @dest))]
    (= expected-event select-keys-set)))

(def wait-time
  "Number of milliseconds wait-for-events! should wait."
  (* 10 1000)) ;; 10 seconds

(defn wait-for-events
  "Waits for events to land in dest.  If they do, events is returned.  Gives up
  and returns the contents of dest after wait-time."
  ([dest events]
   (wait-for-events dest events wait-time))
  ([dest events timeout-ms]
   (let [start-time (System/currentTimeMillis)]
     (loop []
       (let [elapsed-time (- (System/currentTimeMillis) start-time)]
         (if (contains-events? dest events)
           (format-events dest)
           (if (< elapsed-time timeout-ms)
             (do (Thread/sleep 100)
               (recur))
             (do
               (println "timed-out waiting for events")
               @dest))))))))

(defn wait-for-exactly-event
  "Waits for event to land in dest. On first registered event or events in dest,
  determines whether the registered event(s) match the supplied expected
  event. If so, returns the expected event. Otherwise returns contents of dest."
  [dest expected-event]
  (let [start-time (System/currentTimeMillis)]
    (loop [elapsed-time 0]
      (if-not (empty? @dest)
        (if (exactly-matches-event? dest expected-event)
          expected-event
          @dest)
        (if (< elapsed-time wait-time)
          (recur (- (System/currentTimeMillis) start-time))
          (do
            (println "timed-out waiting for events")
            @dest))))))

(defn watch!*
  [watcher root callback types]
  (add-watch-dir! watcher root {:recursive true})
  (add-callback! watcher callback types))

;; TODO perhaps move this (or something similar) up to the TK protocol
(defn watch!
  ([service root callback]
   (watch! service root callback [:create :modify :delete :unknown]))
  ([service root callback types]
   (watch!* (create-watcher service) root callback types)))

(deftest ^:integration single-path-test
  (let [root (fs/temp-dir "single-path-test")
        first-file (fs/file root "first-file")
        second-file (fs/file root "second-file")
        results (atom [])
        callback (make-callback results)]
    (with-app-with-config
     app [filesystem-watch-service] {}
     (let [service (tk-app/get-service app :FilesystemWatchService)]
       (watch! service root callback))
     (testing "callback not invoked until directory changes"
       (is (= @results [])))
     (testing "callback is invoked when a new file is created"
       (spit first-file "foo")
       (let [events #{{:changed-path first-file
                       :type :create}}]
         (is (= events (wait-for-events results events)))))
     (testing "callback invoked again when another new file is created"
       (reset! results [])
       (spit second-file "bar")
       (let [events #{{:changed-path second-file
                       :type :create}}]
         (is (= events (wait-for-events results events)))))
     (testing "watch-dir! also reports file modifications"
       (testing "of a single file"
         (reset! results [])
         (spit first-file "something different")
         (let [events #{{:changed-path first-file
                  :type :modify}}]
           (is (= events (wait-for-events results events)))))
       (testing "of multiple files"
         (reset! results [])
         (spit first-file "still not the same as before")
         (spit second-file "still not the same as before")
         (let [events #{{:changed-path first-file
                         :type :modify}
                        {:changed-path second-file
                         :type :modify}}]
           (is (= events (wait-for-events results events))))))
     (testing "watch-dir! also reports file deletions"
       (testing "of multiple files"
         (reset! results [])
         (is (fs/delete first-file))
         (is (fs/delete second-file))
         (let [events #{{:changed-path first-file
                         :type :delete}
                        {:changed-path second-file
                         :type :delete}}]
           (is (= events (wait-for-events results events))))))
     (testing "re-creation of a deleted directory"
       (reset! results [])
       (let [sub-dir (fs/file root "sub-dir")]
         (testing "Initial directory creation and deletion"
           (is (fs/mkdir sub-dir))
           (let [events #{{:changed-path sub-dir
                           :type :create}}]
             (is (= events (wait-for-events results events))))
           (reset! results [])
           (is (fs/delete sub-dir))
           (let [events #{{:changed-path sub-dir
                           :type :delete}}]
             (is (= events (wait-for-events results events)))))
         (testing "Re-creating the directory fires an event as expected"
           (reset! results [])
           (is (fs/mkdir sub-dir))
           (let [events #{{:changed-path sub-dir
                           :type :create}}]
             (is (= events (wait-for-events results events))))))))))

(deftest ^:integration one-event-type
  (let [root (fs/temp-dir "single-path-test")
        first-file (fs/file root "first-file")
        second-file (fs/file root "second-file")
        results (atom [])
        callback (make-callback results)]
    (with-app-with-config
      app [filesystem-watch-service] {}
      (let [service (tk-app/get-service app :FilesystemWatchService)]
        (watch! service root callback [:create]))
      (testing "callback not invoked until directory changes"
        (is (= @results [])))
      (testing "callback invoked when a new file is created"
        (spit first-file "foo")
        (let [events #{{:changed-path first-file
                        :type :create}}]
          (is (= events (wait-for-events results events)))))
      (testing "callback invoked when another new file is created"
        (reset! results [])
        (spit second-file "bar")
        (let [events #{{:changed-path second-file
                        :type :create}}]
          (is (= events (wait-for-events results events)))))
      (testing "does not reports file modifications"
        (testing "of a single file"
          (reset! results [])
          (spit first-file "something different")
          (Thread/sleep 1500)
          (is (empty? @results)))
        (testing "of multiple files"
          (reset! results [])
          (spit first-file "still not the same as before")
          (spit second-file "still not the same as before")
          (Thread/sleep 1500)
          (is (empty? @results)))
      (testing "watch-dir! does not report file deletions"
        (testing "of multiple files"
          (reset! results [])
          (is (fs/delete first-file))
          (is (fs/delete second-file))
          (Thread/sleep 1500)
           (is (empty? @results)))
      (testing "re-creation of a deleted directory"
        (reset! results [])
        (let [sub-dir (fs/file root "sub-dir")]
          (testing "Initial directory creation and deletion"
            (is (fs/mkdir sub-dir))
            (let [events #{{:changed-path sub-dir
                            :type :create}}]
              (is (= events (wait-for-events results events))))
            (reset! results [])
            (is (fs/delete sub-dir))
            (Thread/sleep 1500)
            (is (empty? @results)))
          (testing "Re-creating the directory fires an event as expected"
            (reset! results [])
            (is (fs/mkdir sub-dir))
            (let [events #{{:changed-path sub-dir
                            :type :create}}]
              (is (= events (wait-for-events results events))))))))))))

(deftest ^:integration one-file-watch
  (let [root (fs/temp-dir "single-path-test")
        first-file (fs/file root "first-file")
        second-file (fs/file root "second-file")
        results (atom [])
        callback (make-callback results)]
    (with-app-with-config
      app [filesystem-watch-service] {}
      (let [service (tk-app/get-service app :FilesystemWatchService)
            watcher (create-watcher service)]
        (add-watch-dir! watcher root)
        ;; watch only the first path
        (add-file-callback! watcher (.toPath first-file) callback))
      (testing "callback not invoked until directory changes"
        (is (= @results [])))
      (testing "callback invoked when a new file is created"
        (spit first-file "foo")
        (let [events #{{:changed-path first-file
                        :type :create}}]
          (is (= events (wait-for-events results events)))))
      (testing "callback not invoked when another new file is created"
        (reset! results [])
        (spit second-file "bar")
        (Thread/sleep 1500)
        (is (empty? @results)))
      (testing "reports file modifications"
        (testing "of a single file"
          (reset! results [])
          (spit first-file "something different")
          (let [events #{{:changed-path first-file
                          :type :modify}}]
            (is (= events (wait-for-events results events)))))
        (testing "of single file when multiple files are changed"
          (reset! results [])
          (spit first-file "still not the same as before")
          (spit second-file "still not the same as before")
          (let [events #{{:changed-path first-file
                          :type :modify}}]
            (is (= events (wait-for-events results events))))))
      (testing "watch-dir! reports file deletion"
        (testing "of single file when multiples are deleted"
          (reset! results [])
          (is (fs/delete first-file))
          (is (fs/delete second-file))
          (let [events #{{:changed-path first-file
                         :type :delete}}]
            (is (= events (wait-for-events results events))))))
      (testing "re-creation of a deleted directory"
        (reset! results [])
        (let [sub-dir (fs/file root "sub-dir")]
          (testing "Initial directory creation and deletion"
            (is (fs/mkdir sub-dir))
            (Thread/sleep 1500)
            (is (empty? @results))
            (is (fs/delete sub-dir))
            (Thread/sleep 1500)
            (is (empty? @results)))
          (testing "Re-creating the directory does not create an event as expected"
            (reset! results [])
            (is (fs/mkdir sub-dir))
            (Thread/sleep 1500)
            (is (empty? @results))))))))

(deftest ^:integration one-file-watch-single-event
  (let [root (fs/temp-dir "single-path-test")
        first-file (fs/file root "first-file")
        second-file (fs/file root "second-file")
        results (atom [])
        callback (make-callback results)]
    (with-app-with-config
      app [filesystem-watch-service] {}
      (let [service (tk-app/get-service app :FilesystemWatchService)
            watcher (create-watcher service)]
        (add-watch-dir! watcher root)
        ;; watch only the first path
        (add-file-callback! watcher (.toPath first-file) callback #{:create}))
      (testing "callback not invoked until directory changes"
        (is (= @results [])))
      (testing "callback invoked when a new file is created"
        (spit first-file "foo")
        (Thread/sleep 1500)
        (let [events #{{:changed-path first-file
                        :type :create}}]
          (is (= events (wait-for-events results events)))))
      (testing "callback not invoked when another new file is created"
        (reset! results [])
        (spit second-file "bar")
        (Thread/sleep 1500)
        (is (empty? @results)))
      (testing "does not report file modifications"
        (testing "of a single file"
          (reset! results [])
          (spit first-file "something different")
          (Thread/sleep 1500)
          (is (empty? @results)))
        (testing "of single file when multiple files are changed"
          (reset! results [])
          (spit first-file "still not the same as before")
          (spit second-file "still not the same as before")
          (Thread/sleep 1500)
          (is (empty? @results)))
      (testing "watch-dir! does not report file deletion"
        (testing "of single file when multiples are deleted"
          (reset! results [])
          (is (fs/delete first-file))
          (is (fs/delete second-file))
          (Thread/sleep 1500)
          (is (empty? @results))))
      (testing "re-creation of a deleted directory"
        (reset! results [])
        (let [sub-dir (fs/file root "sub-dir")]
          (testing "Initial directory creation and deletion"
            (is (fs/mkdir sub-dir))
            (Thread/sleep 1500)
            (is (empty? @results))
            (is (fs/delete sub-dir))
            (Thread/sleep 1500)
            (is (empty? @results)))
          (testing "Re-creating the directory does not create an event as expected"
            (reset! results [])
            (is (fs/mkdir sub-dir))
            (Thread/sleep 1500)
            (is (empty? @results)))))))))

(deftest ^:integration multi-callbacks-test
  (let [root-1 (fs/temp-dir "multi-root-test-1")
        root-2 (fs/temp-dir "multi-root-test-2")
        root-1-file (fs/file root-1 "test-file")
        root-2-file (fs/file root-2 "test-file")
        results-1 (atom [])
        results-2 (atom [])
        callback-1 (make-callback results-1)
        callback-2 (make-callback results-2)]
    (with-app-with-config
     app [filesystem-watch-service] {}
     (let [service (tk-app/get-service app :FilesystemWatchService)]
       (watch! service root-1 callback-1)
       (watch! service root-2 callback-2))
     (testing "callback-1 is invoked when root-1 changes"
       (spit root-1-file "foo")
       (let [events #{{:changed-path root-1-file
                       :type :create}}]
         (is (= events (wait-for-events results-1 events))))
       (testing "but not callback-2"
         (is (= @results-2 []))))
     (testing "callback-2 is invoked when root-2 changes"
       (reset! results-1 [])
       (spit root-2-file "foo")
       (let [events #{{:changed-path root-2-file
                       :type :create}}]
         (is (= events (wait-for-events results-2 events))))
       (testing "but not callback-1"
         (is (= @results-1 []))))
     (testing "both callbacks invoked when both roots change"
       (reset! results-1 [])
       (reset! results-2 [])
       (spit root-1-file "bar")
       (spit root-2-file "bar")
       (let [events-1 #{{:changed-path root-1-file
                         :type :modify}}
             events-2 #{{:changed-path root-2-file
                         :type :modify}}]
         (is (= events-1 (wait-for-events results-1 events-1)))
         (is (= events-2 (wait-for-events results-2 events-2))))))))

(deftest ^:integration nested-files-test
  (let [root-dir (fs/temp-dir "root-dir")
        intermediate-dir (fs/file root-dir "intermediate-dir")
        nested-dir (fs/file intermediate-dir "nested-dir")
        results (atom [])
        callback (make-callback results)]
    (is (fs/mkdirs nested-dir))
    (with-app-with-config
     app [filesystem-watch-service] {}
     (let [service (tk-app/get-service app :FilesystemWatchService)]
       (watch! service root-dir callback))
     (testing "file creation at root dir"
       (let [test-file (fs/file root-dir "foo")]
         (spit test-file "foo")
         (let [events #{{:changed-path test-file
                         :type :create}}]
           (is (= events (wait-for-events results events))))))
     (testing "file creation at one level down"
       (let [test-file (fs/file intermediate-dir "foo")]
         (reset! results [])
         (spit test-file "foo")
         (let [events #{{:changed-path test-file
                         :type :create}}]
           (is (= events (wait-for-events results events))))))
     (testing "file creation two levels down"
       (let [test-file (fs/file nested-dir "foo")]
         (reset! results [])
         (spit test-file "foo")
         (let [events #{{:changed-path test-file
                         :type :create}}]
           (is (= events (wait-for-events results events))))))
     (let [root-file (fs/file root-dir "bar")
           intermediate-file (fs/file intermediate-dir "bar")
           nested-file (fs/file nested-dir "bar")]
       (testing "file creation at all three levels"
         (reset! results [])
         (spit root-file "bar")
         (spit nested-file "bar")
         (spit intermediate-file "bar")
         (let [events #{{:changed-path root-file
                         :type :create}
                        {:changed-path nested-file
                         :type :create}
                        {:changed-path intermediate-file
                         :type :create}}]
           (is (= events (wait-for-events results events)))))
       (testing "modifying a nested file"
         (reset! results [])
         (spit nested-file "something new")
         (let [events #{{:changed-path nested-file
                         :type :modify}}]
           (is (= events (wait-for-events results events)))))
       (testing "deletion of a nested file"
         (reset! results [])
         (is (fs/delete nested-file))
         (let [events #{{:changed-path nested-file
                         :type :delete}}]
           (is (= events (wait-for-events results events)))))
       (testing "burn it all down"
         (reset! results [])
         (is (fs/delete-dir intermediate-dir))
         (let [events #{{:changed-path intermediate-dir
                         :type :delete}}]
           (is (every? #(= :delete (:type %)) (wait-for-events results events)))))
       (let [another-nested-dir (fs/file root-dir "another-nested-dir")
             new-nested-file (fs/file another-nested-dir "new-nested-file")]
         (testing "new nested directory"
           (testing "creation"
             (reset! results [])
             (is (fs/mkdir another-nested-dir))
             (let [events #{{:changed-path another-nested-dir
                             :type :create}}]
               (is (= events (wait-for-events results events)))))
           (testing "creation of a file within"
             (reset! results [])
             (spit new-nested-file "new nested file in nested dir")
             (let [events #{{:changed-path new-nested-file
                             :type :create}}]
               (is (= events (wait-for-events results events)))))
           (testing "deletion"
             (reset! results [])
             (is (fs/delete-dir another-nested-dir))
             (let [events #{{:changed-path another-nested-dir
                             :type :delete}
                            {:changed-path new-nested-file
                             :type :delete}}]
               (is (= events (wait-for-events results events)))))))))))

(deftest ^:integration recurse-false-test
  (testing "Watching of nested directories with recursive disabled"
    (with-app-with-config
      app [filesystem-watch-service] {}
      (let [service (tk-app/get-service app :FilesystemWatchService)
           watcher (create-watcher service {:recursive false})
           results (atom [])
           callback (make-callback results)
           root-dir (fs/temp-dir "root-dir")
           intermediate-dir (fs/file root-dir "intermediate-dir")
           nested-dir (fs/file intermediate-dir "nested-dir")
           canary-file (fs/file root-dir "canary")]

      (add-watch-dir! watcher root-dir)
      (add-callback! watcher callback)
       ;; basic smoke test - ensure with recursive off we haven't broken expected event watching
      (testing "expect events from"
        (testing "creating an intermediate directory"
          (fs/mkdirs intermediate-dir)
          ;; also make the nested-directory we'll use for testing later
          (fs/mkdirs nested-dir)
          (let [events #{{:changed-path intermediate-dir
                          :type :create}}]
            (is (= events (wait-for-events results events)))))
        (reset! results [])
        (testing "creating a file in root directory"
          (let [events #{{:changed-path canary-file
                          :type :create}}]
            (spit canary-file "foo")
            (is (= events (wait-for-events results events)))))

        (reset! results [])
        (testing "modifying a file in root directory"
          (let [events #{{:changed-path canary-file
                         :type :modify}}]
            (spit canary-file "bar")
            (is (= events (wait-for-events results events)))))

        (reset! results [])
        (testing "deleting a file in root directory"
          (let [events #{{:changed-path canary-file
                         :type :delete}}]
            (fs/delete canary-file)
            (is (= events (wait-for-events results events))))))

        ;; In these tests we expect events --not-- to occur. In order to (loosely)
        ;; validate that events are firing as expected and our test isn't giving us
        ;; false negatives, we also modify a canary file as a "control".
      (testing "expect no events from nested directories"
        (let [nested-file (fs/file nested-dir "nested file")]
          (reset! results [])
          (testing "creating a file in a nested directory"
            (let [event #{{:changed-path canary-file
                            :type :create}}]
              (spit nested-file "foo") ;; expect no events from
              (fs/touch canary-file) ;; control
              (is (= event (wait-for-exactly-event results event)))))

          (reset! results [])
          (testing "modifying a file in nested directory"
            (let [event #{{:changed-path canary-file
                            :type :modify}}]
              (spit nested-file "foo")
              (spit canary-file "bar")
              (is (= event (wait-for-exactly-event results event)))))

          (reset! results [])
          (testing "deleting a file in nested directory"
            (let [event #{{:changed-path canary-file
                            :type :modify}}]
              (fs/delete nested-file)
              (spit canary-file "baz")
              (is (= event (wait-for-exactly-event results event)))))

          (reset! results [])
          (testing "creating a directory in a nested directory"
            (let [event #{{:changed-path canary-file
                            :type :modify}}]
              (fs/mkdirs (fs/file nested-dir "foo"))
              (spit canary-file "qux")
              (is (= event (wait-for-exactly-event results event)))))

          (reset! results [])
          (testing "deleting a directory in a nested directory"
            (let [event #{{:changed-path canary-file
                            :type :modify}}]
              (fs/delete-dir (fs/file nested-dir "foo"))
              (spit canary-file "quux")
              (is (= event (wait-for-exactly-event results event)))))))))))

(deftest ^:integration multiple-watch-dirs-test
  (testing "Watching of multiple directories using a single watcher"
    (with-app-with-config
     app [filesystem-watch-service] {}
     (let [service (tk-app/get-service app :FilesystemWatchService)
           watcher (create-watcher service)
           results (atom [])
           callback (make-callback results)
           dir-1 (fs/temp-dir "dir-1")
           dir-2 (fs/temp-dir "dir-2")
           test-file-1 (fs/file dir-1 "test-file")
           test-file-2 (fs/file dir-2 "test-file")]
       (add-watch-dir! watcher dir-1 {:recursive true})
       (add-watch-dir! watcher dir-2 {:recursive true})
       (add-callback! watcher callback)
       (testing "Events in dir-1"
         (spit test-file-1 "foo")
         (let [events #{{:changed-path test-file-1
                         :type :create}}]
           (is (= events (wait-for-events results events)))))
       (reset! results [])
       (testing "Events in dir-2"
         (spit test-file-2 "foo")
         (let [events #{{:changed-path test-file-2
                         :type :create}}]
           (is (= events (wait-for-events results events)))))
       (reset! results [])
       (testing "Events in both dirs"
         (spit test-file-1 "bar")
         (spit test-file-2 "bar")
         (let [events #{{:changed-path test-file-1
                         :type :modify}
                        {:changed-path test-file-2
                         :type :modify}}]
           (is (= events (wait-for-events results events)))))
       (reset! results [])
       (testing "Nested watch directories"
         (let [nested-dir (fs/file dir-1 "nested-dir")
               test-file-nested (fs/file nested-dir "test-file")]
           (is (fs/mkdir nested-dir))
           (add-watch-dir! watcher nested-dir {:recursive true})
           ;; There is a bug in the JDK in which we misreport the changed
           ;; path if we are trying to register a new watch path when an event
           ;; comes in. This sleep allows us to finish registering the
           ;; directory before the CREATE event will come in. We are ignoring
           ;; this for the time being as the current consumer will take the
           ;; same action regardless of the path. See
           ;; https://tickets.puppetlabs.com/browse/TK-387 for more details.
           (Thread/sleep 100)
           (spit test-file-nested "foo")
           (let [events #{{:changed-path nested-dir
                           :type :create}
                          {:changed-path test-file-nested
                           :type :create}}]
             (is (= events (wait-for-events results events))))
           (reset! results [])
           (testing "Events in all three directories"
             (spit test-file-1 "baz")
             (spit test-file-2 "baz")
             (spit test-file-nested "baz")
             (let [events #{{:changed-path test-file-1
                             :type :modify}
                            {:changed-path test-file-2
                             :type :modify}
                            {:changed-path test-file-nested
                             :type :modify}}]
               (is (= events (wait-for-events results events)))))
           (reset! results [])
           (testing "Deletion thereof"
             (is (fs/delete-dir nested-dir))
             (let [events #{{:changed-path nested-dir
                             :type :delete}
                            {:changed-path test-file-nested
                             :type :delete}}]
               (is (= events (wait-for-events results events))))
             (reset! results [])
             (testing "Leaves the parent watched dir unaffected"
               (spit test-file-1 "bamboozle")
               (let [events #{{:changed-path test-file-1
                               :type :modify}}]
                 (is (= events (wait-for-events results events))))))))))))

(deftest ^:integration multiple-watcher-test
  (testing "Multiple watchers"
    (with-app-with-config
     app [filesystem-watch-service] {}
     (let [service (tk-app/get-service app :FilesystemWatchService)
           watcher-1 (create-watcher service)
           watcher-2 (create-watcher service)
           results-1 (atom [])
           callback-1 (make-callback results-1)
           results-2 (atom [])
           callback-2 (make-callback results-2)
           dir-1 (fs/temp-dir "dir-1")
           dir-2 (fs/temp-dir "dir-2")
           test-file-1 (fs/file dir-1 "test-file")
           test-file-2 (fs/file dir-2 "test-file")]
       (testing "Watching separate directories"
         (watch!* watcher-1 dir-1 callback-1 [:create :modify :delete :unknown])
         (watch!* watcher-2 dir-2 callback-2 [:create :modify :delete :unknown])
         (testing "Events do not bleed over between watchers"
           (spit test-file-1 "foo")
           (spit test-file-2 "foo")
           (let [events-1 #{{:changed-path test-file-1
                             :type :create}}
                 events-2 #{{:changed-path test-file-2
                             :type :create}}]
             (is (= events-1 (wait-for-events results-1 events-1)))
             (is (= events-2 (wait-for-events results-2 events-2))))))
       (testing "Watching the same directory"
         (reset! results-1 [])
         (reset! results-2 [])
         ;; Create a third watcher
         (let [watcher-3 (create-watcher service)
               results-3 (atom [])
               callback-3 (make-callback results-3)]
           ;; ... and tell it to watch the same directory as the first one.
           (watch!* watcher-3 dir-1 callback-3 [:create :modify :delete :unknown])
           (spit test-file-1 "bar")
           (spit test-file-2 "bar")
           (let [events-1 #{{:changed-path test-file-1
                             :type :modify}}
                 events-2 #{{:changed-path test-file-2
                             :type :modify}}]
             (is (= events-1 (wait-for-events results-1 events-1)))
             (is (= events-1 (wait-for-events results-3 events-1)))
             (is (= events-2 (wait-for-events results-2 events-2))))))))))

(deftest ^:integration callback-exception-shutdown-test
  (let [root-dir (fs/temp-dir "root-dir")
        error (Exception. "boom")
        callback (fn [& _] (throw error))
        app (tk/boot-services-with-config [filesystem-watch-service] {})
        service (tk-app/get-service app :FilesystemWatchService)]
    (watch! service root-dir callback)
    (with-test-logging
     (spit (fs/file root-dir "test-file") "foo")
     (let [reason (tk-internal/wait-for-app-shutdown app)]
       (is (= (:cause reason) :service-error))
       (is (= (:error reason) error)))
     (is (logged?
          #"shutdown-on-error triggered because of exception"
          :error)))))

(deftest ^:integration debouncing-test
  (testing "debouncing"
    (let [root-dir (fs/temp-dir "debounce")
          actual-events (atom [])
          callback-invocations (atom 0)
          callback (fn [e]
                     (swap! actual-events concat e)
                     (swap! callback-invocations inc))
          app (tk/boot-services-with-config [filesystem-watch-service] {})
          svc (tk-app/get-service app :FilesystemWatchService)
          event-cadence (quot watch-core/window-min 2)]
      ;; We trigger half the minimum number of events that could independently
      ;; occur without exceeding the window-max, given the fixed event-cadence
      ;; set above, we set the event-cadence to half of window-min to increase
      ;; the likelihood that they will debounced into a single callback
      ;; invocation. This requires that window-max be at least four times the
      ;; duration of window-min.
      (testing "multiple events trigger one callback when within window-min and window-max"
        (let [test-dir (fs/file root-dir "window-min")
              num-events (quot (quot watch-core/window-max watch-core/window-min) 2)
              files (for [n (range num-events)]
                      (fs/file test-dir (str n ".txt")))
              expected-events (set (for [f files] {:changed-path f
                                                   :type :create}))]
          (is (> num-events 1))
          (is (fs/mkdirs test-dir))
          (watch! svc test-dir callback)
          (doseq [f files]
            (fs/touch f)
            (Thread/sleep event-cadence))
          (is (= expected-events (wait-for-events actual-events expected-events)))
          (is (= @callback-invocations 1))))
      ;; We trigger events at the same cadence as above to ensure that we will
      ;; be causing events within window-min and continuing the polling loop.
      ;; We trigger them for what should be twice the duration of window-max
      ;; however to see the loop properly short circuited. The callback should be
      ;; invoked at least twice.
      (testing "multiple callbacks are triggered if polling exceeds `window-max`"
        (let [test-dir (fs/file root-dir "window-max")
              event-nums (* (quot watch-core/window-max watch-core/window-min) 4)
              files (map #(fs/file test-dir (str % ".txt")) (range 0 event-nums))
              expected-events (set (map (fn [f] {:changed-path f :type :create}) files))]
          (is (>= event-nums 4))
          (is (fs/mkdirs test-dir))
          (watch! svc test-dir callback)
          (reset! actual-events [])
          (reset! callback-invocations 0)
          (doseq [f files] (fs/touch f) (Thread/sleep event-cadence))
          (is (= expected-events (wait-for-events actual-events expected-events)))
          (is (>= @callback-invocations 2)))))))

(deftest ^:integration cannot-watch-recursive-and-not-recursive
  (testing "Cannot watch directories recursively and non-recursively with same watcher"
    (with-app-with-config
     app [filesystem-watch-service] {}
     (let [service (tk-app/get-service app :FilesystemWatchService)
           watcher (create-watcher service {:recursive false})
           results (atom [])
           _callback (make-callback results)
           first-dir (fs/temp-dir "first")
           second-dir (fs/temp-dir "second")]
      ;; adding another directory with the same recursive value is OK
      (add-watch-dir! watcher first-dir {:recursive false})
      ;; but adding another directory with a different recursive value should fail
      (is (thrown-with-msg?
            IllegalArgumentException
            #"cannot change to :recursive true"
            (add-watch-dir! watcher second-dir {:recursive true})))))))

;; Here we create a stub object that implements the WatchEvent interface as
;; the concrete class is a private inner class. See:
;; https://github.com/openjdk-mirror/jdk7u-jdk/blob/f4d80957e89a19a29bb9f9807d2a28351ed7f7df/src/share/classes/sun/nio/fs/AbstractWatchKey.java#L190-L222
;; We test only the OVERFLOW event because it is special case and will not be
;; triggered in the normal integration tests above.
(deftest clojurize-overflows
  (testing "clojurize"
    (let [watch-path (.toPath ^File (fs/temp-dir "clojurize-overflows"))
          overflow-event (reify
                          WatchEvent
                          (kind [_this] StandardWatchEventKinds/OVERFLOW)
                          (count [_this] 1)
                          (context [_this] nil))
          expected {:type :unknown :count 1 :watched-path (.toFile watch-path)}]
      (testing "overflow events are handled normally"
        (is (= expected (watch-core/clojurize overflow-event watch-path)))))))

(deftest gracefully-handles-tmpdirs
  (testing "Does not fail if directory disappears when attempting to register"
    (with-test-logging
      (DirWatchUtils/register
                 (.newWatchService (FileSystems/getDefault))
                 (Paths/get (URI. "file:///etc/foobar/baznoid")))
      (is (logged? #"Failed to register.*/etc/foobar/baznoid")))))