File: 43detachedcode.t

package info (click to toggle)
libio-async-perl 0.51-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,052 kB
  • sloc: perl: 11,110; makefile: 8
file content (264 lines) | stat: -rw-r--r-- 6,021 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/perl -w

use strict;

use IO::Async::Test;

use Test::More tests => 31;
use Test::Fatal;
use Test::Refcount;

use File::Temp qw( tempdir );
use Time::HiRes qw( sleep );

use IO::Async::DetachedCode;

use IO::Async::Loop::Poll;

my $loop = IO::Async::Loop::Poll->new;

testing_loop( $loop );
is_refcount( $loop, 3, '$loop has refcount 3 after adding to IO::Async::Test' );

my $code = IO::Async::DetachedCode->new(
   loop => $loop,
   code => sub { return $_[0] + $_[1] },
);

ok( defined $code, '$code defined' );
isa_ok( $code, "IO::Async::DetachedCode", '$code isa IO::Async::DetachedCode' );

is_oneref( $code, '$code has refcount 1' );

is( scalar $code->workers, 1, '$code->workers is 1' );
my @workers = $code->workers;
is( scalar @workers, 1, '@workers has 1 value' );
ok( kill( 0, $workers[0] ), '$workers[0] is a PID' );

ok( exception { $code->call( args => [], on_result => "hello" ) },
    'call with on_result not CODE ref fails' );

ok( exception { $code->call( args => [], on_return => sub {} ) },
    'call missing on_error ref fails' );

ok( exception { $code->call( args => [], on_error => sub {} ) },
    'call missing on_return ref fails' );

ok( exception { $code->call( args => [], on_return => "hello", on_error => sub {} ) },
    'call with on_return not a CODE ref fails' );

ok( exception { $code->call( args => [], on_return => sub {}, on_error => "hello" ) },
    'call with on_error not a CODE ref fails' );

my $result;

$code->call(
   args => [ 10, 20 ],
   on_return => sub { $result = shift },
   on_error  => sub { die "Test failed early - @_" },
);

is( $result, undef, '$result before call returns' );

is( scalar $code->workers, 1, '$code->workers is still 1 after call' );

undef $result;
wait_for { defined $result };

is( $result, 30, '$result after call returns' );

my @result;

$code->call(
   args => [ 1, 2 ],
   on_return => sub { push @result, shift },
   on_error  => sub { die "Test failed early - @_" },
);
$code->call(
   args => [ 3, 4 ],
   on_return => sub { push @result, shift },
   on_error  => sub { die "Test failed early - @_" },
);

is( scalar $code->workers, 1, '$code->workers is still 1 after 2 calls' );

undef @result;
wait_for { @result == 2 };

is_deeply( \@result, [ 3, 7 ], '@result after both calls return' );

is( scalar $code->workers, 1, '$code->workers is still 1 after 2 calls return' );

my $err;

$code = IO::Async::DetachedCode->new(
   loop=> $loop,
   code => sub { die shift },
);

$code->call(
   args => [ "exception name" ],
   on_return => sub { },
   on_error  => sub { $err = shift },
);

undef $err;
wait_for { defined $err };

like( $err, qr/^exception name at $0 line \d+\.$/, '$err after exception' );

my $count = 0;
$code = IO::Async::DetachedCode->new(
   loop=> $loop,
   code => sub { $count++; die "$count\n" },
   exit_on_die => 0,
);

my @errs;
$code->call(
   args => [],
   on_return => sub { },
   on_error  => sub { push @errs, shift },
);
$code->call(
   args => [],
   on_return => sub { },
   on_error  => sub { push @errs, shift },
);

undef @errs;
wait_for { scalar @errs == 2 };

is_deeply( \@errs, [ "1\n", "2\n" ], 'Closed variables preserved when exit_on_die => 0' );

$code = IO::Async::DetachedCode->new(
   loop=> $loop,
   code => sub { $count++; die "$count\n" },
   exit_on_die => 1,
);

undef @errs;

$code->call(
   args => [],
   on_return => sub { },
   on_error  => sub { push @errs, shift },
);
wait_for { scalar @errs == 1 };

$code->call(
   args => [],
   on_return => sub { },
   on_error  => sub { push @errs, shift },
);
wait_for { scalar @errs == 2 };

is_deeply( \@errs, [ "1\n", "1\n" ], 'Closed variables no preserved when exit_on_die => 1' );

$code = IO::Async::DetachedCode->new(
   loop => $loop,
   code => sub { $_[0] ? exit shift : return 0 },
);

$code->call(
   args => [ 16 ],
   on_return => sub { $err = "" },
   on_error  => sub { $err = [ @_ ] },
);

undef $err;
wait_for { defined $err };

# Not sure what reason we might get - need to check both
ok( $err->[0] eq "closed" || $err->[0] eq "exit", '$err->[0] after child death' );

$code->call(
   args => [ 0 ],
   on_return => sub { $err = "return" },
   on_error  => sub { $err = [ @_ ] },
);

is( scalar $code->workers, 1, '$code->workers is now 1 again' );

undef $err;
wait_for { defined $err };

is( $err, "return", '$err is "return" after child nondeath' );

## Now test that parallel runs really are parallel

$code = IO::Async::DetachedCode->new(
   loop => $loop,
   code => sub {
      my ( $file, $ret ) = @_;

      open( my $fh, ">", $file ) or die "Cannot write $file - $!";
      close( $file );

      # Wait for synchronisation
      sleep 0.1 while -e $file;

      return $ret;
   },
   workers => 3,
);

is( scalar $code->workers, 3, '$code->workers is 3' );

my $dir = tempdir( CLEANUP => 1 );

my %ret;

foreach my $id ( 1, 2, 3 ) {
   $code->call(
      args => [ "$dir/$id", $id ],
      on_return => sub { $ret{$id} = shift },
      on_error  => sub { die "Test failed early - @_" },
   );
}

my $start = time;

wait_for { -e "$dir/1" and -e "$dir/2" and -e "$dir/3" };

ok( 1, 'synchronise files created' );

# Synchronize deleting them;

for my $f ( "$dir/1", "$dir/2", "$dir/3" ) {
   unlink $f or die "Cannot unlink $f - $!";
}

undef %ret;
wait_for { keys %ret == 3 };

is_deeply( \%ret, { 1 => 1, 2 => 2, 3 => 3 }, 'ret keys after parallel run' );

is( scalar $code->workers, 3, '$code->workers is still 3' );

$code = IO::Async::DetachedCode->new(
   loop => $loop,
   code => sub {
      return $ENV{$_[0]};
   },

   setup => [
      env => { FOO => "Here is a random string" },
   ],
);

$code->call(
   args => [ "FOO" ],
   on_return => sub { $result = shift },
   on_error  => sub { die "Test failed early - @_" },
);

undef $result;
wait_for { defined $result };

is( $result, "Here is a random string", '$result after call with modified ENV' );

is_oneref( $code, '$code has refcount 1 at EOF' );
undef $code;

is_refcount( $loop, 3, '$loop has refcount 3 at EOF' );