Changeset 36

Show
Ignore:
Timestamp:
05/10/06 23:03:25 (4 years ago)
Author:
bradfitz
Message:

make tests test both sqlite and mysql

Location:
trunk/t
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • trunk/t/api.t

    r33 r36  
    88 
    99use TheSchwartz; 
    10 use Test::More tests => 13; 
     10use Test::More tests => 26; 
    1111 
    12 my $client = test_client(dbs => ['ts1']); 
     12run_tests(13, sub { 
    1313 
    14 my $handle; 
     14    my $client = test_client(dbs => ['ts1']); 
    1515 
    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; 
    2017 
    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"); 
    2422 
    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"); 
    2826 
    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'; 
    3430 
    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"); 
    3836 
    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'; 
    4840 
    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); 
    5150 
    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  
    88 
    99use TheSchwartz; 
    10 use Test::More tests => 2; 
     10use Test::More tests => 4; 
    1111 
    12 setup_dbs('ts1'); 
    13 teardown_dbs('ts2');  # doesn't exist 
     12run_tests(2, sub { 
     13    setup_dbs('ts1'); 
     14    teardown_dbs('ts2');  # doesn't exist 
    1415 
    15 my $client = test_client(dbs => ['ts2', 'ts1'], 
    16                          init => 0); 
     16    my $client = test_client(dbs => ['ts2', 'ts1'], 
     17                             init => 0); 
    1718 
    18 # insert a job 
    19 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"); 
    2526 
    26 # let's do some work.  the tedious way, specifying which class should grab a job 
    27 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"); 
    3233 
    33 teardown_dbs('ts1'); 
     34    teardown_dbs('ts1'); 
     35}); 
    3436 
    3537############################################################################ 
  • trunk/t/empty-db.t

    r29 r36  
    88 
    99use TheSchwartz; 
    10 use Test::More tests => 3; 
     10use Test::More tests => 6; 
    1111 
    12 teardown_dbs("tempty1"); 
     12run_tests(3, sub { 
     13    teardown_dbs("tempty1"); 
    1314 
    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                                                ]); 
    2122 
    22 # insert a job 
    23 { 
    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    } 
    3031 
    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  
    88 
    99use TheSchwartz; 
    10 use Test::More tests => 14; 
     10use Test::More tests => 28; 
    1111 
    12 my $client = test_client(dbs => ['ts1']); 
     12run_tests(14, sub { 
     13    my $client = test_client(dbs => ['ts1']); 
    1314 
    14 # insert a job 
    15 { 
    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    } 
    1920 
    20 # let's do some work.  the tedious way, specifying which class should grab a job 
    21 { 
    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 us 
    26     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"); 
    2728 
    28     # insert a dummy job to test that next grab ignors it 
    29     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])); 
    3031 
    31     # verify no more jobs can be grabbed of this type, even though 
    32     # we haven't done the first one 
    33     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"); 
    3536 
    36     my $rv = eval { Worker::Addition->work($job); }; 
    37     # .... 
    38 } 
     37        my $rv = eval { Worker::Addition->work($job); }; 
     38        # .... 
     39    } 
    3940 
    40 # insert some more jobs 
    41 { 
    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    } 
    4647 
    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    } 
    5860 
    59 # errors 
    60 { 
    61     $client->reset_abilities;           # now it, as a worker, can't do anything 
    62     $client->can_do("Worker::Division");   # now it can only do one thing 
     61    # 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 
    6365 
    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); 
    6668 
    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'; 
    6971 
    70     # wrapper around 'work' implemented in the base class which runs work in 
    71     # 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); 
    7375 
    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    } 
    7679 
    77  
    78 } 
    79  
    80 teardown_dbs('ts1'); 
     80    teardown_dbs('ts1'); 
     81}); 
    8182 
    8283############################################################################ 
     
    99100use base 'TheSchwartz::Worker'; 
    100101my %internal_dict; 
     102 
     103sub reset { %internal_dict = (); } 
    101104 
    102105sub dict { \%internal_dict } 
  • trunk/t/lib/db-common.pl

    r34 r36  
    44use File::Spec; 
    55use Carp qw(croak); 
     6 
     7sub 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} 
    626 
    727sub test_client { 
  • trunk/t/replace-with.t

    r35 r36  
    88 
    99use TheSchwartz; 
    10 use Test::More tests => 8; 
     10use Test::More tests => 16; 
    1111 
    12 my $client = test_client(dbs => ['ts1']); 
     12run_tests(8, sub { 
     13    my $client = test_client(dbs => ['ts1']); 
    1314 
    14 my $handle = $client->insert("Worker::Foo", { cluster => 'all'}); 
    15 ok($handle); 
     15    my $handle = $client->insert("Worker::Foo", { cluster => 'all'}); 
     16    ok($handle); 
    1617 
    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"); 
    1920 
    20 Worker::Foo->work_safely($job); 
     21    Worker::Foo->work_safely($job); 
    2122 
    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. 
    2425 
    25 teardown_dbs('ts1'); 
     26    teardown_dbs('ts1'); 
     27}); 
    2628 
    2729############################################################################ 
  • trunk/t/retry-delay.t

    r35 r36  
    88 
    99use TheSchwartz; 
    10 use Test::More tests => 8; 
     10use Test::More tests => 16; 
    1111 
    12 my $client = test_client(dbs => ['ts1']); 
     12run_tests(8, sub { 
     13    my $client = test_client(dbs => ['ts1']); 
    1314 
    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"; 
    1819 
    19     $client->can_do("Worker::CompleteEventually"); 
    20     $client->work_until_done; 
     20        $client->can_do("Worker::CompleteEventually"); 
     21        $client->work_until_done; 
    2122 
    22     is($handle->failures, 1, "job has failed once"); 
     23        is($handle->failures, 1, "job has failed once"); 
    2324 
    24     my $job = Worker::CompleteEventually->grab_job($client); 
    25     ok(!$job, "a job isn't ready yet"); # hasn't been two seconds 
    26     sleep 3;   # 2 seconds plus 1 buffer second 
     25        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 
    2728 
    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"); 
    3031 
    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"); 
    3334 
    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"); 
    3637 
    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    } 
    4142 
    42 teardown_dbs('ts1'); 
     43    teardown_dbs('ts1'); 
     44}); 
    4345 
    4446############################################################################