| 1 | package Test::Data::Riak; | ||
| 2 | { | ||
| 3 | $Test::Data::Riak::VERSION = '2.0'; | ||
| 4 | } | ||
| 5 | |||
| 6 | 27 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 27 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 0 | ▶ | use AnyEvent; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Try::Tiny; | ||
| 11 | use Test::More; | ||
| 12 | use Digest::MD5 qw/md5_hex/; | ||
| 13 | |||
| 14 | use Sub::Exporter; | ||
| 15 | |||
| 16 | use Data::Riak; | ||
| 17 | use Data::Riak::HTTP; | ||
| 18 | use Data::Riak::Async; | ||
| 19 | use Data::Riak::Async::HTTP; | ||
| 20 | use namespace::clean; | ||
| 21 | |||
| 22 | sub _env_key { | ||
| 23 | my ($key, $https) = @_; | ||
| 24 | sprintf 'TEST_DATA_RIAK_HTTP%s_%s', ($https ? 'S' : ''), $key; | ||
| 25 | } | ||
| 26 | |||
| 27 | my %defaults = ( | ||
| 28 | host => '127.0.0.1', | ||
| 29 | port => 8098, | ||
| 30 | timeout => 15, | ||
| 31 | protocol => 'http', | ||
| 32 | ); | ||
| 33 | |||
| 34 | for my $opt (keys %defaults) { | ||
| 35 | my $code = sub { | ||
| 36 | my ($https) = @_; | ||
| 37 | my $env_key = _env_key uc $opt, $https; | ||
| 38 | exists $ENV{$env_key} ? $ENV{$env_key} : $defaults{$opt} | ||
| 39 | }; | ||
| 40 | |||
| 41 | no strict 'refs'; | ||
| 42 | *{"_default_${opt}"} = $code; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _build_transport_args { | ||
| 46 | my ($args) = @_; | ||
| 47 | |||
| 48 | my $protocol = exists $args->{protocol} | ||
| 49 | ? $args->{protocol} : _default_protocol(); | ||
| 50 | |||
| 51 | my $https = $protocol eq 'https'; | ||
| 52 | |||
| 53 | return { | ||
| 54 | protocol => $protocol, | ||
| 55 | timeout => (exists $args->{timeout} | ||
| 56 | ? $args->{timeout} : _default_timeout($https)), | ||
| 57 | host => (exists $args->{host} | ||
| 58 | ? $args->{host} : _default_host($https)), | ||
| 59 | port => (exists $args->{port} | ||
| 60 | ? $args->{port} : _default_port($https)), | ||
| 61 | }; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _build_transport { | ||
| 65 | my $args = _build_transport_args(@_); | ||
| 66 | ($args, | ||
| 67 | Data::Riak->new({ | ||
| 68 | transport => Data::Riak::HTTP->new($args), | ||
| 69 | }), | ||
| 70 | Data::Riak::Async->new({ | ||
| 71 | transport => Data::Riak::Async::HTTP->new($args), | ||
| 72 | }), | ||
| 73 | ); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _build_riak_transport_args { | ||
| 77 | my ($class, $name, $args, $col) = @_; | ||
| 78 | sub { $col->{transport_args} }; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _build_riak_transport { | ||
| 82 | my ($class, $name, $args, $col) = @_; | ||
| 83 | sub { $col->{transport} }; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _build_async_riak_transport { | ||
| 87 | my ($class, $name, $args, $col) = @_; | ||
| 88 | sub { $col->{async_transport} }; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _build_skip_unless_riak { | ||
| 92 | my ($class, $name, $args, $col) = @_; | ||
| 93 | sub { skip_unless_riak($col->{transport}, @_) }; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _build_skip_unless_leveldb_backend { | ||
| 97 | my ($class, $name, $args, $col) = @_; | ||
| 98 | sub { skip_unless_leveldb_backend($col->{transport}, @_) }; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _build_remove_test_bucket { | ||
| 102 | my ($class, $name, $args, $col) = @_; | ||
| 103 | sub { remove_test_bucket($col->{async_transport}, @_) }; | ||
| 104 | } | ||
| 105 | |||
| 106 | my $import = Sub::Exporter::build_exporter({ | ||
| 107 | exports => [ | ||
| 108 | riak_transport_args => \&_build_riak_transport_args, | ||
| 109 | riak_transport => \&_build_riak_transport, | ||
| 110 | async_riak_transport => \&_build_async_riak_transport, | ||
| 111 | remove_test_bucket => \&_build_remove_test_bucket, | ||
| 112 | create_test_bucket_name => sub { \&create_test_bucket_name }, | ||
| 113 | skip_unless_riak => \&_build_skip_unless_riak, | ||
| 114 | skip_unless_leveldb_backend => \&_build_skip_unless_leveldb_backend, | ||
| 115 | ], | ||
| 116 | groups => { | ||
| 117 | default => [qw(riak_transport async_riak_transport riak_transport_args | ||
| 118 | remove_test_bucket create_test_bucket_name | ||
| 119 | skip_unless_riak skip_unless_leveldb_backend)], | ||
| 120 | }, | ||
| 121 | collectors => [qw(transport async_transport transport_args)], | ||
| 122 | into_level => 1, | ||
| 123 | }); | ||
| 124 | |||
| 125 | sub import { | ||
| 126 | my ($class, @args) = @_; | ||
| 127 | |||
| 128 | my ($transport_args, $transport, $async_transport) = | ||
| 129 | _build_transport(ref $args[0] eq 'HASH' ? shift @args : {}); | ||
| 130 | |||
| 131 | $import->( | ||
| 132 | $class, | ||
| 133 | transport => $transport, | ||
| 134 | async_transport => $async_transport, | ||
| 135 | transport_args => $transport_args, | ||
| 136 | @args ? @args : '-default', | ||
| 137 | ); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub create_test_bucket_name { | ||
| 141 | my $prefix = shift || 'data-riak-test'; | ||
| 142 | return sprintf '%s-%s-%s', $prefix, $$, md5_hex(scalar localtime); | ||
| 143 | } | ||
| 144 | |||
| 145 | sub skip_unless_riak { | ||
| 146 | my ($transport) = @_; | ||
| 147 | |||
| 148 | my $up = $transport->ping; | ||
| 149 | unless($up) { | ||
| 150 | plan skip_all => 'Riak did not answer, skipping tests' | ||
| 151 | }; | ||
| 152 | return $up; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub skip_unless_leveldb_backend { | ||
| 156 | my ($transport) = @_; | ||
| 157 | |||
| 158 | my $status = try { | ||
| 159 | $transport->status; | ||
| 160 | } | ||
| 161 | catch { | ||
| 162 | warn $_; | ||
| 163 | plan skip_all => "Failed to identify the Riak node's storage backend"; | ||
| 164 | }; | ||
| 165 | |||
| 166 | plan skip_all => 'This test requires the leveldb Riak storage backend' | ||
| 167 | unless $status->{storage_backend} eq 'riak_kv_eleveldb_backend'; | ||
| 168 | return; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub remove_test_bucket { | ||
| 172 | my $async_transport = shift; | ||
| 173 | my @buckets = map { | ||
| 174 | $_->isa('Data::Riak::Async::Bucket') | ||
| 175 | ? $_ : Data::Riak::Async::Bucket->new({ | ||
| 176 | riak => $async_transport, | ||
| 177 | name => $_->name, | ||
| 178 | }); | ||
| 179 | } @_; | ||
| 180 | |||
| 181 | my @cvs = map { AE::cv } @buckets; | ||
| 182 | |||
| 183 | diag 'Removing test bucket so sleeping for a moment to allow riak to eventually be consistent ...' | ||
| 184 | if $ENV{HARNESS_IS_VERBOSE}; | ||
| 185 | |||
| 186 | for my $i (0 .. $#buckets) { | ||
| 187 | _remove_test_bucket_async( | ||
| 188 | $buckets[$i], | ||
| 189 | sub { $cvs[$i]->send }, | ||
| 190 | sub { $cvs[$i]->croak(@_) }, | ||
| 191 | ); | ||
| 192 | } | ||
| 193 | |||
| 194 | for my $cv (@cvs) { | ||
| 195 | try { | ||
| 196 | $cv->recv; | ||
| 197 | } catch { | ||
| 198 | isa_ok $_, 'Data::Riak::Exception'; | ||
| 199 | }; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | sub _remove_test_bucket_async { | ||
| 205 | my ($bucket, $cb, $error_cb) = @_; | ||
| 206 | |||
| 207 | my ($remove_all_and_wait, $t); | ||
| 208 | $remove_all_and_wait = sub { | ||
| 209 | $bucket->remove_all({ | ||
| 210 | error_cb => $error_cb, | ||
| 211 | cb => sub { | ||
| 212 | $t = AE::timer 1, 0, sub { | ||
| 213 | $bucket->list_keys({ | ||
| 214 | error_cb => $error_cb, | ||
| 215 | cb => sub { | ||
| 216 | my ($keys) = @_; | ||
| 217 | |||
| 218 | if ($keys && @{ $keys }) { | ||
| 219 | $remove_all_and_wait->(); | ||
| 220 | return; | ||
| 221 | } | ||
| 222 | |||
| 223 | $cb->(); | ||
| 224 | }, | ||
| 225 | }); | ||
| 226 | }, | ||
| 227 | }, | ||
| 228 | }); | ||
| 229 | }; | ||
| 230 | |||
| 231 | $remove_all_and_wait->(); | ||
| 232 | } | ||
| 233 | |||
| 234 | __END__ | ||