Data-MinHash-Shared

 view release on metacpan or  search on metacpan

t/03-frozen.t  view on Meta::CPAN

    is $ro->path, $path, 'path readable read-only';
    is $ro->memfd, -1, 'memfd readable read-only (file-backed => -1)';
    eval { $ro->sync };
    ok !$@, 'sync on read-only view is a silent no-op';

    like exception(sub { $ro->add("x") }),        qr/read-only/, 'add on read-only view croaks';
    like exception(sub { $ro->add_many(["x"]) }), qr/read-only/, 'add_many on read-only view croaks';
    like exception(sub { $ro->clear }),           qr/read-only/, 'clear on read-only view croaks';
    like exception(sub { $ro->freeze }),          qr/read-only/, 'freeze on read-only view croaks';
    {
        my $other = Data::MinHash::Shared->new(undef, $k);
        like exception(sub { $ro->merge($other) }), qr/read-only/, 'merge on read-only view croaks';
    }
}

# ---- two independent read-only views of the same file, concurrently ----
{
    my $a = Data::MinHash::Shared->new_readonly($path);
    my $b = Data::MinHash::Shared->new_readonly($path);
    is $a->similarity($b), 1, 'two read-only views of the same file compare identical';
    is_deeply [ $a->registers ], [ $b->registers ], 'two read-only views see the same registers';
}

# ---- refuse a read-write reopen of a sealed file ----
like exception(sub { Data::MinHash::Shared->new($path, $k) }),
     qr/frozen|read-only/, 'read-write reopen of a sealed file is refused';

# ---- new_readonly rejects a non-frozen file ----
{
    my $u = "$dir/unsealed.mh";
    { my $mh = Data::MinHash::Shared->new($u, 64); $mh->add("q"); }
    like exception(sub { Data::MinHash::Shared->new_readonly($u) }),
         qr/not frozen/, 'new_readonly on an unsealed file croaks';
}

# ---- new_readonly error paths ----
like exception(sub { Data::MinHash::Shared->new_readonly("$dir/does-not-exist.mh") }),
     qr/open|No such/, 'new_readonly on a missing path croaks';
like exception(sub { Data::MinHash::Shared->new_readonly(undef) }),
     qr/required/, 'new_readonly requires a path';

# ---- merge FROM a frozen (read-only) other into a mutable sketch (lock-free o read) ----
{
    my $m = "$dir/other.mh";
    my $B = Data::MinHash::Shared->new($m, $k);
    $B->add($_) for qw(xx yy zz);
    $B->freeze;
    my $Bro = Data::MinHash::Shared->new_readonly($m);

    my $A = Data::MinHash::Shared->new(undef, $k);   # same k
    $A->add("local");
    $A->merge($Bro);   # must read Bro's registers without locking its PROT_READ map

    my $U = Data::MinHash::Shared->new(undef, $k);   # reference: union built directly
    $U->add($_) for ("local", qw(xx yy zz));
    is_deeply [ $A->registers ], [ $U->registers ], 'merge from a frozen other == the union sketch';
}

done_testing;

# minimal exception helper (avoid a Test::Fatal dependency)
sub exception {
    my $code = shift;
    my $err;
    { local $@; eval { $code->(); 1 } or $err = $@; }
    return $err;
}



( run in 0.882 second using v1.01-cache-2.11-cpan-54e63673c56 )