From ca885bd5905b7faa9ecb7b0eb02476de1d3a7f88 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 27 Feb 2016 02:14:23 +0000 Subject: initial spawn implementation using vfork Under Linux, vfork maintains constant performance as parent process size increases. fork needs to prepare pages for copy-on-write, requiring a linear scan of the address space. --- t/spawn.t | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 t/spawn.t (limited to 't') diff --git a/t/spawn.t b/t/spawn.t new file mode 100644 index 00000000..ed9b5b08 --- /dev/null +++ b/t/spawn.t @@ -0,0 +1,53 @@ +# Copyright (C) 2015 all contributors +# License: AGPL-3.0+ +use strict; +use warnings; +use Test::More; +use PublicInbox::Spawn qw(which spawn); + +{ + my $true = which('true'); + ok($true, "'true' command found with which()"); +} + +{ + my $pid = spawn(['true']); + ok($pid, 'spawned process'); + is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process'); + is($?, 0, 'true exited successfully'); +} + +{ + my ($r, $w); + pipe $r, $w or die "pipe failed: $!"; + my $pid = spawn(['echo', 'hello world'], undef, { 1 => fileno($w) }); + close $w or die "close pipe[1] failed: $!"; + is(<$r>, "hello world\n", 'read stdout of spawned from pipe'); + is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process'); + is($?, 0, 'true exited successfully'); +} + +{ + my ($r, $w); + pipe $r, $w or die "pipe failed: $!"; + my $pid = spawn(['sh', '-c', 'echo $HELLO'], + { 'HELLO' => 'world' }, { 1 => fileno($w) }); + close $w or die "close pipe[1] failed: $!"; + is(<$r>, "world\n", 'read stdout of spawned from pipe'); + is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process'); + is($?, 0, 'sh exited successfully'); +} + +{ + my ($r, $w); + pipe $r, $w or die "pipe failed: $!"; + my $pid = spawn(['env'], {}, { -env => 1, 1 => fileno($w) }); + close $w or die "close pipe[1] failed: $!"; + ok(!defined(<$r>), 'read stdout of spawned from pipe'); + is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process'); + is($?, 0, 'env(1) exited successfully'); +} + +done_testing(); + +1; -- cgit v1.2.3-24-ge0c7