use strict;
use warnings;
use Test::More;
use MyNote;
use File::Temp;

use vars qw($tmpdir @dat $I $N);

BEGIN {
    $I = 4;    # number for forks
    $N = 1000; # number of uuids / fork
    $tmpdir  = File::Temp->newdir(CLEANUP => 0);
    $dat[$_] = File::Temp::tempnam($tmpdir, 'UUID.test.') for 0 .. $I-1;
    pass 'began';
}

use UUID 'uuid6';

my @kids = ();
my $seen = {};

# Comment this to simulate a prefork process that
# doesnt actually use UUID in the parent.
++$seen->{uuid6()} for 1 .. $N;

for my $i ( 0 .. $I-1 ) {
    my $kid;
    if (!defined($kid = fork)) {
        fail "fork$i";
    }
    else {
        if ($kid) {
            pass "fork$i";
            push @kids, $kid;
        }
        else {
            open my $fh, '>', $dat[$i] or err("open: $dat[$i]: $!");
            print $fh uuid6()."\n"
                for 1 .. $N;
            exit 0;
        }
    }
}

waitpid $_, 0 for @kids;

++$seen->{uuid6()} for 1 .. $N;

for my $i (0 .. $I-1) {
    open my $fh, '<', $dat[$i] or err("open: $dat[$i]: $!");
    ++$seen->{$_} for map{chomp;$_} <$fh>;
}

#use Data::Dumper;
#note "$_\n" for split /\n/, Dumper($seen);
{
    my $expected = ($I + 2) * $N;
    is scalar(keys %$seen), $expected, "seen $expected";
}

cleanup();
done_testing;

sub err {
    my $error = shift;
    my (undef, $F, $L) = caller;
    cleanup();
    die "$error at $F line $L\n";
}

sub cleanup {
    for my $i (0 .. $I-1) {
        unlink $dat[$i] if -e $dat[$i];
    }
    rmdir  $tmpdir if defined $tmpdir;
}
