Commit | Line | Data |
54c7876f |
1 | use strict; |
2 | use warnings; |
3 | |
4 | BEGIN { |
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 | |
16 | use threads; |
17 | use Thread::Queue; |
18 | |
19 | if ($] == 5.008) { |
20 | require 't/test.pl'; # Test::More work-alike for Perl 5.8.0 |
21 | } else { |
22 | require Test::More; |
23 | } |
24 | Test::More->import(); |
25 | plan('tests' => 16); |
26 | |
27 | my $q = Thread::Queue->new(1..10); |
28 | ok($q, 'New queue'); |
29 | |
30 | threads->create(sub { |
31 | $q->insert(5); |
32 | $q->insert(-5); |
33 | $q->insert(100); |
34 | $q->insert(-100); |
35 | })->join(); |
36 | |
37 | my @x = $q->dequeue_nb(100); |
38 | is_deeply(\@x, [1..10], 'No-op inserts'); |
39 | |
40 | |
41 | $q = Thread::Queue->new(1..10); |
42 | ok($q, 'New queue'); |
43 | |
44 | threads->create(sub { |
45 | $q->insert(10, qw/tail/); |
46 | $q->insert(0, qw/head/); |
47 | })->join(); |
48 | |
49 | @x = $q->dequeue_nb(100); |
50 | is_deeply(\@x, ['head',1..10,'tail'], 'Edge inserts'); |
51 | |
52 | |
53 | $q = Thread::Queue->new(1..10); |
54 | ok($q, 'New queue'); |
55 | |
56 | threads->create(sub { |
57 | $q->insert(5, qw/foo bar/); |
58 | $q->insert(-2, qw/qux/); |
59 | })->join(); |
60 | |
61 | @x = $q->dequeue_nb(100); |
62 | is_deeply(\@x, [1..5,'foo','bar',6..8,'qux',9,10], 'Middle inserts'); |
63 | |
64 | |
65 | $q = Thread::Queue->new(1..10); |
66 | ok($q, 'New queue'); |
67 | |
68 | threads->create(sub { |
69 | $q->insert(20, qw/tail/); |
70 | $q->insert(-20, qw/head/); |
71 | })->join(); |
72 | |
73 | @x = $q->dequeue_nb(100); |
74 | is_deeply(\@x, ['head',1..10,'tail'], 'Extreme inserts'); |
75 | |
76 | |
77 | $q = Thread::Queue->new(); |
78 | ok($q, 'New queue'); |
79 | threads->create(sub { $q->insert(0, 1..3); })->join(); |
80 | @x = $q->dequeue_nb(100); |
81 | is_deeply(\@x, [1..3], 'Empty queue insert'); |
82 | |
83 | $q = Thread::Queue->new(); |
84 | ok($q, 'New queue'); |
85 | threads->create(sub { $q->insert(20, 1..3); })->join(); |
86 | @x = $q->dequeue_nb(100); |
87 | is_deeply(\@x, [1..3], 'Empty queue insert'); |
88 | |
89 | $q = Thread::Queue->new(); |
90 | ok($q, 'New queue'); |
91 | threads->create(sub { $q->insert(-1, 1..3); })->join(); |
92 | @x = $q->dequeue_nb(100); |
93 | is_deeply(\@x, [1..3], 'Empty queue insert'); |
94 | |
95 | $q = Thread::Queue->new(); |
96 | ok($q, 'New queue'); |
97 | threads->create(sub { |
98 | $q->insert(2, 1..3); |
99 | $q->insert(1, 'foo'); |
100 | })->join(); |
101 | @x = $q->dequeue_nb(100); |
102 | is_deeply(\@x, [1,'foo',2,3], 'Empty queue insert'); |
103 | |
104 | # EOF |