File: Conveyor.t

package info (click to toggle)
libthread-conveyor-perl 0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 160 kB
  • sloc: perl: 25; makefile: 2
file content (111 lines) | stat: -rw-r--r-- 2,797 bytes parent folder | download | duplicates (4)
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
BEGIN {				# Magic Perl CORE pragma
    if ($ENV{PERL_CORE}) {
        chdir 't' if -d 't';
        @INC = '../lib';
    }
}

use strict;
use warnings;
use Test::More tests => 2 + (2 * (16 + 3 * (3 * 4) ) );

BEGIN { use_ok('Thread::Conveyor') }

my $default_optimize = $] > 5.008 ? 'cpu' : 'memory';
is( Thread::Conveyor->optimize,$default_optimize,"Check default optimization" );

foreach my $optimize (qw(cpu memory)) {

  diag( "test belt optimized for $optimize" );

  my @base = (optimize => $optimize);

  my $belt = Thread::Conveyor->new( {@base} );
  isa_ok( $belt, 'Thread::Conveyor', 'check object type' );

  can_ok( $belt,qw(
   clean
   clean_dontwait
   maxboxes
   minboxes
   new
   onbelt
   peek
   peek_dontwait
   put
   take
   take_dontwait
   shutdown
   thread
   tid
  ) );

  $belt->put( qw(a b c) );
  $belt->put( [qw(a b c)] );
  $belt->put( {a => 1, b => 2, c => 3} );

  is( $belt->onbelt, 3,			'check number boxes on belt');

  my @l = $belt->take;
  is( @l, 3,				'check # elements simple list' );
  ok( ($l[0] eq 'a' and $l[1] eq 'b' and $l[2] eq 'c'), 'check simple list' );

  my @lr = $belt->take_dontwait;
  cmp_ok( @lr, '==', 1,			'check # elements list ref' );
  is( ref($lr[0]), 'ARRAY',		'check type of list ref' );
  ok( ($lr[0]->[0] eq 'a' and $lr[0]->[1] eq 'b' and $lr[0]->[2] eq 'c'),
   'check list ref'
  );

  my @hr = $belt->peek_dontwait;
  cmp_ok( @hr, '==', 1,			'check # elements hash ref, #1' );
  is( ref($hr[0]), 'HASH',		'check type of hash ref, #1' );

  @hr = $belt->peek;
  cmp_ok( @hr, '==', 1,			'check # elements hash ref, #2' );
  is( ref($hr[0]), 'HASH',		'check type of hash ref, #2' );

  @hr = $belt->take;
  cmp_ok( @hr, '==', 1,			'check # elements hash ref, #3' );
  is( ref($hr[0]), 'HASH',		'check type of hash ref, #3' );
  ok( ($hr[0]->{a} == 1 and $hr[0]->{b} == 2 and $hr[0]->{c} == 3),
   'check hash ref'
  );

  my @e = $belt->take_dontwait;
  cmp_ok( @e, '==', 0,			'check # elements dontwait' );
  $belt->shutdown;

  foreach my $times (10,100,1000) {

    foreach (
     {@base},
     {@base, maxboxes => undef},
     {@base, maxboxes => 500, minboxes => 495},
    ) {

      my $belt = Thread::Conveyor->new( $_ );

      isa_ok( $belt,'Thread::Conveyor',	'check object type' );

      my @n : shared = ();
      my $thread = threads->new( sub {
       while (1) {
         my ($n) = $belt->take;
         last unless defined( $n );
         push( @n,$n );
       }
      } );
      isa_ok( $thread,'threads',		'check object type' );

      foreach ((1..$times),undef) {
          $belt->put( $_ );
      }
      ok( !defined( $thread->join ),	'check result of join()' );

      my $check = '';
      $check .= $_ foreach 1..$times;
      is( join('',@n),$check,		'check result of boxes on belt' );
    }
  }
}