File: 41detachedcode-call.t

package info (click to toggle)
libio-async-perl 0.29-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 684 kB
  • ctags: 239
  • sloc: perl: 6,439; makefile: 2
file content (379 lines) | stat: -rw-r--r-- 8,852 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/perl -w

use strict;

use IO::Async::Test;

use Test::More tests => 42;
use Test::Exception;
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, 2, '$loop has refcount 2 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' );

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

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

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

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

dies_ok( sub { $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' );

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

is( scalar $code->workers, 1, '$code->workers is 1 for socket stream' );

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

undef $result;
wait_for { defined $result };

is( $result, 11, '$result of code over socket' );

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

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

is( scalar $code->workers, 1, '$code->workers is 1 for pipe stream' );

undef $result;
wait_for { defined $result };

is( $result, 11, '$result of code over pipe' );

dies_ok( sub { IO::Async::DetachedCode->new(
                  loop => $loop,
                  code => sub { return $_[0] },
                  stream => "oranges",
               ); },
         'Unrecognised stream type fails' );

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

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

undef $result;
wait_for { defined $result };

is( $result, 15, '$result of code over flat' );

dies_ok( sub { $code->call( 
                  args => [ \'a' ], 
                  on_return => sub {},
                  on_error  => sub {},
               );
            },
         'call with reference arguments using flat marshaller dies' );

dies_ok( sub { IO::Async::DetachedCode->new(
                  loop => $loop,
                  code => sub { return $_[0] },
                  marshaller => "grapefruit",
               ); },
         'Unrecognised marshaller type fails' );

$code = IO::Async::DetachedCode->new(
   loop => $loop,
   code => sub { return ref( $_[0] ), \$_[1] },
   marshaller => "storable",
);

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

undef @result;
wait_for { scalar @result };

is_deeply( \@result, [ 'SCALAR', \'b' ], '@result after call to code over storable marshaller' );

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' );

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

$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' );

$code = $loop->detach_code(
   code => sub { return join( "+", @_ ) },
);

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

undef $result;
wait_for { defined $result };

is( $result, "a+b+c", '$result of Set-constructed code' );

## Now test that parallel runs really are parallel

$code = $loop->detach_code(
   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();

while( not( -e "$dir/1" and -e "$dir/2" and -e "$dir/3" ) ) {
   if( time() - $start > 10 ) {
      die "Not all child processes ready after 10second wait";
   }

   $loop->loop_once( 0.1 );
}

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 = $loop->detach_code(
   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, 2, '$loop has refcount 2 at EOF' );