Upgrade to Attribute::Handlers 0.87 (which is just a core sync) -- for real
[p5sagit/p5-mst-13.2.git] / ext / Thread-Queue / t / 07_lock.t
CommitLineData
54c7876f 1use strict;
2use warnings;
3
4BEGIN {
5 if ($ENV{'PERL_CORE'}){
6 chdir('t');
7 unshift(@INC, '../lib');
8 }
9 use Config;
10 if (! $Config{'useithreads'}) {
3d4f2f89 11 print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
54c7876f 12 exit(0);
13 }
14}
15
16use threads;
17use Thread::Queue;
18use Thread::Semaphore;
19
20if ($] == 5.008) {
21 require 't/test.pl'; # Test::More work-alike for Perl 5.8.0
22} else {
23 require Test::More;
24}
25Test::More->import();
26plan('tests' => 3);
27
28# The following tests locking a queue
29
30my $q = Thread::Queue->new(1..10);
31ok($q, 'New queue');
32
33my $sm = Thread::Semaphore->new(0);
34my $st = Thread::Semaphore->new(0);
35
36threads->create(sub {
37 {
38 lock($q);
39 $sm->up();
40 $st->down();
41 threads::yield();
42 select(undef, undef, undef, 0.1);
43 my @x = $q->extract(5,2);
44 is_deeply(\@x, [6,7], 'Thread dequeues under lock');
45 }
46})->detach();
47
48$sm->down();
49$st->up();
50my @x = $q->dequeue_nb(100);
51is_deeply(\@x, [1..5,8..10], 'Main dequeues');
52threads::yield();
53
3d4f2f89 54exit(0);
55
54c7876f 56# EOF