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