Add basic file operation test.
This commit is contained in:
parent
1385250884
commit
9433a1ad65
63
tests/0-file.d
Normal file
63
tests/0-file.d
Normal file
|
@ -0,0 +1,63 @@
|
|||
/++ 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!";
|
||||
auto tm = eventDriver.timers.create();
|
||||
eventDriver.timers.set(tm, 500.msecs, 0.msecs);
|
||||
eventDriver.timers.wait(tm, (tm) {
|
||||
assert(false, "File operation stalled.");
|
||||
});
|
||||
|
||||
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);
|
||||
() @trusted {
|
||||
scope (failure) assert(false);
|
||||
remove("test.txt");
|
||||
} ();
|
||||
eventDriver.files.releaseRef(f);
|
||||
s_done = true;
|
||||
eventDriver.core.exit();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
ExitReason er;
|
||||
do er = eventDriver.core.processEvents(Duration.max);
|
||||
while (er == ExitReason.idle);
|
||||
//assert(er == ExitReason.outOfWaiters); // FIXME: see above
|
||||
assert(s_done);
|
||||
s_done = false;
|
||||
}
|
Loading…
Reference in a new issue