|
| 1 | +#!/usr/bin/perl |
| 2 | +use strict; |
| 3 | +no warnings; |
| 4 | +use Test::More; |
| 5 | +use DBI; |
| 6 | +use Getopt::Long; |
| 7 | + |
| 8 | +my$dbname; |
| 9 | +my$username; |
| 10 | +my$password; |
| 11 | +my$host; |
| 12 | +GetOptions ("--host=s"=> \$host, |
| 13 | +"--dbname=s"=> \$dbname, |
| 14 | +"--username=s"=> \$username, |
| 15 | +"--password=s"=> \$password); |
| 16 | +my$dbh = DBI->connect("dbi:Pg:dbname=$dbname; host=$host","$username","$password", |
| 17 | + {PrintError=> 0}); |
| 18 | +ok($dbh->err == 0)or (print$DBI::errstrand BAIL_OUT); |
| 19 | + |
| 20 | +my$query ="DELETE FROM test_results;"; |
| 21 | +$dbh->do($query); |
| 22 | +ok($dbh->err == 0)or (print$DBI::errstr ."\n"and$dbh->disconnect()and BAIL_OUT); |
| 23 | + |
| 24 | +$query ="SELECT schedule.create_job(\'{\"name\":\"Test 1\", |
| 25 | +\"cron\":\"* * * * *\", |
| 26 | +\"commands\": [\"SELECT pg_sleep(120)\", |
| 27 | +\"INSERT INTO test_results (time_mark, commentary) VALUES(now(), ''jobMaxInstances'')\"], |
| 28 | +\"run_as\":\"tester\", |
| 29 | +\"use_same_transaction\":\"true\", |
| 30 | +\"max_instances\": 1 |
| 31 | + }\' |
| 32 | + );"; |
| 33 | +my$sth =$dbh->prepare($query); |
| 34 | +ok($sth->execute())or (print$DBI::errstr ."\n"and$dbh->disconnect()and BAIL_OUT); |
| 35 | +my$job_id =$sth->fetchrow_array()and$sth->finish(); |
| 36 | + |
| 37 | +sleep 120; |
| 38 | +$query ="SELECT schedule.deactivate_job(?)"; |
| 39 | +$sth =$dbh->prepare($query); |
| 40 | +$sth->bind_param(1,$job_id); |
| 41 | +ok($sth->execute(),$dbh->errstr)orprint$DBI::errstr ."\n"; |
| 42 | +$sth->finish(); |
| 43 | + |
| 44 | +$query ="SELECT message FROM schedule.get_log() WHERE cron=$job_id AND status=\'error\' ORDER BY cron DESC LIMIT 1"; |
| 45 | +my$sth =$dbh->prepare($query); |
| 46 | +ok($sth->execute())or (print$DBI::errstr ."\n"and$dbh->disconnect()and BAIL_OUT); |
| 47 | +my$errorstr =$sth->fetchrow_array()and$sth->finish(); |
| 48 | +ok($errorstreq"max instances limit reached")orprint$DBI::errstr ."\n"; |
| 49 | + |
| 50 | +$query ="DELETE FROM test_results;"; |
| 51 | +$dbh->do($query); |
| 52 | +ok($dbh->err == 0,$dbh->errstr)orprint$DBI::errstr ."\n"; |
| 53 | + |
| 54 | +$query ="SELECT schedule.drop_job(?)"; |
| 55 | +$sth =$dbh->prepare($query); |
| 56 | +$sth->bind_param(1,$job_id); |
| 57 | +ok($sth->execute(),$dbh->errstr)orprint$DBI::errstr ."\n"; |
| 58 | +$sth->finish(); |
| 59 | + |
| 60 | +$dbh->disconnect(); |
| 61 | + |
| 62 | +done_testing(); |
| 63 | + |