Markdown
[ZOOM-Perl-moved-to-github.git] / t / 19-events.t
1 # Before `make install' is performed this script should be runnable with
2 # `make test'. After `make install' it should work as `perl 19-events.t'
3
4 use strict;
5 use warnings;
6 use Test::More tests => 23;
7
8 BEGIN { use_ok('Net::Z3950::ZOOM') };
9
10 ok(Net::Z3950::ZOOM::event_str(Net::Z3950::ZOOM::EVENT_CONNECT) eq "connect",
11    "connect event properly translated");
12
13 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
14
15 my $options = Net::Z3950::ZOOM::options_create();
16 Net::Z3950::ZOOM::options_set($options, async => 1);
17
18 my $host = "z3950.indexdata.com/gils";
19 my $conn = Net::Z3950::ZOOM::connection_create($options);
20 Net::Z3950::ZOOM::connection_connect($conn, $host, 0);
21 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
22 ok($errcode == 0, "connection to '$host'");
23
24 my $val = Net::Z3950::ZOOM::event(1);
25 ok($val == -1, "non-reference argument rejected");
26
27 $val = Net::Z3950::ZOOM::event($conn);
28 ok($val == -2, "non-array reference argument rejected");
29
30 $val = Net::Z3950::ZOOM::event([]);
31 ok($val == -3, "empty array reference argument rejected");
32
33 # The old test for giant array reference can't be done now that the
34 # corresponding array internal to the glue-code is allocated
35 # dynamically.
36 ok(1, "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 }