EV-Websockets

 view release on metacpan or  search on metacpan

t/27-review-coverage.t  view on Meta::CPAN

    isa_ok($saved, 'EV::Websockets::Connection', "conn captured in on_error");
    is($saved->state, 'closed', "captured conn reports 'closed'");
    is($saved->send_queue_size, 0, "send_queue_size is 0 on closed conn (no croak)");
    $saved = undef;   # drop the last reference -> DESTROY
    pass("no crash after dropping the saved conn reference");
}

# 8. send_queue_size returns 0 (no croak) after the context is destroyed.
{
    my $conn;
    {
        my $ctx = EV::Websockets::Context->new();
        my %keep;
        my $port = $ctx->listen(
            port       => 0,
            on_connect => sub { $keep{srv} = $_[0] },
            on_message => sub { },
            on_close   => sub { delete $keep{srv} },
        );
        $keep{cli} = $ctx->connect(
            url        => "ws://127.0.0.1:$port",
            on_connect => sub { $conn = $_[0]; EV::break },
            on_error   => sub { EV::break },
        );
        my $to = EV::timer(10, 0, sub { diag "Timeout!"; EV::break });
        EV::run;
        # $ctx and %keep drop here -> Context::DESTROY -> conn becomes "closed"
    }

    ok($conn, "captured a live connection");
    is($conn->state, 'closed', "conn is 'closed' after context destroyed");
    is($conn->send_queue_size, 0, "send_queue_size returns 0 on closed conn");
    $conn = undef;
}

# 9. connections() excludes a still-connecting conn, includes it once open.
#    (get_protocol, peer_address and send_queue_size>0 happy paths are already
#    covered by t/18 and t/24; only the connecting-exclusion is untested.)
{
    my $ctx = EV::Websockets::Context->new();
    my %keep;
    my $port = $ctx->listen(
        port       => 0,
        on_connect => sub { $keep{srv} = $_[0] },
        on_message => sub { },
        on_close   => sub { delete $keep{srv} },
    );

    $keep{cli} = $ctx->connect(
        url        => "ws://127.0.0.1:$port",
        on_connect => sub { EV::break },
        on_error   => sub { EV::break },
    );

    my @during = $ctx->connections;   # synchronous: client is still "connecting"
    is(scalar(@during), 0, "connections() excludes a still-connecting conn");

    my $to = EV::timer(10, 0, sub { diag "Timeout!"; EV::break });
    EV::run;

    my @after = $ctx->connections;    # client + server endpoints now open
    ok(scalar(@after) >= 1, "connections() includes the established conn(s)");
    $keep{cli}->close(1000) if $keep{cli};
}

# 10. send_fragment binary path: binary fragments reassemble into one binary
#     message; $is_binary on continuation frames is ignored (type set by first).
{
    my $ctx = EV::Websockets::Context->new();
    my %keep;
    my ($got_data, $got_binary, $closed);

    my $port = $ctx->listen(
        port       => 0,
        on_connect => sub { $keep{srv} = $_[0] },
        on_message => sub {
            my ($c, $data, $is_binary) = @_;
            $got_data = $data; $got_binary = $is_binary;
            $c->close(1000);
        },
        on_close   => sub { delete $keep{srv} },
    );

    $keep{cli} = $ctx->connect(
        url        => "ws://127.0.0.1:$port",
        on_connect => sub {
            my ($c) = @_;
            $c->send_fragment("AB", 1, 0);   # binary, not final
            $c->send_fragment("CD", 0, 0);   # continuation ($is_binary ignored)
            $c->send_fragment("EF", 0, 1);   # continuation, final
        },
        on_message => sub { },
        on_close   => sub {
            $closed = 1; delete $keep{cli};
            my $t; $t = EV::timer(0.3, 0, sub { undef $t; EV::break });
        },
        on_error   => sub { delete $keep{cli}; EV::break },
    );

    my $to = EV::timer(10, 0, sub { diag "Timeout!"; EV::break });
    EV::run;

    is($got_data, "ABCDEF", "binary fragments reassembled in order");
    ok($got_binary, "reassembled message reported as binary");
    ok($closed, "connection closed");
}

# 11. connections() includes a conn in the "closing" state (drain in progress).
{
    my $ctx = EV::Websockets::Context->new();
    my %keep;
    my ($closing_listed, $closed);

    my $port = $ctx->listen(
        port       => 0,
        on_connect => sub { $keep{srv} = $_[0] },
        on_message => sub { },
        on_close   => sub { delete $keep{srv} },
    );

    $keep{cli} = $ctx->connect(



( run in 0.878 second using v1.01-cache-2.11-cpan-524268b4103 )