~ubuntu-branches/ubuntu/oneiric/libanyevent-redis-perl/oneiric

« back to all changes in this revision

Viewing changes to t/pubsub.t

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghedini
  • Date: 2011-02-09 19:31:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110209193148-asa3jeyaol6nikd2
Tags: upstream-0.23
ImportĀ upstreamĀ versionĀ 0.23

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
use strict;
 
2
use Test::More;
 
3
use t::Redis;
 
4
 
 
5
test_redis {
 
6
    my $sub = shift;
 
7
    my $port = shift;
 
8
 
 
9
    my $info = $sub->info->recv;
 
10
    if($info->{redis_version} lt "1.3.10") {
 
11
      plan skip_all => "No PUBLISH/SUBSCRIBE support in this Redis version";
 
12
    }
 
13
 
 
14
    my $pub = AnyEvent::Redis->new(host => "127.0.0.1", port => $port);
 
15
 
 
16
    my $all_cv = AE::cv;
 
17
 
 
18
    # $pub is for publishing
 
19
    # $sub is for subscribing
 
20
 
 
21
    my $x = 0;
 
22
    my $expected_x = 0;
 
23
 
 
24
    my $count = 0;
 
25
    my $expected_count = 10;
 
26
 
 
27
    my $sub1_cv = $sub->subscribe("test.1", sub {
 
28
            my($message, $chan) = @_;
 
29
            $x += $message;
 
30
            if(++$count == $expected_count) {
 
31
                $sub->unsubscribe("test.1");
 
32
                is $x, $expected_x, "Messages received, values as expected";
 
33
            }
 
34
        });
 
35
    $all_cv->begin;
 
36
    $sub1_cv->cb(sub { $sub1_cv->recv; $all_cv->end });
 
37
 
 
38
    for(1 .. $expected_count) {
 
39
        my $cv = $pub->publish("test.1" => $_);
 
40
        $expected_x += $_;
 
41
        # Need to be sure a client has subscribed
 
42
        $expected_x = 0, redo unless $cv->recv;
 
43
    }
 
44
 
 
45
    # Pattern subscription
 
46
    my $y = 0;
 
47
    my $expected_y = 0;
 
48
 
 
49
    my $count2 = 0;
 
50
    my $expected_count2 = 10;
 
51
 
 
52
    my $sub2_cv = $sub->psubscribe("test.*", sub {
 
53
            my($message, $chan) = @_;
 
54
            $y += $message;
 
55
            if(++$count2 == $expected_count2) {
 
56
                $sub->punsubscribe("test.*");
 
57
                is $y, $expected_y, "Messages received, values as expected";
 
58
            }
 
59
        });
 
60
    $all_cv->begin;
 
61
    $sub2_cv->cb(sub { $sub2_cv->recv; $all_cv->end });
 
62
 
 
63
    for(1 .. $expected_count2) {
 
64
        my $cv = $pub->publish("test.$_" => $_);
 
65
        $expected_y += $_;
 
66
        # Need to be sure a client has subscribed
 
67
        $expected_y = 0, redo unless $cv->recv;
 
68
    }
 
69
 
 
70
    $all_cv->recv;
 
71
    done_testing;
 
72
};
 
73