summaryrefslogtreecommitdiffstats
path: root/tests/lib
diff options
context:
space:
mode:
authorblizzz <blizzz@owncloud.com>2013-12-06 11:56:53 -0800
committerblizzz <blizzz@owncloud.com>2013-12-06 11:56:53 -0800
commit6a747106db7d3c35d461f0b4cb3e9c48feea7780 (patch)
treec064eac18f48e8517bd4b87684c076b83bb2e24e /tests/lib
parent123bc9921a0369582b547cd3d8abc7d466d1f7f3 (diff)
parent2ff1bdaba3292cc5baff71d8d79674bbf0b69fc8 (diff)
downloadnextcloud-server-6a747106db7d3c35d461f0b4cb3e9c48feea7780.tar.gz
nextcloud-server-6a747106db7d3c35d461f0b4cb3e9c48feea7780.zip
Merge pull request #6150 from owncloud/backgroundjob-log-exception
Remove background jobs that are giving errors
Diffstat (limited to 'tests/lib')
-rw-r--r--tests/lib/backgroundjob/dummyjoblist.php3
-rw-r--r--tests/lib/backgroundjob/job.php59
-rw-r--r--tests/lib/backgroundjob/queuedjob.php30
-rw-r--r--tests/lib/backgroundjob/timedjob.php51
4 files changed, 105 insertions, 38 deletions
diff --git a/tests/lib/backgroundjob/dummyjoblist.php b/tests/lib/backgroundjob/dummyjoblist.php
index d91d6b344b5..e1579c273bb 100644
--- a/tests/lib/backgroundjob/dummyjoblist.php
+++ b/tests/lib/backgroundjob/dummyjoblist.php
@@ -8,9 +8,6 @@
namespace Test\BackgroundJob;
-class JobRun extends \Exception {
-}
-
/**
* Class DummyJobList
*
diff --git a/tests/lib/backgroundjob/job.php b/tests/lib/backgroundjob/job.php
new file mode 100644
index 00000000000..7d66fa772d2
--- /dev/null
+++ b/tests/lib/backgroundjob/job.php
@@ -0,0 +1,59 @@
+<?php
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\BackgroundJob;
+
+
+class TestJob extends \OC\BackgroundJob\Job {
+ private $testCase;
+
+ /**
+ * @var callable $callback
+ */
+ private $callback;
+
+ /**
+ * @param Job $testCase
+ * @param callable $callback
+ */
+ public function __construct($testCase, $callback) {
+ $this->testCase = $testCase;
+ $this->callback = $callback;
+ }
+
+ public function run($argument) {
+ $this->testCase->markRun();
+ $callback = $this->callback;
+ $callback($argument);
+ }
+}
+
+class Job extends \PHPUnit_Framework_TestCase {
+ private $run = false;
+
+ public function setUp() {
+ $this->run = false;
+ }
+
+ public function testRemoveAfterException() {
+ $jobList = new DummyJobList();
+ $job = new TestJob($this, function () {
+ throw new \Exception();
+ });
+ $jobList->add($job);
+
+ $this->assertCount(1, $jobList->getAll());
+ $job->execute($jobList);
+ $this->assertTrue($this->run);
+ $this->assertCount(0, $jobList->getAll());
+ }
+
+ public function markRun() {
+ $this->run = true;
+ }
+}
diff --git a/tests/lib/backgroundjob/queuedjob.php b/tests/lib/backgroundjob/queuedjob.php
index 1d373473cd7..19c1b28a507 100644
--- a/tests/lib/backgroundjob/queuedjob.php
+++ b/tests/lib/backgroundjob/queuedjob.php
@@ -9,8 +9,17 @@
namespace Test\BackgroundJob;
class TestQueuedJob extends \OC\BackgroundJob\QueuedJob {
+ private $testCase;
+
+ /**
+ * @param QueuedJob $testCase
+ */
+ public function __construct($testCase) {
+ $this->testCase = $testCase;
+ }
+
public function run($argument) {
- throw new JobRun(); //throw an exception so we can detect if this function is called
+ $this->testCase->markRun();
}
}
@@ -24,19 +33,22 @@ class QueuedJob extends \PHPUnit_Framework_TestCase {
*/
private $job;
+ private $jobRun = false;
+
+ public function markRun() {
+ $this->jobRun = true;
+ }
+
public function setup() {
$this->jobList = new DummyJobList();
- $this->job = new TestQueuedJob();
+ $this->job = new TestQueuedJob($this);
$this->jobList->add($this->job);
+ $this->jobRun = false;
}
public function testJobShouldBeRemoved() {
- try {
- $this->assertTrue($this->jobList->has($this->job, null));
- $this->job->execute($this->jobList);
- $this->fail("job should have been run");
- } catch (JobRun $e) {
- $this->assertFalse($this->jobList->has($this->job, null));
- }
+ $this->assertTrue($this->jobList->has($this->job, null));
+ $this->job->execute($this->jobList);
+ $this->assertTrue($this->jobRun);
}
}
diff --git a/tests/lib/backgroundjob/timedjob.php b/tests/lib/backgroundjob/timedjob.php
index f3c3eb4d0dd..646a2607ef3 100644
--- a/tests/lib/backgroundjob/timedjob.php
+++ b/tests/lib/backgroundjob/timedjob.php
@@ -9,12 +9,18 @@
namespace Test\BackgroundJob;
class TestTimedJob extends \OC\BackgroundJob\TimedJob {
- public function __construct() {
+ private $testCase;
+
+ /**
+ * @param TimedJob $testCase
+ */
+ public function __construct($testCase) {
$this->setInterval(10);
+ $this->testCase = $testCase;
}
public function run($argument) {
- throw new JobRun(); //throw an exception so we can detect if this function is called
+ $this->testCase->markRun();
}
}
@@ -28,44 +34,37 @@ class TimedJob extends \PHPUnit_Framework_TestCase {
*/
private $job;
+ private $jobRun = false;
+
+ public function markRun() {
+ $this->jobRun = true;
+ }
+
public function setup() {
$this->jobList = new DummyJobList();
- $this->job = new TestTimedJob();
+ $this->job = new TestTimedJob($this);
$this->jobList->add($this->job);
+ $this->jobRun = false;
}
public function testShouldRunAfterInterval() {
$this->job->setLastRun(time() - 12);
- try {
- $this->job->execute($this->jobList);
- $this->fail("job should have run");
- } catch (JobRun $e) {
- }
- $this->assertTrue(true);
+ $this->job->execute($this->jobList);
+ $this->assertTrue($this->jobRun);
}
public function testShouldNotRunWithinInterval() {
$this->job->setLastRun(time() - 5);
- try {
- $this->job->execute($this->jobList);
- } catch (JobRun $e) {
- $this->fail("job should not have run");
- }
- $this->assertTrue(true);
+ $this->job->execute($this->jobList);
+ $this->assertFalse($this->jobRun);
}
public function testShouldNotTwice() {
$this->job->setLastRun(time() - 15);
- try {
- $this->job->execute($this->jobList);
- $this->fail("job should have run the first time");
- } catch (JobRun $e) {
- try {
- $this->job->execute($this->jobList);
- } catch (JobRun $e) {
- $this->fail("job should not have run the second time");
- }
- }
- $this->assertTrue(true);
+ $this->job->execute($this->jobList);
+ $this->assertTrue($this->jobRun);
+ $this->jobRun = false;
+ $this->job->execute($this->jobList);
+ $this->assertFalse($this->jobRun);
}
}