Async-Event-Interval

 view release on metacpan or  search on metacpan

t/13-shared_scalar_protected.t  view on Meta::CPAN

use TestHelper;
use IPC::Shareable qw(SEM_PROTECTED);
use Test::More;

use Async::Event::Interval;

my $mod = 'Async::Event::Interval';

# Helper: pull the tied knot out of a SCALAR ref returned by shared_scalar()

sub knot_of {
    my $ref = shift;
    return tied $$ref;
}

# 1) shared_scalar() segments carry the protected attribute, matching
#    _shm_lock(), and the value is persisted in SEM_PROTECTED.

{
    my $e = $mod->new(0, sub {});
    my $expected_lock = $e->_shm_lock;

    my $s    = $e->shared_scalar;
    my $knot = knot_of($s);

    is
        $knot->attributes('protected'),
        $expected_lock,
        "shared_scalar() knot's 'protected' attribute equals _shm_lock()";

    is
        $knot->sem->getval(SEM_PROTECTED),
        $expected_lock,
        "shared_scalar() segment's SEM_PROTECTED equals _shm_lock()";

t/13-shared_scalar_protected.t  view on Meta::CPAN


# 2) IPC::Shareable->clean_up_all does NOT remove a shared_scalar
#    segment, because it's protected. This was the foot-gun before
#    the protected attribute was added.

{
    my $e = $mod->new(0, sub {});
    my $s = $e->shared_scalar;
    $$s   = "before clean_up_all";

    my $id_before = knot_of($s)->seg->id;
    my $segs_before = IPC::Shareable::seg_count();

    IPC::Shareable::clean_up_all();

    my $segs_after = IPC::Shareable::seg_count();

    is
        $segs_after,
        $segs_before,
        "clean_up_all() does not remove the protected shared_scalar segment";

t/13-shared_scalar_protected.t  view on Meta::CPAN


{
    my $segs_at_start = IPC::Shareable::seg_count();

    my ($seg_id, $sem_id);
    {
        my $e = $mod->new(0, sub {});
        my $s = $e->shared_scalar;
        $$s = 42;

        $seg_id = knot_of($s)->seg->id;
        $sem_id = knot_of($s)->sem->id;

        cmp_ok
            IPC::Shareable::seg_count(),
            '>',
            $segs_at_start,
            "creating event + shared_scalar increases seg_count";

        # event goes out of scope → DESTROY → $knot->remove on the scalar
    }

t/13-shared_scalar_protected.t  view on Meta::CPAN

{
    my $segs_at_start = IPC::Shareable::seg_count();

    my @ids;
    {
        my $e = $mod->new(0, sub {});

        my @s = map { $e->shared_scalar } 1 .. 3;

        for my $ref (@s) {
            my $knot = knot_of($ref);
            push @ids, $knot->seg->id;

            is
                $knot->attributes('protected'),
                $e->_shm_lock,
                "scalar at id=" . $knot->seg->id . " is protected with event's _shm_lock()";
        }

        cmp_ok
            scalar(@ids),



( run in 1.201 second using v1.01-cache-2.11-cpan-9581c071862 )