File: 15local-errsv.t

package info (click to toggle)
libfuture-asyncawait-perl 0.70-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 528 kB
  • sloc: perl: 2,647; ansic: 118; pascal: 34; makefile: 3
file content (66 lines) | stat: -rw-r--r-- 1,599 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0 0.000148; # is_refcount

use Future;

use Future::AsyncAwait;

my $orig_cxstack_ix = Future::AsyncAwait::__cxstack_ix;

my $errgv_ref = \*@;

# $@ can be localized
#   For some odd reason, SvREFCNT(PL_errgv) increases by 1 the first time
#   this code is run, but is stable thereafter. So only test it the second
#   time. Perhaps something somewhere needs to get lazily created?
foreach my $idx ( 1, 2 )
{
   my $errsv_refcount = refcount(\$@);
   my $errgv_refcount = refcount($errgv_ref);

   my $f1 = Future->new;
   my $fret = (async sub {
      local $@ = "inside";
      await $f1;
      return $@;
   })->();

   $@ = "OUTSIDE";
   $f1->done;
   is( scalar $fret->get, "inside", 'result from async sub with local $@' );

   is_refcount( \$@, $errsv_refcount, '$@ refcount preserved' );
   is_refcount( $errgv_ref, $errgv_refcount, '*@ refcount preserved' ) if $idx > 1;
}

# localised $@ plays nicely with eval{}
{
   my $errsv_refcount = refcount(\$@);
   my $errgv_refcount = refcount($errgv_ref);

   my $f1 = Future->new;
   my $fret = (async sub {
      local $@ = "inside";
      await $f1;
      eval {
         die "oopsie\n";
      };
      return $@;
   })->();

   $@ = "OUTSIDE";
   $f1->done;
   is( scalar $fret->get, "oopsie\n", 'result from eval { die }' );

   is_refcount( \$@, $errsv_refcount, '$@ refcount preserved' );
   is_refcount( $errgv_ref, $errgv_refcount, '*@ refcount preserved' );
}

is( Future::AsyncAwait::__cxstack_ix, $orig_cxstack_ix,
   'cxstack_ix did not grow during the test' );

done_testing;