Re: Intermittent failure to delete file

Discussion in 'Java' started by John B. Matthews, Feb 11, 2009.

  1. In article <>,
    Spud <> wrote:

    > I have a large list of unit tests using JUnit. Many of the tests call
    > a method to create a data file, do something to it, and then close
    > it. Each test creates the file from scratch so I have a clean version
    > of it.
    >
    > The trouble is that createFile() method fails intermittently because
    > it cannot delete the version left over from the previous unit test.
    > Sometimes it deletes, sometimes it doesn't.


    Just to be sure: Do all tests execute on the same thread?

    > I'm guessing that it's an operating system issue. My code closes the
    > file, but the OS doesn't. This is Windows, obviously.
    >
    > Is there any way in Java to tell the OS to commit all changes and
    > really close the files?
    >
    > Or otherwise isolate the unit tests so one doesn't trip up the others?


    You might look into File.createTempFile() and deleteOnExit(), as a way
    to give each test its own file.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Feb 11, 2009
    #1
    1. Advertising

  2. John B. Matthews

    Roedy Green Guest

    On Wed, 11 Feb 2009 22:31:18 -0600, Spud <> wrote,
    quoted or indirectly quoted someone who said :

    >
    >Good question. It's possible there's a stray thread hanging around
    >longer than it should. Is there any way to know which thread opened a file?


    you could create a collection where you track files and threads/open
    close status. When you shutdown it should be empty.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Here is a point of no return after which warming becomes unstoppable
    and we are probably going to sail right through it.
    It is the point at which anthropogenic (human-caused) warming triggers
    huge releases of carbon dioxide from warming oceans, or similar releases
    of both carbon dioxide and methane from melting permafrost, or both.
    Most climate scientists think that point lies not far beyond 2°C (4°F) C hotter."
    ~ Gwynne Dyer
     
    Roedy Green, Feb 12, 2009
    #2
    1. Advertising

  3. In article <>,
    Spud <> wrote:

    > John B. Matthews wrote:
    > > In article <>,
    > > Spud <> wrote:
    > >
    > >> I have a large list of unit tests using JUnit. Many of the tests call
    > >> a method to create a data file, do something to it, and then close
    > >> it. Each test creates the file from scratch so I have a clean version
    > >> of it.
    > >>
    > >> The trouble is that createFile() method fails intermittently because
    > >> it cannot delete the version left over from the previous unit test.
    > >> Sometimes it deletes, sometimes it doesn't.

    > >
    > > Just to be sure: Do all tests execute on the same thread?

    >
    > Good question. It's possible there's a stray thread hanging around
    > longer than it should. Is there any way to know which thread opened a file?


    I've used lsof to get the process ID:

    <http://www.netadmintools.com/html/lsof.man.html>

    Of course, one PID may represent several lightweight threads. Would it
    help to check Thread.currentThread().holdsLock(file) when file.delete()
    returns false?

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Feb 12, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. akhare1
    Replies:
    4
    Views:
    867
    Alvin Bruney [MVP]
    Aug 1, 2004
  2. H J van Rooyen

    Intermittent Failure on Serial Port

    H J van Rooyen, Jun 10, 2006, in forum: Python
    Replies:
    4
    Views:
    3,253
    H J van Rooyen
    Jun 12, 2006
  3. H J van Rooyen
    Replies:
    2
    Views:
    507
    H J van Rooyen
    Jun 13, 2006
  4. Sky
    Replies:
    1
    Views:
    421
  5. Shinz62

    Intermittent Failure to respond

    Shinz62, Jan 6, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    118
    Shinz62
    Jan 7, 2005
Loading...

Share This Page