2009-06-17 13:36:49 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2011-08-30 12:53:06 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-08-30 12:53:06 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2009-06-17 13:36:49 +02:00
|
|
|
|
|
|
|
class MemoryLimitTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testIncreaseMemoryLimitTo() {
|
2010-10-15 01:51:03 +02:00
|
|
|
if(!$this->canChangeMemory()) return;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
ini_set('memory_limit', '64M');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
// It can go up
|
|
|
|
increase_memory_limit_to('128M');
|
|
|
|
$this->assertEquals('128M', ini_get('memory_limit'));
|
|
|
|
|
|
|
|
// But not down
|
|
|
|
increase_memory_limit_to('64M');
|
|
|
|
$this->assertEquals('128M', ini_get('memory_limit'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
// Test the different kinds of syntaxes
|
|
|
|
increase_memory_limit_to(1024*1024*200);
|
|
|
|
$this->assertEquals(1024*1024*200, ini_get('memory_limit'));
|
|
|
|
|
|
|
|
increase_memory_limit_to('409600K');
|
|
|
|
$this->assertEquals('409600K', ini_get('memory_limit'));
|
|
|
|
|
|
|
|
increase_memory_limit_to('1G');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-09-24 09:14:30 +02:00
|
|
|
// If memory limit was left at 409600K, that means that the current testbox doesn't have
|
|
|
|
// 1G of memory available. That's okay; let's not report a failure for that.
|
|
|
|
if(ini_get('memory_limit') != '409600K') {
|
|
|
|
$this->assertEquals('1G', ini_get('memory_limit'));
|
|
|
|
}
|
2009-06-28 04:36:46 +02:00
|
|
|
|
|
|
|
// No argument means unlimited
|
|
|
|
increase_memory_limit_to();
|
|
|
|
$this->assertEquals(-1, ini_get('memory_limit'));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testIncreaseTimeLimitTo() {
|
2010-10-15 01:51:03 +02:00
|
|
|
if(!$this->canChangeMemory()) return;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-28 04:36:46 +02:00
|
|
|
set_time_limit(6000);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-28 04:36:46 +02:00
|
|
|
// It can go up
|
|
|
|
increase_time_limit_to(7000);
|
|
|
|
$this->assertEquals(7000, ini_get('max_execution_time'));
|
|
|
|
|
|
|
|
// But not down
|
|
|
|
increase_time_limit_to(5000);
|
|
|
|
$this->assertEquals(7000, ini_get('max_execution_time'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-28 04:36:46 +02:00
|
|
|
// 0/nothing means infinity
|
|
|
|
increase_time_limit_to();
|
|
|
|
$this->assertEquals(0, ini_get('max_execution_time'));
|
|
|
|
|
|
|
|
// Can't go down from there
|
|
|
|
increase_time_limit_to(10000);
|
|
|
|
$this->assertEquals(0, ini_get('max_execution_time'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-28 04:36:46 +02:00
|
|
|
private $origMemLimit, $origTimeLimit;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2009-06-28 04:36:46 +02:00
|
|
|
$this->origMemLimit = ini_get('memory_limit');
|
|
|
|
$this->origTimeLimit = ini_get('max_execution_time');
|
2011-08-30 12:53:06 +02:00
|
|
|
$this->origMemLimitMax = get_increase_memory_limit_max();
|
|
|
|
$this->origTimeLimitMax = get_increase_time_limit_max();
|
|
|
|
set_increase_memory_limit_max(-1);
|
|
|
|
set_increase_time_limit_max(-1);
|
2009-06-17 13:36:49 +02:00
|
|
|
}
|
2012-09-19 12:07:39 +02:00
|
|
|
public function tearDown() {
|
2009-06-28 04:36:46 +02:00
|
|
|
ini_set('memory_limit', $this->origMemLimit);
|
|
|
|
set_time_limit($this->origTimeLimit);
|
2011-08-30 12:53:06 +02:00
|
|
|
set_increase_memory_limit_max($this->origMemLimitMax);
|
|
|
|
set_increase_time_limit_max($this->origTimeLimitMax);
|
2009-06-17 13:36:49 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 01:51:03 +02:00
|
|
|
/**
|
|
|
|
* Determines wether the environment generally allows
|
|
|
|
* to change the memory limits, which is not always the case.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 01:51:03 +02:00
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
protected function canChangeMemory() {
|
|
|
|
$exts = get_loaded_extensions();
|
|
|
|
// see http://www.hardened-php.net/suhosin/configuration.html#suhosin.memory_limit
|
|
|
|
if(in_array('suhosin', $exts)) return false;
|
2010-10-15 04:58:39 +02:00
|
|
|
|
|
|
|
// We can't change memory limit in safe mode
|
2010-10-15 04:58:55 +02:00
|
|
|
if(ini_get('safe_mode')) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 01:51:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|