| 1 | # -*-perl-*- |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | require 't/lib/db-common.pl'; |
|---|
| 7 | |
|---|
| 8 | use Test::More tests => 30; |
|---|
| 9 | |
|---|
| 10 | use TheSchwartz; |
|---|
| 11 | use File::Spec qw(); |
|---|
| 12 | use File::Temp qw(tempdir); |
|---|
| 13 | # create a tmp directory with a unique name. This stops |
|---|
| 14 | # us conflicting with any other runs of this process and means |
|---|
| 15 | # we tidy up after ourselves |
|---|
| 16 | my $tempdir = tempdir( CLEANUP => 1 ); |
|---|
| 17 | |
|---|
| 18 | run_tests(10, sub { |
|---|
| 19 | my $pfx = ''; |
|---|
| 20 | my $dbs = ['ts1']; |
|---|
| 21 | |
|---|
| 22 | setup_dbs({prefix => $pfx}, $dbs); |
|---|
| 23 | |
|---|
| 24 | my $client = TheSchwartz->new(scoreboard => $tempdir, |
|---|
| 25 | databases => [ |
|---|
| 26 | map { { |
|---|
| 27 | dsn => dsn_for($_), |
|---|
| 28 | user => $ENV{TS_DB_USER}, |
|---|
| 29 | pass => $ENV{TS_DB_PASS}, |
|---|
| 30 | prefix => $pfx, |
|---|
| 31 | } } @$dbs |
|---|
| 32 | ]); |
|---|
| 33 | |
|---|
| 34 | my $sb_file = $client->scoreboard; |
|---|
| 35 | { |
|---|
| 36 | (undef, my ($sb_dir, $sb_name)) = File::Spec->splitpath($sb_file); |
|---|
| 37 | ok(-e $sb_dir, "Looking for dir $sb_dir"); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | { |
|---|
| 41 | my $handle = $client->insert("Worker::Addition", |
|---|
| 42 | {numbers => [1, 2]}); |
|---|
| 43 | my $job = Worker::Addition->grab_job($client); |
|---|
| 44 | |
|---|
| 45 | my $rv = eval { Worker::Addition->work_safely($job); }; |
|---|
| 46 | ok(length($@) == 0, 'Finished job with out error') |
|---|
| 47 | or diag($@); |
|---|
| 48 | |
|---|
| 49 | unless (ok(-e $sb_file, "Scoreboard file exists")) { |
|---|
| 50 | return; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | open(FH, $sb_file) or die "Can't open '$sb_file': $!\n"; |
|---|
| 54 | |
|---|
| 55 | my %info = map { chomp; /^([^=]+)=(.*)$/ } <FH>; |
|---|
| 56 | close(FH); |
|---|
| 57 | |
|---|
| 58 | ok($info{pid} == $$, 'Has our PID'); |
|---|
| 59 | ok($info{funcname} eq 'Worker::Addition', 'Has our funcname'); |
|---|
| 60 | ok($info{started} =~ /\d+/, 'Started time is a number'); |
|---|
| 61 | ok($info{started} <= time, 'Started time is in the past'); |
|---|
| 62 | ok($info{arg} =~ /^numbers=ARRAY/, 'Has right args'); |
|---|
| 63 | ok($info{done} =~ /\d+/, 'Job has done time'); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | { |
|---|
| 67 | $client->DESTROY; |
|---|
| 68 | ok(! -e $sb_file, 'Scoreboard file goes away when worker finishes'); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | teardown_dbs('ts1'); |
|---|
| 72 | }); |
|---|
| 73 | |
|---|
| 74 | ############################################################################ |
|---|
| 75 | package Worker::Addition; |
|---|
| 76 | use base 'TheSchwartz::Worker'; |
|---|
| 77 | |
|---|
| 78 | sub work { |
|---|
| 79 | my ($class, $job) = @_; |
|---|
| 80 | |
|---|
| 81 | # .... |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | 1; |
|---|