This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Thread::Queue 2.11
[perl5.git] / lib / Thread / Queue / t / 01_basic.t
CommitLineData
54c7876f
JH
1use strict;
2use warnings;
3
4BEGIN {
5 if ($ENV{'PERL_CORE'}){
6 chdir('t');
7 unshift(@INC, '../lib');
8 }
9 use Config;
10 if (! $Config{'useithreads'}) {
11 print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
12 exit(0);
13 }
14}
15
16use threads;
17use Thread::Queue;
18
19if ($] == 5.008) {
20 require 't/test.pl'; # Test::More work-alike for Perl 5.8.0
21} else {
22 require Test::More;
23}
24Test::More->import();
25plan('tests' => 81);
26
27### Basic usage with multiple threads ###
28
29my $nthreads = 5;
30
31my $q = Thread::Queue->new(1..$nthreads);
32ok($q, 'New queue');
33is($q->pending(), $nthreads, 'Pre-populated queue count');
34
35sub reader {
36 my $id = threads->tid();
37 while ((my $el = $q->dequeue()) != -1) {
38 ok($el >= 1, "Thread $id got $el");
39 select(undef, undef, undef, rand(1));
40 }
41 ok(1, "Thread $id done");
42}
43
44my @threads;
45push(@threads, threads->create('reader')) for (1..$nthreads);
46
47for (1..20) {
48 select(undef, undef, undef, rand(1));
49 $q->enqueue($_);
50}
51
52$q->enqueue((-1) x $nthreads); # One end marker for each thread
53
54$_->join() foreach @threads;
55undef(@threads);
56
57is($q->pending(), 0, 'Empty queue');
58
59
60### ->dequeue_nb() test ###
61
62$q = Thread::Queue->new();
63ok($q, 'New queue');
64is($q->pending(), 0, 'Empty queue');
65
66my @items = qw/foo bar baz/;
67$q->enqueue(@items);
68
69threads->create(sub {
70 is($q->pending(), scalar(@items), 'Queue count in thread');
71 while (my $el = $q->dequeue_nb()) {
72 is($el, shift(@items), "Thread got $el");
73 }
74 is($q->pending(), 0, 'Empty queue');
75 $q->enqueue('done');
76})->join();
77
78is($q->pending(), 1, 'Queue count after thread');
79is($q->dequeue(), 'done', 'Thread reported done');
80is($q->pending(), 0, 'Empty queue');
81
82
83### ->dequeue(COUNT) test ###
84
85my $count = 3;
86
87sub reader2 {
88 my $id = threads->tid();
89 while (1) {
90 my @el = $q->dequeue($count);
91 is(scalar(@el), $count, "Thread $id got @el");
92 select(undef, undef, undef, rand(1));
93 return if ($el[0] == 0);
94 }
95}
96
97push(@threads, threads->create('reader2')) for (1..$nthreads);
98
99$q->enqueue(1..4*$count*$nthreads);
100$q->enqueue((0) x ($count*$nthreads));
101
102$_->join() foreach @threads;
103undef(@threads);
104
105is($q->pending(), 0, 'Empty queue');
106
107
108### ->dequeue_nb(COUNT) test ###
109
110@items = qw/foo bar baz qux exit/;
111$q->enqueue(@items);
112is($q->pending(), scalar(@items), 'Queue count');
113
114threads->create(sub {
115 is($q->pending(), scalar(@items), 'Queue count in thread');
116 while (my @el = $q->dequeue_nb(2)) {
117 is($el[0], shift(@items), "Thread got $el[0]");
118 if ($el[0] eq 'exit') {
119 is(scalar(@el), 1, 'Thread to exit');
120 } else {
121 is($el[1], shift(@items), "Thread got $el[1]");
122 }
123 }
124 is($q->pending(), 0, 'Empty queue');
125 $q->enqueue('done');
126})->join();
127
128is($q->pending(), 1, 'Queue count after thread');
129is($q->dequeue(), 'done', 'Thread reported done');
130is($q->pending(), 0, 'Empty queue');
131
132# EOF