Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 50 lines (33 sloc) 1.26 kB
2052e29 first version of spec tests for S17
buchetc authored
1 use v6-alpha;
2 use Test;
3
7aa8072 use contend to avoid race condition
buchetc authored
4 plan 10;
2052e29 first version of spec tests for S17
buchetc authored
5
7aa8072 use contend to avoid race condition
buchetc authored
6 # L<S17/Threads>
2052e29 first version of spec tests for S17
buchetc authored
7 # try to stop duration of a simple async call
8 my $timestamp = time;
9
10 async {
11 ok 1, 'async call started';
12 };
13
14 my $async_duration = time - $timestamp;
15
16 # now if the follwing call is really asynchron, next time stop should
17 # be smaller than C<$async_duration + .5>
18
19 $timestamp = time;
20 my $thr = async {
7aa8072 use contend to avoid race condition
buchetc authored
21 sleep .1;
2052e29 first version of spec tests for S17
buchetc authored
22 };
23
fdcdbcf [t] testcase derived from S17
buchetc authored
24 sleep .5; # avoid Parse errors: Tests out of sequence. Found (2) but expected (1)
25
26 ok time - $timestamp < $async_duration + .5, "yes, 'Im out of sync!";
2052e29 first version of spec tests for S17
buchetc authored
27
1a02f20 [async.t] numer-/stringify more perl6ish
buchetc authored
28 ok ~$thr, 'stringify a thread';
2052e29 first version of spec tests for S17
buchetc authored
29
1a02f20 [async.t] numer-/stringify more perl6ish
buchetc authored
30 ok +$thr, 'numerify a thread should be the thread id';
2052e29 first version of spec tests for S17
buchetc authored
31
1a02f20 [async.t] numer-/stringify more perl6ish
buchetc authored
32 isnt +$thr, $*PID, 'childs id is not parents thread id';
2052e29 first version of spec tests for S17
buchetc authored
33
7aa8072 use contend to avoid race condition
buchetc authored
34 ok $thr.join, 'thread now joined and back home';
2052e29 first version of spec tests for S17
buchetc authored
35
7aa8072 use contend to avoid race condition
buchetc authored
36 # two async calls should do something important
2052e29 first version of spec tests for S17
buchetc authored
37 sub do_something_very_important {
38 return 1;
39 }
40
7aa8072 use contend to avoid race condition
buchetc authored
41 my @threads;
42 @threads[0] = async { ok do_something_very_important(),'very important things from first thread' };
fdcdbcf [t] testcase derived from S17
buchetc authored
43 sleep .5; # avoid Parse errors: Tests out of sequence. Found (9) but expected (8)
7aa8072 use contend to avoid race condition
buchetc authored
44 @threads[1] = async { ok do_something_very_important(),'very important things from second thread' };
2052e29 first version of spec tests for S17
buchetc authored
45
46
7aa8072 use contend to avoid race condition
buchetc authored
47 ok @threads[0].join,'first thread joined';
48 ok @threads[1].join,'second thread joined';
49 # race condition test moved to L<content.t>
Something went wrong with that request. Please try again.