Check sequence of events involved in connect/init.
[ZOOM-Perl-moved-to-github.git] / t / 19-events.t
1 # $Id: 19-events.t,v 1.3 2006-04-12 11:02:42 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 => 19;
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($conn, Net::Z3950::ZOOM::EVENT_CONNECT);
45 assert_event($conn, Net::Z3950::ZOOM::EVENT_SEND_APDU);
46 assert_event($conn, Net::Z3950::ZOOM::EVENT_SEND_DATA);
47 assert_event($conn, Net::Z3950::ZOOM::EVENT_RECV_DATA);
48 assert_event($conn, Net::Z3950::ZOOM::EVENT_RECV_APDU);
49 assert_event($conn, Net::Z3950::ZOOM::EVENT_END);
50 assert_event($conn, 0);
51
52 ### Now we need to actually do something.
53
54
55 sub assert_event {
56     my($conn, $expected) = @_;
57
58     my $val = Net::Z3950::ZOOM::event([$conn]);
59     if ($expected == 0) {
60         ok($val == 0, "no events left");
61         return;
62     }
63
64     ok($val == 1, "call with an good connection returns its index");
65
66     my $ev = Net::Z3950::ZOOM::connection_last_event($conn);
67     ok($ev == $expected, ("event is $ev (" .
68                           Net::Z3950::ZOOM::event_str($ev) .
69                           "), expected $expected (" .
70                           Net::Z3950::ZOOM::event_str($expected) . ")"));
71 }
72