| 1 | package KiokuDB::Test::Fixture::Concurrency; | ||||||||||
| 2 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 2 | ▶ | use Test::More; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Test::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use List::Util qw(sum); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Scope::Guard; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use POSIX qw(_exit :sys_wait_h); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | ▶ | use namespace::clean -except => 'meta'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 2 | with qw(KiokuDB::Test::Fixture) => { excludes => [qw/run required_backend_roles/] }; | |||||||||
| 15 | |||||||||||
| 16 | 2 | ▶ | use constant required_backend_roles => qw(Clear TXN Concurrency::POSIX); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 2 | ▶ | use constant FORKS => 20; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 2 | ▶ | use constant COUNTERS => 250; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 2 | ▶ | use constant ACCOUNTS => 250; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use constant ITER => 10; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 2 | my @ids = qw(foo bar gorch baz); | |||||||||
| 24 | |||||||||||
| 25 | { | ||||||||||
| 26 | 2 | package Foo; | |||||||||
| 27 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 2 | has bar => ( is => 'rw' ); | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 2 | has exit => ( | |||||||||
| 33 | isa => "Int", | ||||||||||
| 34 | is => "rw", | ||||||||||
| 35 | default => 0, | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | before precheck => sub { | ||||||||||
| 39 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 40 | |||||||||||
| 41 | 2 | }; | |||||||||
| 42 | |||||||||||
| 43 | sub create { | ||||||||||
| 44 | return ( | ||||||||||
| 45 | counter => { value => 0 }, | ||||||||||
| 46 | 0 | (map { ( "counter_$_" => { value => 0 } ) } 1 .. COUNTERS ), | |||||||||
| 47 | 0 | ▶ | (map { ( "${_}_account" => { value => 0 } ) } 1 .. ACCOUNTS), | ||||||||
|
Subroutine
create: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 48 | ); | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | sub run { | ||||||||||
| 52 | 33 | ▶ | my $self = shift; | ||||||||
|
Subroutine
run: called CC=2 SCAR=12.3
Pod
undocumented
| |||||||||||
| 53 | |||||||||||
| 54 | SKIP: { | ||||||||||
| 55 | 33 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||||||||
| 56 | |||||||||||
| 57 | 33 | ▶ | skip "Set KIOKUDB_STRESS_TEST to run this fixture", 1 unless $ENV{KIOKUDB_STRESS_TEST}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | 0 | $self->precheck; | |||||||||
| 60 | |||||||||||
| 61 | lives_ok { | ||||||||||
| 62 | 0 | ▶ | local $Test::Builder::Level = $Test::Builder::Level - 1; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 63 | $self->txn_do(sub { | ||||||||||
| 64 | 0 | ▶ | my $s = $self->new_scope; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 65 | 0 | $self->backend->clear; | |||||||||
| 66 | 0 | $self->populate; | |||||||||
| 67 | 0 | }); | |||||||||
| 68 | 0 | } "populated OK"; | |||||||||
| 69 | |||||||||||
| 70 | 0 | $self->clear_directory; | |||||||||
| 71 | |||||||||||
| 72 | 0 | $self->verify; | |||||||||
| 73 | |||||||||||
| 74 | 0 | is_deeply( [ $self->live_objects ], [ ], "no live objects at end of " . $self->name . " fixture" ); | |||||||||
| 75 | |||||||||||
| 76 | 0 | $self->clear_live_objects; | |||||||||
| 77 | } | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | sub verify { | ||||||||||
| 81 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
verify: not called CC=7 SCAR=37.5
Pod
undocumented
| |||||||||||
| 82 | |||||||||||
| 83 | 0 | ok( !$self->has_directory, "no directory object" ); | |||||||||
| 84 | |||||||||||
| 85 | # force re-instantiation of directory | ||||||||||
| 86 | 0 | $self->clear_directory; | |||||||||
| 87 | |||||||||||
| 88 | 0 | foreach my $num ( 1 .. FORKS ) { | |||||||||
| 89 | 0 | ▶ | defined(my $pid = fork) or die $!; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | ▶ | next if $pid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | |||||||||||
| 92 | my $guard = Scope::Guard->new(sub { | ||||||||||
| 93 | # avoid cleanups on errors | ||||||||||
| 94 | 2 | ▶ | use POSIX qw(_exit); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 95 | 0 | ▶ | _exit($self->exit); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 96 | 0 | }); | |||||||||
| 97 | |||||||||||
| 98 | # make sure each child gets a different random seed | ||||||||||
| 99 | 0 | srand($$ ^ time); | |||||||||
| 100 | |||||||||||
| 101 | 0 | $self->run_child($num); | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 0 | my $skip = 0; | |||||||||
| 105 | |||||||||||
| 106 | 0 | ▶ | while ( wait > 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 0 | ▶ | do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 0 | ▶ | $skip++ if $? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | } while waitpid(-1, WNOHANG) > 0; | ||||||||||
| 110 | |||||||||||
| 111 | 0 | $self->check_consistency; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | $self->check_counters($skip); | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | sub check_consistency { | ||||||||||
| 118 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
check_consistency: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 119 | |||||||||||
| 120 | 0 | my $ok; | |||||||||
| 121 | |||||||||||
| 122 | 0 | my ( $counter, @accounts ); | |||||||||
| 123 | |||||||||||
| 124 | 0 | attempt: foreach my $attempt ( 1 .. FORKS ) { | |||||||||
| 125 | last attempt if try { | ||||||||||
| 126 | $self->txn_do(sub { | ||||||||||
| 127 | 0 | ▶ | my $s = $self->new_scope; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 128 | |||||||||||
| 129 | 0 | $counter = $self->lookup("counter")->{value}; | |||||||||
| 130 | |||||||||||
| 131 | 0 | @accounts = map { $_->{value} } $self->lookup(map { "${_}_account" } 1 .. ACCOUNTS); | |||||||||
| 132 | 0 | ▶ | }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 133 | |||||||||||
| 134 | 0 | ++$ok; | |||||||||
| 135 | 0 | ▶ | }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | |||||||||||
| 139 | SKIP: { | ||||||||||
| 140 | 0 | ▶ | skip "lock contention", 3 unless $ok; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | |||||||||||
| 142 | 0 | cmp_ok( $counter, '>=', 0, "counter not 0" ); | |||||||||
| 143 | 0 | cmp_ok( $counter, '<=', FORKS, "counter <= counters" ); | |||||||||
| 144 | |||||||||||
| 145 | 0 | is( sum(@accounts), 0, "account sum is 0 (state is consistent)" ); | |||||||||
| 146 | }; | ||||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | sub check_counters { | ||||||||||
| 150 | 0 | ▶ | my ( $self, $skip ) = @_; | ||||||||
|
Subroutine
check_counters: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 151 | |||||||||||
| 152 | $self->txn_do(sub { | ||||||||||
| 153 | 0 | ▶ | my $s = $self->new_scope; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 154 | |||||||||||
| 155 | 0 | my $counter = $self->lookup_ok("counter"); | |||||||||
| 156 | 0 | is( $counter->{value}, FORKS-$skip, "total counter value" ); | |||||||||
| 157 | |||||||||||
| 158 | 0 | my @counters = $self->lookup_ok(map { "counter_$_" } 1 .. COUNTERS); | |||||||||
| 159 | |||||||||||
| 160 | 0 | is( sum(map { $_->{value} } @counters), FORKS-$skip, "counters sum" ); | |||||||||
| 161 | 0 | }); | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | sub run_child { | ||||||||||
| 165 | 0 | ▶ | my ( $self, $child ) = @_; | ||||||||
|
Subroutine
run_child: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 166 | |||||||||||
| 167 | 0 | for ( 1 .. ITER ) { | |||||||||
| 168 | try { | ||||||||||
| 169 | $self->txn_do(sub { | ||||||||||
| 170 | 0 | ▶ | my $s = $self->new_scope; | ||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||
| 171 | |||||||||||
| 172 | 0 | my $id = @ids[int rand @ids]; | |||||||||
| 173 | |||||||||||
| 174 | 0 | ▶ | if ( my $foo = $self->lookup($id) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 175 | 0 | ▶ | if ( rand > 0.5 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | 0 | $foo->bar("foo"); | |||||||||
| 177 | 0 | $self->update($foo); | |||||||||
| 178 | } else { | ||||||||||
| 179 | 0 | $self->delete($foo); | |||||||||
| 180 | } | ||||||||||
| 181 | } else { | ||||||||||
| 182 | 0 | $self->insert( foo => Foo->new( bar => "bar" ) ); | |||||||||
| 183 | } | ||||||||||
| 184 | |||||||||||
| 185 | 0 | my ( $one, $two ) = $self->lookup( map { int(rand ACCOUNTS) . "_account" } 1 .. 2 ); | |||||||||
| 186 | |||||||||||
| 187 | 0 | my $amount = int(rand 10000); | |||||||||
| 188 | 0 | $one->{value} += $amount; | |||||||||
| 189 | 0 | $two->{value} -= $amount; | |||||||||
| 190 | |||||||||||
| 191 | 0 | ▶ | select(undef,undef,undef,0.01) if rand > 0.5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 192 | |||||||||||
| 193 | 0 | $self->update($one, $two); | |||||||||
| 194 | 0 | ▶ | }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 195 | 0 | }; | |||||||||
| 196 | |||||||||||
| 197 | 0 | select(undef,undef,undef, 0.01); | |||||||||
| 198 | } | ||||||||||
| 199 | |||||||||||
| 200 | 0 | my $ok; | |||||||||
| 201 | |||||||||||
| 202 | 0 | attempt: foreach my $attempt ( 1 .. FORKS*2 ) { | |||||||||
| 203 | last attempt if try { | ||||||||||
| 204 | $self->txn_do(sub { | ||||||||||
| 205 | 0 | ▶ | my $s = $self->new_scope; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 206 | |||||||||||
| 207 | 0 | my $counter = $self->lookup("counter"); | |||||||||
| 208 | |||||||||||
| 209 | 0 | my $counter_two = $self->lookup("counter_" . int rand COUNTERS); | |||||||||
| 210 | |||||||||||
| 211 | 0 | ▶ | select(undef,undef,undef,0.02 * rand) if rand > 0.5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 212 | |||||||||||
| 213 | 0 | $counter_two->{value}++; | |||||||||
| 214 | 0 | $self->update($counter_two); | |||||||||
| 215 | |||||||||||
| 216 | 0 | $counter->{value}++; | |||||||||
| 217 | 0 | $self->update($counter); | |||||||||
| 218 | 0 | ▶ | }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 219 | |||||||||||
| 220 | 0 | ++$ok; | |||||||||
| 221 | 0 | ▶ | }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 222 | |||||||||||
| 223 | 0 | select(undef,undef,undef, 0.05); | |||||||||
| 224 | } | ||||||||||
| 225 | |||||||||||
| 226 | 0 | ▶ | $self->exit(1) unless $ok; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 227 | } | ||||||||||
| 228 | |||||||||||
| 229 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||
| 230 | |||||||||||
| 231 | 2 | __PACKAGE__ | |||||||||
| 232 | |||||||||||
| 233 | __END__ | ||||||||||