X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=t%2F19-events.t;h=3c286b23c96bd4b5eaf58f4dbd7a5a6bbbf9506c;hb=87c72eaf97a3dbf51a93dab782c2909539addc48;hp=36b430ffb367676958c88162aca03befc7c1402d;hpb=19d483e57a65cadc4c1d4f7f284636c07f78ea26;p=ZOOM-Perl-moved-to-github.git diff --git a/t/19-events.t b/t/19-events.t index 36b430f..3c286b2 100644 --- a/t/19-events.t +++ b/t/19-events.t @@ -1,20 +1,21 @@ -# $Id: 19-events.t,v 1.3 2006-04-12 11:02:42 mike Exp $ - # Before `make install' is performed this script should be runnable with # `make test'. After `make install' it should work as `perl 19-events.t' use strict; use warnings; -use Test::More tests => 19; +use Test::More tests => 23; BEGIN { use_ok('Net::Z3950::ZOOM') }; +ok(Net::Z3950::ZOOM::event_str(Net::Z3950::ZOOM::EVENT_CONNECT) eq "connect", + "connect event properly translated"); + my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy"); my $options = Net::Z3950::ZOOM::options_create(); Net::Z3950::ZOOM::options_set($options, async => 1); -my $host = "indexdata.com/gils"; +my $host = "z3950.indexdata.com/gils"; my $conn = Net::Z3950::ZOOM::connection_create($options); Net::Z3950::ZOOM::connection_connect($conn, $host, 0); $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo); @@ -29,8 +30,10 @@ ok($val == -2, "non-array reference argument rejected"); $val = Net::Z3950::ZOOM::event([]); ok($val == -3, "empty array reference argument rejected"); -$val = Net::Z3950::ZOOM::event([1..32767]); -ok($val == -4, "huge array reference argument rejected"); +# The old test for giant array reference can't be done now that the +# corresponding array internal to the glue-code is allocated +# dynamically. +ok(1, "huge array reference argument rejected"); # Test the sequence of events that come from just creating the # connection: there's the physical connect; the sending the Init @@ -41,32 +44,62 @@ ok($val == -4, "huge array reference argument rejected"); # event() will return 0 to indicate that there are no events pending # on any of the connections we pass in. -assert_event($conn, Net::Z3950::ZOOM::EVENT_CONNECT); -assert_event($conn, Net::Z3950::ZOOM::EVENT_SEND_APDU); -assert_event($conn, Net::Z3950::ZOOM::EVENT_SEND_DATA); -assert_event($conn, Net::Z3950::ZOOM::EVENT_RECV_DATA); -assert_event($conn, Net::Z3950::ZOOM::EVENT_RECV_APDU); -assert_event($conn, Net::Z3950::ZOOM::EVENT_END); -assert_event($conn, 0); - -### Now we need to actually do something. - - -sub assert_event { - my($conn, $expected) = @_; - - my $val = Net::Z3950::ZOOM::event([$conn]); - if ($expected == 0) { - ok($val == 0, "no events left"); - return; +assert_event_stream($conn, + Net::Z3950::ZOOM::EVENT_CONNECT, + Net::Z3950::ZOOM::EVENT_SEND_APDU, + Net::Z3950::ZOOM::EVENT_SEND_DATA, + Net::Z3950::ZOOM::EVENT_RECV_DATA, + Net::Z3950::ZOOM::EVENT_RECV_APDU, + Net::Z3950::ZOOM::EVENT_END, + 0); + +# Now we need to actually _do_ something, and watch the stream of +# resulting events: issue a piggy-back search. +Net::Z3950::ZOOM::connection_option_set($conn, count => 1); +my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, "mineral"); +$errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo); +ok($errcode == 0, "search for 'mineral'"); + +assert_event_stream($conn, + Net::Z3950::ZOOM::EVENT_SEND_APDU, + Net::Z3950::ZOOM::EVENT_SEND_DATA, + -(Net::Z3950::ZOOM::EVENT_RECV_DATA), + Net::Z3950::ZOOM::EVENT_RECV_APDU, + Net::Z3950::ZOOM::EVENT_RECV_SEARCH, + Net::Z3950::ZOOM::EVENT_RECV_RECORD, + Net::Z3950::ZOOM::EVENT_END, + 0); + +# Some events, especially RECV_DATA, may randomly occur multiple +# times, depending on network chunking; so if an expected event's +# value is negated, we allow that event to occur one or more times, +# and treat the sequence of repeated events as a single test. +# +sub assert_event_stream { + my($conn, @expected) = @_; + + my $previousExpected = -1; + my $expected = shift @expected; + while (defined $expected) { + my $val = Net::Z3950::ZOOM::event([$conn]); + if ($expected == 0) { + ok($val == 0, "no events left"); + $expected = shift @expected; + next; + } + + die "impossible" if $val != 1; + my $ev = Net::Z3950::ZOOM::connection_last_event($conn); + next if $previousExpected > 0 && $ev == $previousExpected; + + if ($expected < 0) { + $expected = -$expected; + $previousExpected = $expected; + } + ok($ev == $expected, ("event is $ev (" . + Net::Z3950::ZOOM::event_str($ev) . + "), expected $expected (" . + Net::Z3950::ZOOM::event_str($expected) . ")")); + $expected = shift @expected; } - - ok($val == 1, "call with an good connection returns its index"); - - my $ev = Net::Z3950::ZOOM::connection_last_event($conn); - ok($ev == $expected, ("event is $ev (" . - Net::Z3950::ZOOM::event_str($ev) . - "), expected $expected (" . - Net::Z3950::ZOOM::event_str($expected) . ")")); } -