File Coverage

blib/lib/Prophet/Test/Participant.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Prophet::Test::Participant;
2 6     6   31 use Any::Moose;
  6         10  
  6         34  
3 6     6   6009 use Prophet::Test;
  0            
  0            
4             use Test::Exception;
5              
6             has name => (
7             is => 'rw',
8             isa => 'Str',
9             );
10              
11             has arena => (
12             is => 'rw',
13             isa => 'Prophet::Test::Arena',
14             weak_ref => 1,
15             );
16              
17             sub BUILD {
18             my $self = shift;
19             as_user( $self->name, sub { call_func( [qw(init)] ) } );
20             as_user( $self->name, sub { call_func_ok( [qw(search --type Bug --regex .)] ) } );
21              
22             }
23              
24             use List::Util qw(shuffle);
25              
26             my @CHICKEN_DO
27             = qw(create_record create_record delete_record update_record update_record update_record update_record update_record sync_from_peer sync_from_peer noop);
28              
29             sub take_one_step {
30             my $self = shift;
31             my $action = shift || ( shuffle(@CHICKEN_DO) )[0];
32             my $args = shift;
33             @_ = ( $self, $args );
34             goto $self->can($action);
35             }
36              
37             sub _random_props {
38             my @prop_values = qw(A B C D E);
39             my @prop_keys = qw(1 2 3 4 5);
40              
41             return ( map { "--" . $prop_keys[$_] => $prop_values[$_] } ( 0 .. 4 ) );
42              
43             }
44              
45             sub _permute_props {
46             my %props = (@_);
47             @props{ keys %props } = shuffle( values %props );
48              
49             for ( keys %props ) {
50             if ( int( rand(10) < 2 ) ) {
51             delete $props{$_};
52             }
53             }
54              
55             if ( int( rand(10) < 3 ) ) {
56             $props{int(rand(5))+1 } = chr(rand(5)+65);
57             }
58              
59             return %props;
60             }
61              
62             sub noop {
63             my $self = shift;
64             ok( 1, $self->name . ' - NOOP' );
65             }
66              
67             sub delete_record {
68             my $self = shift;
69             my $args = shift;
70             $args->{record} ||= get_random_local_record();
71              
72             return undef unless ( $args->{record} );
73             $self->record_action( 'delete_record', $args );
74             call_func_ok( [ qw(delete --type Scratch --uuid), $args->{record} ] );
75              
76             }
77              
78             sub create_record {
79             my $self = shift;
80             my $args = shift;
81             @{ $args->{props} } = _random_props() unless $args->{props};
82              
83             my ( $ret, $out, $err ) = call_func_ok( [ qw(create --type Scratch --), @{ $args->{props} } ] );
84              
85             # ok($ret, $self->name . " created a record");
86             if ( $out =~ /Created\s+(.*?)\s+(\d+)\s+\((.*)\)/i ) {
87             $args->{result} = $3;
88             }
89             $self->record_action( 'create_record', $args );
90             }
91              
92             sub update_record {
93             my $self = shift;
94             my $args = shift;
95              
96             $args->{record} ||= get_random_local_record();
97             return undef unless ( $args->{'record'} );
98              
99             my ( $ok, $stdout, $stderr ) = call_func( [ qw(show --type Scratch --uuid), $args->{record} ] );
100              
101             my %props = map { split( /: /, $_, 2 ) } split( /\n/, $stdout );
102             delete $props{id};
103              
104             %{ $args->{props} } = _permute_props(%props) unless $args->{props};
105             %props = %{ $args->{props} };
106              
107             call_func_ok( [ qw(update --type Scratch --uuid), $args->{record}, '--', map { '--' . $_ => $props{$_} } keys %props ],
108             $self->name . " updated a record" );
109              
110             $self->record_action( 'update_record', $args );
111              
112             }
113              
114             sub sync_from_peer {
115             my $self = shift;
116             my $args = shift;
117              
118             my $from = $args->{from} ||= ( shuffle( grep { $_->name ne $self->name } $self->arena->chickens ) )[0]->name;
119              
120             $self->record_action( 'sync_from_peer', $args );
121              
122             @_ = (
123             [ 'merge', '--prefer', 'to', '--from', repo_uri_for($from), '--to', repo_uri_for( $self->name ), '--force' ],
124             $self->name . " sync from " . $from . " ran ok!"
125             );
126             goto \&call_func_ok;
127              
128             }
129              
130             sub get_random_local_record {
131             my ( $ok, $stdout, $stderr ) = call_func( [qw(search --type Scratch --regex .)] );
132             my $update_record = ( shuffle( map { $_ =~ /'uuid': '(\S*?)'/ } split( /\n/, $stdout ) ) )[0];
133             return $update_record;
134             }
135              
136             sub dump_state {
137             my $self = shift;
138             my $cli = Prophet::CLI->new();
139              
140             my $state;
141              
142             my $records = Prophet::Collection->new( handle => $cli->handle, type => 'Scratch' );
143             my $merges = Prophet::Collection->new( handle => $cli->handle, type => $Prophet::Replica::MERGETICKET_METATYPE );
144             my $resolutions = Prophet::Collection->new( handle => $cli->app_handle->handle->resolution_db_handle, type => '_prophet_resolution' );
145              
146             $records->matching( sub {1} );
147             $resolutions->matching( sub {1} );
148             $merges->matching( sub {1} );
149              
150             %{ $state->{records} } = map { $_->uuid => $_->get_props } $records->items;
151             %{ $state->{merges} } = map { $_->uuid => $_->get_props } $merges->items;
152             %{ $state->{resolutions} } = map { $_->uuid => $_->get_props } $resolutions->items;
153              
154             return $state;
155              
156             }
157              
158             sub dump_history { }
159              
160             sub record_action {
161             my ( $self, $action, @arg ) = @_;
162             $self->arena->record( $self->name, $action, @arg );
163             }
164              
165             use Test::Exception;
166              
167             sub call_func_ok {
168             my @args = @_;
169             my @ret;
170             lives_and {
171             @ret = call_func(@args);
172             diag("As ".$ENV{'PROPHET_EMAIL'}. " ".join(' ',@{$args[0]}));
173             ok( 1, join( " ", $ENV{'PROPHET_EMAIL'}, @{ $args[0] } ) );
174             };
175             return @ret;
176             }
177              
178             sub call_func {
179             Carp::cluck unless ref $_[0];
180              
181             my @args = @{ shift @_ };
182             my $cli = Prophet::CLI->new();
183              
184             my $str = '';
185             open my $str_fh, '>', \$str;
186              
187             my $old_fh = select($str_fh);
188              
189             my $ret;
190             if (my $p = SVN::Pool->can('new_default')) {
191             $p->('SVN::Pool');
192             };
193              
194             $ret = $cli->run_one_command(@args);
195             select($old_fh) if defined $old_fh;
196              
197             return ( $ret, $str, undef );
198             }
199              
200             __PACKAGE__->meta->make_immutable;
201             no Any::Moose;
202              
203             1;