From 52f0d52f8f3c183d567856bead4edd35fa976bf9 Mon Sep 17 00:00:00 2001 From: mike Date: Wed, 12 Apr 2006 11:59:20 +0000 Subject: [PATCH] Adequate, at last. Tests for sensible sequences of events issued on a single asynchronous connections. Writing a test-case for actual multiplexing is a project in itself, and not something that I want to delay the release for. --- t/19-events.t | 88 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 29 deletions(-) diff --git a/t/19-events.t b/t/19-events.t index 36b430f..d11bf84 100644 --- a/t/19-events.t +++ b/t/19-events.t @@ -1,11 +1,11 @@ -# $Id: 19-events.t,v 1.3 2006-04-12 11:02:42 mike Exp $ +# $Id: 19-events.t,v 1.4 2006-04-12 11:59:20 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 => 22; BEGIN { use_ok('Net::Z3950::ZOOM') }; @@ -41,32 +41,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) . ")")); } - -- 1.7.10.4