miyagawa / AnyEvent-Redis

Asynchronous Redis client
http://search.cpan.org/dist/AnyEvent-Redis
41 stars 16 forks source link

Some some "subscription" event callback #22

Open basiliscos opened 10 years ago

basiliscos commented 10 years ago

It is needed to have such an notification, when the subscription succeeds, and only then publish events (to be caught by subscriber).

The following test demonstrates the issue:

use strict;
use AnyEvent;
use Test::More;
use t::Redis;

test_redis {
    my $sub = shift;
    my $port = shift;

    my $info = $sub->info->recv;
    if($info->{redis_version} lt "1.3.10") {
      plan skip_all => "No PUBLISH/SUBSCRIBE support in this Redis version";
    }

    my $sub = AnyEvent::Redis->new(host => "127.0.0.1", port => $port);
    # umcomment the following to let the test pass
    # $sub->ping->recv;
    my $pub = AnyEvent::Redis->new(host => "127.0.0.1", port => $port);

    my $cv = AnyEvent->condvar;
    $sub->subscribe("test", sub {
            my($message, $chan) = @_;
            $cv->send(1);
        });
    $pub->publish("test", "test")->recv;
    my $guard = AnyEvent->timer(after => 5, cb => sub { $cv->send(0); });

    ok $cv->recv, "got published message";

    done_testing;
};

The test passed and fails time-to-time. If ping will be uncommented it will always succeed. But pingin isn't elegant solution.

Thanks.