Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Tests for Lock.
  • Loading branch information
jnthn committed Oct 31, 2013
1 parent 5051341 commit 1f4fd4a
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions S17-concurrency/lock.t
@@ -0,0 +1,48 @@
use v6;
use Test;

plan 6;

{
my $l = Lock.new;
$l.run({
pass "Running code under lock";
});
$l.run({
pass "Running another piece of code under lock";
});
}

{
my $l = Lock.new;
dies_ok { $l.run({ die "oops" }) }, "code that dies under lock throws";
$l.run({
pass "Code that dies in run does release the lock";
});
Thread.run({
$l.run({ pass "Even from another thread"; });
}).join();
}

{
# Attempt to check lock actually enforces some locking.
my $output = '';
my $l = Lock.new;
my $t1 = Thread.run({
$l.run({
for 1..10000 {
$output ~= 'a'
}
});
});
my $t2 = Thread.run({
$l.run({
for 1..10000 {
$output ~= 'b'
}
});
});
$t1.join;
$t2.join;
ok $output ~~ /^ [ a+: b+: | b+: a+: ] $/, 'Lock is at least somewhat effective';
}

0 comments on commit 1f4fd4a

Please sign in to comment.