1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014 Robin Appelman <[email protected]> |
4
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
5
|
|
|
* later. |
6
|
|
|
* See the COPYING-README file. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Test\BackgroundJob; |
10
|
|
|
|
11
|
|
|
use OCP\BackgroundJob\IJob; |
12
|
|
|
use OCP\IDBConnection; |
13
|
|
|
|
14
|
|
|
class JobList extends \Test\TestCase { |
15
|
|
|
/** |
16
|
|
|
* @var \OC\BackgroundJob\JobList |
17
|
|
|
*/ |
18
|
|
|
protected $instance; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \OCP\IConfig | \PHPUnit_Framework_MockObject_MockObject $config |
22
|
|
|
*/ |
23
|
|
|
protected $config; |
24
|
|
|
|
25
|
|
|
protected function setUp() { |
26
|
|
|
parent::setUp(); |
27
|
|
|
|
28
|
|
|
$conn = \OC::$server->getDatabaseConnection(); |
29
|
|
|
$this->clearJobsList($conn); |
30
|
|
|
$this->config = $this->getMock('\OCP\IConfig'); |
31
|
|
|
$this->instance = new \OC\BackgroundJob\JobList($conn, $this->config); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function clearJobsList(IDBConnection $connection) { |
35
|
|
|
$query = $connection->getQueryBuilder(); |
36
|
|
|
$query->delete('jobs'); |
37
|
|
|
$query->execute(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function getAllSorted() { |
41
|
|
|
$jobs = $this->instance->getAll(); |
42
|
|
|
|
43
|
|
|
usort($jobs, function (IJob $job1, IJob $job2) { |
44
|
|
|
return $job1->getId() - $job2->getId(); |
45
|
|
|
}); |
46
|
|
|
|
47
|
|
|
return $jobs; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function argumentProvider() { |
51
|
|
|
return array( |
52
|
|
|
array(null), |
53
|
|
|
array(false), |
54
|
|
|
array('foobar'), |
55
|
|
|
array(12), |
56
|
|
|
array(array( |
57
|
|
|
'asd' => 5, |
58
|
|
|
'foo' => 'bar' |
59
|
|
|
)) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @dataProvider argumentProvider |
65
|
|
|
* @param $argument |
66
|
|
|
*/ |
67
|
|
|
public function testAddRemove($argument) { |
68
|
|
|
$existingJobs = $this->getAllSorted(); |
69
|
|
|
$job = new TestJob(); |
70
|
|
|
$this->instance->add($job, $argument); |
71
|
|
|
|
72
|
|
|
$jobs = $this->getAllSorted(); |
73
|
|
|
|
74
|
|
|
$this->assertCount(count($existingJobs) + 1, $jobs); |
75
|
|
|
$addedJob = $jobs[count($jobs) - 1]; |
76
|
|
|
$this->assertInstanceOf('\Test\BackgroundJob\TestJob', $addedJob); |
77
|
|
|
$this->assertEquals($argument, $addedJob->getArgument()); |
78
|
|
|
|
79
|
|
|
$this->instance->remove($job, $argument); |
80
|
|
|
|
81
|
|
|
$jobs = $this->getAllSorted(); |
82
|
|
|
$this->assertEquals($existingJobs, $jobs); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @dataProvider argumentProvider |
87
|
|
|
* @param $argument |
88
|
|
|
*/ |
89
|
|
|
public function testRemoveDifferentArgument($argument) { |
90
|
|
|
$existingJobs = $this->getAllSorted(); |
91
|
|
|
$job = new TestJob(); |
92
|
|
|
$this->instance->add($job, $argument); |
93
|
|
|
|
94
|
|
|
$jobs = $this->getAllSorted(); |
95
|
|
|
$this->instance->remove($job, 10); |
96
|
|
|
$jobs2 = $this->getAllSorted(); |
97
|
|
|
|
98
|
|
|
$this->assertEquals($jobs, $jobs2); |
99
|
|
|
|
100
|
|
|
$this->instance->remove($job, $argument); |
101
|
|
|
|
102
|
|
|
$jobs = $this->getAllSorted(); |
103
|
|
|
$this->assertEquals($existingJobs, $jobs); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @dataProvider argumentProvider |
108
|
|
|
* @param $argument |
109
|
|
|
*/ |
110
|
|
|
public function testHas($argument) { |
111
|
|
|
$job = new TestJob(); |
112
|
|
|
$this->assertFalse($this->instance->has($job, $argument)); |
113
|
|
|
$this->instance->add($job, $argument); |
114
|
|
|
|
115
|
|
|
$this->assertTrue($this->instance->has($job, $argument)); |
116
|
|
|
|
117
|
|
|
$this->instance->remove($job, $argument); |
118
|
|
|
|
119
|
|
|
$this->assertFalse($this->instance->has($job, $argument)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @dataProvider argumentProvider |
124
|
|
|
* @param $argument |
125
|
|
|
*/ |
126
|
|
|
public function testHasDifferentArgument($argument) { |
127
|
|
|
$job = new TestJob(); |
128
|
|
|
$this->instance->add($job, $argument); |
129
|
|
|
|
130
|
|
|
$this->assertFalse($this->instance->has($job, 10)); |
131
|
|
|
|
132
|
|
|
$this->instance->remove($job, $argument); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testGetLastJob() { |
136
|
|
|
$this->config->expects($this->once()) |
137
|
|
|
->method('getAppValue') |
138
|
|
|
->with('backgroundjob', 'lastjob', 0) |
139
|
|
|
->will($this->returnValue(15)); |
140
|
|
|
|
141
|
|
|
$this->assertEquals(15, $this->instance->getLastJob()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
View Code Duplication |
public function testGetNext() { |
145
|
|
|
$job = new TestJob(); |
146
|
|
|
$this->instance->add($job, 1); |
147
|
|
|
$this->instance->add($job, 2); |
148
|
|
|
|
149
|
|
|
$jobs = $this->getAllSorted(); |
150
|
|
|
|
151
|
|
|
$savedJob1 = $jobs[count($jobs) - 2]; |
152
|
|
|
$savedJob2 = $jobs[count($jobs) - 1]; |
153
|
|
|
|
154
|
|
|
$this->config->expects($this->once()) |
155
|
|
|
->method('getAppValue') |
156
|
|
|
->with('backgroundjob', 'lastjob', 0) |
157
|
|
|
->will($this->returnValue($savedJob2->getId())); |
158
|
|
|
|
159
|
|
|
$nextJob = $this->instance->getNext(); |
160
|
|
|
|
161
|
|
|
$this->assertEquals($savedJob1, $nextJob); |
162
|
|
|
|
163
|
|
|
$this->instance->remove($job, 1); |
164
|
|
|
$this->instance->remove($job, 2); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
View Code Duplication |
public function testGetNextWrapAround() { |
168
|
|
|
$job = new TestJob(); |
169
|
|
|
$this->instance->add($job, 1); |
170
|
|
|
$this->instance->add($job, 2); |
171
|
|
|
|
172
|
|
|
$jobs = $this->getAllSorted(); |
173
|
|
|
|
174
|
|
|
$savedJob1 = $jobs[count($jobs) - 2]; |
175
|
|
|
$savedJob2 = $jobs[count($jobs) - 1]; |
176
|
|
|
|
177
|
|
|
$this->config->expects($this->once()) |
178
|
|
|
->method('getAppValue') |
179
|
|
|
->with('backgroundjob', 'lastjob', 0) |
180
|
|
|
->will($this->returnValue($savedJob1->getId())); |
181
|
|
|
|
182
|
|
|
$nextJob = $this->instance->getNext(); |
183
|
|
|
|
184
|
|
|
$this->assertEquals($savedJob2, $nextJob); |
185
|
|
|
|
186
|
|
|
$this->instance->remove($job, 1); |
187
|
|
|
$this->instance->remove($job, 2); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @dataProvider argumentProvider |
192
|
|
|
* @param $argument |
193
|
|
|
*/ |
194
|
|
|
public function testGetById($argument) { |
195
|
|
|
$job = new TestJob(); |
196
|
|
|
$this->instance->add($job, $argument); |
197
|
|
|
|
198
|
|
|
$jobs = $this->getAllSorted(); |
199
|
|
|
|
200
|
|
|
$addedJob = $jobs[count($jobs) - 1]; |
201
|
|
|
|
202
|
|
|
$this->assertEquals($addedJob, $this->instance->getById($addedJob->getId())); |
203
|
|
|
|
204
|
|
|
$this->instance->remove($job, $argument); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testSetLastRun() { |
208
|
|
|
$job = new TestJob(); |
209
|
|
|
$this->instance->add($job); |
210
|
|
|
|
211
|
|
|
$jobs = $this->getAllSorted(); |
212
|
|
|
|
213
|
|
|
$addedJob = $jobs[count($jobs) - 1]; |
214
|
|
|
|
215
|
|
|
$timeStart = time(); |
216
|
|
|
$this->instance->setLastRun($addedJob); |
217
|
|
|
$timeEnd = time(); |
218
|
|
|
|
219
|
|
|
$addedJob = $this->instance->getById($addedJob->getId()); |
220
|
|
|
|
221
|
|
|
$this->assertGreaterThanOrEqual($timeStart, $addedJob->getLastRun()); |
222
|
|
|
$this->assertLessThanOrEqual($timeEnd, $addedJob->getLastRun()); |
223
|
|
|
|
224
|
|
|
$this->instance->remove($job); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function testGetNextNonExisting() { |
228
|
|
|
$job = new TestJob(); |
229
|
|
|
$this->instance->add($job, 1); |
230
|
|
|
$this->instance->add('\OC\Non\Existing\Class'); |
231
|
|
|
$this->instance->add($job, 2); |
232
|
|
|
|
233
|
|
|
$jobs = $this->getAllSorted(); |
234
|
|
|
|
235
|
|
|
$savedJob1 = $jobs[count($jobs) - 2]; |
236
|
|
|
$savedJob2 = $jobs[count($jobs) - 1]; |
237
|
|
|
|
238
|
|
|
$this->config->expects($this->any()) |
239
|
|
|
->method('getAppValue') |
240
|
|
|
->with('backgroundjob', 'lastjob', 0) |
241
|
|
|
->will($this->returnValue($savedJob1->getId())); |
242
|
|
|
|
243
|
|
|
$this->instance->getNext(); |
244
|
|
|
|
245
|
|
|
$nextJob = $this->instance->getNext(); |
246
|
|
|
|
247
|
|
|
$this->assertEquals($savedJob2, $nextJob); |
248
|
|
|
|
249
|
|
|
$this->instance->remove($job, 1); |
250
|
|
|
$this->instance->remove($job, 2); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|