1 package DBICTest::Util;
6 # this noop trick initializes the STDOUT, so that the TAP::Harness
7 # issued IO::Select->can_read calls (which are blocking wtf wtf wtf)
8 # keep spinning and scheduling jobs
9 # This results in an overall much smoother job-queue drainage, since
10 # the Harness blocks less
11 # (ideally this needs to be addressed in T::H, but a quick patchjob
12 # broke everything so tabling it for now)
14 if ($INC{'Test/Builder.pm'}) {
20 use constant DEBUG_TEST_CONCURRENCY_LOCKS =>
21 ( ($ENV{DBICTEST_DEBUG_CONCURRENCY_LOCKS}||'') =~ /^(\d+)$/ )[0]
29 use Scalar::Util qw(blessed refaddr);
30 use DBIx::Class::_Util;
38 await_flock DEBUG_TEST_CONCURRENCY_LOCKS
41 if (DEBUG_TEST_CONCURRENCY_LOCKS) {
43 my $oc = DBI->can('connect');
44 no warnings 'redefine';
46 DBICTest::Util::dbg("Connecting to $_[1]");
53 printf STDERR "\n%.06f %5s %-78s %s\n",
54 scalar Time::HiRes::time(),
61 # File locking is hard. Really hard. By far the best lock implementation
62 # I've seen is part of the guts of File::Temp. However it is sadly not
63 # reusable. Since I am not aware of folks doing NFS parallel testing,
64 # nor are we known to work on VMS, I am just going to punt this and
65 # use the portable-ish flock() provided by perl itself. If this does
66 # not work for you - patches more than welcome.
68 # This figure esentially means "how long can a single test hold a
69 # resource before everyone else gives up waiting and aborts" or
70 # in other words "how long does the longest test-group legitimally run?"
71 my $lock_timeout_minutes = 15; # yes, that's long, I know
72 my $wait_step_seconds = 0.25;
74 sub await_flock ($$) {
75 my ($fh, $locktype) = @_;
79 ! ( $res = flock( $fh, $locktype | LOCK_NB ) )
81 ++$tries <= $lock_timeout_minutes * 60 / $wait_step_seconds
83 select( undef, undef, undef, $wait_step_seconds );
85 # "say something" every 10 cycles to work around RT#108390
86 # jesus christ our tooling is such a crock of shit :(
87 print "#\n" if not $tries % 10;
94 return unless defined $Config{d_umask};
96 die 'Calling local_umask() in void context makes no sense'
97 if ! defined wantarray;
99 my $old_umask = umask(shift());
100 die "Setting umask failed: $!" unless defined $old_umask;
102 return bless \$old_umask, 'DBICTest::Util::UmaskGuard';
105 package DBICTest::Util::UmaskGuard;
107 &DBIx::Class::_Util::detected_reinvoked_destructor;
110 eval { defined (umask ${$_[0]}) or die };
111 warn ( "Unable to reset old umask ${$_[0]}: " . ($!||'Unknown error') )
121 while (@frame = CORE::caller($frame++)) {
122 push @stack, [@frame[3,1,2]];
125 return undef unless @stack;
128 return join "\tinvoked as ", map { sprintf ("%s at %s line %d\n", @$_ ) } @stack;
131 sub check_customcond_args ($) {
134 confess "Expecting a hashref"
135 unless ref $args eq 'HASH';
137 for (qw(rel_name foreign_relname self_alias foreign_alias)) {
138 confess "Custom condition argument '$_' must be a plain string"
139 if length ref $args->{$_} or ! length $args->{$_};
142 confess "Current and legacy rel_name arguments do not match"
143 if $args->{rel_name} ne $args->{foreign_relname};
145 confess "Custom condition argument 'self_resultsource' must be a rsrc instance"
146 unless defined blessed $args->{self_resultsource} and $args->{self_resultsource}->isa('DBIx::Class::ResultSource');
148 confess "Passed resultsource has no record of the supplied rel_name - likely wrong \$rsrc"
149 unless ref $args->{self_resultsource}->relationship_info($args->{rel_name});
153 if (defined $args->{self_result_object} or defined $args->{self_rowobj} ) {
155 for (qw(self_result_object self_rowobj)) {
156 confess "Custom condition argument '$_' must be a result instance"
157 unless defined blessed $args->{$_} and $args->{$_}->isa('DBIx::Class::Row');
160 confess "Current and legacy self_result_object arguments do not match"
161 if refaddr($args->{self_result_object}) != refaddr($args->{self_rowobj});
164 if (defined $args->{foreign_values}) {
167 confess "Custom condition argument 'foreign_values' must be a hash reference"
168 unless ref $args->{foreign_values} eq 'HASH';
171 confess "Data structures supplied on both ends of a relationship"
177 sub visit_namespaces {
178 my $args = { (ref $_[0]) ? %{$_[0]} : @_ };
180 my $visited_count = 1;
182 # A package and a namespace are subtly different things
183 $args->{package} ||= 'main';
184 $args->{package} = 'main' if $args->{package} =~ /^ :: (?: main )? $/x;
185 $args->{package} =~ s/^:://;
187 if ( $args->{action}->($args->{package}) ) {
189 ( ($args->{package} eq 'main') ? '' : $args->{package} )
194 $visited_count += visit_namespaces( %$args, package => $_ ) for
196 # this happens sometimes on %:: traversal
199 { $_ =~ /^(.+?)::$/ ? "$ns$1" : () }
200 do { no strict 'refs'; keys %$ns }
204 return $visited_count;