1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
10 if( ! $Config{useithreads} ) {
11 $skip_threads = 'your perl does not support ithreads';
13 elsif( "$]" < 5.008005 ) {
14 $skip_threads = 'DBIC does not actively support threads before perl 5.8.5';
16 elsif( $INC{'Devel/Cover.pm'} ) {
17 $skip_threads = 'Devel::Cover does not work with ithreads yet';
20 unless( $skip_threads ) {
28 use DBIx::Class::_Util qw(
29 quote_sub describe_class_methods
30 serialize refdesc sigwarn_silencer
32 use List::Util 'shuffle';
37 my $pkg_gen_history = {};
39 { package UEBERVERSAL; sub ueber {} }
40 @UNIVERSAL::ISA = "UEBERVERSAL";
41 sub UNIVERSAL::uni { "unistuff" }
43 sub grab_pkg_gen ($) {
44 push @{ $pkg_gen_history->{$_[0]} }, [
45 DBIx::Class::_Util::get_real_pkg_gen($_[0]),
46 'line ' . ( (caller(0))[2] ),
50 @DBICTest::AttrLegacy::ISA = 'DBIx::Class';
51 sub DBICTest::AttrLegacy::VALID_DBIC_CODE_ATTRIBUTE { 1 }
53 grab_pkg_gen("DBICTest::AttrLegacy");
57 'DBICTest::AttrLegacy::attr',
61 attributes => [qw( ResultSet DBIC_random_attr )],
62 package => 'DBICTest::AttrLegacy',
66 grab_pkg_gen("DBICTest::AttrLegacy");
68 is $s, \&DBICTest::AttrLegacy::attr, 'Same cref installed';
70 is DBICTest::AttrLegacy::attr(), 42, 'Sub properly installed and callable';
73 [ sort( attributes::get( $s ) ) ],
74 [qw( DBIC_random_attr ResultSet )],
75 'Attribute installed',
79 package DBICTest::SomeGrandParentClass;
80 use base 'DBIx::Class::MethodAttributes';
81 sub VALID_DBIC_CODE_ATTRIBUTE { shift->next::method(@_) };
84 package DBICTest::SomeParentClass;
85 use base qw(DBICTest::SomeGrandParentClass);
88 package DBICTest::AnotherParentClass;
89 use base 'DBIx::Class::MethodAttributes';
90 sub VALID_DBIC_CODE_ATTRIBUTE { $_[1] =~ /DBIC_attr/ };
94 package DBICTest::AttrTest;
96 @DBICTest::AttrTest::ISA = qw( DBICTest::SomeParentClass DBICTest::AnotherParentClass );
99 # pathological case - but can (and sadly does) happen
100 *VALID_DBIC_CODE_ATTRIBUTE = \&DBICTest::SomeGrandParentClass::VALID_DBIC_CODE_ATTRIBUTE;
102 ::grab_pkg_gen("DBICTest::AttrTest");
104 eval <<'EOS' or die $@;
105 sub attr :lvalue :method :DBIC_attr1 { $$var}
109 ::grab_pkg_gen("DBICTest::AttrTest");
113 'DBICTest::AttrTest',
114 DBICTest::AttrTest->can('attr'),
117 } qr/DBIC-specific attribute 'DBIC_unknownattr' did not pass validation/;
121 [ sort( attributes::get( DBICTest::AttrTest->can("attr") )) ],
122 [qw( DBIC_attr1 lvalue method )],
123 'Attribute installed',
127 ! DBICTest::AttrTest->can('__attr_cache'),
128 'Inherited classdata never created on core attrs'
132 DBICTest::AttrTest->_attr_cache,
134 'Cache never instantiated on core attrs'
139 # Test that secondary attribute application works
141 'DBICTest::AttrLegacy',
142 DBICTest::AttrLegacy->can('attr'),
143 'SomethingNobodyUses',
146 # and that double-application also works
148 'DBICTest::AttrLegacy',
149 DBICTest::AttrLegacy->can('attr'),
150 'SomethingNobodyUses',
153 grab_pkg_gen("DBICTest::AttrLegacy");
156 [ sort( attributes::get( $s ) )],
157 [ qw( DBIC_random_attr ResultSet SomethingNobodyUses ) ],
158 'Secondary attributes installed',
162 DBICTest::AttrLegacy->_attr_cache->{$s},
163 [ qw( ResultSet SomethingNobodyUses ) ],
164 'Attributes visible in legacy DBIC attribute API',
167 # Test that secondary attribute application works
169 'DBICTest::AttrTest',
170 DBICTest::AttrTest->can('attr'),
174 grab_pkg_gen("DBICTest::AttrTest");
176 # and that double-application also works
178 'DBICTest::AttrTest',
179 DBICTest::AttrTest->can('attr'),
184 grab_pkg_gen("DBICTest::AttrTest");
187 [ sort( attributes::get( DBICTest::AttrTest->can("attr") )) ],
188 [qw( DBIC_attr1 DBIC_attr2 DBIC_attr3 lvalue method )],
189 'DBIC-specific attribute installed',
193 ! DBICTest::AttrTest->can('__attr_cache'),
194 'Inherited classdata never created on core+DBIC-specific attrs'
198 DBICTest::AttrTest->_attr_cache,
200 'Legacy DBIC attribute cache never instantiated on core+DBIC-specific attrs'
203 # no point dragging in threads::shared, just do the check here
204 for my $class ( keys %$pkg_gen_history ) {
205 my $stack = $pkg_gen_history->{$class};
207 for my $i ( 1 .. $#$stack ) {
210 ( DBIx::Class::_ENV_::OLD_MRO ? '!=' : '<' ),
212 "pkg_gen for $class changed from $stack->[$i-1][1] to $stack->[$i][1]"
218 # check that class description is stable, and changes when needed
220 # FIXME - this list used to contain 'main', but that started failing as
221 # of the commit introducing this line with bizarre "unstable gen" errors
222 # Punting for the time being - will fix at some point in the future
229 my $desc = describe_class_methods($class);
232 describe_class_methods($class),
234 "describe_class_methods result is stable over '$class' (pass $_)"
242 eval "sub UEBERVERSAL::some_unimethod_$cnt {}; 1" or die $@;
244 my $rv = describe_class_methods($class);
246 delete ${"UEBERVERSAL::"}{"some_unimethod_$cnt"};
251 delete $_->{cumulative_gen} for $desc, $desc2;
256 "touching UNIVERSAL changed '$class' method availability"
260 my $bottom_most_V_D_C_A = refdesc(
261 describe_class_methods("DBIx::Class::MethodAttributes")
263 ->{VALID_DBIC_CODE_ATTRIBUTE}
267 for my $class ( shuffle( qw(
270 DBICTest::SomeGrandParentClass
272 DBIx::Class::ResultSet
273 DBICTest::Schema::Track
275 my $desc = describe_class_methods($class);
278 refdesc( $desc->{methods}{VALID_DBIC_CODE_ATTRIBUTE}[-1] ),
279 $bottom_most_V_D_C_A,
280 "Same physical structure returned for last VALID_DBIC_CODE_ATTRIBUTE via class $class"
284 refdesc( $desc->{methods_with_supers}{VALID_DBIC_CODE_ATTRIBUTE}[-1] ),
285 $bottom_most_V_D_C_A,
286 "Same physical structure returned for bottom-most SUPER of VALID_DBIC_CODE_ATTRIBUTE via class $class"
287 ) if $desc->{methods_with_supers}{VALID_DBIC_CODE_ATTRIBUTE};
290 # check that describe_class_methods returns the right stuff
291 # ( on the simpler class )
292 my $expected_AttrTest_linear_ISA = [qw(
293 DBICTest::SomeParentClass
294 DBICTest::SomeGrandParentClass
295 DBICTest::AnotherParentClass
296 DBIx::Class::MethodAttributes
299 my $expected_AttrTest_full_ISA = { map { $_ => 1 } (
300 qw( UEBERVERSAL UNIVERSAL DBICTest::AttrTest ),
301 @$expected_AttrTest_linear_ISA,
304 my $expected_desc = {
305 class => "DBICTest::AttrTest",
307 # sum and/or is_deeply are buggy on old List::Util/Test::More
308 # do the sum by hand ourselves to be sure
309 cumulative_gen => do {
310 require Math::BigInt;
311 my $gen = Math::BigInt->new(0);
313 $gen += DBIx::Class::_Util::get_real_pkg_gen($_)
314 for keys %$expected_AttrTest_full_ISA;
322 linear_isa => $expected_AttrTest_linear_ISA,
323 isa => $expected_AttrTest_full_ISA,
325 FETCH_CODE_ATTRIBUTES => [
328 name => "FETCH_CODE_ATTRIBUTES",
329 via_class => "DBIx::Class::MethodAttributes"
332 MODIFY_CODE_ATTRIBUTES => [
335 name => "MODIFY_CODE_ATTRIBUTES",
336 via_class => "DBIx::Class::MethodAttributes"
339 VALID_DBIC_CODE_ATTRIBUTE => ( my $V_D_C_A_stack = [
342 name => 'VALID_DBIC_CODE_ATTRIBUTE',
343 via_class => 'DBICTest::AttrTest'
347 name => "VALID_DBIC_CODE_ATTRIBUTE",
348 via_class => "DBICTest::SomeGrandParentClass",
352 name => "VALID_DBIC_CODE_ATTRIBUTE",
353 via_class => "DBICTest::AnotherParentClass"
357 name => "VALID_DBIC_CODE_ATTRIBUTE",
358 via_class => "DBIx::Class::MethodAttributes"
364 name => "_attr_cache",
365 via_class => "DBIx::Class::MethodAttributes"
378 via_class => "DBICTest::AttrTest"
385 via_class => "UEBERVERSAL",
392 via_class => "UNIVERSAL",
399 via_class => "UNIVERSAL",
406 via_class => "UNIVERSAL",
413 via_class => "UNIVERSAL",
416 ( DBIx::Class::_ENV_::OLD_MRO ? () : (
420 via_class => "UNIVERSAL",
426 $expected_desc->{methods_with_supers}{VALID_DBIC_CODE_ATTRIBUTE}
429 $expected_desc->{methods_defined_in_class}{VALID_DBIC_CODE_ATTRIBUTE}
430 = $V_D_C_A_stack->[0];
432 $expected_desc->{methods_defined_in_class}{attr}
433 = $expected_desc->{methods}{attr}[0];
436 describe_class_methods("DBICTest::AttrTest"),
438 'describe_class_methods returns correct data',
441 # ensure that asking with a different MRO will not perturb the cache
442 my $cached_desc = serialize(
443 $DBIx::Class::_Util::__describe_class_query_cache->{"DBICTest::AttrTest|c3"}
446 # now try to ask for DFS explicitly, adjust our expectations
447 $expected_desc->{mro} = { type => 'dfs', is_c3 => 0 };
449 # due to DFS the last 2 entries of ISA and the VALID_DBIC_CODE_ATTRIBUTE
450 # sourcing-list will change places
451 splice @$_, -2, 2, @{$_}[-1, -2]
452 for $V_D_C_A_stack, $expected_AttrTest_linear_ISA;
455 # work around taint, see TODO below
457 %{ describe_class_methods({ class => "DBICTest::AttrTest", use_mro => "dfs" }) },
458 cumulative_gen => $expected_desc->{cumulative_gen},
461 'describing with explicit mro returns correct data'
464 # FIXME: TODO does not work on new T::B under threads sigh
465 # https://github.com/Test-More/test-more/issues/683
467 ! DBIx::Class::_ENV_::OLD_MRO
469 DBIx::Class::_ENV_::TAINT_MODE
471 #local $TODO = "On 5.10+ -T combined with stash peeking invalidates the pkg_gen (wtf)" if ...
475 serialize( describe_class_methods("DBICTest::AttrTest") )
479 "Asking for alternative mro type did not invalidate cache"
483 # setting mro explicitly still matches what we expect
484 mro::set_mro("DBICTest::AttrTest", "dfs");
487 # in case set_mro starts increasing pkg_gen...
489 %{describe_class_methods("DBICTest::AttrTest")},
490 cumulative_gen => $expected_desc->{cumulative_gen},
493 'describing with implicit mro returns correct data'
496 # check that a UNIVERSAL-parent interrogation makes sense
497 # ( it should not list anything from UNIVERSAL itself )
499 describe_class_methods("UEBERVERSAL"),
501 # should be cached by now, thus safe to rely on...?
502 cumulative_gen => DBIx::Class::_Util::get_real_pkg_gen('UEBERVERSAL'),
504 class => 'UEBERVERSAL',
505 mro => { is_c3 => 0, type => 'dfs' },
506 isa => { UEBERVERSAL => 1 },
509 ueber => $expected_desc->{methods}{ueber}
511 methods_defined_in_class => {
512 ueber => $expected_desc->{methods}{ueber}[0]
515 "Expected description of a parent-of-UNIVERSAL class (pathological case)",
520 SKIP: { skip "Skipping the thread test: $skip_threads", 1 }
526 my $t = threads->create(sub {
528 my $t = threads->create(sub {
531 select( undef, undef, undef, 0.2 ); # without this many tasty crashes even on latest perls
536 die "Unable to start thread: $!"
537 unless $! == Errno::EAGAIN();
539 SKIP: { skip "EAGAIN encountered, your system is likely bogged down: skipping rest of test", 1 }
546 select( undef, undef, undef, 0.2 ); # without this many tasty crashes even on latest perls
550 die "Unable to start thread: $!"
551 unless $! == Errno::EAGAIN();
553 skip "EAGAIN encountered, your system is likely bogged down: skipping rest of test", 1;
559 'Thread stack exitted succesfully'
563 # check "crosed-over" mro
566 package DBICTest::WackyDFS;
567 use base qw( DBICTest::SomeGrandParentClass DBICTest::SomeParentClass );
571 describe_class_methods("DBICTest::WackyDFS")->{methods}{VALID_DBIC_CODE_ATTRIBUTE},
575 name => "VALID_DBIC_CODE_ATTRIBUTE",
576 via_class => "DBICTest::SomeGrandParentClass",
580 name => "VALID_DBIC_CODE_ATTRIBUTE",
581 via_class => "DBIx::Class::MethodAttributes"
584 'Expected description on unusable inheritance hierarchy'
588 # check pathological cases ( combinations of cases from
589 # Package::Stash and Devel::Isa::Explainer )
592 package DBICTest::Exotic;
594 use constant CSCALAR => 1;
595 use constant CSCALARREF => \1;
596 use constant CARRAYREF => [];
597 use constant CHASHREF => {};
598 use constant CSUB => sub { };
602 sub subnormalproto () { }
607 sub Bsubnormalproto () { }
608 sub Bsubstubproto ();
614 *someXSUB = \&DBIx::Class::_Util::deep_clone;
616 *EMPTYGLOB = *EMPTYGLOB;
620 sub GLOBCOLLISION { }
623 ${'DBICTest::'}{stubUNDEF} = undef;
624 ${'DBICTest::'}{stubSCALAR} = 1;
628 { \&{"DBICTest::Exotic::Bsub$_"} }
632 bless $_, __PACKAGE__
634 { \&{"DBICTest::Exotic::Bsub$_"} }
635 qw( normalproto stubproto )
638 package DBICTest::Exotic::SubPackage;
639 *CHILDGLOB = *CHILDGLOB;
642 my $expected = [ sort
644 CSCALAR CSCALARREF CARRAYREF CHASHREF CSUB
645 GLOBCOLLISION someXSUB
648 {( "Bsub$_", "sub$_" )}
649 qw( normal stub normalproto stubproto )
653 # FIXME because attributes::get() has an error in its signature parser
654 local $SIG{__WARN__} = sigwarn_silencer qr/Unable to determine attributes of/;
658 describe_class_methods('DBICTest::Exotic')->{methods_defined_in_class}
661 'All expected methods recognized in pathological cases'
665 *DBICTest::Exotic::zzz_extra_method = sub {};
669 describe_class_methods('DBICTest::Exotic')->{methods_defined_in_class}
671 [ @$expected, 'zzz_extra_method' ],
672 'All expected methods yet again recognized in pathological cases'