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