e104f3d3edf9fa9d5364dd3271b4102f06355fa6
[ZOOM-Perl-moved-to-github.git] / t / 19-events.t
1 # $Id: 19-events.t,v 1.8 2007-02-22 20:38:11 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 = "z3950.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 # The old test for giant array reference can't be done now that the
36 # corresponding array internal to the glue-code is allocated
37 # dynamically.
38 ok(1, "huge array reference argument rejected");
39
40 # Test the sequence of events that come from just creating the
41 # connection: there's the physical connect; the sending the Init
42 # request (sending the APDU results in sending the data); the
43 # receiving of the Init response (receiving the data results in
44 # receiving the APDU); then the END "event" indicating that there are
45 # no further events on the specific connection we're using; finally,
46 # event() will return 0 to indicate that there are no events pending
47 # on any of the connections we pass in.
48
49 assert_event_stream($conn, 
50                     Net::Z3950::ZOOM::EVENT_CONNECT,
51                     Net::Z3950::ZOOM::EVENT_SEND_APDU,
52                     Net::Z3950::ZOOM::EVENT_SEND_DATA,
53                     Net::Z3950::ZOOM::EVENT_RECV_DATA,
54                     Net::Z3950::ZOOM::EVENT_RECV_APDU,
55                     Net::Z3950::ZOOM::EVENT_END,
56                     0);
57
58 # Now we need to actually _do_ something, and watch the stream of
59 # resulting events: issue a piggy-back search.
60 Net::Z3950::ZOOM::connection_option_set($conn, count => 1);
61 my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, "mineral");
62 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
63 ok($errcode == 0, "search for 'mineral'");
64
65 assert_event_stream($conn,
66                     Net::Z3950::ZOOM::EVENT_SEND_APDU,
67                     Net::Z3950::ZOOM::EVENT_SEND_DATA,
68                     -(Net::Z3950::ZOOM::EVENT_RECV_DATA),
69                     Net::Z3950::ZOOM::EVENT_RECV_APDU,
70                     Net::Z3950::ZOOM::EVENT_RECV_SEARCH,
71                     Net::Z3950::ZOOM::EVENT_RECV_RECORD,
72                     Net::Z3950::ZOOM::EVENT_END,
73                     0);
74
75 # Some events, especially RECV_DATA, may randomly occur multiple
76 # times, depending on network chunking; so if an expected event's
77 # value is negated, we allow that event to occur one or more times,
78 # and treat the sequence of repeated events as a single test.
79 #
80 sub assert_event_stream {
81     my($conn, @expected) = @_;
82
83     my $previousExpected = -1;
84     my $expected = shift @expected;
85     while (defined $expected) {
86         my $val = Net::Z3950::ZOOM::event([$conn]);
87         if ($expected == 0) {
88             ok($val == 0, "no events left");
89             $expected = shift @expected;
90             next;
91         }
92
93         die "impossible" if $val != 1;
94         my $ev = Net::Z3950::ZOOM::connection_last_event($conn);
95         next if $previousExpected > 0 && $ev == $previousExpected;
96
97         if ($expected < 0) {
98             $expected = -$expected;
99             $previousExpected = $expected;
100         }
101         ok($ev == $expected, ("event is $ev (" .
102                               Net::Z3950::ZOOM::event_str($ev) .
103                               "), expected $expected (" .
104                               Net::Z3950::ZOOM::event_str($expected) . ")"));
105         $expected = shift @expected;
106     }
107 }