eventcore/tests/0-file.d
Sönke Ludwig 85dc8d54e6 Make file and pipe closing asynchronous.
CloseHandle can block for extended periods of time in certain cases, causing the whole thread to be blocked otherwise.
2020-05-12 09:47:06 +02:00

60 lines
1.7 KiB
D

/++ dub.sdl:
name "test"
dependency "eventcore" path=".."
debugVersions "EventCoreLogFiles"
+/
module test;
import eventcore.core;
import eventcore.socket;
import std.file : remove;
import std.socket : InternetAddress;
import core.time : Duration, msecs;
bool s_done;
void main()
{
auto f = eventDriver.files.open("test.txt", FileOpenMode.createTrunc);
assert(eventDriver.files.getSize(f) == 0);
auto data = cast(const(ubyte)[])"Hello, World!";
eventDriver.files.write(f, 0, data[0 .. 7], IOMode.all, (f, status, nbytes) {
assert(status == IOStatus.ok);
assert(nbytes == 7);
assert(eventDriver.files.getSize(f) == 7);
eventDriver.files.write(f, 5, data[5 .. $], IOMode.all, (f, status, nbytes) {
assert(status == IOStatus.ok);
assert(nbytes == data.length - 5);
assert(eventDriver.files.getSize(f) == data.length);
auto dst = new ubyte[data.length];
eventDriver.files.read(f, 0, dst[0 .. 7], IOMode.all, (f, status, nbytes) {
assert(status == IOStatus.ok);
assert(nbytes == 7);
assert(dst[0 .. 7] == data[0 .. 7]);
eventDriver.files.read(f, 5, dst[5 .. $], IOMode.all, (f, status, nbytes) {
assert(status == IOStatus.ok);
assert(nbytes == data.length - 5);
assert(dst == data);
eventDriver.files.close(f, (f, s) {
assert(s == CloseStatus.ok);
() @trusted {
scope (failure) assert(false);
remove("test.txt");
} ();
eventDriver.files.releaseRef(f);
s_done = true;
});
});
});
});
});
ExitReason er;
do er = eventDriver.core.processEvents(Duration.max);
while (er == ExitReason.idle);
assert(er == ExitReason.outOfWaiters);
assert(s_done);
s_done = false;
}