1use strict;
2use warnings;
3
4BEGIN {
5    use Config;
6    if (! $Config{'useithreads'}) {
7        print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
8        exit(0);
9    }
10}
11
12use threads;
13use Thread::Queue;
14
15BEGIN { # perl RT 133382
16if ($] == 5.008) {
17    require 't/test.pl';   # Test::More work-alike for Perl 5.8.0
18} else {
19    require Test::More;
20}
21Test::More->import();
22} # end BEGIN
23plan('tests' => 19);
24
25### ->dequeue_timed(TIMEOUT, COUNT) test ###
26
27my $q = Thread::Queue->new();
28ok($q, 'New queue');
29
30my @items = qw/foo bar baz qux exit/;
31$q->enqueue(@items);
32is($q->pending(), scalar(@items), 'Queue count');
33
34threads->create(sub {
35    is($q->pending(), scalar(@items), 'Queue count in thread');
36    while (my @el = $q->dequeue_timed(2.5, 2)) {
37        is($el[0], shift(@items), "Thread got $el[0]");
38        if ($el[0] eq 'exit') {
39            is(scalar(@el), 1, 'Thread to exit');
40        } else {
41            is($el[1], shift(@items), "Thread got $el[1]");
42        }
43    }
44    is($q->pending(), 0, 'Empty queue');
45    $q->enqueue('done');
46})->join();
47
48is($q->pending(), 1, 'Queue count after thread');
49is($q->dequeue(), 'done', 'Thread reported done');
50is($q->pending(), 0, 'Empty queue');
51
52### ->dequeue_timed(TIMEOUT) test on empty queue ###
53
54threads->create(sub {
55    is($q->pending(), 0, 'Empty queue in thread');
56    my @el = $q->dequeue_timed(1.5);
57    is($el[0], undef, "Thread got no items");
58    is($q->pending(), 0, 'Empty queue in thread');
59    $q->enqueue('done');
60})->join();
61
62is($q->pending(), 1, 'Queue count after thread');
63is($q->dequeue(), 'done', 'Thread reported done');
64is($q->pending(), 0, 'Empty queue');
65
66exit(0);
67
68# EOF
69