File: 02_do_callback_result.t

package info (click to toggle)
libmce-perl 1.517-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,864 kB
  • ctags: 377
  • sloc: perl: 10,516; makefile: 2
file content (118 lines) | stat: -rwxr-xr-x 2,116 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
#!/usr/bin/env perl

use strict;
use warnings;

BEGIN {
   eval 'use threads; use threads::shared;' if $^O eq 'MSWin32';
}

use Test::More tests => 5;

use MCE;

my (@ans, @rpl, %hsh, $mce);

###############################################################################

sub callback {
   push @ans, $_[0];
   return;
}

$mce = MCE->new(
   use_threads => ($^O eq 'MSWin32') ? 1 : 0,
   spawn_delay => 0.2,
   max_workers => 4,

   user_func => sub {
      my ($self) = @_;
      $self->do('callback', $self->wid());
   }
);

@ans = ();
$mce->run;

is(join('', sort @ans), '1234', 'check that wid is correct for children');

###############################################################################

sub callback2 {
   push @ans, $_[0];
   return $_[0] * 2;
}

sub callback3 {
   push @rpl, $_[0];
   return;
}

$mce = MCE->new(
   use_threads => ($^O eq 'MSWin32') ? 1 : 0,
   spawn_delay => 0.2,
   max_workers => 4,

   user_func => sub {
      my ($self) = @_;
      my $reply  = $self->do('callback2', $self->wid());
      $self->do('callback3', $reply);
   }
);

@ans = (); @rpl = ();
$mce->run;

is(join('', sort @ans), '1234', 'check that wid is correct for threads');
is(join('', sort @rpl), '2468', 'check that scalar is correct');

###############################################################################

sub callback4 {
   return @rpl;
}

sub callback5 {
   my $a_ref = $_[0];
   my %h = ();

   @ans = ();

   foreach (@{ $a_ref }) {
      push @ans, $_ / 2;
      $h{$_ / 2} = $_;
   }

   return %h;
}

sub callback6 {
   my $h_ref = $_[0];

   @rpl = (); 

   foreach (sort keys %{ $h_ref }) {
      $rpl[$_ - 1] = $h_ref->{$_};
   }

   return;
}

$mce = MCE->new(
   use_threads => ($^O eq 'MSWin32') ? 1 : 0,
   spawn_delay => 0.2,
   max_workers => 1,

   user_func => sub {
      my ($self) = @_;
      my @reply  = $self->do('callback4');
      my %reply  = $self->do('callback5', \@reply);
      $self->do('callback6', \%reply);
   }
);

$mce->run;

is(join('', sort @ans), '1234', 'check that list is correct');
is(join('', sort @rpl), '2468', 'check that hash is correct');