1use strict;
2use warnings;
3use Test::More;
4use IPC::PubSub;
5use IO::Socket::INET;
6use File::Temp ':POSIX';
7
8my @backends = qw(PlainHash);
9
10unshift @backends, 'DBM_Deep' if eval { require DBM::Deep };
11unshift @backends, 'JiftyDBI' if eval { require Jifty::DBI };
12unshift @backends, 'Memcached' if eval { require Cache::Memcached } and IO::Socket::INET->new('127.0.0.1:11211');
13
14plan tests => 33 * scalar @backends;
15
16my $tmp = tmpnam();
17END { unlink $tmp }
18
19my %init_args = (
20    DBM_Deep    => [ $tmp ],
21    JiftyDBI    => [ db_init => 1 ],
22    Memcached   => [ rand() . $$ ],
23);
24
25SKIP: for my $backend (@backends) {
26    diag("Testing backend $backend");
27
28    my $bus = IPC::PubSub->new( $backend, @{ $init_args{$backend} } );
29    my $pub = $bus->new_publisher( "first", "second" );
30    my $cache = $bus->_cache;
31
32    is_deeply( scalar $pub->channels, { first => 1, second => 1 } );
33    is_deeply( [ sort $pub->channels ], [ "first", "second" ] );
34    is_deeply( $cache->publisher_indices("first"),  { $pub->uuid => 0 } );
35    is_deeply( $cache->publisher_indices("second"), { $pub->uuid => 0 } );
36    is_deeply( $cache->publisher_indices("third"),  {} );
37
38    $pub->publish("third");
39    is_deeply( scalar $pub->channels,
40        { first => 1, second => 1, third => 1 } );
41    is_deeply( [ sort $pub->channels ], [ "first", "second", "third" ] );
42    is_deeply( $cache->publisher_indices("first"),  { $pub->uuid => 0 } );
43    is_deeply( $cache->publisher_indices("second"), { $pub->uuid => 0 } );
44    is_deeply( $cache->publisher_indices("third"),  { $pub->uuid => 0 } );
45
46    $pub->publish("third");
47    is_deeply( scalar $pub->channels,
48        { first => 1, second => 1, third => 1 } );
49    is_deeply( [ sort $pub->channels ], [ "first", "second", "third" ] );
50    is_deeply( $cache->publisher_indices("third"),  { $pub->uuid => 0 } );
51
52    $pub->msg("message 1");
53    is_deeply( scalar $pub->channels,
54        { first => 2, second => 2, third => 2 } );
55
56    $pub->unpublish("second");
57    is_deeply( scalar $pub->channels, { first => 2, third => 2 } );
58
59    $pub->msg("message 2");
60    is_deeply( scalar $pub->channels, { first => 3, third => 3 } );
61
62    is_deeply( $cache->publisher_indices("first"),  { $pub->uuid => 2 } );
63    is_deeply( $cache->publisher_indices("second"), {} );
64    is_deeply( $cache->publisher_indices("third"),  { $pub->uuid => 2 } );
65
66    is($cache->get_index( first => $pub->uuid  ), 2 );
67    $cache->set_index( first => $pub->uuid, 5 );
68    is($cache->get_index( first => $pub->uuid ), 5 );
69    is_deeply( $cache->publisher_indices("first"),  { $pub->uuid => 5 } );
70
71    {
72        my $pub2 = $bus->new_publisher( "first", "second", "third" );
73        is_deeply( scalar $pub2->channels, { first => 1, second => 1, third => 1 } );
74        is_deeply( $cache->publisher_indices("first"),  { $pub->uuid => 5, $pub2->uuid => 0 } );
75        is_deeply( $cache->publisher_indices("second"), {                  $pub2->uuid => 0 } );
76        is_deeply( $cache->publisher_indices("third"),  { $pub->uuid => 2, $pub2->uuid => 0 } );
77
78        $pub2->unpublish("first");
79        is_deeply( scalar $pub2->channels, { second => 1, third => 1 } );
80        is_deeply( $cache->publisher_indices("first"),  { $pub->uuid => 5                   } );
81        is_deeply( $cache->publisher_indices("second"), {                  $pub2->uuid => 0 } );
82        is_deeply( $cache->publisher_indices("third"),  { $pub->uuid => 2, $pub2->uuid => 0 } );
83    }
84    is_deeply( $cache->publisher_indices("first"),  { $pub->uuid => 5 } );
85    is_deeply( $cache->publisher_indices("second"), {} );
86    is_deeply( $cache->publisher_indices("third"),  { $pub->uuid => 2 } );
87}
88