This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new perldelta
[perl5.git] / dist / Thread-Queue / t / 03_peek.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 BEGIN { # perl RT 133382
16 if ($] == 5.008) {
17     require 't/test.pl';   # Test::More work-alike for Perl 5.8.0
18 } else {
19     require Test::More;
20 }
21 Test::More->import();
22 } # end BEGIN
23 plan('tests' => 19);
24
25 my $q = Thread::Queue->new(1..10);
26 ok($q, 'New queue');
27
28 $q->enqueue([ qw/foo bar/ ]);
29
30 sub q_check
31 {
32     is($q->peek(3), 4, 'Peek at queue');
33     is($q->peek(-3), 9, 'Negative peek');
34
35     my $nada = $q->peek(20);
36     ok(! defined($nada), 'Big peek');
37     $nada = $q->peek(-20);
38     ok(! defined($nada), 'Big negative peek');
39
40     my $ary = $q->peek(-1);
41     is_deeply($ary, [ qw/foo bar/ ], 'Peek array');
42
43     is($q->pending(), 11, 'Queue count in thread');
44 }
45
46 threads->create(sub {
47     q_check();
48     threads->create('q_check')->join();
49 })->join();
50 q_check();
51
52 exit(0);
53
54 # EOF