| 1 | package Lab::Connection::Mock; | ||
| 2 | #ABSTRACT: ??? | ||
| 3 | $Lab::Connection::Mock::VERSION = '3.899'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use warnings; | ||
| 7 | use strict; | ||
| 8 | |||
| 9 | use Class::Method::Modifiers; | ||
| 10 | use YAML::XS qw/Dump LoadFile/; | ||
| 11 | use Data::Dumper; | ||
| 12 | use autodie; | ||
| 13 | use Carp; | ||
| 14 | |||
| 15 | use Lab::Connection::LogMethodCall qw/dump_method_call/; | ||
| 16 | use parent 'Lab::Connection'; | ||
| 17 | |||
| 18 | |||
| 19 | our %fields = ( | ||
| 20 | logfile => undef, | ||
| 21 | log_index => 0, | ||
| 22 | log_list => undef, | ||
| 23 | ); | ||
| 24 | |||
| 25 | around 'new' => sub { | ||
| 26 | my $orig = shift; | ||
| 27 | my $proto = shift; | ||
| 28 | my $class = ref($proto) || $proto; | ||
| 29 | my $twin = undef; | ||
| 30 | |||
| 31 | # getting fields and _permitted from parent class | ||
| 32 | my $self = $class->$orig(@_); | ||
| 33 | |||
| 34 | $self->_construct($class); | ||
| 35 | |||
| 36 | # Open the log file. | ||
| 37 | my $logfile = $self->logfile(); | ||
| 38 | if ( not defined $logfile ) { | ||
| 39 | croak 'missing "logfile" parameter in connection'; | ||
| 40 | } | ||
| 41 | |||
| 42 | my @logs = LoadFile($logfile); | ||
| 43 | $self->log_list( [@logs] ); | ||
| 44 | |||
| 45 | return $self; | ||
| 46 | }; | ||
| 47 | |||
| 48 | # If all values are scalars, we don't need stuff like Data::Compare. | ||
| 49 | sub compare_hashs { | ||
| 50 | my $a = shift; | ||
| 51 | my $b = shift; | ||
| 52 | |||
| 53 | my @keys_a = keys %{$a}; | ||
| 54 | my @keys_b = keys %{$b}; | ||
| 55 | |||
| 56 | my $len_a = @keys_a; | ||
| 57 | my $len_b = @keys_b; | ||
| 58 | |||
| 59 | # compare size | ||
| 60 | if ( $len_a != $len_b ) { | ||
| 61 | return 1; | ||
| 62 | } | ||
| 63 | for my $key (@keys_a) { | ||
| 64 | if ( ref $a->{$key} ) { | ||
| 65 | die "expected scalar"; | ||
| 66 | } | ||
| 67 | if ( not exists $b->{$key} ) { | ||
| 68 | return 1; | ||
| 69 | } | ||
| 70 | if ( $a->{$key} ne $b->{$key} ) { | ||
| 71 | return 1; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | return 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub process_call { | ||
| 78 | my $method = shift; | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | my $index = $self->log_index(); | ||
| 82 | |||
| 83 | # Hack: $self->timeout is called early in Lab::Connection::configure. | ||
| 84 | if ( not defined $self->log_list() and $method eq 'timeout' ) { | ||
| 85 | return $self->{config}->{timeout}; | ||
| 86 | } | ||
| 87 | |||
| 88 | my $received = dump_method_call( $index, $method, @_ ); | ||
| 89 | |||
| 90 | my $expected = $self->log_list()->[$index]; | ||
| 91 | |||
| 92 | my $retval = delete $expected->{retval}; | ||
| 93 | |||
| 94 | if ( compare_hashs( $received, $expected ) ) { | ||
| 95 | croak "Mock connection:\nreceived:\n", Dump($received), | ||
| 96 | "\nexpected:\n", Dump($expected); | ||
| 97 | } | ||
| 98 | |||
| 99 | $self->log_index( ++$index ); | ||
| 100 | return $retval; | ||
| 101 | } | ||
| 102 | |||
| 103 | for my $method ( | ||
| 104 | qw/Clear Write Read Query BrutalRead LongQuery BrutalQuery timeout | ||
| 105 | block_connection unblock_connection is_blocked/ | ||
| 106 | ) { | ||
| 107 | around $method => sub { | ||
| 108 | my $orig = shift; | ||
| 109 | return process_call( $method, @_ ); | ||
| 110 | }; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _setbus { | ||
| 114 | |||
| 115 | # No bus for this connection, so do nothing. | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | 1; | ||
| 119 | |||
| 120 | __END__ |