6 Replies Latest reply on Apr 14, 2009 8:04 AM by jaikiran pai

    File last modified diff behavior

    Ales Justin Master

      I have this piece of test code

       // update some file
       File updateFile = new File(rootFile, "test.jsp");
       assertTrue(updateFile.exists());
       assertTrue(updateFile.setLastModified(System.currentTimeMillis()));
       @SuppressWarnings("deprecation")
       VirtualFile testJsp = tempRoot.findChild("test.jsp");
       long tempTimestamp = testJsp.getLastModified();
       // this should override testJsp, hence new timestamp
       assertFalse(checker.hasStructureBeenModified(originalRoot));
       long lastModified = testJsp.getLastModified();
       long diff = lastModified - tempTimestamp;
       assertTrue("Last modified diff is not bigger then 0, diff: " + diff, diff > 0);
      


      While this work for me locally (winz machine), it fails for me remotely (linux machine).

      This test mocks the behavior for JBAS-6722,
      where the user updates his original file (updateFile),
      and then the checker updates (overrides) the temp copy (testJsp).

      Any idea why this would diff based on OS?


        • 2. Re: File last modified diff behavior
          jaikiran pai Master

           


          File updateFile = new File(rootFile, "test.jsp");
          assertTrue(updateFile.setLastModified(System.currentTimeMillis()));


          As per the javadoc of java.io.File.setLastModified:

          All platforms support file-modification times to the nearest second, but some provide more precision. The argument will be truncated to fit the supported precision. If the operation succeeds and no intervening operations on the file take place, then the next invocation of the lastModified() method will return the (possibly truncated) time argument that was passed to this method.


          So unless there is a difference of 1 second, there's no guarantee of that test case passing on all platforms. I guess the test case could be changed to add some delay before updating the last modified timestamp on the original file.




          • 3. Re: File last modified diff behavior
            Ales Justin Master

            I just added additional 1500ms.
            Can you check (if you're on Linux :-))?

            • 4. Re: File last modified diff behavior
              jaikiran pai Master

              Yep, i have Kubuntu system where this still fails:

              Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.425 sec <<< FAILURE!
              testWAR(org.jboss.test.deployers.vfs.structure.modified.test.SynchModificationTestCase) Time elapsed: 0.121 sec <<< FAILURE!
              junit.framework.AssertionFailedError: Last modified diff is not bigger then 0, diff: 0
               at junit.framework.Assert.fail(Assert.java:47)
               at junit.framework.Assert.assertTrue(Assert.java:20)
               at org.jboss.test.deployers.vfs.structure.modified.test.SynchModificationTestCase.testWAR(SynchModificationTestCase.java:122)
              


              I see that the testcase was changed to:

              assertTrue(updateFile.setLastModified(System.currentTimeMillis() + 1500l));
              

              This probably won't introduce the "delay" that we are expecting.

              Looking at this:
              long tempTimestamp = testJsp.getLastModified();
               assertFalse(checker.hasStructureBeenModified(originalRoot));
               long lastModified = testJsp.getLastModified();


              I guess, internally its a file "copy" (original deployment to temp deployment) if the original file has changed. I am not really sure whether the "copy" copies over the last modified timestamp from the source. Maybe the copy just copies the file and updates the last modified time to current system timestamp?
              The delay that we need, should be between the change of the original file and copying to temp file. Something like:

              long tempTimestamp = testJsp.getLastModified();
              // Platform dependent precision for last modified. Let's wait a minimum of 1 sec
               Thread.sleep(1500);
               assertFalse(checker.hasStructureBeenModified(originalRoot));
               long lastModified = testJsp.getLastModified();



              • 5. Re: File last modified diff behavior
                Ales Justin Master

                 

                "jaikiran" wrote:
                Yep, i have Kubuntu system where this still fails:

                Yeah, for me too.

                "jaikiran" wrote:

                The delay that we need, should be between the change of the original file and copying to temp file. Something like:

                long tempTimestamp = testJsp.getLastModified();
                // Platform dependent precision for last modified. Let's wait a minimum of 1 sec
                 Thread.sleep(1500);
                 assertFalse(checker.hasStructureBeenModified(originalRoot));
                 long lastModified = testJsp.getLastModified();


                Does this work for you?

                Although, the temp should be compiled/copied to test-classes long before System.currentTimemillis - 1sec.
                As mvn first creates all the files and only then runs the tests.

                Checker then deletes and re-creates the temp file,
                resulting in new last modified.


                • 6. Re: File last modified diff behavior
                  jaikiran pai Master

                   

                  "alesj" wrote:

                  long tempTimestamp = testJsp.getLastModified();
                  // Platform dependent precision for last modified. Let's wait a minimum of 1 sec
                   Thread.sleep(1500);
                   assertFalse(checker.hasStructureBeenModified(originalRoot));
                   long lastModified = testJsp.getLastModified();


                  Does this work for you?

                  Yes, it does:
                  -------------------------------------------------------------------------------
                  Test set: org.jboss.test.deployers.vfs.structure.modified.test.SynchModificationTestCase
                  -------------------------------------------------------------------------------
                  Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.816 sec
                  


                  "alesj" wrote:

                  Checker then deletes and re-creates the temp file,
                  resulting in new last modified.

                  Right, it's the checker which sets the last modified on the new temp to the current system time and should be atleast 1 second diff from the old temp file's last modified time. That delay, with Thread.sleep achieves this.