summaryrefslogtreecommitdiff
path: root/t/10_async.t
blob: 9e88280bca4c0043ec8ff005861448a2ed3840dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use strict;
use warnings;

use Test::More;
use JSON::XS;
use Test::Exception;
use AnyEvent::Riak;
use YAML::Syck;

plan tests => 5;

my ( $host, $path );

BEGIN {
    my $riak_test = $ENV{RIAK_TEST_SERVER};
    ($host, $path) = split ";", $riak_test if $riak_test;
    plan skip_all => 'set $ENV{RIAK_TEST_SERVER} if you want to run the tests'
      unless ($host && $path);
}

my $bucket = 'test';

ok my $riak = AnyEvent::Riak->new(
    host => $host,
    path => $path,
    w    => 1,
    dw   => 1
  ),
  'create riak object';

{
    my $cv = AnyEvent->condvar;
    $cv->begin(sub { $cv->send });
    $cv->begin;
    # ping
    $riak->is_alive(
        callback => sub {
            my $res = shift;
            pass "is alive in cb" if $res;
            $cv->end;
        }
    );
    $cv->end;
    $cv->recv;
}

{
    my $cv = AnyEvent->condvar;
    $cv->begin(sub { $cv->send });
    $cv->begin;
    # list bucket
    $riak->list_bucket(
        $bucket,
        parameters => {props => 'true', keys => 'true'},
        callback   => sub {
            my $res = shift;
            ok $res->{props}, 'got props';
            $cv->end;
        }
    );
    $cv->end;
    $cv->recv;
}

{
    my $value = {foo => 'bar',};
    my $cv = AnyEvent->condvar;
    $cv->begin(sub { $cv->send });
    $cv->begin;

    # store object
    $riak->store(
        $bucket, 'bar3', $value,
        callback => sub {
            pass "store value ok";
            $riak->fetch(
                'foo', 'bar3',
                callback => sub {
                    my $body = shift;
                    is_deeply($body, $value, 'value is ok in cb');
                    $cv->end;
                }
            );
        }
    );
    $cv->end;
    $cv->recv;
}