Changeset 36
- Timestamp:
- 05/10/06 23:03:25 (4 years ago)
- Location:
- trunk/t
- Files:
-
- 7 modified
-
api.t (modified) (1 diff)
-
dead-dbs.t (modified) (1 diff)
-
empty-db.t (modified) (1 diff)
-
insert-and-do.t (modified) (2 diffs)
-
lib/db-common.pl (modified) (1 diff)
-
replace-with.t (modified) (1 diff)
-
retry-delay.t (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/t/api.t
r33 r36 8 8 9 9 use TheSchwartz; 10 use Test::More tests => 13;10 use Test::More tests => 26; 11 11 12 my $client = test_client(dbs => ['ts1']); 12 run_tests(13, sub { 13 13 14 my $handle;14 my $client = test_client(dbs => ['ts1']); 15 15 16 $handle = $client->insert("feedmajor", { scoops => 2, with => ['cheese','love'] }); 17 isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job"; 18 is($handle->is_pending, 1, "job is still pending"); 19 is($handle->exit_status, undef, "job hasn't exitted yet"); 16 my $handle; 20 17 21 # to give to javascript, perl, etc... 22 my $hstr = $handle->as_string; # <digestofdsn>-<jobid> 23 ok($hstr, "handle stringifies"); 18 $handle = $client->insert("feedmajor", { scoops => 2, with => ['cheese','love'] }); 19 isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job"; 20 is($handle->is_pending, 1, "job is still pending"); 21 is($handle->exit_status, undef, "job hasn't exitted yet"); 24 22 25 my $job = $handle->job; 26 isa_ok $job, 'TheSchwartz::Job'; 27 is $job->funcname, 'feedmajor', 'handle->job gives us the right job';23 # to give to javascript, perl, etc... 24 my $hstr = $handle->as_string; # <digestofdsn>-<jobid> 25 ok($hstr, "handle stringifies"); 28 26 29 # getting a handle object back 30 my $hand2 = $client->handle_from_string($hstr); 31 ok($hand2, "handle recreated from stringified version"); 32 is($handle->is_pending, 1, "job is still pending"); 33 is($handle->exit_status, undef, "job hasn't exitted yet"); 27 my $job = $handle->job; 28 isa_ok $job, 'TheSchwartz::Job'; 29 is $job->funcname, 'feedmajor', 'handle->job gives us the right job'; 34 30 35 $job = $handle->job; 36 isa_ok $job, 'TheSchwartz::Job'; 37 is $job->funcname, 'feedmajor', 'recreated handle gives us the right job'; 31 # getting a handle object back 32 my $hand2 = $client->handle_from_string($hstr); 33 ok($hand2, "handle recreated from stringified version"); 34 is($handle->is_pending, 1, "job is still pending"); 35 is($handle->exit_status, undef, "job hasn't exitted yet"); 38 36 39 $job = TheSchwartz::Job->new( 40 funcname => 'feedmajor', 41 run_after=> time() + 60, 42 priority => 7, 43 arg => { scoops => 2, with => ['cheese','love'] }, 44 coalesce => 'major', 45 jobid => int rand(5000), 46 ); 47 ok($job); 37 $job = $handle->job; 38 isa_ok $job, 'TheSchwartz::Job'; 39 is $job->funcname, 'feedmajor', 'recreated handle gives us the right job'; 48 40 49 $handle = $client->insert($job); 50 isa_ok $handle, 'TheSchwartz::JobHandle'; 41 $job = TheSchwartz::Job->new( 42 funcname => 'feedmajor', 43 run_after=> time() + 60, 44 priority => 7, 45 arg => { scoops => 2, with => ['cheese','love'] }, 46 coalesce => 'major', 47 jobid => int rand(5000), 48 ); 49 ok($job); 51 50 52 teardown_dbs('ts1'); 51 $handle = $client->insert($job); 52 isa_ok $handle, 'TheSchwartz::JobHandle'; 53 54 teardown_dbs('ts1'); 55 }); -
trunk/t/dead-dbs.t
r34 r36 8 8 9 9 use TheSchwartz; 10 use Test::More tests => 2;10 use Test::More tests => 4; 11 11 12 setup_dbs('ts1'); 13 teardown_dbs('ts2'); # doesn't exist 12 run_tests(2, sub { 13 setup_dbs('ts1'); 14 teardown_dbs('ts2'); # doesn't exist 14 15 15 my $client = test_client(dbs => ['ts2', 'ts1'],16 init => 0);16 my $client = test_client(dbs => ['ts2', 'ts1'], 17 init => 0); 17 18 18 # insert a job19 my $n_handles = 0;20 for (1..50) {21 my $handle = $client->insert("Worker::Addition", { numbers => [1, 2] });22 $n_handles++ if $handle;23 }24 is($n_handles, 50, "got 50 handles");19 # insert a job 20 my $n_handles = 0; 21 for (1..50) { 22 my $handle = $client->insert("Worker::Addition", { numbers => [1, 2] }); 23 $n_handles++ if $handle; 24 } 25 is($n_handles, 50, "got 50 handles"); 25 26 26 # let's do some work. the tedious way, specifying which class should grab a job27 my $n_grabbed = 0;28 while (my $job = Worker::Addition->grab_job($client)) {29 $n_grabbed++;30 }31 is($n_grabbed, 50, "grabbed 50 times");27 # let's do some work. the tedious way, specifying which class should grab a job 28 my $n_grabbed = 0; 29 while (my $job = Worker::Addition->grab_job($client)) { 30 $n_grabbed++; 31 } 32 is($n_grabbed, 50, "grabbed 50 times"); 32 33 33 teardown_dbs('ts1'); 34 teardown_dbs('ts1'); 35 }); 34 36 35 37 ############################################################################ -
trunk/t/empty-db.t
r29 r36 8 8 9 9 use TheSchwartz; 10 use Test::More tests => 3;10 use Test::More tests => 6; 11 11 12 teardown_dbs("tempty1"); 12 run_tests(3, sub { 13 teardown_dbs("tempty1"); 13 14 14 my $client = TheSchwartz->new(databases => [15 {16 dsn => dsn_for('tempty1'),17 user => "",18 pass => "",19 },20 ]);15 my $client = TheSchwartz->new(databases => [ 16 { 17 dsn => dsn_for('tempty1'), 18 user => "root", 19 pass => "", 20 }, 21 ]); 21 22 22 # insert a job23 {24 my $handle;25 $handle = $client->insert("Worker::Addition", { numbers => [1, 2] });26 ok(!$handle, "can't insert into empty database");27 $handle = $client->insert("Worker::Addition", { numbers => [1, 2] });28 ok(!$handle, "still can't insert into empty database");29 }23 # insert a job 24 { 25 my $handle; 26 $handle = $client->insert("Worker::Addition", { numbers => [1, 2] }); 27 ok(!$handle, "can't insert into empty database"); 28 $handle = $client->insert("Worker::Addition", { numbers => [1, 2] }); 29 ok(!$handle, "still can't insert into empty database"); 30 } 30 31 31 ok(1, "test finishes"); 32 teardown_dbs("tempty1"); 32 ok(1, "test finishes"); 33 teardown_dbs("tempty1"); 34 }); 35 -
trunk/t/insert-and-do.t
r34 r36 8 8 9 9 use TheSchwartz; 10 use Test::More tests => 14;10 use Test::More tests => 28; 11 11 12 my $client = test_client(dbs => ['ts1']); 12 run_tests(14, sub { 13 my $client = test_client(dbs => ['ts1']); 13 14 14 # insert a job15 {16 my $handle = $client->insert("Worker::Addition", { numbers => [1, 2] });17 isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job";18 }15 # insert a job 16 { 17 my $handle = $client->insert("Worker::Addition", { numbers => [1, 2] }); 18 isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job"; 19 } 19 20 20 # let's do some work. the tedious way, specifying which class should grab a job21 {22 my $job = Worker::Addition->grab_job($client);23 isa_ok $job, 'TheSchwartz::Job';24 my $args = $job->arg;25 is(ref $args, "HASH"); # thawed it for us26 is_deeply($args, { numbers => [1, 2] }, "got our args back");21 # let's do some work. the tedious way, specifying which class should grab a job 22 { 23 my $job = Worker::Addition->grab_job($client); 24 isa_ok $job, 'TheSchwartz::Job'; 25 my $args = $job->arg; 26 is(ref $args, "HASH"); # thawed it for us 27 is_deeply($args, { numbers => [1, 2] }, "got our args back"); 27 28 28 # insert a dummy job to test that next grab ignors it29 ok($client->insert("dummy", [1,2,3]));29 # insert a dummy job to test that next grab ignors it 30 ok($client->insert("dummy", [1,2,3])); 30 31 31 # verify no more jobs can be grabbed of this type, even though32 # we haven't done the first one33 my $job2 = Worker::Addition->grab_job($client);34 ok(!$job2, "no addition jobs to be grabbed");32 # verify no more jobs can be grabbed of this type, even though 33 # we haven't done the first one 34 my $job2 = Worker::Addition->grab_job($client); 35 ok(!$job2, "no addition jobs to be grabbed"); 35 36 36 my $rv = eval { Worker::Addition->work($job); };37 # ....38 }37 my $rv = eval { Worker::Addition->work($job); }; 38 # .... 39 } 39 40 40 # insert some more jobs41 {42 ok($client->insert("Worker::MergeInternalDict", { foo => 'bar' }));43 ok($client->insert("Worker::MergeInternalDict", { bar => 'baz' }));44 ok($client->insert("Worker::MergeInternalDict", { baz => 'foo' }));45 }41 # insert some more jobs 42 { 43 ok($client->insert("Worker::MergeInternalDict", { foo => 'bar' })); 44 ok($client->insert("Worker::MergeInternalDict", { bar => 'baz' })); 45 ok($client->insert("Worker::MergeInternalDict", { baz => 'foo' })); 46 } 46 47 47 # work the easier way 48 { 49 $client->can_do("Worker::MergeInternalDict"); # single arg form: say we can do this job name, which is also its package 50 $client->work_until_done; # blocks until all databases are empty 51 is_deeply(Worker::MergeInternalDict->dict, 52 { 53 foo => "bar", 54 bar => "baz", 55 baz => "foo", 56 }, "all jobs got completed"); 57 } 48 # work the easier way 49 { 50 Worker::MergeInternalDict->reset; 51 $client->can_do("Worker::MergeInternalDict"); # single arg form: say we can do this job name, which is also its package 52 $client->work_until_done; # blocks until all databases are empty 53 is_deeply(Worker::MergeInternalDict->dict, 54 { 55 foo => "bar", 56 bar => "baz", 57 baz => "foo", 58 }, "all jobs got completed"); 59 } 58 60 59 # errors60 {61 $client->reset_abilities; # now it, as a worker, can't do anything62 $client->can_do("Worker::Division"); # now it can only do one thing61 # errors 62 { 63 $client->reset_abilities; # now it, as a worker, can't do anything 64 $client->can_do("Worker::Division"); # now it can only do one thing 63 65 64 my $handle = $client->insert("Worker::Division", { n => 5, d => 0 });65 ok($handle);66 my $handle = $client->insert("Worker::Division", { n => 5, d => 0 }); 67 ok($handle); 66 68 67 my $job = Worker::Division->grab_job($client);68 isa_ok $job, 'TheSchwartz::Job';69 my $job = Worker::Division->grab_job($client); 70 isa_ok $job, 'TheSchwartz::Job'; 69 71 70 # wrapper around 'work' implemented in the base class which runs work in71 # eval and notes a failure (with backoff) if job died.72 Worker::Division->work_safely($job);72 # wrapper around 'work' implemented in the base class which runs work in 73 # eval and notes a failure (with backoff) if job died. 74 Worker::Division->work_safely($job); 73 75 74 is($handle->failures, 1, "job has failed once"); 75 like(join('', $handle->failure_log), qr/Illegal division by zero/, "noted that we divided by zero"); 76 is($handle->failures, 1, "job has failed once"); 77 like(join('', $handle->failure_log), qr/Illegal division by zero/, "noted that we divided by zero"); 78 } 76 79 77 78 } 79 80 teardown_dbs('ts1'); 80 teardown_dbs('ts1'); 81 }); 81 82 82 83 ############################################################################ … … 99 100 use base 'TheSchwartz::Worker'; 100 101 my %internal_dict; 102 103 sub reset { %internal_dict = (); } 101 104 102 105 sub dict { \%internal_dict } -
trunk/t/lib/db-common.pl
r34 r36 4 4 use File::Spec; 5 5 use Carp qw(croak); 6 7 sub run_tests { 8 my ($n, $code) = @_; 9 10 # MySQL 11 SKIP: { 12 local $ENV{USE_MYSQL} = 1; 13 my $dbh = eval { mysql_dbh() }; 14 skip "MySQL not accessible as root on localhost", $n if $@; 15 $code->(); 16 } 17 18 # SQLite 19 SKIP: { 20 my $rv = eval "use DBD::SQLite; 1"; 21 skip "SQLite not installed", $n if $@; 22 $code->(); 23 } 24 25 } 6 26 7 27 sub test_client { -
trunk/t/replace-with.t
r35 r36 8 8 9 9 use TheSchwartz; 10 use Test::More tests => 8;10 use Test::More tests => 16; 11 11 12 my $client = test_client(dbs => ['ts1']); 12 run_tests(8, sub { 13 my $client = test_client(dbs => ['ts1']); 13 14 14 my $handle = $client->insert("Worker::Foo", { cluster => 'all'});15 ok($handle);15 my $handle = $client->insert("Worker::Foo", { cluster => 'all'}); 16 ok($handle); 16 17 17 my $job = Worker::Foo->grab_job($client);18 ok($job, "no addition jobs to be grabbed");18 my $job = Worker::Foo->grab_job($client); 19 ok($job, "no addition jobs to be grabbed"); 19 20 20 Worker::Foo->work_safely($job);21 Worker::Foo->work_safely($job); 21 22 22 $client->can_do("Worker::Foo");23 $client->work_until_done; # should process 5 jobs.23 $client->can_do("Worker::Foo"); 24 $client->work_until_done; # should process 5 jobs. 24 25 25 teardown_dbs('ts1'); 26 teardown_dbs('ts1'); 27 }); 26 28 27 29 ############################################################################ -
trunk/t/retry-delay.t
r35 r36 8 8 9 9 use TheSchwartz; 10 use Test::More tests => 8;10 use Test::More tests => 16; 11 11 12 my $client = test_client(dbs => ['ts1']); 12 run_tests(8, sub { 13 my $client = test_client(dbs => ['ts1']); 13 14 14 # insert a job which will fail, fail, then succeed.15 {16 my $handle = $client->insert("Worker::CompleteEventually");17 isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job";15 # insert a job which will fail, fail, then succeed. 16 { 17 my $handle = $client->insert("Worker::CompleteEventually"); 18 isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job"; 18 19 19 $client->can_do("Worker::CompleteEventually");20 $client->work_until_done;20 $client->can_do("Worker::CompleteEventually"); 21 $client->work_until_done; 21 22 22 is($handle->failures, 1, "job has failed once");23 is($handle->failures, 1, "job has failed once"); 23 24 24 my $job = Worker::CompleteEventually->grab_job($client);25 ok(!$job, "a job isn't ready yet"); # hasn't been two seconds26 sleep 3; # 2 seconds plus 1 buffer second25 my $job = Worker::CompleteEventually->grab_job($client); 26 ok(!$job, "a job isn't ready yet"); # hasn't been two seconds 27 sleep 3; # 2 seconds plus 1 buffer second 27 28 28 $job = Worker::CompleteEventually->grab_job($client);29 ok($job, "got a job, since time has gone by");29 $job = Worker::CompleteEventually->grab_job($client); 30 ok($job, "got a job, since time has gone by"); 30 31 31 Worker::CompleteEventually->work_safely($job);32 is($handle->failures, 2, "job has failed twice");32 Worker::CompleteEventually->work_safely($job); 33 is($handle->failures, 2, "job has failed twice"); 33 34 34 $job = Worker::CompleteEventually->grab_job($client);35 ok($job, "got the job back");35 $job = Worker::CompleteEventually->grab_job($client); 36 ok($job, "got the job back"); 36 37 37 Worker::CompleteEventually->work_safely($job);38 ok(! $handle->is_pending, "job has exited");39 is($handle->exit_status, 0, "job succeeded");40 }38 Worker::CompleteEventually->work_safely($job); 39 ok(! $handle->is_pending, "job has exited"); 40 is($handle->exit_status, 0, "job succeeded"); 41 } 41 42 42 teardown_dbs('ts1'); 43 teardown_dbs('ts1'); 44 }); 43 45 44 46 ############################################################################
