]> git.vpit.fr Git - perl/modules/Thread-Cleanup.git/blobdiff - t/21-ctl.t
Don't exit() when dieing during cleanup
[perl/modules/Thread-Cleanup.git] / t / 21-ctl.t
diff --git a/t/21-ctl.t b/t/21-ctl.t
new file mode 100644 (file)
index 0000000..4029b90
--- /dev/null
@@ -0,0 +1,58 @@
+#!perl -T
+
+use strict;
+use warnings;
+
+use Config qw/%Config/;
+
+BEGIN {
+ if (!$Config{useithreads}) {
+  require Test::More;
+  Test::More->import;
+  plan(skip_all => 'This perl wasn\'t built to support threads');
+ }
+}
+
+use threads;
+use threads::shared;
+
+use Test::More tests => 5 + 1;
+
+BEGIN {
+ defined and diag "Using threads $_"         for $threads::VERSION;
+ defined and diag "Using threads::shared $_" for $threads::shared::VERSION;
+}
+
+use Thread::Cleanup;
+
+my @stack : shared;
+
+sub msg { lock @stack; push @stack, join ':', @_ }
+
+Thread::Cleanup::register {
+ msg 'cleanup';
+ die 'cleanup';
+ msg 'not reached 1';
+};
+
+{
+ local $SIG{__DIE__} = sub { msg 'sig', @_ };
+ no warnings 'threads';
+ threads->create(sub {
+  msg 'spawn';
+  die 'thread';
+  msg 'not reached 2';
+ })->join;
+}
+
+msg 'done';
+
+{
+ lock @stack;
+ is   shift(@stack), 'spawn';
+ like shift(@stack), qr/sig:thread at \Q$0\E line \d+/;
+ is   shift(@stack), 'cleanup';
+ like shift(@stack), qr/sig:cleanup at \Q$0\E line \d+/;
+ is   shift(@stack), 'done';
+ is_deeply \@stack,  [ ], 'nothing more';
+}