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
31 modver_gt_or_eq_and_lt
33 use List::Util 'shuffle';
38 my $pkg_gen_history = {};
40 { package UEBERVERSAL; sub ueber {} }
41 @UNIVERSAL::ISA = "UEBERVERSAL";
42 sub UNIVERSAL::uni { "unistuff" }
44 sub grab_pkg_gen ($) {
45 push @{ $pkg_gen_history->{$_[0]} }, [
46 DBIx::Class::_Util::get_real_pkg_gen($_[0]),
47 'line ' . ( (caller(0))[2] ),
51 @DBICTest::AttrLegacy::ISA = 'DBIx::Class';
52 sub DBICTest::AttrLegacy::VALID_DBIC_CODE_ATTRIBUTE { 1 }
54 grab_pkg_gen("DBICTest::AttrLegacy");
58 'DBICTest::AttrLegacy::attr',
62 attributes => [qw( ResultSet DBIC_random_attr )],
63 package => 'DBICTest::AttrLegacy',
67 grab_pkg_gen("DBICTest::AttrLegacy");
69 is $s, \&DBICTest::AttrLegacy::attr, 'Same cref installed';
71 is DBICTest::AttrLegacy::attr(), 42, 'Sub properly installed and callable';
74 [ sort( attributes::get( $s ) ) ],
75 [qw( DBIC_random_attr ResultSet )],
76 'Attribute installed',
80 package DBICTest::SomeGrandParentClass;
81 use base 'DBIx::Class::MethodAttributes';
82 sub VALID_DBIC_CODE_ATTRIBUTE { shift->next::method(@_) };
85 package DBICTest::SomeParentClass;
86 use base qw(DBICTest::SomeGrandParentClass);
89 package DBICTest::AnotherParentClass;
90 use base 'DBIx::Class::MethodAttributes';
91 sub VALID_DBIC_CODE_ATTRIBUTE { $_[1] =~ /DBIC_attr/ };
95 package DBICTest::AttrTest;
97 @DBICTest::AttrTest::ISA = qw( DBICTest::SomeParentClass DBICTest::AnotherParentClass );
100 # pathological case - but can (and sadly does) happen
101 *VALID_DBIC_CODE_ATTRIBUTE = \&DBICTest::SomeGrandParentClass::VALID_DBIC_CODE_ATTRIBUTE;
103 ::grab_pkg_gen("DBICTest::AttrTest");
105 eval <<'EOS' or die $@;
106 sub attr :lvalue :method :DBIC_attr1 { $$var}
110 ::grab_pkg_gen("DBICTest::AttrTest");
114 'DBICTest::AttrTest',
115 DBICTest::AttrTest->can('attr'),
118 } qr/DBIC-specific attribute 'DBIC_unknownattr' did not pass validation/;
122 [ sort( attributes::get( DBICTest::AttrTest->can("attr") )) ],
123 [qw( DBIC_attr1 lvalue method )],
124 'Attribute installed',
128 ! DBICTest::AttrTest->can('__attr_cache'),
129 'Inherited classdata never created on core attrs'
133 DBICTest::AttrTest->_attr_cache,
135 'Cache never instantiated on core attrs'
140 # Test that secondary attribute application works
142 'DBICTest::AttrLegacy',
143 DBICTest::AttrLegacy->can('attr'),
144 'SomethingNobodyUses',
147 # and that double-application also works
149 'DBICTest::AttrLegacy',
150 DBICTest::AttrLegacy->can('attr'),
151 'SomethingNobodyUses',
154 grab_pkg_gen("DBICTest::AttrLegacy");
157 [ sort( attributes::get( $s ) )],
158 [ qw( DBIC_random_attr ResultSet SomethingNobodyUses ) ],
159 'Secondary attributes installed',
163 DBICTest::AttrLegacy->_attr_cache->{$s},
164 [ qw( ResultSet SomethingNobodyUses ) ],
165 'Attributes visible in legacy DBIC attribute API',
168 # Test that secondary attribute application works
170 'DBICTest::AttrTest',
171 DBICTest::AttrTest->can('attr'),
175 grab_pkg_gen("DBICTest::AttrTest");
177 # and that double-application also works
179 'DBICTest::AttrTest',
180 DBICTest::AttrTest->can('attr'),
185 grab_pkg_gen("DBICTest::AttrTest");
188 [ sort( attributes::get( DBICTest::AttrTest->can("attr") )) ],
189 [qw( DBIC_attr1 DBIC_attr2 DBIC_attr3 lvalue method )],
190 'DBIC-specific attribute installed',
194 ! DBICTest::AttrTest->can('__attr_cache'),
195 'Inherited classdata never created on core+DBIC-specific attrs'
199 DBICTest::AttrTest->_attr_cache,
201 'Legacy DBIC attribute cache never instantiated on core+DBIC-specific attrs'
204 # no point dragging in threads::shared, just do the check here
205 for my $class ( keys %$pkg_gen_history ) {
206 my $stack = $pkg_gen_history->{$class};
208 for my $i ( 1 .. $#$stack ) {
211 ( DBIx::Class::_ENV_::OLD_MRO ? '!=' : '<' ),
213 "pkg_gen for $class changed from $stack->[$i-1][1] to $stack->[$i][1]"
219 # check that class description is stable, and changes when needed
221 # FIXME - this list used to contain 'main', but that started failing as
222 # of the commit introducing this line with bizarre "unstable gen" errors
223 # Punting for the time being - will fix at some point in the future
230 my $desc = describe_class_methods($class);
233 describe_class_methods($class),
235 "describe_class_methods result is stable over '$class' (pass $_)"
243 eval "sub UEBERVERSAL::some_unimethod_$cnt {}; 1" or die $@;
245 my $rv = describe_class_methods($class);
247 delete ${"UEBERVERSAL::"}{"some_unimethod_$cnt"};
252 delete $_->{cumulative_gen} for $desc, $desc2;
257 "touching UNIVERSAL changed '$class' method availability"
261 my $bottom_most_V_D_C_A = refdesc(
262 describe_class_methods("DBIx::Class::MethodAttributes")
264 ->{VALID_DBIC_CODE_ATTRIBUTE}
268 for my $class ( shuffle( qw(
271 DBICTest::SomeGrandParentClass
273 DBIx::Class::ResultSet
274 DBICTest::Schema::Track
276 my $desc = describe_class_methods($class);
279 refdesc( $desc->{methods}{VALID_DBIC_CODE_ATTRIBUTE}[-1] ),
280 $bottom_most_V_D_C_A,
281 "Same physical structure returned for last VALID_DBIC_CODE_ATTRIBUTE via class $class"
285 refdesc( $desc->{methods_with_supers}{VALID_DBIC_CODE_ATTRIBUTE}[-1] ),
286 $bottom_most_V_D_C_A,
287 "Same physical structure returned for bottom-most SUPER of VALID_DBIC_CODE_ATTRIBUTE via class $class"
288 ) if $desc->{methods_with_supers}{VALID_DBIC_CODE_ATTRIBUTE};
291 # check that describe_class_methods returns the right stuff
292 # ( on the simpler class )
293 my $expected_AttrTest_linear_ISA = [qw(
294 DBICTest::SomeParentClass
295 DBICTest::SomeGrandParentClass
296 DBICTest::AnotherParentClass
297 DBIx::Class::MethodAttributes
300 my $expected_AttrTest_full_ISA = { map { $_ => 1 } (
301 qw( UEBERVERSAL UNIVERSAL DBICTest::AttrTest ),
302 @$expected_AttrTest_linear_ISA,
305 my $expected_desc = {
306 class => "DBICTest::AttrTest",
308 # sum and/or is_deeply are buggy on old List::Util/Test::More
309 # do the sum by hand ourselves to be sure
310 cumulative_gen => do {
311 require Math::BigInt;
312 my $gen = Math::BigInt->new(0);
314 $gen += DBIx::Class::_Util::get_real_pkg_gen($_)
315 for keys %$expected_AttrTest_full_ISA;
323 linear_isa => $expected_AttrTest_linear_ISA,
324 isa => $expected_AttrTest_full_ISA,
326 FETCH_CODE_ATTRIBUTES => [
329 name => "FETCH_CODE_ATTRIBUTES",
330 via_class => "DBIx::Class::MethodAttributes"
333 MODIFY_CODE_ATTRIBUTES => [
336 name => "MODIFY_CODE_ATTRIBUTES",
337 via_class => "DBIx::Class::MethodAttributes"
340 VALID_DBIC_CODE_ATTRIBUTE => ( my $V_D_C_A_stack = [
343 name => 'VALID_DBIC_CODE_ATTRIBUTE',
344 via_class => 'DBICTest::AttrTest'
348 name => "VALID_DBIC_CODE_ATTRIBUTE",
349 via_class => "DBICTest::SomeGrandParentClass",
353 name => "VALID_DBIC_CODE_ATTRIBUTE",
354 via_class => "DBICTest::AnotherParentClass"
358 name => "VALID_DBIC_CODE_ATTRIBUTE",
359 via_class => "DBIx::Class::MethodAttributes"
365 name => "_attr_cache",
366 via_class => "DBIx::Class::MethodAttributes"
379 via_class => "DBICTest::AttrTest"
386 via_class => "UEBERVERSAL",
393 via_class => "UNIVERSAL",
400 via_class => "UNIVERSAL",
407 via_class => "UNIVERSAL",
414 via_class => "UNIVERSAL",
417 ( DBIx::Class::_ENV_::OLD_MRO ? () : (
421 via_class => "UNIVERSAL",
427 $expected_desc->{methods_with_supers}{VALID_DBIC_CODE_ATTRIBUTE}
430 $expected_desc->{methods_defined_in_class}{VALID_DBIC_CODE_ATTRIBUTE}
431 = $V_D_C_A_stack->[0];
433 $expected_desc->{methods_defined_in_class}{attr}
434 = $expected_desc->{methods}{attr}[0];
437 describe_class_methods("DBICTest::AttrTest"),
439 'describe_class_methods returns correct data',
442 # ensure that asking with a different MRO will not perturb the cache
443 my $cached_desc = serialize(
444 $DBIx::Class::_Util::__describe_class_query_cache->{"DBICTest::AttrTest|c3"}
447 # now try to ask for DFS explicitly, adjust our expectations
448 $expected_desc->{mro} = { type => 'dfs', is_c3 => 0 };
450 # due to DFS the last 2 entries of ISA and the VALID_DBIC_CODE_ATTRIBUTE
451 # sourcing-list will change places
452 splice @$_, -2, 2, @{$_}[-1, -2]
453 for $V_D_C_A_stack, $expected_AttrTest_linear_ISA;
456 # work around taint, see TODO below
458 %{ describe_class_methods({ class => "DBICTest::AttrTest", use_mro => "dfs" }) },
459 cumulative_gen => $expected_desc->{cumulative_gen},
462 'describing with explicit mro returns correct data'
466 DBIx::Class::_ENV_::OLD_MRO
468 ! DBIx::Class::_ENV_::TAINT_MODE
472 # $TODO did not work on T::B under threads in this range
473 # https://github.com/Test-More/test-more/issues/683
474 ! modver_gt_or_eq_and_lt( 'Test::More', '1.300', '1.302027' )
476 local $TODO = "On 5.10+ -T combined with stash peeking invalidates the pkg_gen (wtf)"
478 DBIx::Class::_ENV_::TAINT_MODE
480 DBIx::Class::_ENV_::PERL_VERSION > 5.009
485 serialize( describe_class_methods("DBICTest::AttrTest") )
489 "Asking for alternative mro type did not invalidate cache"
493 # setting mro explicitly still matches what we expect
494 mro::set_mro("DBICTest::AttrTest", "dfs");
497 # in case set_mro starts increasing pkg_gen...
499 %{describe_class_methods("DBICTest::AttrTest")},
500 cumulative_gen => $expected_desc->{cumulative_gen},
503 'describing with implicit mro returns correct data'
506 # check that a UNIVERSAL-parent interrogation makes sense
507 # ( it should not list anything from UNIVERSAL itself )
509 describe_class_methods("UEBERVERSAL"),
511 # should be cached by now, thus safe to rely on...?
512 cumulative_gen => DBIx::Class::_Util::get_real_pkg_gen('UEBERVERSAL'),
514 class => 'UEBERVERSAL',
515 mro => { is_c3 => 0, type => 'dfs' },
516 isa => { UEBERVERSAL => 1 },
519 ueber => $expected_desc->{methods}{ueber}
521 methods_defined_in_class => {
522 ueber => $expected_desc->{methods}{ueber}[0]
525 "Expected description of a parent-of-UNIVERSAL class (pathological case)",
530 SKIP: { skip "Skipping the thread test: $skip_threads", 1 }
536 my $t = threads->create(sub {
538 my $t = threads->create(sub {
541 select( undef, undef, undef, 0.2 ); # without this many tasty crashes even on latest perls
546 die "Unable to start thread: $!"
547 unless $! == Errno::EAGAIN();
549 SKIP: { skip "EAGAIN encountered, your system is likely bogged down: skipping rest of test", 1 }
556 select( undef, undef, undef, 0.2 ); # without this many tasty crashes even on latest perls
560 die "Unable to start thread: $!"
561 unless $! == Errno::EAGAIN();
563 skip "EAGAIN encountered, your system is likely bogged down: skipping rest of test", 1;
569 'Thread stack exitted succesfully'
573 # check "crosed-over" mro
576 package DBICTest::WackyDFS;
577 use base qw( DBICTest::SomeGrandParentClass DBICTest::SomeParentClass );
581 describe_class_methods("DBICTest::WackyDFS")->{methods}{VALID_DBIC_CODE_ATTRIBUTE},
585 name => "VALID_DBIC_CODE_ATTRIBUTE",
586 via_class => "DBICTest::SomeGrandParentClass",
590 name => "VALID_DBIC_CODE_ATTRIBUTE",
591 via_class => "DBIx::Class::MethodAttributes"
594 'Expected description on unusable inheritance hierarchy'
598 # check pathological cases ( combinations of cases from
599 # Package::Stash and Devel::Isa::Explainer )
602 package DBICTest::Exotic;
604 use constant CSCALAR => 1;
605 use constant CSCALARREF => \1;
606 use constant CARRAYREF => [];
607 use constant CHASHREF => {};
608 use constant CSUB => sub { };
612 sub subnormalproto () { }
617 sub Bsubnormalproto () { }
618 sub Bsubstubproto ();
624 *someXSUB = \&DBIx::Class::_Util::deep_clone;
626 *EMPTYGLOB = *EMPTYGLOB;
630 sub GLOBCOLLISION { }
633 ${'DBICTest::'}{stubUNDEF} = undef;
634 ${'DBICTest::'}{stubSCALAR} = 1;
638 { \&{"DBICTest::Exotic::Bsub$_"} }
642 bless $_, __PACKAGE__
644 { \&{"DBICTest::Exotic::Bsub$_"} }
645 qw( normalproto stubproto )
648 package DBICTest::Exotic::SubPackage;
649 *CHILDGLOB = *CHILDGLOB;
652 my $expected = [ sort
654 CSCALAR CSCALARREF CARRAYREF CHASHREF CSUB
655 GLOBCOLLISION someXSUB
658 {( "Bsub$_", "sub$_" )}
659 qw( normal stub normalproto stubproto )
663 # FIXME because attributes::get() has an error in its signature parser
664 local $SIG{__WARN__} = sigwarn_silencer qr/Unable to determine attributes of/;
668 describe_class_methods('DBICTest::Exotic')->{methods_defined_in_class}
671 'All expected methods recognized in pathological cases'
675 *DBICTest::Exotic::zzz_extra_method = sub {};
679 describe_class_methods('DBICTest::Exotic')->{methods_defined_in_class}
681 [ @$expected, 'zzz_extra_method' ],
682 'All expected methods yet again recognized in pathological cases'