| 1 | use strict; |
|---|
| 2 | use Test::More; |
|---|
| 3 | |
|---|
| 4 | BEGIN |
|---|
| 5 | { |
|---|
| 6 | eval { require Cache::Memcached }; |
|---|
| 7 | if ($@) { |
|---|
| 8 | plan(skip_all => "Cache::Memcached not installed"); |
|---|
| 9 | } elsif (! $ENV{MVALVE_Q4M_DSN} ) { |
|---|
| 10 | plan(skip_all => "Define MVALVE_Q4M_DSN to run this test"); |
|---|
| 11 | } else { |
|---|
| 12 | plan(tests => 47); |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 15 | $ENV{MEMCACHED_SERVERS} ||= '127.0.0.1:11211'; |
|---|
| 16 | $ENV{MEMCAHCED_NAMESPACE} ||= join('_', __FILE__, $$, {}, rand()); |
|---|
| 17 | $ENV{MEMCACHED_SERVERS} = [ |
|---|
| 18 | split(/\s*,\s*/, $ENV{MEMCACHED_SERVERS}) ]; |
|---|
| 19 | |
|---|
| 20 | use_ok("Mvalve"); |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | can_ok( "Mvalve" => qw( |
|---|
| 24 | next fallback next_retry retry |
|---|
| 25 | ) ); |
|---|
| 26 | |
|---|
| 27 | { |
|---|
| 28 | my $mvalve = Mvalve->new( |
|---|
| 29 | timeout => 1, |
|---|
| 30 | queue => { |
|---|
| 31 | connect_info => [ |
|---|
| 32 | $ENV{MVALVE_Q4M_DSN}, |
|---|
| 33 | $ENV{MVALVE_Q4M_USERNAME}, |
|---|
| 34 | $ENV{MVALVE_Q4M_PASSWORD}, |
|---|
| 35 | { RaiseError => 1, AutoCommit => 1 }, |
|---|
| 36 | ] |
|---|
| 37 | }, |
|---|
| 38 | throttler => { |
|---|
| 39 | module => 'Data::Throttler::Memcached', |
|---|
| 40 | max_items => 1, |
|---|
| 41 | interval => 10, |
|---|
| 42 | cache => { |
|---|
| 43 | data => $ENV{MEMCACHED_SERVERS}, |
|---|
| 44 | namespace => $ENV{MEMCACHED_NAMESPACE}, |
|---|
| 45 | }, |
|---|
| 46 | }, |
|---|
| 47 | state => { |
|---|
| 48 | module => 'Mvalve::State::Memcached', |
|---|
| 49 | servers => $ENV{MEMCACHED_SERVERS}, |
|---|
| 50 | namespace => $ENV{MEMCACHED_NAMESPACE}, |
|---|
| 51 | } |
|---|
| 52 | ); |
|---|
| 53 | $mvalve->clear_all; |
|---|
| 54 | |
|---|
| 55 | ok( $mvalve ); |
|---|
| 56 | isa_ok( $mvalve, "Mvalve" ); |
|---|
| 57 | |
|---|
| 58 | my $count = 32; |
|---|
| 59 | |
|---|
| 60 | diag( "Generating $count messages...." ); |
|---|
| 61 | for my $i (1..$count) { |
|---|
| 62 | my $message = Mvalve::Message->new( |
|---|
| 63 | headers => { |
|---|
| 64 | 'X-Mvalve-Destination' => 'test' |
|---|
| 65 | }, |
|---|
| 66 | content => $i, |
|---|
| 67 | ); |
|---|
| 68 | $mvalve->insert( message => $message ); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | { |
|---|
| 72 | my $message = $mvalve->next; |
|---|
| 73 | isa_ok( $message, 'Mvalve::Message', 'first message should not be throttled' ); |
|---|
| 74 | |
|---|
| 75 | $count--; |
|---|
| 76 | for my $i (1..$count) { |
|---|
| 77 | my $message = $mvalve->next; |
|---|
| 78 | is( $message, undef, "subsequent messages should be throttled" ); |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | { |
|---|
| 83 | my $i = 0; |
|---|
| 84 | while ($i < $count) { |
|---|
| 85 | my $rv = $mvalve->next_retry; |
|---|
| 86 | $i++ if $rv; |
|---|
| 87 | } |
|---|
| 88 | is( $i, $count ); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|