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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
use strict;
use warnings;
BEGIN {
if ($ENV{'PERL_CORE'}){
chdir('t');
unshift(@INC, '../lib');
}
use Config;
if (! $Config{'useithreads'}) {
print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
exit(0);
}
}
use threads;
use Thread::Queue;
if ($] == 5.008) {
require 't/test.pl'; # Test::More work-alike for Perl 5.8.0
} else {
require Test::More;
}
Test::More->import();
plan('tests' => 16);
my $q = Thread::Queue->new(1..10);
ok($q, 'New queue');
threads->create(sub {
$q->insert(5);
$q->insert(-5);
$q->insert(100);
$q->insert(-100);
})->join();
my @x = $q->dequeue_nb(100);
is_deeply(\@x, [1..10], 'No-op inserts');
$q = Thread::Queue->new(1..10);
ok($q, 'New queue');
threads->create(sub {
$q->insert(10, qw/tail/);
$q->insert(0, qw/head/);
})->join();
@x = $q->dequeue_nb(100);
is_deeply(\@x, ['head',1..10,'tail'], 'Edge inserts');
$q = Thread::Queue->new(1..10);
ok($q, 'New queue');
threads->create(sub {
$q->insert(5, qw/foo bar/);
$q->insert(-2, qw/qux/);
})->join();
@x = $q->dequeue_nb(100);
is_deeply(\@x, [1..5,'foo','bar',6..8,'qux',9,10], 'Middle inserts');
$q = Thread::Queue->new(1..10);
ok($q, 'New queue');
threads->create(sub {
$q->insert(20, qw/tail/);
$q->insert(-20, qw/head/);
})->join();
@x = $q->dequeue_nb(100);
is_deeply(\@x, ['head',1..10,'tail'], 'Extreme inserts');
$q = Thread::Queue->new();
ok($q, 'New queue');
threads->create(sub { $q->insert(0, 1..3); })->join();
@x = $q->dequeue_nb(100);
is_deeply(\@x, [1..3], 'Empty queue insert');
$q = Thread::Queue->new();
ok($q, 'New queue');
threads->create(sub { $q->insert(20, 1..3); })->join();
@x = $q->dequeue_nb(100);
is_deeply(\@x, [1..3], 'Empty queue insert');
$q = Thread::Queue->new();
ok($q, 'New queue');
threads->create(sub { $q->insert(-1, 1..3); })->join();
@x = $q->dequeue_nb(100);
is_deeply(\@x, [1..3], 'Empty queue insert');
$q = Thread::Queue->new();
ok($q, 'New queue');
threads->create(sub {
$q->insert(2, 1..3);
$q->insert(1, 'foo');
})->join();
@x = $q->dequeue_nb(100);
is_deeply(\@x, [1,'foo',2,3], 'Empty queue insert');
# EOF
|