+ $tname = "Main" if !defined $tname;
+ $this->{tree} = $this->_gather_tests($tname)
+ or die "No tests defined";
+ #$this->{tree}->print(0);
+ my $nskipped = 0;
+
+ my @conn = @{ $this->{connections} };
+
+ while (1) {
+ my @copy_conn = @conn; # avoid alias problems after splice()
+ foreach my $i0 (0 .. $#copy_conn) {
+ my $conn = $copy_conn[$i0];
+ #print "connection $i0 of ", scalar(@copy_conn), " from ", scalar(@conn), " is $conn\n";
+ if (!$conn->current_task()) {
+ if (!$conn->next_task()) {
+ # Out of tasks: we need a new test
+ NEXT_TEST:
+ my $address = $conn->option("current_test_address");
+ my $nextaddr;
+ if (!defined $address) {
+ $nextaddr = "";
+ } else {
+ $this->log("irspy_test", "checking for next test after '$address'");
+ $nextaddr = $this->_next_test($address);
+ }
+ if (!defined $nextaddr) {
+ $conn->log("irspy", "has no more tests: removing");
+ splice @conn, $i0, 1;
+ next;
+ }
+
+ my $node = $this->{tree}->select($nextaddr)
+ or die "invalid nextaddr '$nextaddr'";
+ $conn->option(current_test_address => $nextaddr);
+ my $tname = $node->name();
+ $conn->log("irspy_test", "starting test '$nextaddr' = $tname");
+ my $tasks = $conn->tasks();
+ my $oldcount = @$tasks;
+ "ZOOM::IRSpy::Test::$tname"->start($conn);
+ $tasks = $conn->tasks();
+ if (@$tasks > $oldcount) {
+ # Prepare to start the first of the newly added tasks
+ $conn->next_task($tasks->[$oldcount]);
+ } else {
+ $conn->log("irspy_task", "no tasks added by new test $tname");
+ goto NEXT_TEST;
+ }
+ }
+
+ my $task = $conn->next_task();
+ die "no next task queued for $conn" if !defined $task;
+ $conn->log("irspy_task", "starting task $task");
+ $conn->next_task(0);
+ $conn->current_task($task);
+ $task->run();
+ }
+
+ # Do we need to test $conn->is_idle()? I don't think so!
+ }
+
+ my $i0 = ZOOM::event(\@conn);
+ $this->log("irspy_event", "ZOOM_event(", scalar(@conn), " connections) = $i0");
+ last if $i0 == 0 || $i0 == -3; # no events or no connections
+ my $conn = $conn[$i0-1];
+ my $ev = $conn->last_event();
+ my $evstr = ZOOM::event_str($ev);
+ $conn->log("irspy_event", "event $ev ($evstr)");
+
+ my $task = $conn->current_task();
+ die "$conn has no current task for event $ev ($evstr)" if !$task;
+ eval { $conn->_check() };
+ if ($@ &&
+ ($ev == ZOOM::Event::RECV_DATA ||
+ $ev == ZOOM::Event::RECV_APDU ||
+ $ev == ZOOM::Event::ZEND)) {
+ # An error in, say, a search response, becomes visible to
+ # ZOOM before the Receive Data event is sent and persists
+ # until after the End, which means that successive events
+ # each report the same error. So we just ignore errors on
+ # "unimportant" events. ### But this doesn't work for,
+ # say, a Connection Refused, as the only event that shows
+ # us this error is the End.
+ $conn->log("irspy_event", "ignoring error ",
+ "on event $ev ($evstr): $@");
+ next;
+ }
+
+ my $res;
+ if ($@) {
+ my $sub = $task->{cb}->{exception};
+ die $@ if !defined $sub;
+ $res = &$sub($conn, $task, $task->udata(), $@);
+ } else {
+ my $sub = $task->{cb}->{$ev};
+ if (!defined $sub) {
+ $conn->log("irspy_unhandled", "event $ev ($evstr)");
+ next;
+ }
+
+ $res = &$sub($conn, $task, $task->udata(), $ev);
+ }
+
+ if ($res == ZOOM::IRSpy::Status::OK) {
+ # Nothing to do -- life continues
+
+ } elsif ($res == ZOOM::IRSpy::Status::TASK_DONE) {
+ my $task = $conn->current_task();
+ die "no task for TASK_DONE on $conn" if !$task;
+ die "next task already defined for $conn" if $conn->next_task();
+ $conn->log("irspy_task", "completed task $task");
+ $conn->next_task($task->{next});
+ $conn->current_task(0);
+
+ } elsif ($res == ZOOM::IRSpy::Status::TEST_GOOD ||
+ $res == ZOOM::IRSpy::Status::TEST_BAD) {
+ my $x = ($res == ZOOM::IRSpy::Status::TEST_GOOD) ? "good" : "bad";
+ $conn->log("irspy_test", "test completed ($x)");
+ $conn->current_task(0);
+ $conn->next_task(0);
+ if ($res == ZOOM::IRSpy::Status::TEST_BAD) {
+ ### Should skip over remaining sibling tests if TEST_BAD
+ ### Should count the number of skipped siblings
+ $nskipped += 1;
+ }
+ }
+ }
+
+ $this->log("irspy", "exiting main loop");
+
+ #$this->_rewrite_records();
+ return $nskipped;
+}
+
+
+sub _gather_tests {
+ my $this = shift();
+ my($tname, @ancestors) = @_;
+