On Wednesday, June 23, 2004, at 01:09 PM, Ian Langworth wrote:

Thanks for all of the comments and suggestions, though I'm still
stuck. To provide a little background, this actually comes from the
lock.t test for IO::All.
[stuff deleted]
  - in the test, the child process requests an exclusive lock, writes
a few lines and sleeps, then unlocks.

Not quite. The way the script you posted currently works is:

The process opens the file.
The process grabs an exclusive lock

The process forks into parent and child
(implicitly, the child gives up the exclusive lock)


The parent downgrades its | The child sleeps for 1 sec.
lock from exclusive to shared |
|
The parent tries to read lines from the file. | The child writes lines to the file
| without the benefit of the lock.
|
The parent unlocks | The child unlocks what it doesn't have.
|
the parent exits. | the child exits.





Is there anything implicit in Test::More that requires the parent to do the testing? I thought it was just reading standard output, and the parent's exit status, but I might be mistaken. If you reverse the jobs, locking works correctly since the child would be the one reacquiring the lock.


If you need for the parent to do the tests, then I think you will need to acquire the lock within the child, and do some sort of coordination between the two. Maybe something like this:

#!/usr/bin/perl -w
use Test::More;

open ONE, ">foo" or die $!;
pipe RD,WR or die "Can't create communication pipes: $!\n";

$pid = fork;
$pid and do {
    <RD>; # wait until child is ready.
    open TWO, "<foo" or die $!;
    flock TWO, LOCK_SH or die "Can't lock";
    is <TWO>, "line 1\n";
    is <TWO>, "line 2\n";
    is <TWO>, "line 3\n";
    close TWO;
    is wait $pid, 0, 'catch-all child errors';
    exit 0;
};

flock ONE, LOCK_EX or die "Can't lock";
print WR "READY!!!!\n"; # flag parent that we have the lock.
print ONE "line 1\n";
print ONE "line 2\n";
print ONE "line 3\n";
close ONE;
exit 0; # make sure parent knows we got this far without catastrophic errors.


_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to