| 1 | use strict; |
| 2 | use warnings; |
| 3 | use Test::More; |
| 4 | use IPC::PubSub; |
| 5 | use IO::Socket::INET; |
| 6 | use File::Temp ':POSIX'; |
| 7 | |
| 8 | |
| 9 | my @backends = qw(PlainHash); |
| 10 | |
| 11 | unshift @backends, 'DBM_Deep' if eval { require DBM::Deep }; |
| 12 | unshift @backends, 'JiftyDBI' if eval { require Jifty::DBI }; |
| 13 | unshift @backends, 'Memcached' if eval { require Cache::Memcached } and IO::Socket::INET->new('127.0.0.1:11211'); |
| 14 | |
| 15 | plan tests => 15 * scalar @backends; |
| 16 | |
| 17 | my $tmp = tmpnam(); |
| 18 | END { unlink $tmp } |
| 19 | |
| 20 | my %init_args = ( |
| 21 | DBM_Deep => [ $tmp ], |
| 22 | JiftyDBI => [ db_init => 1 ], |
| 23 | Memcached => [ rand() . $$ ], |
| 24 | ); |
| 25 | |
| 26 | SKIP: for my $backend (@backends) { |
| 27 | diag("Testing backend $backend"); |
| 28 | |
| 29 | my $bus = IPC::PubSub->new($backend, @{$init_args{$backend}}); |
| 30 | |
| 31 | my @sub; $sub[0] = $bus->new_subscriber; |
| 32 | |
| 33 | is_deeply([map {$_->[1]} @{$sub[0]->get_all->{''}}], [], 'get_all worked when there is no pubs'); |
| 34 | is_deeply([$sub[0]->get], [], 'get_all worked when there is no pubs'); |
| 35 | |
| 36 | my $pub = $bus->new_publisher; |
| 37 | |
| 38 | $pub->msg('foo'); |
| 39 | |
| 40 | $sub[1] = $bus->new_subscriber; |
| 41 | |
| 42 | $pub->msg(['bar', 'bar']); |
| 43 | $pub->msg('baz'); |
| 44 | |
| 45 | my $got = $sub[0]->get; |
| 46 | is($got->[0], 'foo', 'get worked'); |
| 47 | is($got->[1][0], 'bar', 'get worked'); |
| 48 | is($got->[1][1], 'bar', 'get worked'); |
| 49 | is($got->[2], 'baz', 'get worked'); |
| 50 | is_deeply([$sub[0]->get], [], 'get emptied the cache'); |
| 51 | |
| 52 | is_deeply([map {ref($_) ? [@$_] : $_} map {$_->[1]} @{$sub[1]->get_all->{''}}], [['bar', 'bar'], 'baz'], 'get_all worked'); |
| 53 | is_deeply([map {ref($_) ? [@$_] : $_} map {$_->[1]} @{$sub[1]->get_all->{''}}], [], 'get_all emptied the cache'); |
| 54 | |
| 55 | is($bus->modify('key'), undef, 'modify (1)'); |
| 56 | is($bus->modify('key' => 'val'), 'val', 'modify (2)'); |
| 57 | is($bus->modify('key'), 'val', 'modify (3)'); |
| 58 | is($bus->modify('key' => sub { s/v/h/ }), 1, 'modify (4)'); |
| 59 | is($bus->modify('key'), 'hal', 'modify (5)'); |
| 60 | is($bus->modify('key' => undef), undef, 'modify (6)'); |
| 61 | } |