Blame t/fork.t

Packit 6427f8
#!/usr/bin/perl -w
Packit 6427f8
use strict;
Packit 6427f8
use Test::More;
Packit 6427f8
use constant TESTS => 3;
Packit 6427f8
Packit 6427f8
BEGIN {
Packit 6427f8
    eval { require BSD::Resource; BSD::Resource->import() };
Packit 6427f8
Packit 6427f8
    if ($@) {
Packit 6427f8
        plan skip_all => "BSD::Resource required to test fork()";
Packit 6427f8
    }
Packit 6427f8
}
Packit 6427f8
Packit 6427f8
plan tests => TESTS;
Packit 6427f8
Packit 6427f8
# This should prevent our process from being allowed to have
Packit 6427f8
# any children.
Packit 6427f8
Packit 6427f8
my $rlimit_success = eval { setrlimit(RLIMIT_NPROC, 0, 0); };
Packit 6427f8
Packit 6427f8
SKIP: {
Packit 6427f8
    skip("setrlimit does not allow child limiting",TESTS)
Packit 6427f8
        if not $rlimit_success;
Packit 6427f8
Packit 6427f8
    # This should return undef quietly, as well as testing that
Packit 6427f8
    # fork is failing.
Packit 6427f8
    my $retval = fork();
Packit 6427f8
Packit 6427f8
    # If our fork was successful, we had better skip out!
Packit 6427f8
    if (defined $retval) {
Packit 6427f8
        $retval or exit(0);   # The child process should just exit.
Packit 6427f8
        skip("fork() still creates children after setrlimit",TESTS);
Packit 6427f8
    }
Packit 6427f8
Packit 6427f8
    eval {
Packit 6427f8
        use autodie qw(fork);
Packit 6427f8
Packit 6427f8
        fork();         # Should die.
Packit 6427f8
    };
Packit 6427f8
Packit 6427f8
    if ($@) {
Packit 6427f8
        ok(1, "autodying fork throws an exception");
Packit 6427f8
        isa_ok($@, 'autodie::exception', '... with the correct class');
Packit 6427f8
        ok($@->matches('fork'), '... which matches fork()');
Packit 6427f8
    }
Packit 6427f8
}