<commit_before>use strict;
use warnings;

use Test::More;

use AngelPS1::System;

use Sub::Util ();

AngelPS1::System->use;

my @gen_count_jobs_impl = AngelPS1::System->_count_jobs_impl;

plan tests => 1 + 6 * @gen_count_jobs_impl;

cmp_ok(scalar @gen_count_jobs_impl, '>=', 1, 'Has available implementations');

note "\$\$: $$";

for my $impl (@gen_count_jobs_impl) {

    my $impl_name = Sub::Util::subname($impl);

    # We will count jobs child or our process
    my $count_jobs = AngelPS1::System->$impl($$);

    SKIP: {
	ok($count_jobs, "$impl_name works")
	    or skip 5, "$impl_name doesn't work :(";
	is_deeply([ $count_jobs->() ], [ 0, 0 ], "First run")
	    or skip 4, 'basic test failed';

	if (my $child = fork) {
	    select undef, undef, undef, 0.25; # Sleep 0.25 second
	    system("ps --ppid $$ -o ppid -o pgid -o pid -o stat");
	    is_deeply([ $count_jobs->() ], [ 0, 1 ], "Background: 1");
	    kill TERM => $child;
	    wait;
	} else {
	    setpgrp 0, $$;
	    note "Sleeping child $$...\n";
	    sleep 1;
	    exit 0;
	}

	is_deeply([ $count_jobs->() ], [ 0, 0 ], "Done.");

	if (my $child = fork) {
	    select undef, undef, undef, 0.25; # Sleep 0.25 second
	    kill STOP => $child;
	    system("ps --ppid $$ -o ppid -o pgid -o pid -o stat");
	    is_deeply([ $count_jobs->() ], [ 1, 0 ], "Suspended: 1");
	    kill CONT => $child;
	    kill TERM => $child;
	    wait;
	} else {
	    setpgrp 0, $$;
	    note "Sleeping child $$...\n";
	    sleep 2;
	    exit 0;
	}

	is_deeply([ $count_jobs->() ], [ 0, 0 ], "Done.");
    }
}
<commit_msg>41-count_jobs.t: rewrite usings pipes for parent/child sync. Faster!<commit_after>use strict;
use warnings;

use Test::More;

use AngelPS1::System;

use Sub::Util ();

AngelPS1::System->use;

my @gen_count_jobs_impl = AngelPS1::System->_count_jobs_impl;

plan tests => 1 + 7 * @gen_count_jobs_impl;

cmp_ok(scalar @gen_count_jobs_impl, '>=', 1, 'Has available implementations');

note "\$\$: $$";

for my $impl (@gen_count_jobs_impl) {

    my $impl_name = Sub::Util::subname($impl);

    # We will count jobs child or our process
    my $count_jobs = AngelPS1::System->$impl($$);

    SKIP: {
	ok($count_jobs, "$impl_name works")
	    or skip 5, "$impl_name doesn't work :(";
	is_deeply([ $count_jobs->() ], [ 0, 0 ], "First run")
	    or skip 4, 'basic test failed';

	my ($in, $out);
	ok(pipe($in, $out), "pipe")
	    or do {
	    diag "\$!: $!";
	    skip "pipe failed", 3
	};

	if (my $child = fork) {
	    close $out;
	    <$in>; close $in;
	    system("ps --ppid $$ -o ppid -o pgid -o pid -o stat");
	    is_deeply([ $count_jobs->() ], [ 0, 1 ], "Background: 1");
	    kill TERM => $child;
	    wait;
	} else {
	    setpgrp 0, $$;
	    close $in;
	    note "Sleeping child $$...\n";
	    print $out "Ready.\n"; close $out;
	    sleep 1;
	    exit 0;
	}

	is_deeply([ $count_jobs->() ], [ 0, 0 ], "Done.");
	pipe $in, $out;

	if (my $child = fork) {
	    close $out;
	    <$in>; close $in;
	    kill STOP => $child;
	    system("ps --ppid $$ -o ppid -o pgid -o pid -o stat");
	    is_deeply([ $count_jobs->() ], [ 1, 0 ], "Suspended: 1");
	    kill CONT => $child;
	    kill TERM => $child;
	    wait;
	} else {
	    setpgrp 0, $$;
	    close $in;
	    note "Sleeping child $$...\n";
	    print $out "Ready.\n"; close $out;
	    sleep 1;
	    exit 0;
	}

	is_deeply([ $count_jobs->() ], [ 0, 0 ], "Done.");
    }
}

{'fraction_non_alphanumeric': 0.19592198581560283, 'fraction_numerical': 0.020390070921985817, 'mean_word_length': 2.7156970362239297, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2438329', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1282, 'n_words': 367}