#!/usr/bin/env perl use strict; use warnings; use Test::More; use DBI; my $dbname = $ENV{PGDATABASE} || 'postgres'; my $host = $ENV{PGHOST} || 'localhost'; my $port = 5432; my $user = $ENV{PGUSER} || $ENV{USER}; # connect my $creds = "dbi:Pg:dbname=$dbname;host=$host;port=$port"; my $sess1 = DBI->connect($creds, $user, '', { RaiseError => 1, AutoCommit => 0 }); my $pid; # forking alert process $pid = fork(); if ($pid == 0) { my $sess2 = DBI->connect($creds, $user, '', { RaiseError => 1, AutoCommit => 0 }); sleep(3); # Вызываем функцию пробуждения eval { $sess2->do("SELECT dbms_alert_signal('test_alert_event', 'test_msg')") }; die "function_b failed: $@" if $@; $sess2->commit(); exit(0); } my $test_count = 0; $sess1->do("SELECT dbms_alert_register('test_alert_event')"); # this is going to hang until alert arrives my $wait = $sess1->prepare("SELECT * FROM dbms_alert_waitone('test_alert_event', NULL, NULL, 30)"); $wait->execute(); # somewhere here an alert happens sleep 1; # back to life and get data my ($raw_msg, $raw_stat) = $wait->fetchrow_array(); #$sess1->commit(); chomp($raw_msg); chomp($raw_stat); # purge \n $raw_msg =~ s/^\s+|\s+$//g; # strips spaces $raw_stat =~ s/^\s+|\s+$//g; my $msg = $raw_msg; my $stat = $raw_stat; $test_count++; ok($msg eq 'test_msg' && $stat eq '0', 'Alert signal received from another session'); # Cleanup #$sess1->do(qq(SELECT DBMS_ALERT_REMOVE('test_alert_event'))); done_testing($test_count);