5 BEGIN { require "t/tools.pl" };
6 use Test2::Util qw/CAN_THREAD CAN_REALLY_FORK USE_THREADS get_tid/;
8 skip_all("Leaks shm blocks");
10 my $CLASS = 'Test2::API::Instance';
12 my $one = $CLASS->new;
31 post_load_callbacks => [],
32 context_acquire_callbacks => [],
33 context_init_callbacks => [],
34 context_release_callbacks => [],
38 "Got initial settings"
42 is_deeply($one, {}, "wiped object");
63 post_load_callbacks => [],
64 context_acquire_callbacks => [],
65 context_init_callbacks => [],
66 context_release_callbacks => [],
73 ok(!$one->formatter_set, "no formatter set");
74 $one->set_formatter('Foo');
75 ok($one->formatter_set, "formatter set");
79 my $callback = sub { $ran++ };
80 $one->add_post_load_callback($callback);
81 ok(!$ran, "did not run yet");
82 is_deeply($one->post_load_callbacks, [$callback], "stored callback for later");
84 ok(!$one->loaded, "not loaded");
86 ok($one->loaded, "loaded");
87 is($ran, 1, "ran the callback");
90 is($ran, 1, "Did not run the callback again");
92 $one->add_post_load_callback($callback);
93 is($ran, 2, "ran the new callback");
94 is_deeply($one->post_load_callbacks, [$callback, $callback], "stored callback for the record");
97 exception { $one->add_post_load_callback({}) },
98 qr/Post-load callbacks must be coderefs/,
99 "Post-load callbacks must be coderefs"
103 ok($one->ipc, 'got ipc');
104 ok($one->finalized, "calling ipc finalized the object");
107 ok($one->stack, 'got stack');
108 ok(!$one->finalized, "calling stack did not finaliz the object");
111 ok($one->formatter, 'Got formatter');
112 ok($one->finalized, "calling format finalized the object");
115 $one->set_formatter('Foo');
116 is($one->formatter, 'Foo', "got specified formatter");
117 ok($one->finalized, "calling format finalized the object");
120 local $ENV{T2_FORMATTER} = 'TAP';
122 is($one->formatter, 'Test2::Formatter::TAP', "got specified formatter");
123 ok($one->finalized, "calling format finalized the object");
125 local $ENV{T2_FORMATTER} = '+Test2::Formatter::TAP';
127 is($one->formatter, 'Test2::Formatter::TAP', "got specified formatter");
128 ok($one->finalized, "calling format finalized the object");
130 local $ENV{T2_FORMATTER} = '+Fake';
133 exception { $one->formatter },
134 qr/COULD NOT LOAD FORMATTER 'Fake' \(set by the 'T2_FORMATTER' environment variable\)/,
141 $one->add_exit_callback($callback);
142 is(@{$one->exit_callbacks}, 1, "added an exit callback");
143 $one->add_exit_callback($callback);
144 is(@{$one->exit_callbacks}, 2, "added another exit callback");
147 exception { $one->add_exit_callback({}) },
148 qr/End callbacks must be coderefs/,
149 "Exit callbacks must be coderefs"
152 if (CAN_REALLY_FORK) {
155 die "Failed to fork!" unless defined $pid;
156 unless($pid) { exit 0 }
158 is($one->_ipc_wait, 0, "No errors");
161 die "Failed to fork!" unless defined $pid;
162 unless($pid) { exit 255 }
165 local $SIG{__WARN__} = sub { push @warnings => @_ };
166 is($one->_ipc_wait, 255, "Process exited badly");
168 like($warnings[0], qr/Process .* did not exit cleanly \(status: 255\)/, "Warn about exit");
171 if (CAN_THREAD && $] ge '5.010') {
175 threads->new(sub { 1 });
176 is($one->_ipc_wait, 0, "No errors");
178 if (threads->can('error')) {
186 local $SIG{__WARN__} = sub { push @warnings => @_ };
187 is($one->_ipc_wait, 255, "Thread exited badly");
189 like($warnings[0], qr/Thread .* did not end cleanly: xxx/, "Warn about exit");
197 is($?, 0, "no errors on exit");
205 is($?, 0, "no errors on exit");
214 is($?, 0, "no errors on exit");
219 $one->stack->top->set_no_ending(1);
222 is($?, 0, "no errors on exit");
228 $one->stack->top->set_failed(2);
231 is($?, 2, "number of failures");
239 is($?, 255, "set exit code to a sane number");
244 delete $INC{'Test2/IPC.pm'};
248 $one->stack->top->filter(sub { push @events => $_[1]; undef});
249 $one->stack->new_hub;
252 is($?, 255, "errors on exit");
253 like($events[0]->message, qr/Test ended with extra hubs on the stack!/, "got diag");
260 local $INC{'Test/Builder.pm'} = __FILE__;
261 local $Test2::API::VERSION = '0.002';
262 local $Test::Builder::VERSION = '0.001';
264 open(STDERR, '>', \$stderr) or print "Failed to open new STDERR";
269 is($stderr, <<' EOT', "Got warning about version mismatch");
271 ********************************************************************************
273 * Test::Builder -- Test2::API version mismatch detected *
275 ********************************************************************************
276 Test2::API Version: 0.002
277 Test::Builder Version: 0.001
279 This is not a supported configuration, you will have problems.
285 require Test2::API::Breakage;
286 no warnings qw/redefine once/;
288 local *Test2::API::Breakage::report = sub { $ran++; return "foo" };
289 use warnings qw/redefine once/;
296 open(STDERR, '>', \$stderr) or print "Failed to open new STDERR";
301 is($stderr, <<" EOT", "Reported bad modules");
303 You have loaded versions of test modules known to have problems with Test2.
304 This could explain some test failures.
315 $one->stack->top->filter(sub { push @events => $_[1]; undef});
316 $one->stack->new_hub;
317 ok($one->stack->top->ipc, "Have IPC");
318 $one->stack->new_hub;
319 ok($one->stack->top->ipc, "Have IPC");
320 $one->stack->top->set_ipc(undef);
321 ok(!$one->stack->top->ipc, "no IPC");
322 $one->stack->new_hub;
325 is($?, 255, "errors on exit");
326 like($events[0]->message, qr/Test ended with extra hubs on the stack!/, "got diag");
329 if (CAN_REALLY_FORK) {
330 local $SIG{__WARN__} = sub { };
333 die "Failed to fork!" unless defined $pid;
334 unless ($pid) { exit 255 }
340 is($?, 255, "errors on exit");
344 die "Failed to fork!" unless defined $pid;
345 unless ($pid) { exit 255 }
351 is($?, 122, "kept original exit");
356 trace => Test2::Util::Trace->new(frame => ['Foo::Bar', 'Foo/Bar.pm', 42, 'xxx']),
357 hub => Test2::Hub->new(),
358 }, 'Test2::API::Context';
359 $one->contexts->{1234} = $ctx;
362 my $warnings = warnings { $one->set_exit };
363 is($?, 255, "set exit code to a sane number");
368 "context object was never released! This means a testing tool is behaving very badly at Foo/Bar.pm line 42.\n"
370 "Warned about unfreed context"
376 delete $INC{'Test2/IPC.pm'};
377 delete $INC{'threads.pm'};
378 ok(!USE_THREADS, "Sanity Check");
381 ok(!$one->ipc, 'IPC not loaded, no IPC object');
382 ok($one->finalized, "calling ipc finalized the object");
383 is($one->ipc_polling, undef, "no polling defined");
384 ok(!@{$one->ipc_drivers}, "no driver");
387 local $INC{'threads.pm'} = 1;
389 local *threads::tid = sub { 0 } unless threads->can('tid');
391 ok($one->ipc, 'IPC loaded if threads are');
392 ok($one->finalized, "calling ipc finalized the object");
393 ok($one->ipc_polling, "polling on by default");
394 is($one->ipc_drivers->[0], 'Test2::IPC::Driver::Files', "default driver");
398 local $INC{'Test2/IPC.pm'} = 1;
400 ok($one->ipc, 'IPC loaded if Test2::IPC is');
401 ok($one->finalized, "calling ipc finalized the object");
402 ok($one->ipc_polling, "polling on by default");
403 is($one->ipc_drivers->[0], 'Test2::IPC::Driver::Files', "default driver");
406 require Test2::IPC::Driver::Files;
408 $one->add_ipc_driver('Test2::IPC::Driver::Files');
409 ok($one->ipc, 'IPC loaded if drivers have been added');
410 ok($one->finalized, "calling ipc finalized the object");
411 ok($one->ipc_polling, "polling on by default");
414 my $line = __LINE__ + 1;
415 my $warnings = warnings { $one->add_ipc_driver('Test2::IPC::Driver::Files') };
418 qr{^IPC driver Test2::IPC::Driver::Files loaded too late to be used as the global ipc driver at \Q$file\E line $line},
419 "Got warning at correct frame"
423 $one->add_ipc_driver('Fake::Fake::XXX');
425 exception { $one->ipc },
426 "IPC has been requested, but no viable drivers were found. Aborting...\n",
427 "Failed without viable IPC driver"
433 ok(!@{$one->context_init_callbacks}, "no callbacks");
434 is($one->ipc_polling, undef, "no polling, undef");
436 $one->disable_ipc_polling;
437 ok(!@{$one->context_init_callbacks}, "no callbacks");
438 is($one->ipc_polling, undef, "no polling, still undef");
442 local *Fake::Hub::cull = sub { $cull++ };
445 $one->enable_ipc_polling;
446 is(@{$one->context_init_callbacks}, 1, "added the callback");
447 is($one->ipc_polling, 1, "polling on");
448 $one->set_ipc_shm_last('abc1');
449 $one->context_init_callbacks->[0]->({'hub' => 'Fake::Hub'});
450 is($cull, 1, "called cull once");
453 $one->disable_ipc_polling;
454 is(@{$one->context_init_callbacks}, 1, "kept the callback");
455 is($one->ipc_polling, 0, "no polling, set to 0");
456 $one->set_ipc_shm_last('abc3');
457 $one->context_init_callbacks->[0]->({'hub' => 'Fake::Hub'});
458 is($cull, 0, "did not call cull");
461 $one->enable_ipc_polling;
462 is(@{$one->context_init_callbacks}, 1, "did not add the callback");
463 is($one->ipc_polling, 1, "polling on");
464 $one->set_ipc_shm_last('abc3');
465 $one->context_init_callbacks->[0]->({'hub' => 'Fake::Hub'});
466 is($cull, 1, "called cull once");