Fix handling of piggybacking errors not to break on success!
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Test / Record / PiggyBack.pm
1 # See the "Main" test package for documentation
2
3 ### Too much common code with OPAC.pm: need to refactor
4
5 package ZOOM::IRSpy::Test::Record::PiggyBack;
6
7 use 5.008;
8 use strict;
9 use warnings;
10
11 use ZOOM::IRSpy::Test;
12 our @ISA = qw(ZOOM::IRSpy::Test);
13
14 my @queries = (
15                "\@attr 1=4 mineral",
16                "\@attr 1=4 computer",
17                "\@attr 1=44 mineral", # Smithsonian doesn't support AP 4!
18                "\@attr 1=1016 water", # Connector Framework only does 1016
19                ### We can add more queries here
20                );
21
22 # We'd like to use this temporary-options hash to set
23 # preferredRecordSyntax, as well  But that doesn't work because the
24 # same value needs to be in force later on when we make the
25 # record_immediate() call, otherwise it misses its cache.
26 my %options = (
27     piggyback => 1,
28     count => 3,
29 #    preferredRecordSyntax => "usmarc"
30     );
31
32 sub start {
33     my $class = shift();
34     my($conn) = @_;
35
36     ### It would be better to consult previous tests to find a working RS
37     $conn->option(preferredRecordSyntax => "usmarc");
38     $conn->irspy_search_pqf($queries[0], { queryindex => 0 }, \%options,
39                             ZOOM::Event::ZEND, \&completed_search,
40                             exception => \&completed_search);
41 }
42
43
44 sub completed_search {
45     my($conn, $task, $udata, $event) = @_;
46
47     # $event can be a ZOOM::Event::* number or a ZOOM::Exception object
48     if (ref $event &&
49         $event->isa("ZOOM::Exception") &&
50         $event->code() == 1005) {
51         $conn->log("irspy_test", "Piggyback searching not supported");  
52         $conn->record()->store_result('piggyback', 'ok' => 0);
53         return ZOOM::IRSpy::Status::TEST_BAD;
54     }
55
56     my $n = $task->{rs}->size();
57     $conn->log("irspy_test", "Piggyback test search (", $task->render_query(), ") ",
58                ref $event && $event->isa("ZOOM::Exception") ?
59                "failed: $event" : "found $n records (event=$event)");
60
61     # remember how often a target record hit a timeout
62     if (ref $event && $event->isa("ZOOM::Exception")) {
63         if ($event =~ /Timeout/i) {
64             $conn->record->zoom_error->{TIMEOUT}++;
65             $conn->log("irspy_test", "Increase timeout error counter to: " . 
66                 $conn->record->zoom_error->{TIMEOUT});
67         }
68     }
69
70     if ($n < 3) {
71         $task->{rs}->destroy();
72         my $qindex = $udata->{queryindex}+1;
73         my $q = $queries[$qindex];
74         return ZOOM::IRSpy::Status::TEST_SKIPPED
75             if !defined $q || $conn->record->zoom_error->{TIMEOUT} >= $ZOOM::IRSpy::max_timeout_errors;
76
77         $conn->log("irspy_test", "Trying another search ...");
78         $conn->irspy_search_pqf($queries[$qindex], { queryindex => $qindex }, \%options,
79                                 ZOOM::Event::ZEND, \&completed_search,
80                                 exception => \&completed_search);
81         return ZOOM::IRSpy::Status::TASK_DONE;
82     }
83
84     # We have a result-set of three of more records, and we requested
85     # that those records be included in the Search Response using
86     # piggybacking.  Was it done?
87     my $rec = $task->{rs}->record_immediate(2);
88     my $ok = defined $rec && $rec->error() == 0;
89
90     $task->{rs}->destroy();
91     $conn->record()->store_result('piggyback', 'ok' => $ok);
92     return $ok ? ZOOM::IRSpy::Status::TEST_GOOD : ZOOM::IRSpy::Status::TEST_BAD;
93 }
94
95
96 1;