eventcore/tests/0-dirwatcher.d
Sönke Ludwig 86ee5c9477 Remove FileChange.isDirectory.
isDir can be a huge performance issue when watching a network based directory and can effectively block the thread almost completely in case of frequent file changes. This does mean than high-level code now needs to perform the check manually, if required, and the free information provided by inotify goes unused.
2020-05-11 19:30:40 +02:00

87 lines
2.2 KiB
D

/++ dub.sdl:
name "test"
dependency "eventcore" path=".."
+/
module test;
import eventcore.core;
import std.file : exists, isDir, mkdir, remove, rmdirRecurse;
import std.path : buildPath;
import std.stdio : File, writefln;
import core.time : Duration, msecs;
bool s_done;
int s_cnt = 0;
enum testDir = "watcher_test";
enum testFilename = "test.dat";
void main()
{
if (exists(testDir))
rmdirRecurse(testDir);
mkdir(testDir);
scope (exit) rmdirRecurse(testDir);
auto id = eventDriver.watchers.watchDirectory(testDir, false, (id, ref change) {
try {
if (change.kind == FileChangeKind.modified && isDir(buildPath(change.baseDirectory, change.directory, change.name)))
return;
} catch (Exception e) assert(false, e.msg);
switch (s_cnt++) {
default:
import std.conv : to;
assert(false, "Unexpected change: "~change.to!string);
case 0:
assert(change.kind == FileChangeKind.added);
assert(change.baseDirectory == testDir);
assert(change.directory == "");
assert(change.name == testFilename);
break;
case 1:
assert(change.kind == FileChangeKind.modified);
assert(change.baseDirectory == testDir);
assert(change.directory == "");
assert(change.name == testFilename);
break;
case 2:
assert(change.kind == FileChangeKind.removed);
assert(change.baseDirectory == testDir);
assert(change.directory == "");
assert(change.name == testFilename);
eventDriver.watchers.releaseRef(id);
s_done = true;
break;
}
});
File fil;
auto tm = eventDriver.timers.create();
eventDriver.timers.set(tm, 500.msecs, 0.msecs);
eventDriver.timers.wait(tm, (tm) {
try fil = File(testDir~"/"~testFilename, "wt");
catch (Exception e) assert(false, e.msg);
eventDriver.timers.set(tm, 1500.msecs, 0.msecs);
eventDriver.timers.wait(tm, (tm) {
scope (failure) assert(false);
fil.write("test");
fil.close();
eventDriver.timers.set(tm, 1500.msecs, 0.msecs);
eventDriver.timers.wait(tm, (tm) {
scope (failure) assert(false);
remove(testDir~"/"~testFilename);
});
});
});
ExitReason er;
do er = eventDriver.core.processEvents(Duration.max);
while (er == ExitReason.idle);
assert(er == ExitReason.outOfWaiters);
assert(s_done);
s_done = false;
}