-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |