summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/bootstrap.inc8
-rw-r--r--modules/simpletest/tests/bootstrap.test32
2 files changed, 39 insertions, 1 deletions
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index ad0186d5d..192f28e1a 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -248,6 +248,7 @@ function timer_read($name) {
}
return $diff;
}
+ return $timers[$name]['time'];
}
/**
@@ -262,7 +263,12 @@ function timer_read($name) {
function timer_stop($name) {
global $timers;
- $timers[$name]['time'] = timer_read($name);
+ if (isset($timers[$name]['time'])) {
+ $timers[$name]['time'] += timer_read($name);
+ }
+ else {
+ $timers[$name]['time'] = timer_read($name);
+ }
unset($timers[$name]['start']);
return $timers[$name];
diff --git a/modules/simpletest/tests/bootstrap.test b/modules/simpletest/tests/bootstrap.test
index ddc94460d..b6bfd54e8 100644
--- a/modules/simpletest/tests/bootstrap.test
+++ b/modules/simpletest/tests/bootstrap.test
@@ -311,3 +311,35 @@ class BootstrapGetFilenameTestCase extends DrupalUnitTestCase {
$this->assertIdentical(drupal_get_filename('profile', 'default'), 'profiles/default/default.profile', t('Retrieve install profile location.'));
}
}
+
+class BootstrapTimerTestCase extends DrupalUnitTestCase {
+
+ public static function getInfo() {
+ return array(
+ 'name' => 'Timer test',
+ 'description' => 'Test that timer_read() works both when a timer is running and when a timer is stopped.',
+ 'group' => 'Bootstrap',
+ );
+ }
+
+ /**
+ * Test timer_read() to ensure it properly accumulates time when the timer
+ * started and stopped multiple times.
+ * @return
+ */
+ function testTimer() {
+ timer_start('test');
+ sleep(1);
+ $this->assertTrue(timer_read('test') >= 1000, t('Timer measured 1 second of sleeping while running.'));
+ sleep(1);
+ timer_stop('test');
+ $this->assertTrue(timer_read('test') >= 2000, t('Timer measured 2 seconds of sleeping after being stopped.'));
+ timer_start('test');
+ sleep(1);
+ $this->assertTrue(timer_read('test') >= 3000, t('Timer measured 3 seconds of sleeping after being restarted.'));
+ sleep(1);
+ $timer = timer_stop('test');
+ $this->assertTrue(timer_read('test') >= 4000, t('Timer measured 4 seconds of sleeping after being stopped for a second time.'));
+ $this->assertEqual($timer['count'], 2, t('Timer counted 2 instances of being started.'));
+ }
+}