diff options
author | franck cuny <franck@lumberjaph.net> | 2010-05-09 15:39:56 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-05-09 15:39:56 +0200 |
commit | b5ab9921fda792d0fcc37b29b19c69d0686ea258 (patch) | |
tree | 6455f2a9895b93b680302da29705572fc8e3485a /t | |
parent | error, check if queue is open before sending job, some small tweak (diff) | |
download | presque-b5ab9921fda792d0fcc37b29b19c69d0686ea258.tar.gz |
start to write tests
Diffstat (limited to 't')
-rw-r--r-- | t/10_basic.t | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/t/10_basic.t b/t/10_basic.t new file mode 100644 index 0000000..5b12d4f --- /dev/null +++ b/t/10_basic.t @@ -0,0 +1,108 @@ +use strict; +use warnings; + +use Test::More; +use Plack::Test; + +use JSON; +use HTTP::Request; +use presque; + +$Plack::Test::Impl = 'Server'; + +my $app = presque->app( + config => { + redis => { + host => '127.0.0.1', + port => 6379 + } + } +); + +my $queue = 'presque_test'; +my $queue_url = "http://localhost/q/$queue"; +my $job_url = "http://localhost/j/$queue"; +my $stat_url = "http://localhost/stats/"; +my $control_url = "http://localhost/control/$queue"; + +test_psgi $app, sub { + my $cb = shift; + + # get queue informations + my $req = HTTP::Request->new( GET => $job_url ); + ok my $res = $cb->($req), 'get info on an empty queue'; + is_deeply JSON::decode_json $res->content, { + jobs => undef, + job_count => 0, + queue => $queue, + queue_size => 0 + }, 'good job info result'; + + # no job in queue + $req = HTTP::Request->new( GET => $queue_url ); + ok $res = $cb->($req), 'first request done'; + ok !$res->is_success, 'no job for this queue'; + is_deeply JSON::decode_json( $res->content ), { error => "no job" }, + 'error message is valid'; + + # fail to create a new job + $req = HTTP::Request->new( POST => $queue_url ); + $req->content( JSON::encode_json( { foo => 'bar' } ) ); + $res = $cb->($req); + ok !$res->is_success, 'content-type is not set to json'; + + # insert a job + $req->header( 'Content-Type' => 'application/json' ); + $res = $cb->($req); + ok $res->is_success, 'new job inserted'; + + # info about a queue + $req = HTTP::Request->new( GET => $job_url ); + $res = $cb->($req); + my $content = JSON::decode_json $res->content; + ok grep {/presque_test:1/} @{$content->{jobs}}, 'find jobs'; + + # delayed job + ok $cb->( HTTP::Request->new( GET => $queue_url ) ), 'purged jobs'; + $req = HTTP::Request->new( + POST => $queue_url . '?delayed=' . ( time() + 2 ) ); + $req->header( 'Content-Type' => 'application/json' ); + $req->content( JSON::encode_json { foo => 'baz' } ); + ok $res = $cb->($req), 'delayed job inserted'; + $req = HTTP::Request->new( GET => $queue_url ); + $res = $cb->($req); + ok !$res->is_success, 'no job'; + sleep(2); + $res = $cb->($req); + ok $res->is_success, 'job found'; + like $res->content, qr/baz/, 'delayed job'; + + # control queue + $req = HTTP::Request->new( GET => $control_url ); + $res = $cb->($req); + $content = JSON::decode_json $res->content; + is_deeply $content, { + status => 1, + queue => 'presque_test' + }, + 'queue is open'; + + # close queue + $req = HTTP::Request->new(POST => $control_url); + $req->content(JSON::encode_json({status => 'stop'})); + $res = $cb->($req); + like $res->content, qr/OK/, 'queue status change'; + + # status of a closed queue + $req = HTTP::Request->new( GET => $control_url ); + $res = $cb->($req); + like $res->content, qr/0/, 'queue is closed'; + + # can't get job on a stopped queue + $req = HTTP::Request->new( GET => $queue_url ); + $res = $cb->($req); + ok !$res->is_success, 'no job for this queue'; +}; + +done_testing; + |