Acme-Parataxis

 view release on metacpan or  search on metacpan

t/353_stm.t  view on Meta::CPAN

            $got = atomically {
                my $x = $v->get;
                retry unless $x;    # parks the fiber until $v changes
                $x;
            };
        };
        await_sleep(30);            # let the executor schedule the waiter past the park
        ok $v->waiters >= 1, 'the retry waiter is registered on the TVar it read';

        # NB: a bare $v->get / $v->set here would itself croak ("must occur inside an atomically
        # block") - the waiter's read was already journaled by the fiber's own atomically above,
        # and a retry waiter only wakes on a *commit* to a TVar it read:
        atomically( sub { $v->set(7) } );    # a commit is what wakes a retry waiter
        $w->await;
    };
    is $got, 7, 'retry reran and returned once its read TVar changed';
    ok $v->waiters == 0, 'the waiter deregistered after the wake';
};
subtest 'a nested atomically joins the outer log (read-your-writes)' => sub {
    my $a = Acme::Parataxis::TVar->new( value => 10 );
    my $got;
    async {
        my $f = fiber {
            $got = atomically {
                my $x = $a->get;
                $a->set( $x + 5 );
                my $inner = atomically {
                    my $y = $a->get;      # must see the staged +5
                    $a->set( $y * 2 );    # joins the outer write set
                    $y;
                };
                $inner;
            };
        };
        $f->await;
    };
    is $got,      15, 'inner atomically saw the outer write (read-your-writes)';
    is $a->value, 30, 'the nested write set committed as one transaction';
};
subtest 'with_timeout aborts a parked transaction cleanly' => sub {
    my $v = Acme::Parataxis::TVar->new( value => 1 );
    my $caught;
    async {
        my $f = fiber {
            $caught = dies {
                with_timeout(
                    30,
                    sub {
                        atomically {
                            my $x = $v->get;
                            $v->set( $x + 1 );
                            retry;    # park; the deadline must interrupt the txn
                            1;
                        };
                    }
                );
            };
        };
        $f->await;
    };
    ok ref($caught) && $caught->isa('Acme::Parataxis::Error::Timeout'), 'a timeout interrupts a retry-parked transaction';
    is $v->value, 1, 'the aborted transaction committed nothing';
};
subtest 'TVar / atomically / retry croak outside the right context' => sub {
    my $v = Acme::Parataxis::TVar->new( value => 1 );
    like dies { $v->get }, qr[TVar operations must occur inside a scheduled fiber], 'get() outside a fiber';
    like dies {
        atomically( sub {1} )
    }, qr[must be called from inside a scheduled fiber], 'atomically() outside a fiber';
    async {
        fiber {
            like dies { $v->get },    qr[TVar operations must occur inside an atomically block],    'get() outside a txn';
            like dies { $v->set(2) }, qr[TVar operations must occur inside an atomically block],    'set() outside a txn';
            like dies {retry},        qr[retry\(\) must be called from inside an atomically block], 'retry() outside a txn';
            like dies {retry},        qr[inside],                                                   'retry() outside a fiber';
            like dies {retry},        qr[retry\(\) must be called from inside an atomically block], 'retry() outside a txn';
            like dies { $v->get },    qr[TVar operations must occur inside an atomically block],    'get() must still go through the txn';
        };
    };
};
#
done_testing;



( run in 1.328 second using v1.01-cache-2.11-cpan-234fa220139 )