|
| 1 | +# pg_query_state/t/test_bad_progress_bar.pl |
| 2 | +# |
| 3 | +# Check uncorrect launches of functions progress_bar(pid) |
| 4 | +# and progress_bar_visual(pid, delay) |
| 5 | + |
| 6 | +use strict; |
| 7 | +use warnings; |
| 8 | +use PostgresNode; |
| 9 | +use TestLib; |
| 10 | +use Test::Moretests=> 2; |
| 11 | + |
| 12 | +# List of checks for bad cases: |
| 13 | +# 1) appealing to a bad pid |
| 14 | +# ------- requires DBI and DBD::Pg modules ------- |
| 15 | +# 2) extracting the state of the process itself |
| 16 | + |
| 17 | +# Test whether we have both DBI and DBD::pg |
| 18 | +my$dbdpg_rc =eval |
| 19 | +{ |
| 20 | +require DBI; |
| 21 | +require DBD::Pg; |
| 22 | + DBD::Pg->import(':async'); |
| 23 | + 1; |
| 24 | +}; |
| 25 | + |
| 26 | +# start backend for function progress_bar |
| 27 | +my$node = PostgresNode->get_new_node('master'); |
| 28 | +$node->init; |
| 29 | +$node->start; |
| 30 | +$node->append_conf('postgresql.conf',"shared_preload_libraries = 'pg_query_state'"); |
| 31 | +$node->restart; |
| 32 | +$node->psql('postgres','CREATE EXTENSION pg_query_state;'); |
| 33 | + |
| 34 | +subtest'Extracting from bad pid'=>sub { |
| 35 | +my$stderr; |
| 36 | +$node->psql('postgres','SELECT * from progress_bar(-1)',stderr=> \$stderr); |
| 37 | + is ($stderr,'psql:<stdin>:1: ERROR: backend with pid=-1 not found',"appealing to a bad pid for progress_bar"); |
| 38 | +$node->psql('postgres','SELECT * from progress_bar(-1)_visual',stderr=> \$stderr); |
| 39 | + is ($stderr,'psql:<stdin>:1: ERROR: backend with pid=-1 not found',"appealing to a bad pid for progress_bar_visual"); |
| 40 | +}; |
| 41 | + |
| 42 | +if (not$dbdpg_rc) { |
| 43 | + diag('DBI and DBD::Pg are not available, skip 2/3 tests'); |
| 44 | +} |
| 45 | + |
| 46 | +SKIP: { |
| 47 | + skip"DBI and DBD::Pg are not available", 2ifnot$dbdpg_rc; |
| 48 | + |
| 49 | +my$dbh_status = DBI->connect('DBI:Pg:' .$node->connstr($_)); |
| 50 | +if ( !defined$dbh_status ) |
| 51 | + { |
| 52 | +die"Cannot connect to database for dbh with progress_bar\n"; |
| 53 | + } |
| 54 | + |
| 55 | +my$pid_status =$dbh_status->{pg_pid}; |
| 56 | + |
| 57 | + subtest'Extracting your own status'=>sub { |
| 58 | +$dbh_status->do('SELECT * from progress_bar(' .$pid_status .')'); |
| 59 | + is($dbh_status->errstr,'ERROR: attempt to extract state of current process',"extracting the state of the process itself for progress_bar"); |
| 60 | +$dbh_status->do('SELECT * from progress_bar_visual(' .$pid_status .')'); |
| 61 | + is($dbh_status->errstr,'ERROR: attempt to extract state of current process',"extracting the state of the process itself for progress_bar_visual"); |
| 62 | + }; |
| 63 | + |
| 64 | +$dbh_status->disconnect; |
| 65 | +} |
| 66 | + |
| 67 | +$node->stop('fast'); |