File: 01signal.t

package info (click to toggle)
libevent-distributor-perl 0.06-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 140 kB
  • sloc: perl: 401; makefile: 2
file content (101 lines) | stat: -rw-r--r-- 2,154 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/perl

use v5.14;
use warnings;

use Test::More;
use Test::Fatal;

use Future;
use Event::Distributor::Signal;

# async->async
{
   my $sig = Event::Distributor::Signal->new;

   my $called_f;
   my @called_args;
   $sig->subscribe( sub {
      @called_args = @_;
      return $called_f = Future->new
   });

   my $f = $sig->fire( "args", "here" );
   ok( $f, '->fire yields Future' );
   ok( !$f->is_ready, '$f not yet ready' );

   is_deeply( \@called_args, [ "args", "here" ], 'Args to subscriber' );

   $called_f->done();

   ok( $f->is_ready, '$f is now ready after $called_f->done' );

   is_deeply( [ $f->get ], [], '$f->get yields nothing' );
}

# two subscribers
{
   my $sig = Event::Distributor::Signal->new;

   my $f1;
   $sig->subscribe( sub { $f1 = Future->new } );

   my $f2;
   $sig->subscribe( sub { $f2 = Future->new } );

   my $f = $sig->fire;

   ok( $f1 && $f2, 'Both subscribers invoked' );

   $f1->done;
   ok( !$f->is_ready, 'Result future still waiting after $f1->done' );

   $f2->done;
   ok( $f->is_ready, 'Result future now done after $f2->done' );
}

# failure
{
   my $sig = Event::Distributor::Signal->new;

   $sig->subscribe( sub { die "Failure" } );

   my $called;
   $sig->subscribe( sub { $called++; Future->done } );

   like( exception { $sig->fire->get },
         qr/^Failure /,
         '->fire_sync raises exception' );
   ok( $called, 'second subscriber still invoked after first failure' );
}

# Multiple failures
{
   my $sig = Event::Distributor::Signal->new;

   $sig->subscribe( sub { die "One failed\n" } );
   $sig->subscribe( sub { die "Two failed\n" } );

   is( exception { $sig->fire->get },
      "Multiple subscribers failed:\n" .
      " | One failed\n" .
      " | Two failed\n",
      '->fire_sync raises special multiple failure' );
}

# subscribers cannot corrupt object by $_ leakage
{
   my $sig = Event::Distributor::Signal->new;

   my $called;
   $sig->subscribe( sub { $called++; undef $_; Future->done } );

   Future->needs_all(
      $sig->fire( whail => ),
      $sig->fire( whail => ),
   )->get;

   is( $called, 2, 'Subscriber invoked twice' );
}

done_testing;