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( quote_sub describe_class_methods serialize refdesc );
29 use List::Util 'shuffle';
34 my $pkg_gen_history = {};
36 { package UEBERVERSAL; sub ueber {} }
37 @UNIVERSAL::ISA = "UEBERVERSAL";
38 sub UNIVERSAL::uni { "unistuff" }
40 sub grab_pkg_gen ($) {
41 push @{ $pkg_gen_history->{$_[0]} }, [
42 DBIx::Class::_Util::get_real_pkg_gen($_[0]),
43 'line ' . ( (caller(0))[2] ),
47 @DBICTest::AttrLegacy::ISA = 'DBIx::Class';
48 sub DBICTest::AttrLegacy::VALID_DBIC_CODE_ATTRIBUTE { 1 }
50 grab_pkg_gen("DBICTest::AttrLegacy");
54 'DBICTest::AttrLegacy::attr',
58 attributes => [qw( ResultSet DBIC_random_attr )],
59 package => 'DBICTest::AttrLegacy',
63 grab_pkg_gen("DBICTest::AttrLegacy");
65 is $s, \&DBICTest::AttrLegacy::attr, 'Same cref installed';
67 is DBICTest::AttrLegacy::attr(), 42, 'Sub properly installed and callable';
70 [ sort( attributes::get( $s ) ) ],
71 [qw( DBIC_random_attr ResultSet )],
72 'Attribute installed',
76 package DBICTest::SomeGrandParentClass;
77 use base 'DBIx::Class::MethodAttributes';
78 sub VALID_DBIC_CODE_ATTRIBUTE { shift->next::method(@_) };
81 package DBICTest::SomeParentClass;
82 use base qw(DBICTest::SomeGrandParentClass);
85 package DBICTest::AnotherParentClass;
86 use base 'DBIx::Class::MethodAttributes';
87 sub VALID_DBIC_CODE_ATTRIBUTE { $_[1] =~ /DBIC_attr/ };
91 package DBICTest::AttrTest;
93 @DBICTest::AttrTest::ISA = qw( DBICTest::SomeParentClass DBICTest::AnotherParentClass );
96 # pathological case - but can (and sadly does) happen
97 *VALID_DBIC_CODE_ATTRIBUTE = \&DBICTest::SomeGrandParentClass::VALID_DBIC_CODE_ATTRIBUTE;
99 ::grab_pkg_gen("DBICTest::AttrTest");
101 eval <<'EOS' or die $@;
102 sub attr :lvalue :method :DBIC_attr1 { $$var}
106 ::grab_pkg_gen("DBICTest::AttrTest");
110 'DBICTest::AttrTest',
111 DBICTest::AttrTest->can('attr'),
114 } qr/DBIC-specific attribute 'DBIC_unknownattr' did not pass validation/;
118 [ sort( attributes::get( DBICTest::AttrTest->can("attr") )) ],
119 [qw( DBIC_attr1 lvalue method )],
120 'Attribute installed',
124 ! DBICTest::AttrTest->can('__attr_cache'),
125 'Inherited classdata never created on core attrs'
129 DBICTest::AttrTest->_attr_cache,
131 'Cache never instantiated on core attrs'
136 # Test that secondary attribute application works
138 'DBICTest::AttrLegacy',
139 DBICTest::AttrLegacy->can('attr'),
140 'SomethingNobodyUses',
143 # and that double-application also works
145 'DBICTest::AttrLegacy',
146 DBICTest::AttrLegacy->can('attr'),
147 'SomethingNobodyUses',
150 grab_pkg_gen("DBICTest::AttrLegacy");
153 [ sort( attributes::get( $s ) )],
154 [ qw( DBIC_random_attr ResultSet SomethingNobodyUses ) ],
155 'Secondary attributes installed',
159 DBICTest::AttrLegacy->_attr_cache->{$s},
160 [ qw( ResultSet SomethingNobodyUses ) ],
161 'Attributes visible in legacy DBIC attribute API',
164 # Test that secondary attribute application works
166 'DBICTest::AttrTest',
167 DBICTest::AttrTest->can('attr'),
171 grab_pkg_gen("DBICTest::AttrTest");
173 # and that double-application also works
175 'DBICTest::AttrTest',
176 DBICTest::AttrTest->can('attr'),
181 grab_pkg_gen("DBICTest::AttrTest");
184 [ sort( attributes::get( DBICTest::AttrTest->can("attr") )) ],
185 [qw( DBIC_attr1 DBIC_attr2 DBIC_attr3 lvalue method )],
186 'DBIC-specific attribute installed',
190 ! DBICTest::AttrTest->can('__attr_cache'),
191 'Inherited classdata never created on core+DBIC-specific attrs'
195 DBICTest::AttrTest->_attr_cache,
197 'Legacy DBIC attribute cache never instantiated on core+DBIC-specific attrs'
200 # no point dragging in threads::shared, just do the check here
201 for my $class ( keys %$pkg_gen_history ) {
202 my $stack = $pkg_gen_history->{$class};
204 for my $i ( 1 .. $#$stack ) {
207 ( DBIx::Class::_ENV_::OLD_MRO ? '!=' : '<' ),
209 "pkg_gen for $class changed from $stack->[$i-1][1] to $stack->[$i][1]"
215 # check that class description is stable, and changes when needed
217 # FIXME - this list used to contain 'main', but that started failing as
218 # of the commit introducing this line with bizarre "unstable gen" errors
219 # Punting for the time being - will fix at some point in the future
226 my $desc = describe_class_methods($class);
229 describe_class_methods($class),
231 "describe_class_methods result is stable over '$class' (pass $_)"
239 eval "sub UEBERVERSAL::some_unimethod_$cnt {}; 1" or die $@;
241 my $rv = describe_class_methods($class);
243 delete ${"UEBERVERSAL::"}{"some_unimethod_$cnt"};
248 delete $_->{cumulative_gen} for $desc, $desc2;
253 "touching UNIVERSAL changed '$class' method availability"
257 my $bottom_most_V_D_C_A = refdesc(
258 describe_class_methods("DBIx::Class::MethodAttributes")
260 ->{VALID_DBIC_CODE_ATTRIBUTE}
264 for my $class ( shuffle( qw(
267 DBICTest::SomeGrandParentClass
269 DBIx::Class::ResultSet
270 DBICTest::Schema::Track
272 my $desc = describe_class_methods($class);
275 refdesc( $desc->{methods}{VALID_DBIC_CODE_ATTRIBUTE}[-1] ),
276 $bottom_most_V_D_C_A,
277 "Same physical structure returned for last VALID_DBIC_CODE_ATTRIBUTE via class $class"
281 refdesc( $desc->{methods_with_supers}{VALID_DBIC_CODE_ATTRIBUTE}[-1] ),
282 $bottom_most_V_D_C_A,
283 "Same physical structure returned for bottom-most SUPER of VALID_DBIC_CODE_ATTRIBUTE via class $class"
284 ) if $desc->{methods_with_supers}{VALID_DBIC_CODE_ATTRIBUTE};
287 # check that describe_class_methods returns the right stuff
288 # ( on the simpler class )
289 my $expected_AttrTest_ISA = [qw(
290 DBICTest::SomeParentClass
291 DBICTest::SomeGrandParentClass
292 DBICTest::AnotherParentClass
293 DBIx::Class::MethodAttributes
296 my $expected_desc = {
297 class => "DBICTest::AttrTest",
299 # sum and/or is_deeply are buggy on old List::Util/Test::More
300 # do the sum by hand ourselves to be sure
301 cumulative_gen => do {
302 require Math::BigInt;
303 my $gen = Math::BigInt->new(0);
305 $gen += DBIx::Class::_Util::get_real_pkg_gen($_) for (
308 'DBICTest::AttrTest',
309 @$expected_AttrTest_ISA,
318 isa => $expected_AttrTest_ISA,
320 FETCH_CODE_ATTRIBUTES => [
323 name => "FETCH_CODE_ATTRIBUTES",
324 via_class => "DBIx::Class::MethodAttributes"
327 MODIFY_CODE_ATTRIBUTES => [
330 name => "MODIFY_CODE_ATTRIBUTES",
331 via_class => "DBIx::Class::MethodAttributes"
334 VALID_DBIC_CODE_ATTRIBUTE => ( my $V_D_C_A_stack = [
337 name => 'VALID_DBIC_CODE_ATTRIBUTE',
338 via_class => 'DBICTest::AttrTest'
342 name => "VALID_DBIC_CODE_ATTRIBUTE",
343 via_class => "DBICTest::SomeGrandParentClass",
347 name => "VALID_DBIC_CODE_ATTRIBUTE",
348 via_class => "DBICTest::AnotherParentClass"
352 name => "VALID_DBIC_CODE_ATTRIBUTE",
353 via_class => "DBIx::Class::MethodAttributes"
359 name => "_attr_cache",
360 via_class => "DBIx::Class::MethodAttributes"
373 via_class => "DBICTest::AttrTest"
380 via_class => "UEBERVERSAL",
387 via_class => "UNIVERSAL",
394 via_class => "UNIVERSAL",
401 via_class => "UNIVERSAL",
408 via_class => "UNIVERSAL",
411 ( DBIx::Class::_ENV_::OLD_MRO ? () : (
415 via_class => "UNIVERSAL",
421 $expected_desc->{methods_with_supers}{VALID_DBIC_CODE_ATTRIBUTE}
424 $expected_desc->{methods_defined_in_class}{VALID_DBIC_CODE_ATTRIBUTE}
425 = $V_D_C_A_stack->[0];
427 $expected_desc->{methods_defined_in_class}{attr}
428 = $expected_desc->{methods}{attr}[0];
431 describe_class_methods("DBICTest::AttrTest"),
433 'describe_class_methods returns correct data',
436 # ensure that asking with a different MRO will not perturb the cache
437 my $cached_desc = serialize(
438 $DBIx::Class::_Util::describe_class_query_cache->{"DBICTest::AttrTest|c3"}
441 # now try to ask for DFS explicitly, adjust our expectations
442 $expected_desc->{mro} = { type => 'dfs', is_c3 => 0 };
444 # due to DFS the last 2 entries of ISA and the VALID_DBIC_CODE_ATTRIBUTE
445 # sourcing-list will change places
446 splice @$_, -2, 2, @{$_}[-1, -2]
447 for $V_D_C_A_stack, $expected_AttrTest_ISA;
450 # work around taint, see TODO below
452 %{describe_class_methods("DBICTest::AttrTest", "dfs")},
453 cumulative_gen => $expected_desc->{cumulative_gen},
456 'describing with explicit mro returns correct data'
459 # FIXME: TODO does not work on new T::B under threads sigh
460 # https://github.com/Test-More/test-more/issues/683
462 ! DBIx::Class::_ENV_::OLD_MRO
466 #local $TODO = "On 5.10+ -T combined with stash peeking invalidates the pkg_gen (wtf)" if ...
470 serialize( describe_class_methods("DBICTest::AttrTest") )
474 "Asking for alternative mro type did not invalidate cache"
478 # setting mro explicitly still matches what we expect
479 mro::set_mro("DBICTest::AttrTest", "dfs");
482 # in case set_mro starts increasing pkg_gen...
484 %{describe_class_methods("DBICTest::AttrTest")},
485 cumulative_gen => $expected_desc->{cumulative_gen},
488 'describing with implicit mro returns correct data'
491 # check that a UNIVERSAL-parent interrogation makes sense
492 # ( it should not list anything from UNIVERSAL itself )
494 describe_class_methods("UEBERVERSAL"),
496 # should be cached by now, thus safe to rely on...?
497 cumulative_gen => DBIx::Class::_Util::get_real_pkg_gen('UEBERVERSAL'),
499 class => 'UEBERVERSAL',
500 mro => { is_c3 => 0, type => 'dfs' },
503 ueber => $expected_desc->{methods}{ueber}
505 methods_defined_in_class => {
506 ueber => $expected_desc->{methods}{ueber}[0]
509 "Expected description of a parent-of-UNIVERSAL class (pathological case)",
514 SKIP: { skip "Skipping the thread test: $skip_threads", 1 }
520 my $t = threads->create(sub {
522 my $t = threads->create(sub {
525 select( undef, undef, undef, 0.2 ); # without this many tasty crashes even on latest perls
530 die "Unable to start thread: $!"
531 unless $! == Errno::EAGAIN();
533 SKIP: { skip "EAGAIN encountered, your system is likely bogged down: skipping rest of test", 1 }
540 select( undef, undef, undef, 0.2 ); # without this many tasty crashes even on latest perls
544 die "Unable to start thread: $!"
545 unless $! == Errno::EAGAIN();
547 skip "EAGAIN encountered, your system is likely bogged down: skipping rest of test", 1;
553 'Thread stack exitted succesfully'
557 # this doesn't really belong in this test, but screw it
559 package DBICTest::WackyDFS;
560 use base qw( DBICTest::SomeGrandParentClass DBICTest::SomeParentClass );
564 describe_class_methods("DBICTest::WackyDFS")->{methods}{VALID_DBIC_CODE_ATTRIBUTE},
568 name => "VALID_DBIC_CODE_ATTRIBUTE",
569 via_class => "DBICTest::SomeGrandParentClass",
573 name => "VALID_DBIC_CODE_ATTRIBUTE",
574 via_class => "DBIx::Class::MethodAttributes"
577 'Expected description on unusable inheritance hierarchy'