Send dropdb packets as Zebra now works.
[ZOOM-Perl-moved-to-github.git] / t / 19-events.t
1 # $Id: 19-events.t,v 1.5 2006-04-12 12:07:32 mike Exp $
2
3 # Before `make install' is performed this script should be runnable with
4 # `make test'. After `make install' it should work as `perl 19-events.t'
5
6 use strict;
7 use warnings;
8 use Test::More tests => 23;
9
10 BEGIN { use_ok('Net::Z3950::ZOOM') };
11
12 ok(Net::Z3950::ZOOM::event_str(Net::Z3950::ZOOM::EVENT_CONNECT) eq "connect",
13    "connect event properly translated");
14
15 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
16
17 my $options = Net::Z3950::ZOOM::options_create();
18 Net::Z3950::ZOOM::options_set($options, async => 1);
19
20 my $host = "indexdata.com/gils";
21 my $conn = Net::Z3950::ZOOM::connection_create($options);
22 Net::Z3950::ZOOM::connection_connect($conn, $host, 0);
23 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
24 ok($errcode == 0, "connection to '$host'");
25
26 my $val = Net::Z3950::ZOOM::event(1);
27 ok($val == -1, "non-reference argument rejected");
28
29 $val = Net::Z3950::ZOOM::event($conn);
30 ok($val == -2, "non-array reference argument rejected");
31
32 $val = Net::Z3950::ZOOM::event([]);
33 ok($val == -3, "empty array reference argument rejected");
34
35 $val = Net::Z3950::ZOOM::event([1..32767]);
36 ok($val == -4, "huge array reference argument rejected");
37
38 # Test the sequence of events that come from just creating the
39 # connection: there's the physical connect; the sending the Init
40 # request (sending the APDU results in sending the data); the
41 # receiving of the Init response (receiving the data results in
42 # receiving the APDU); then the END "event" indicating that there are
43 # no further events on the specific connection we're using; finally,
44 # event() will return 0 to indicate that there are no events pending
45 # on any of the connections we pass in.
46
47 assert_event_stream($conn, 
48                     Net::Z3950::ZOOM::EVENT_CONNECT,
49                     Net::Z3950::ZOOM::EVENT_SEND_APDU,
50                     Net::Z3950::ZOOM::EVENT_SEND_DATA,
51                     Net::Z3950::ZOOM::EVENT_RECV_DATA,
52                     Net::Z3950::ZOOM::EVENT_RECV_APDU,
53                     Net::Z3950::ZOOM::EVENT_END,
54                     0);
55
56 # Now we need to actually _do_ something, and watch the stream of
57 # resulting events: issue a piggy-back search.
58 Net::Z3950::ZOOM::connection_option_set($conn, count => 1);
59 my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, "mineral");
60 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
61 ok($errcode == 0, "search for 'mineral'");
62
63 assert_event_stream($conn,
64                     Net::Z3950::ZOOM::EVENT_SEND_APDU,
65                     Net::Z3950::ZOOM::EVENT_SEND_DATA,
66                     -(Net::Z3950::ZOOM::EVENT_RECV_DATA),
67                     Net::Z3950::ZOOM::EVENT_RECV_APDU,
68                     Net::Z3950::ZOOM::EVENT_RECV_SEARCH,
69                     Net::Z3950::ZOOM::EVENT_RECV_RECORD,
70                     Net::Z3950::ZOOM::EVENT_END,
71                     0);
72
73 # Some events, especially RECV_DATA, may randomly occur multiple
74 # times, depending on network chunking; so if an expected event's
75 # value is negated, we allow that event to occur one or more times,
76 # and treat the sequence of repeated events as a single test.
77 #
78 sub assert_event_stream {
79     my($conn, @expected) = @_;
80
81     my $previousExpected = -1;
82     my $expected = shift @expected;
83     while (defined $expected) {
84         my $val = Net::Z3950::ZOOM::event([$conn]);
85         if ($expected == 0) {
86             ok($val == 0, "no events left");
87             $expected = shift @expected;
88             next;
89         }
90
91         die "impossible" if $val != 1;
92         my $ev = Net::Z3950::ZOOM::connection_last_event($conn);
93         next if $previousExpected > 0 && $ev == $previousExpected;
94
95         if ($expected < 0) {
96             $expected = -$expected;
97             $previousExpected = $expected;
98         }
99         ok($ev == $expected, ("event is $ev (" .
100                               Net::Z3950::ZOOM::event_str($ev) .
101                               "), expected $expected (" .
102                               Net::Z3950::ZOOM::event_str($expected) . ")"));
103         $expected = shift @expected;
104     }
105 }