I have a question on a testing strategy for a CPAN module test
script.... I've got two competing ideas on how to do it; I wonder if
anyone can suggest one or the other..

The function under test sends some data down a socket, then awaits a
response. Do I A: pre-send a response and get the kernel to buffer it,
call it, and check the sent value on return, or B: pipe() and fork() a
child, and have the checking/sending done in there?

A: would look something like:

  print $socket "The response you read\n";
  $result = call_function( "args" );
  is( $result, "foo", "return value" );
  is( <$socket>, "bar", "sent data" );

Some guards would be needed to avoid short reads, likely a $SIG{ALRM}
and lots of alarm(3) / alarm(0) wrapping going on.

B: would look somewhere more complicated; starting off with the usual
socketpair() / fork() code; then in pseudo-code:

  if( child ) {
     $buffer = read_socket();
     $buffer eq "expected" or die "Bad data sent";
     send_socket( "response data" );
     exit(0);
  }

  else {
    $result = call_function( "args" );
    is( $result, "foo", "return value" );
    is( waitpid $kid, 0, 0, "child exit status" );
  }

The former has the advantage of being shorter and easier to read, but
not "correct" in that the function under test technically gets its
response data before it sent the request - in a general case that might
confuse it. Also it relies on the size of the kernel's buffer to be able
to store that much data. When writing to yourself on a socket, you
always run the risk of deadlock like that.

The latter avoids any issues of deadlock, and obeys proper ordering
constraint, but is more complicated, and could break in all other sorts
of nasty ways.

If anyone has any thoughts on this, I'd appreciate hearing it....

-- 

Paul "LeoNerd" Evans

[EMAIL PROTECTED]
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/

Attachment: signature.asc
Description: Digital signature

Reply via email to