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
|
use strict;
use warnings;
BEGIN {
use Config;
if (! $Config{'useithreads'}) {
print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
exit(0);
}
}
use threads;
use Thread::Queue;
BEGIN { # perl RT 133382
if ($] == 5.008) {
require 't/test.pl'; # Test::More work-alike for Perl 5.8.0
} else {
require Test::More;
}
Test::More->import();
} # end BEGIN
plan('tests' => 19);
### ->dequeue_timed(TIMEOUT, COUNT) test ###
my $q = Thread::Queue->new();
ok($q, 'New queue');
my @items = qw/foo bar baz qux exit/;
$q->enqueue(@items);
is($q->pending(), scalar(@items), 'Queue count');
threads->create(sub {
is($q->pending(), scalar(@items), 'Queue count in thread');
while (my @el = $q->dequeue_timed(2.5, 2)) {
is($el[0], shift(@items), "Thread got $el[0]");
if ($el[0] eq 'exit') {
is(scalar(@el), 1, 'Thread to exit');
} else {
is($el[1], shift(@items), "Thread got $el[1]");
}
}
is($q->pending(), 0, 'Empty queue');
$q->enqueue('done');
})->join();
is($q->pending(), 1, 'Queue count after thread');
is($q->dequeue(), 'done', 'Thread reported done');
is($q->pending(), 0, 'Empty queue');
### ->dequeue_timed(TIMEOUT) test on empty queue ###
threads->create(sub {
is($q->pending(), 0, 'Empty queue in thread');
my @el = $q->dequeue_timed(1.5);
is($el[0], undef, "Thread got no items");
is($q->pending(), 0, 'Empty queue in thread');
$q->enqueue('done');
})->join();
is($q->pending(), 1, 'Queue count after thread');
is($q->dequeue(), 'done', 'Thread reported done');
is($q->pending(), 0, 'Empty queue');
exit(0);
# EOF
|