Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-47572

PHP unit test for cron deletion of temporary files & directories fails.

    XMLWordPrintable

Details

    • MOODLE_26_STABLE, MOODLE_27_STABLE
    • MOODLE_26_STABLE, MOODLE_27_STABLE
    • wip-MDL-47572-master
    • Hide
      In 2.6
      1. Add sleep(1); before calling cron_delete_from_temp() function in function test_cron_delete_from_temp() in lib/tests/cronlib_test.php
      2. Run this unittest, make sure it passes.
      In 2.7 and master
      1. Add sleep(1); before executing the \core\task\file_temp_cleanup_task() task in function test_cron_delete_from_temp() in lib/tests/cronlib_test.php
      2. Run this unittest, make sure it passes.
      Show
      In 2.6 Add sleep(1); before calling cron_delete_from_temp() function in function test_cron_delete_from_temp() in lib/tests/cronlib_test.php Run this unittest, make sure it passes. In 2.7 and master Add sleep(1); before executing the \core\task\file_temp_cleanup_task() task in function test_cron_delete_from_temp() in lib/tests/cronlib_test.php Run this unittest, make sure it passes.

    Description

      This bug is further work on MDL-45654, which didn't properly fix the issue described there. To help searchers, I'll reproduce the sample error output provided in one of the comments below:

      There was 1 failure:

      1) cronlib_testcase::test_cron_delete_from_temp with data set #0 (array(stdClass, stdClass, stdClass, stdClass, stdClass, stdClass, stdClass, stdClass, stdClass, stdClass, stdClass, stdClass, stdClass), array('/home/marina/repositories/mysql_master/testdata/temp/dir1', '/home/marina/repositories/mysql_master/testdata/temp/dir1/dir1_1', '/home/marina/repositories/mysql_master/testdata/temp/dir1/dir1_1/dir1_1_1', '/home/marina/repositories/mysql_master/testdata/temp/dir1/dir1_1/dir1_1_1/file1_1_1_2', '/home/marina/repositories/mysql_master/testdata/temp/dir1/dir1_2', '/home/marina/repositories/mysql_master/testdata/temp/dir1/dir1_2/file1_1_2_2', '/home/marina/repositories/mysql_master/testdata/temp/dir2', '/home/marina/repositories/mysql_master/testdata/temp/dir4', '/home/marina/repositories/mysql_master/testdata/temp/dir4/dir4_1', '/home/marina/repositories/mysql_master/testdata/temp/dir4/dir4_1/dir4_1_1', '/home/marina/repositories/mysql_master/testdata/temp/file1'))
      Failed asserting that two arrays are equal.
      — Expected
      +++ Actual
      @@ @@
      5 => '/home/marina/repositories/mys..._1_2_2'

      • 6 => '/home/marina/repositories/mysql_master/testdata/temp/dir2'
      • 7 => '/home/marina/repositories/mysql_master/testdata/temp/dir4'
      • 8 => '/home/marina/repositories/mysql_master/testdata/temp/dir4/dir4_1'
      • 9 => '/home/marina/repositories/mysql_master/testdata/temp/dir4/dir4_1/dir4_1_1'
      • 10 => '/home/marina/repositories/mys.../file1'
        + 6 => '/home/marina/repositories/mysql_master/testdata/temp/dir4'
        + 7 => '/home/marina/repositories/mysql_master/testdata/temp/dir4/dir4_1'
        + 8 => '/home/marina/repositories/mysql_master/testdata/temp/dir4/dir4_1/dir4_1_1'
        + 9 => '/home/marina/repositories/mysql_master/testdata/temp/file1'
        )

      /home/marina/repositories/mysql_master/moodle/lib/tests/cronlib_test.php:179
      /home/marina/repositories/mysql_master/moodle/lib/phpunit/classes/basic_testcase.php:64

      To re-run:
      vendor/bin/phpunit cronlib_testcase lib/tests/cronlib_test.php

      As described in my comment toward the end of that issue (when I was seeking to reopen it):

      I've been able to reliably reproduce the PHP unit testing failure with Totara 2.6.10 (based on Moodle 2.6.5), and have a patch that does fix the issue.

      There are two problems.

      First, the same timestamp is not used throughout testing, so that failures can happen because a test takes too long to run. The patch I will seek to add fixes this by storing the result of time() once and passing it in a new optional parameter to cron_delete_from_temp so that its tests can use that fixed timestamp and not be subject to variations in the time taken to run tests.

      Secondly, in the week after daylight saving starts or ends (this week in Oz, which is why I found it!), the number of seconds to the same time last week is not 7 * 24 * 60 * 60. The $lastweekstime variable therefore needs to calculate the difference between strtotime('-1 week') and time(), and use that value.

      With both of these changes, I've reliably and successfully run the unit test.

      Attachments

        Issue Links

          Activity

            People

              marina Marina Glancy
              nigelccatalyst Nigel Cunningham
              Simey Lameze Simey Lameze
              Damyon Wiese Damyon Wiese
              Mark Nelson Mark Nelson
              Andrew Lyons, Huong Nguyen, Jun Pataleta, Michael Hawkins, Shamim Rezaie, Simey Lameze
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                10/Nov/14