Skip to content

Commit

Permalink
Fix AbstractScriptFileWatcher for windows (#3388)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K authored Feb 20, 2023
1 parent cd45dfe commit c739c85
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ public abstract class AbstractScriptFileWatcher implements WatchService.WatchEve

private static final Set<String> EXCLUDED_FILE_EXTENSIONS = Set.of("txt", "old", "example", "backup", "md", "swp",
"tmp", "bak");
private static final String REGEX_SEPARATOR = "\\".equals(File.separator) ? "\\\\" : File.separator;

private static final List<Pattern> START_LEVEL_PATTERNS = List.of( //
Pattern.compile(".*/sl(\\d{2})/[^/]+"), // script in immediate slXX directory
Pattern.compile(".*/[^/]+\\.sl(\\d{2})\\.[^/.]+") // script named <name>.slXX.<ext>
);
// script in immediate slXX directory
Pattern.compile(".*" + REGEX_SEPARATOR + "sl(\\d{2})" + REGEX_SEPARATOR + "[^" + REGEX_SEPARATOR + "]+"),
// script named <name>.slXX.<ext>
Pattern.compile(".*" + REGEX_SEPARATOR + "[^" + REGEX_SEPARATOR + "]+\\.sl(\\d{2})\\.[^" + REGEX_SEPARATOR
+ ".]+"));

private final Logger logger = LoggerFactory.getLogger(AbstractScriptFileWatcher.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class AbstractScriptFileWatcherTest extends JavaTest {
private static final int DEFAULT_TEST_TIMEOUT_MS = 10000;

private @NonNullByDefault({}) AbstractScriptFileWatcher scriptFileWatcher;

Expand Down Expand Up @@ -114,7 +115,7 @@ public void testLoadOneDefaultFileAlreadyStarted() {

scriptFileWatcher.processWatchEvent(CREATE, p);

verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js", p.toString());
}

@Test
Expand All @@ -132,9 +133,9 @@ public void testSubDirectoryIncludedInInitialImport() {

awaitEmptyQueue();

verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js",
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
ScriptFileReference.getScriptIdentifier(p0));
verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js",
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
ScriptFileReference.getScriptIdentifier(p1));
}

Expand All @@ -154,7 +155,7 @@ public void testSubDirectoryIgnoredInInitialImport() {

awaitEmptyQueue();

verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js",
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
ScriptFileReference.getScriptIdentifier(p0));
verify(scriptEngineManagerMock, never()).createScriptEngine("js", ScriptFileReference.getScriptIdentifier(p1));
}
Expand Down Expand Up @@ -182,7 +183,7 @@ public void testLoadOneDefaultFileWaitUntilStarted() {

awaitEmptyQueue();

verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js", p.toString());
}

@Test
Expand All @@ -209,7 +210,7 @@ public void testLoadOneCustomFileWaitUntilStarted() {

awaitEmptyQueue();

verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js", p.toString());
}

@Test
Expand Down Expand Up @@ -243,14 +244,14 @@ public void testLoadTwoCustomFilesDifferentStartLevels() {

awaitEmptyQueue();

verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p2.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js", p2.toString());
verify(scriptEngineManagerMock, never()).createScriptEngine(anyString(), eq(p1.toString()));

updateStartLevel(80);

awaitEmptyQueue();

verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p1.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js", p1.toString());
}

@Test
Expand All @@ -277,14 +278,14 @@ public void testLoadTwoCustomFilesAlternativePatternDifferentStartLevels() {

updateStartLevel(60);

verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p2.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js", p2.toString());
verify(scriptEngineManagerMock, never()).createScriptEngine(anyString(), eq(p1.toString()));

updateStartLevel(80);

awaitEmptyQueue();

verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p1.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js", p1.toString());
}

@Test
Expand All @@ -311,7 +312,7 @@ public void testLoadOneDefaultFileDelayedSupport() {
awaitEmptyQueue();

// verify script has now been processed
verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js", p.toString());
}

@Test
Expand All @@ -337,9 +338,12 @@ public void testOrderingWithinSingleStartLevel() {

InOrder inOrder = inOrder(scriptEngineManagerMock);

inOrder.verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p64.toString());
inOrder.verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p65.toString());
inOrder.verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p66.toString());
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
p64.toString());
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
p65.toString());
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
p66.toString());
}

@Test
Expand All @@ -365,9 +369,12 @@ public void testOrderingStartlevelFolders() {
awaitEmptyQueue();

InOrder inOrder = inOrder(scriptEngineManagerMock);
inOrder.verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p30.toString());
inOrder.verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p40.toString());
inOrder.verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p50.toString());
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
p30.toString());
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
p40.toString());
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
p50.toString());
}

@Test
Expand All @@ -389,13 +396,15 @@ public void testReloadActiveWhenDependencyChanged() {

awaitEmptyQueue();

verify(scriptEngineManagerMock, timeout(10000).times(1)).createScriptEngine("js", p.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS).times(1)).createScriptEngine("js",
p.toString());

scriptFileWatcher.onDependencyChange(p.toString());

awaitEmptyQueue();

verify(scriptEngineManagerMock, timeout(10000).times(2)).createScriptEngine("js", p.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS).times(2)).createScriptEngine("js",
p.toString());
}

@Test
Expand Down Expand Up @@ -439,13 +448,15 @@ public void testRemoveBeforeReAdd() {
scriptFileWatcher.processWatchEvent(CREATE, p);

awaitEmptyQueue();
inOrder.verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", scriptIdentifier);
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
scriptIdentifier);

scriptFileWatcher.processWatchEvent(MODIFY, p);

awaitEmptyQueue();
inOrder.verify(scriptEngineManagerMock, timeout(10000)).removeEngine(scriptIdentifier);
inOrder.verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", scriptIdentifier);
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).removeEngine(scriptIdentifier);
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
scriptIdentifier);
}

@Test
Expand All @@ -466,8 +477,8 @@ public void testDirectoryAdded() {

awaitEmptyQueue();

verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p1.toString());
verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p2.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js", p1.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js", p2.toString());
}

@Test
Expand All @@ -488,9 +499,9 @@ public void testDirectoryAddedSubDirIncluded() {

awaitEmptyQueue();

verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js",
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
ScriptFileReference.getScriptIdentifier(p1));
verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js",
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
ScriptFileReference.getScriptIdentifier(p2));
}

Expand All @@ -512,7 +523,7 @@ public void testDirectoryAddedSubDirIgnored() {

awaitEmptyQueue();

verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js",
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
ScriptFileReference.getScriptIdentifier(p1));
verify(scriptEngineManagerMock, never()).createScriptEngine("js", ScriptFileReference.getScriptIdentifier(p2));
}
Expand All @@ -536,8 +547,10 @@ public void testSortsAllFilesInNewDirectory() {
awaitEmptyQueue();

InOrder inOrder = inOrder(scriptEngineManagerMock);
inOrder.verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p10.toString());
inOrder.verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p20.toString());
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
p10.toString());
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
p20.toString());
}

@Test
Expand All @@ -560,10 +573,10 @@ public void testDirectoryRemoved() {

awaitEmptyQueue();

verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p1.toString());
verify(scriptEngineManagerMock, timeout(10000)).createScriptEngine("js", p2.toString());
verify(scriptEngineManagerMock, timeout(10000)).removeEngine(p1.toString());
verify(scriptEngineManagerMock, timeout(10000)).removeEngine(p2.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js", p1.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js", p2.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).removeEngine(p1.toString());
verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).removeEngine(p2.toString());
}

@Test
Expand All @@ -585,11 +598,12 @@ public void testScriptEngineRemovedOnFailedLoad() {
awaitEmptyQueue();

InOrder inOrder = inOrder(scriptEngineManagerMock);
inOrder.verify(scriptEngineManagerMock, timeout(1000)).createScriptEngine("js",
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS)).createScriptEngine("js",
ScriptFileReference.getScriptIdentifier(p));
inOrder.verify(scriptEngineManagerMock, timeout(1000))
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS))
.loadScript(eq(ScriptFileReference.getScriptIdentifier(p)), any());
inOrder.verify(scriptEngineManagerMock, timeout(1000)).removeEngine(ScriptFileReference.getScriptIdentifier(p));
inOrder.verify(scriptEngineManagerMock, timeout(DEFAULT_TEST_TIMEOUT_MS))
.removeEngine(ScriptFileReference.getScriptIdentifier(p));
}

@Test
Expand Down

0 comments on commit c739c85

Please sign in to comment.