use warnings;
use strict;

use Data::Dumper;
use Test::More;

BEGIN {
    use_ok('IPC::Shareable');
};

# Clear stale testing segments left behind by a previously crashed run.
# clean_up_testing() only removes segments this process created or whose creator
# process has exited, so it is safe to run even under a parallel harness -- it
# will not touch a concurrently-running sibling test file's live segments.
IPC::Shareable->clean_up_testing('IPC::Shareable');
IPC::Shareable->testing_set('IPC::Shareable');

# The whole-suite *count* comparison (below, and in t/99-end) is still inherently
# global, so it stays serial-only: skip it under a parallel harness (eg. a
# smoker running with HARNESS_OPTIONS=jN).
my $parallel = defined $ENV{HARNESS_OPTIONS} && $ENV{HARNESS_OPTIONS} =~ /(?:^|:)j[0-9]/;

my $segs_before = IPC::Shareable::seg_count();
my $sems_before = IPC::Shareable::sem_count();
warn "Segs Before: $segs_before\n" if $ENV{PRINT_SEGS};

print "Starting with $segs_before segments\n";
is $segs_before, $segs_before, "Initial test ok";

tie my %store, 'IPC::Shareable', {key => 'async_tests', create => 1, serializer => 'storable' };

# Measure the baseline AFTER tying and subtract 1 to exclude the async_tests
# segment/semaphore themselves.  This keeps t/99-end.t's comparison correct
# even when a stale async_tests semaphore was orphaned by a previous crashed
# run (segment removed, semaphore not), causing the pre-tie count to be off.

$store{segs} = IPC::Shareable::seg_count() - 1;
$store{sems} = IPC::Shareable::sem_count() - 1;

{
    my $a = tie my $x, 'IPC::Shareable';
    my $b = tie my $y, 'IPC::Shareable', { create => 1, destroy => 1 , serializer => 'storable' };

    is $a->{_key}, 0, "tie with no glue or options is IPC_PRIVATE ok";
    is $b->{_key}, 0, "tie with no glue but with options is IPC_PRIVATE ok";

    $a->remove;
}

IPC::Shareable::_end;

warn "Segs After: " . IPC::Shareable::seg_count() . "\n" if $ENV{PRINT_SEGS};

SKIP: {
    skip "whole-suite IPC count check is serial-only (parallel harness detected)", 1
        if $parallel;
    is IPC::Shareable::seg_count(), $segs_before + 1, "No segs left after test suite run ok";
}

done_testing();