|
| 1 | +/* |
| 2 | + * BSD 2-Clause License |
| 3 | + * |
| 4 | + * Copyright (c) 2023, Swat.engineering |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 10 | + * list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 20 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | +package engineering.swat.watch.impl.overflows; |
| 28 | + |
| 29 | +import static org.awaitility.Awaitility.await; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.concurrent.ForkJoinPool; |
| 33 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 34 | + |
| 35 | +import org.awaitility.Awaitility; |
| 36 | +import org.junit.jupiter.api.AfterEach; |
| 37 | +import org.junit.jupiter.api.BeforeAll; |
| 38 | +import org.junit.jupiter.api.BeforeEach; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | + |
| 41 | +import engineering.swat.watch.OnOverflow; |
| 42 | +import engineering.swat.watch.TestDirectory; |
| 43 | +import engineering.swat.watch.TestHelper; |
| 44 | +import engineering.swat.watch.WatchEvent; |
| 45 | +import engineering.swat.watch.WatchScope; |
| 46 | +import engineering.swat.watch.Watcher; |
| 47 | +import engineering.swat.watch.impl.EventHandlingWatch; |
| 48 | + |
| 49 | +class IndexingRescannerTests { |
| 50 | + |
| 51 | + private TestDirectory testDir; |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + void setup() throws IOException { |
| 55 | + testDir = new TestDirectory(); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterEach |
| 59 | + void cleanup() { |
| 60 | + if (testDir != null) { |
| 61 | + testDir.close(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @BeforeAll |
| 66 | + static void setupEverything() { |
| 67 | + Awaitility.setDefaultTimeout(TestHelper.NORMAL_WAIT); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void onlyEventsForFilesInScopeAreIssued() throws IOException, InterruptedException { |
| 72 | + var path = testDir.getTestDirectory(); |
| 73 | + |
| 74 | + // Configure a non-recursive directory watch that monitors only the |
| 75 | + // children (not all descendants) of `path` |
| 76 | + var eventsOnlyForChildren = new AtomicBoolean(true); |
| 77 | + var watchConfig = Watcher.watch(path, WatchScope.PATH_AND_CHILDREN) |
| 78 | + .approximate(OnOverflow.NONE) // Disable the auto-handler here; we'll have an explicit one below |
| 79 | + .on(e -> { |
| 80 | + if (e.getRelativePath().getNameCount() > 1) { |
| 81 | + eventsOnlyForChildren.set(false); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + try (var watch = (EventHandlingWatch) watchConfig.start()) { |
| 86 | + // Create a rescanner that initially indexes all descendants (not |
| 87 | + // only the children) of `path`. The resulting initial index is an |
| 88 | + // overestimation of the files monitored by the watch. |
| 89 | + var rescanner = new IndexingRescanner( |
| 90 | + ForkJoinPool.commonPool(), path, |
| 91 | + WatchScope.PATH_AND_ALL_DESCENDANTS); |
| 92 | + |
| 93 | + // Trigger a rescan. Because only the children (not all descendants) |
| 94 | + // of `path` are watched, the rescan should issue events only for |
| 95 | + // those children (even though the initial index contains entries |
| 96 | + // for all descendants). |
| 97 | + var overflow = new WatchEvent(WatchEvent.Kind.OVERFLOW, path); |
| 98 | + rescanner.accept(watch, overflow); |
| 99 | + Thread.sleep(TestHelper.SHORT_WAIT.toMillis()); |
| 100 | + |
| 101 | + await("No events for non-children descendants should have been issued") |
| 102 | + .until(eventsOnlyForChildren::get); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments