Skip to content

Commit

Permalink
Add test for SIGCHLD coalescing.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ludwig committed Aug 21, 2019
1 parent 99f018c commit 0a6031e
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions tests/issue-122-coalesced-sigchld.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env dub
/+ dub.sdl:
name "test"
dependency "eventcore" path=".."
+/

module test;

import core.time : Duration, msecs;
import eventcore.core;
import std.process : thisProcessID;
import std.stdio;

version (Windows) {
void main()
{
writefln("Skipping SIGCHLD coalesce test on Windows.");
}
} else:

import core.sys.posix.sys.wait : waitpid, WNOHANG;

int numProc;

void main(string[] args)
{
// child mode
if (args.length == 2)
{
import core.thread : Thread;
writeln("Child: ", args[1], " from ", thisProcessID);
Thread.sleep((Clock.currStdTime - args[1].to!long).hnsecs);
return;
}

auto tm = eventDriver.timers.create();
eventDriver.timers.set(tm, 5.seconds, 0.msecs);
eventDriver.timers.wait(tm, (tm) @trusted {
assert(false, "Test hung.");
});

// attempt to let all child processes finish in exactly 1 second to force
// signal coalescing
auto targettime = Clock.currTime(UTC()) + 1.seconds;

auto procs = new ProcessID[](20);
foreach (i, ref p; procs) {
p = eventDriver.processes.spawn(
["./test", targettime.stdTime.to!string],
ProcessStdinFile(ProcessRedirect.inherit),
ProcessStdoutFile(ProcessRedirect.inherit),
ProcessStderrFile(ProcessRedirect.inherit),
null, ProcessConfig.none, null
);
assert(p != Process.init);

writeln("Started child: ", p.pid);
numProc++;
}

foreach (p; procs) {
auto wres = eventDriver.processes.wait(p.pid, (ProcessID pid, int res) nothrow
{
numProc--;
try writefln("Child %s exited with %s", pid, res);
catch(Exception){}
});

if (wres == 0) numProc--;
}

do eventDriver.core.processEvents(Duration.max);
while (numProc);

foreach (p; procs) assert(waitpid(cast(int)p, null, WNOHANG) == -1);
}

0 comments on commit 0a6031e

Please sign in to comment.