|
| 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; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.time.Duration; |
| 35 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 36 | + |
| 37 | +import org.awaitility.Awaitility; |
| 38 | +import org.junit.jupiter.api.AfterEach; |
| 39 | +import org.junit.jupiter.api.BeforeAll; |
| 40 | +import org.junit.jupiter.api.BeforeEach; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | + |
| 43 | +class ParallelWatches { |
| 44 | + private TestDirectory testDir; |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + void setup() throws IOException { |
| 48 | + testDir = new TestDirectory(); |
| 49 | + } |
| 50 | + |
| 51 | + @AfterEach |
| 52 | + void cleanup() { |
| 53 | + if (testDir != null) { |
| 54 | + testDir.close(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @BeforeAll |
| 59 | + static void setupEverything() { |
| 60 | + Awaitility.setDefaultTimeout(TestHelper.NORMAL_WAIT); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void directoryAndFile() throws IOException { |
| 65 | + var dirTriggered = new AtomicBoolean(); |
| 66 | + var fileTriggered = new AtomicBoolean(); |
| 67 | + var file = testDir.getTestFiles().get(0); |
| 68 | + var file2 = testDir.getTestFiles().get(1); |
| 69 | + assertEquals(testDir.getTestDirectory(), file.getParent(), "Test file should be a direct child of the test dir"); |
| 70 | + assertEquals(testDir.getTestDirectory(), file2.getParent(), "Test file should be a direct child of the test dir"); |
| 71 | + try (var dirWatch = Watch.build(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN) |
| 72 | + .on(e -> dirTriggered.set(true)) |
| 73 | + .start()) { |
| 74 | + try (var fileWatch = Watch.build(file, WatchScope.PATH_ONLY).on(e -> fileTriggered.set(true)).start()) { |
| 75 | + Files.write(file, "test".getBytes()); |
| 76 | + Awaitility.await() |
| 77 | + .alias("Directory should have picked up the file") |
| 78 | + .untilTrue(dirTriggered); |
| 79 | + Awaitility.await() |
| 80 | + .alias("File should have picked up the file") |
| 81 | + .untilTrue(fileTriggered); |
| 82 | + |
| 83 | + dirTriggered.set(false); |
| 84 | + fileTriggered.set(false); |
| 85 | + |
| 86 | + Awaitility.await() |
| 87 | + .alias("should remain false") |
| 88 | + .pollDelay(TestHelper.SHORT_WAIT.minus(Duration.ofMillis(100))) |
| 89 | + .untilFalse(fileTriggered); |
| 90 | + |
| 91 | + assertFalse(dirTriggered.get(), "Directory should also have not seen new events"); |
| 92 | + |
| 93 | + Files.write(file2, "test2".getBytes()); |
| 94 | + Awaitility.await() |
| 95 | + .alias("Directory should have picked up the file") |
| 96 | + .untilTrue(dirTriggered); |
| 97 | + Awaitility.await() |
| 98 | + .alias("File should not have picked up the file") |
| 99 | + .during(TestHelper.SHORT_WAIT) |
| 100 | + .untilFalse(fileTriggered); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void nestedDirectory() throws IOException { |
| 107 | + var dirTriggered = new AtomicBoolean(); |
| 108 | + var nestedDirTriggered = new AtomicBoolean(); |
| 109 | + var dir1 = testDir.getTestDirectory(); |
| 110 | + var dir2 = dir1.resolve("nested"); |
| 111 | + Files.createDirectories(dir2); |
| 112 | + try (var dirWatch = Watch.build(dir1, WatchScope.PATH_AND_ALL_DESCENDANTS) |
| 113 | + .on(e -> dirTriggered.set(true)) |
| 114 | + .start()) { |
| 115 | + try (var nestedDirWatch = Watch.build(dir2, WatchScope.PATH_AND_CHILDREN).on(e -> nestedDirTriggered.set(true)).start()) { |
| 116 | + Files.write(dir1.resolve("a1.txt"), "1".getBytes()); |
| 117 | + Awaitility.await().alias("Directory should have picked up the file") |
| 118 | + .untilTrue(dirTriggered); |
| 119 | + Awaitility.await().alias("Nested dir should not have picked up the file") |
| 120 | + .during(TestHelper.SHORT_WAIT) |
| 121 | + .untilFalse(nestedDirTriggered); |
| 122 | + |
| 123 | + dirTriggered.set(false); |
| 124 | + |
| 125 | + Awaitility.await() |
| 126 | + .alias("should remain false") |
| 127 | + .pollDelay(TestHelper.SHORT_WAIT.minus(Duration.ofMillis(100))) |
| 128 | + .untilFalse(dirTriggered); |
| 129 | + |
| 130 | + assertFalse(nestedDirTriggered.get(), "Nested directory should also have not seen new events"); |
| 131 | + |
| 132 | + Files.write(dir2.resolve("a2.txt"), "test2".getBytes()); |
| 133 | + Awaitility.await() |
| 134 | + .alias("Directory should have picked up the file") |
| 135 | + .untilTrue(dirTriggered); |
| 136 | + Awaitility.await() |
| 137 | + .alias("Nested directory should have picked up the file") |
| 138 | + .untilTrue(nestedDirTriggered); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | +} |
0 commit comments