1 package DBICTest::Util;
6 use constant DEBUG_TEST_CONCURRENCY_LOCKS =>
7 ( ($ENV{DBICTEST_DEBUG_CONCURRENCY_LOCKS}||'') =~ /^(\d+)$/ )[0]
13 use Carp qw(cluck confess croak);
15 use Scalar::Util qw(blessed refaddr);
16 use DBIx::Class::_Util 'scope_guard';
24 await_flock DEBUG_TEST_CONCURRENCY_LOCKS
27 if (DEBUG_TEST_CONCURRENCY_LOCKS) {
29 my $oc = DBI->can('connect');
30 no warnings 'redefine';
32 DBICTest::Util::dbg("Connecting to $_[1]");
39 printf STDERR "\n%.06f %5s %-78s %s\n",
40 scalar Time::HiRes::time(),
47 # File locking is hard. Really hard. By far the best lock implementation
48 # I've seen is part of the guts of File::Temp. However it is sadly not
49 # reusable. Since I am not aware of folks doing NFS parallel testing,
50 # nor are we known to work on VMS, I am just going to punt this and
51 # use the portable-ish flock() provided by perl itself. If this does
52 # not work for you - patches more than welcome.
54 # This figure esentially means "how long can a single test hold a
55 # resource before everyone else gives up waiting and aborts" or
56 # in other words "how long does the longest test-group legitimally run?"
57 my $lock_timeout_minutes = 15; # yes, that's long, I know
58 my $wait_step_seconds = 0.25;
60 sub await_flock ($$) {
61 my ($fh, $locktype) = @_;
65 ! ( $res = flock( $fh, $locktype | LOCK_NB ) )
67 ++$tries <= $lock_timeout_minutes * 60 / $wait_step_seconds
69 select( undef, undef, undef, $wait_step_seconds );
71 # "say something" every 10 cycles to work around RT#108390
72 # jesus christ our tooling is such a crock of shit :(
73 print "#\n" if not $tries % 10;
81 return unless defined $Config{d_umask};
83 croak 'Calling local_umask() in void context makes no sense'
84 if ! defined wantarray;
86 my $old_umask = umask($_[0]);
87 die "Setting umask failed: $!" unless defined $old_umask;
93 defined(umask $old_umask) or die "nope";
96 "Unable to reset old umask '$old_umask': " . ($! || 'Unknown error')
106 while (@frame = CORE::caller($frame++)) {
107 push @stack, [@frame[3,1,2]];
110 return undef unless @stack;
113 return join "\tinvoked as ", map { sprintf ("%s at %s line %d\n", @$_ ) } @stack;
116 sub check_customcond_args ($) {
119 confess "Expecting a hashref"
120 unless ref $args eq 'HASH';
122 for (qw(rel_name foreign_relname self_alias foreign_alias)) {
123 confess "Custom condition argument '$_' must be a plain string"
124 if length ref $args->{$_} or ! length $args->{$_};
127 confess "Current and legacy rel_name arguments do not match"
128 if $args->{rel_name} ne $args->{foreign_relname};
130 confess "Custom condition argument 'self_resultsource' must be a rsrc instance"
131 unless defined blessed $args->{self_resultsource} and $args->{self_resultsource}->isa('DBIx::Class::ResultSource');
133 confess "Passed resultsource has no record of the supplied rel_name - likely wrong \$rsrc"
134 unless ref $args->{self_resultsource}->relationship_info($args->{rel_name});
138 if (defined $args->{self_result_object} or defined $args->{self_rowobj} ) {
140 for (qw(self_result_object self_rowobj)) {
141 confess "Custom condition argument '$_' must be a result instance"
142 unless defined blessed $args->{$_} and $args->{$_}->isa('DBIx::Class::Row');
145 confess "Current and legacy self_result_object arguments do not match"
146 if refaddr($args->{self_result_object}) != refaddr($args->{self_rowobj});
149 if (defined $args->{foreign_values}) {
152 confess "Custom condition argument 'foreign_values' must be a hash reference"
153 unless ref $args->{foreign_values} eq 'HASH';
156 confess "Data structures supplied on both ends of a relationship"
162 sub visit_namespaces {
163 my $args = { (ref $_[0]) ? %{$_[0]} : @_ };
165 my $visited_count = 1;
167 # A package and a namespace are subtly different things
168 $args->{package} ||= 'main';
169 $args->{package} = 'main' if $args->{package} =~ /^ :: (?: main )? $/x;
170 $args->{package} =~ s/^:://;
172 if ( $args->{action}->($args->{package}) ) {
174 ( ($args->{package} eq 'main') ? '' : $args->{package} )
179 $visited_count += visit_namespaces( %$args, package => $_ ) for
181 # this happens sometimes on %:: traversal
184 { $_ =~ /^(.+?)::$/ ? "$ns$1" : () }
185 do { no strict 'refs'; keys %$ns }
189 return $visited_count;