[ Version ( deprecated_since = "2.38" , replacement = "trap_subprocess" , since = "2.16" ) ]
public bool trap_fork (uint64 usec_timeout, TestTrapFlags test_trap_flags)

Warning: trap_fork is deprecated since 2.38. Use trap_subprocess.

Fork the current test program to execute a test case that might not return or that might abort.


This function is implemented only on Unix platforms, is not always reliable due to problems inherent in fork-without-exec and doesn't set close-on-exec flag on its file descriptors. Use trap_subprocess instead.

If usec_timeout is non-0, the forked test case is aborted and considered failing if its run time exceeds it.

The forking behavior can be configured with the TestTrapFlags flags.

In the following example, the test code forks, the forked child process produces some sample output and exits successfully. The forking parent process then asserts successful child program termination and validates child program outputs.

  static void
test_fork_patterns (void)
g_print ("some stdout text: somagic17\n");
g_printerr ("some stderr text: semagic43\n");
exit (0); // successful test run
g_test_trap_assert_passed ();
g_test_trap_assert_stdout ("*somagic17*");
g_test_trap_assert_stderr ("*semagic43*");

Example: Run a case in a subprocess (deprecated):

public static int main (string[] args) {
Test.init (ref args);
Test.bug_base ("");

Test.add_func ("/valadoc/driver-0.14.x", () => {
// start a fork & reduce its runtime to 9000 micro seconds
if (Test.trap_fork (9000, TestTrapFlags.SILENCE_STDOUT | TestTrapFlags.SILENCE_STDERR)) {
// Simulate driver output:
print ("warning: unexpected token: ==\n");
stderr.printf ("error: unexpected token: ==\n");

// use assert () and friends to verify your code
// assert (false) marks a test as "not-passed"

// Make sure the fork exists at the end of our test case:
Process.exit (0);

// Optional checks & asserts:

// Check whether the last fork passed/succeeded:
if (Test.trap_has_passed ()) {
// Whatever

// Check whether GTest killed the fork due to a timeout
if (Test.trap_reached_timeout ()) {
Test.message ("timeout reached!!");

// Make sure the last forked test passed/succeeded:
Test.trap_assert_passed ();

// Make sure the forked output does match the following patterns:
// See GLib.PatternSpec for details
Test.trap_assert_stdout ("*warning:*");
Test.trap_assert_stderr ("*error:*");

// Make sure the forked output does not match the following patterns:
Test.trap_assert_stdout_unmatched ("*debug:*");
Test.trap_assert_stderr_unmatched ("*critical-error:*");
}); ();
return 0;

valac --pkg glib-2.0 GLib.Test.trap_fork.vala

./GLib.Test.trap_fork --verbose



Timeout for the forked test in micro seconds.


Flags to modify forking behaviour.


true for the forked child and false for the executing parent process.

Namespace: GLib.Test
Package: glib-2.0