Stop testing a target if we got to many timeouts (>= 3). See bug #3382
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Test / Record / Fetch.pm
1
2 # See the "Main" test package for documentation
3
4 package ZOOM::IRSpy::Test::Record::Fetch;
5
6 use 5.008;
7 use strict;
8 use warnings;
9
10 use ZOOM::IRSpy::Test;
11 our @ISA = qw(ZOOM::IRSpy::Test);
12
13 our $max_timeout_errors = $ZOOM::IRSpy::max_timeout_errors;
14
15 # These queries 
16 my @queries = (
17                "\@attr 1=4 mineral",
18                "\@attr 1=4 computer",
19                "\@attr 1=44 mineral", # Smithsonian doesn't support AP 4!
20                "\@attr 1=1016 water", # Connector Framework only does 1016
21                ### We can add more queries here
22                );
23
24
25 sub start {
26     my $class = shift();
27     my($conn) = @_;
28
29     # Here I want to get a use attribute from the session, which we've
30     # managed to search for in the Search/Bib1 or Search/Dan1 tests.
31     # But how?  So far we search for title: 1=4
32     $conn->irspy_search_pqf($queries[0], { queryindex => 0 }, {},
33                             ZOOM::Event::ZEND, \&completed_search,
34                             exception => \&completed_search);
35 }
36
37
38 sub completed_search {
39     my($conn, $task, $udata, $event) = @_;
40
41     my $n = $task->{rs}->size();
42     $conn->log("irspy_test", "Fetch test search (", $task->render_query(), ") ",
43                ref $event && $event->isa("ZOOM::Exception") ?
44                "failed: $event" : "found $n records (event=$event)");
45
46     # remember how often a target record hit a timeout
47     if (ref $event && $event->isa("ZOOM::Exception")) {
48         if ($event =~ /Timeout/i) {
49             $conn->record->zoom_error->{TIMEOUT}++;
50             $conn->log("irspy_test", "Increase timeout error counter to: " . 
51                 $conn->record->zoom_error->{TIMEOUT});
52         }
53     }
54
55     if ($n == 0) {
56         $task->{rs}->destroy();
57         my $qindex = $udata->{queryindex}+1;
58         my $q = $queries[$qindex];
59         return ZOOM::IRSpy::Status::TEST_SKIPPED
60             if !defined $q;
61
62         if ($conn->record->zoom_error->{TIMEOUT} >= $max_timeout_errors) {
63             $conn->log("irspy_test", "Got $max_timeout_errors timeouts, give up...");
64         } else {
65             $conn->log("irspy_test", "Trying another search ...");
66             $conn->irspy_search_pqf($queries[$qindex], { queryindex => $qindex }, {},
67                                 ZOOM::Event::ZEND, \&completed_search,
68                                 exception => \&completed_search);
69         }
70         return ZOOM::IRSpy::Status::TASK_DONE;
71     }
72
73     my @syntax = (
74                    'canmarc',
75                    'danmarc',
76                    'grs-1',
77                    'ibermarc',
78                    'intermarc',
79                    'jpmarc',
80                    'librismarc',
81                    'mab',
82                    'normarc',
83 #                   'opac',
84                    'picamarc',
85                    'rusmarc',
86                    'summary',
87                    'sutrs',
88                    'swemarc',
89                    'ukmarc',
90                    'unimarc',
91                    'usmarc',
92                    'xml'
93                 );
94     #@syntax = qw(grs-1 sutrs usmarc xml); # simplify for debugging
95     foreach my $i (0 ..$#syntax) {
96         my $syntax = $syntax[$i];
97         $conn->irspy_rs_record($task->{rs}, 0,
98                                { syntax => $syntax,
99                                  last => ($i == $#syntax) },
100                                { start => 0, count => 1,
101                                  preferredRecordSyntax => $syntax },
102                                 ZOOM::Event::ZEND, \&record,
103                                 exception => \&fetch_error);
104     }
105
106     return ZOOM::IRSpy::Status::TASK_DONE;
107 }
108
109
110 sub record {
111     my($conn, $task, $udata, $event) = @_;
112     my $syn = $udata->{'syntax'};
113     my $rs = $task->{rs};
114
115     my $record = _fetch_record($rs, 0, $syn);
116     my $ok = 0;
117     if (!$record || $record->error()) {
118         $conn->log("irspy_test", "retrieval of $syn record failed: ",
119                    defined $record ? $record->exception() :
120                                      $conn->exception());
121     } else {
122         $ok = 1;
123         my $text = $record->render();
124         $conn->log("irspy_test", "Successfully retrieved a $syn record");
125         if (0) {
126             print STDERR "Hits: ", $rs->size(), "\n";
127             print STDERR "Syntax: ", $syn, "\n";
128             print STDERR $text;
129         }
130     }
131
132     $conn->record()->store_result('record_fetch',
133                                   'syntax'   => $syn,
134                                   'ok'       => $ok);
135
136     $rs->destroy() if $udata->{last};
137     return ($udata->{last} ?
138             ZOOM::IRSpy::Status::TEST_GOOD :
139             ZOOM::IRSpy::Status::TASK_DONE);
140 }
141
142
143 sub _fetch_record {
144     my($rs, $index0, $syntax) = @_;
145
146     my $oldSyntax = $rs->option(preferredRecordSyntax => $syntax);
147     my $record = $rs->record(0);
148     $oldSyntax = "" if !defined $oldSyntax;
149     $rs->option(preferredRecordSyntax => $oldSyntax);
150
151     return $record;
152 }
153
154
155 sub __UNUSED_search_error {
156     my($conn, $task, $test_args, $exception) = @_;
157
158     $conn->log("irspy_test", "Initial search failed: ", $exception);
159     return ZOOM::IRSpy::Status::TEST_SKIPPED;
160 }
161
162
163 sub fetch_error {
164     my($conn, $task, $udata, $exception) = @_;
165     my $syn = $udata->{'syntax'};
166
167     $conn->log("irspy_test", "Retrieval of $syn record failed: ", $exception);
168     $conn->record()->store_result('record_fetch',
169                                   'syntax'       => $syn,
170                                   'ok'        => 0);
171     $task->{rs}->destroy() if $udata->{last};
172     return ZOOM::IRSpy::Status::TASK_DONE;
173 }
174
175
176 1;