Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

how to make close() fail

by kgoess (Beadle)
on Sep 29, 2012 at 00:34 UTC ( #996306=perlquestion: print w/ replies, xml ) Need Help??
kgoess has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing unit tests on some code that deals with the filesystem, and I'd like to demonstrate the code path works when close $fh or die $! fails. Any suggestions on how to trigger a failure in close()?

The first idea that occurs to me is to create a tiny loopback filesystem and write too many bytes to it and try to close it, but I'm wondering if there's an easier way.

I was hoping I could do local *other::package::close = sub { return 0 } in my test, but that doesn't take at all.

Comment on how to make close() fail
Select or Download Code
Re: how to make close() fail
by Anonymous Monk on Sep 29, 2012 at 01:05 UTC
    ?
    $ perl -e " use autodie; close EMPTY; " Can't close filehandle 'EMPTY': 'Bad file descriptor' at -e line 1
Re: how to make close() fail
by toolic (Chancellor) on Sep 29, 2012 at 01:11 UTC
    Can you close the filehandle, then try closing it again?
    open my $fh, '>', 'foo'; close $fh; close $fh or die $!; __END__ Bad file descriptor at
Re: how to make close() fail
by BrowserUk (Pope) on Sep 29, 2012 at 01:17 UTC

    Seems like a completely unnecessary test to me, but ...

    sub TIEHANDLE { bless \$_[1], $_[0] };; sub CLOSE{ 0 };; tie *FH, 'main';; close FH or die 'Aaaaaaaaaaaaarg!';; Aaaaaaaaaaaaarg! at (eval 12) line 1, <STDIN> line 4.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      Seems liek a completely unnecessary test to me
      NFS for example does buffered writes "over the wire", so all the writes apparently work, only for the final flush associated with the close() to fail (e.g. due to filesystem full)

      Dave.

        I know close can fail, but that wasn't my point.

        How complicated can a failed-to-close code path be that you can't test it 'by inspection'?

        And if it is complicated enough to warrant it, it probably indicates poor design.

        More likely this is the triumph of a "100% code coverage" mandate over reason.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

Re: how to make close() fail
by ikegami (Pope) on Sep 29, 2012 at 01:18 UTC
    $ perl -MErrno=EIO -E' BEGIN { *CORE::GLOBAL::close = sub { if (caller eq "other::package") { $! = EIO; return 0; } return CORE::close($_[0]); }; } package other::package; close(STDOUT) or die $!; ' Input/output error at -e line 14.
Re: how to make close() fail
by pvaldes (Chaplain) on Sep 29, 2012 at 11:25 UTC

    I'm wondering if there's an easier way.

    You shouldn't ever need to use a close line

    open my $fo, '<', "somefile" or die $!; ... blah blah # close($fo) implicit, not necessary

    Any suggestions to make close fail?

    use warnings; use strict; close (undef) or die;
Re: how to make close() fail
by tobyink (Abbot) on Sep 29, 2012 at 15:13 UTC

    Create a "filehandle" using IO::Callback. This allows you to run a callback coderef on file accesses and when the file is closed. The callback can simulate system errors.

    In particular, see examples 6 and 7 in the pod.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996306]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2014-12-22 07:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (112 votes), past polls