-
Notifications
You must be signed in to change notification settings - Fork 3
/
100-rw.t
144 lines (111 loc) · 3.14 KB
/
100-rw.t
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use v6;
use Test;
use Semaphore::ReadersWriters;
my Bool $debug = False;
#-------------------------------------------------------------------------------
subtest {
my Semaphore::ReadersWriters $rw .= new;
$rw.debug = $debug;
my $shared-var = 10;
isa-ok $rw, 'Semaphore::ReadersWriters';
$rw.add-mutex-names(<shv def>);
cmp-ok 'shv', '~~', any($rw.get-mutex-names), 'shv key set';
cmp-ok 'def', '~~', any($rw.get-mutex-names), 'def key set';
ok $rw.check-mutex-names('def'), 'Key def found';
ok !$rw.check-mutex-names('xyz'), 'Key xyz not found';
ok $rw.check-mutex-names(<def xyz>), 'any of def or xyz is found';
$rw.rm-mutex-names('def');
cmp-ok 'def', '!~~', any($rw.get-mutex-names), 'def key removed';
$rw.writer( 'def', {$shared-var += 2});
CATCH {
when X::AdHoc {
cmp-ok .message,
'~~',
/:s mutex name \'def\' does not exist/,
.message;
}
}
}, 'basic tests';
#-------------------------------------------------------------------------------
subtest {
my Semaphore::ReadersWriters $rw .= new;
$rw.debug = $debug;
$rw.add-mutex-names('shv 2');
my $shared-var = 10;
my @p;
for ^10 {
my $i = $_;
@p.push: Promise.start( {
#say "$*THREAD.id() Try reading $i" if $debug;
$rw.reader( 'shv 2', { sleep((rand * 2).Int); $shared-var;});
}
);
}
pass "Result {.result}" for @p;
pass "All reader threads have ended, no hangups";
}, 'only readers';
#-------------------------------------------------------------------------------
subtest {
my Semaphore::ReadersWriters $rw .= new;
$rw.debug = $debug;
try {
$rw.add-mutex-names('shv');
CATCH {
default {
ok .message ~~ m:s/Key \'shv\' already in use/, .message;
}
}
}
my $shared-var = 10;
my @p;
for ^10 {
my $i = $_;
@p.push: Promise.start( {
#say "$*THREAD.id() Try writing $i" if $debug;
$rw.writer( 'shv', { sleep((rand * 2).Int); ++$shared-var;});
}
);
}
pass "Result {.result}" for @p;
pass "All writers threads have ended, no hangups";
}, 'only writers';
#-------------------------------------------------------------------------------
subtest {
my Semaphore::ReadersWriters $rw .= new;
#$debug = True;
$rw.debug = $debug;
$rw.add-mutex-names('shv 3');
my $shared-var = 10;
my @p;
for (^10).pick(30) {
my $i = $_;
@p.push: Promise.start( {
my $r;
# Only when $i <= 2 then thread becomes a writer.
# All others become readers
if $i <= 2 {
#say "$*THREAD.id() Try writing $i" if $debug;
$r = $rw.writer( 'shv 3', {$shared-var += $i});
}
else {
#say "$*THREAD.id() Try reading $i" if $debug;
$r = $rw.reader( 'shv 3', {$shared-var});
}
CATCH {
default {
.say;
$r = -1;
}
}
# $i.fmt('%03d') ~ ', ' ~ $r.fmt('%04d');
# $r.fmt('%04d');
$r;
}
);
}
pass "Result {.result} " for @p;
# .result for @p;
pass "All threads have ended, no hangups";
}, 'readers and writers';
#-------------------------------------------------------------------------------
done-testing;