diff --git a/lib/Rethinkdb/IO.pm b/lib/Rethinkdb/IO.pm index 5919e49..239d857 100644 --- a/lib/Rethinkdb/IO.pm +++ b/lib/Rethinkdb/IO.pm @@ -400,6 +400,13 @@ sub _send { say {*STDERR} Dumper $res; } + # if there is callback return data to that + if ( $self->_callbacks->{$token} ) { + my $cb = $self->_callbacks->{$token}; + delete $self->_callbacks->{$token}; + return $cb->($res); + } + return $res; } diff --git a/t/connect.t b/t/connect.t index 586ac46..024e377 100644 --- a/t/connect.t +++ b/t/connect.t @@ -46,6 +46,12 @@ eval { r->connect( 'localhost', 28015, 'better', 'hiddenkey' ); } or do { my $r = r->connect( 'localhost', 28015, 'better', '', 100 ); is $r->timeout, 100, 'Correct timeout set'; +# query without connection should throw an error +eval { r->db('test')->create->run; } or do { + like $@, qr/ERROR: run\(\) was not given a connection/, + 'Correct error on `run` without connection'; +}; + # internal stuff r->connect; is r->io, undef; @@ -149,6 +155,14 @@ r->db('test')->table('battle')->run({array_limit => 2}); $res = r->db('test')->table('battle')->run({noreply => 1}); is $res, undef, 'Correct response for noreply'; +# test a callback +$res = r->db('test')->table('battle')->run(sub { + my $res = shift; + isa_ok $res, 'Rethinkdb::Response', 'Correct response for callback'; +}); + +isa_ok $res, 'Rethinkdb::Response', 'Correct response for callback return'; + # clean up r->db('test')->drop->run;