-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPushTaskTest.php
More file actions
370 lines (294 loc) · 12.4 KB
/
Copy pathPushTaskTest.php
File metadata and controls
370 lines (294 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
* Copyright 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Unit tests for the PushTask class.
*
*/
namespace google\appengine\api\taskqueue;
require_once 'google/appengine/api/taskqueue/PushTask.php';
require_once 'google/appengine/testing/ApiProxyTestBase.php';
use google\appengine\api\taskqueue\PushTask;
use google\appengine\testing\ApiProxyTestBase;
use \google\appengine\TaskQueueAddRequest\RequestMethod;
use \google\appengine\TaskQueueBulkAddRequest;
use \google\appengine\TaskQueueBulkAddResponse;
use \google\appengine\TaskQueueServiceError\ErrorCode;
$mockTime = 12345.6;
// This mocks out PHP's microtime() function.
function microtime($get_as_float=false) {
if (!$get_as_float) {
die('microtime called with get_as_float=false');
}
global $mockTime;
return $mockTime;
}
class PushTaskTest extends ApiProxyTestBase {
/**
* Override time() in current namespace for testing.
*/
public function setUp() {
parent::setUp();
$this->_SERVER = $_SERVER;
}
public function tearDown() {
$_SERVER = $this->_SERVER;
parent::tearDown();
}
private static function buildBulkAddRequest() {
$req = new TaskQueueBulkAddRequest();
$task = $req->addAddRequest();
$task->setQueueName('default');
$task->setTaskName('');
$task->setUrl('/someUrl');
global $mockTime;
$task->setEtaUsec($mockTime * 1e6);
$task->setMethod(RequestMethod::POST);
return $req;
}
public function testConstructorUrlWrongType() {
$this->setExpectedException('\InvalidArgumentException',
'url_path must be a string. Actual type: integer');
$task = new PushTask(999, ['key' => 'some value']);
}
public function testConstructorUrlEmpty() {
$this->setExpectedException('\InvalidArgumentException',
"url_path must begin with '/'.");
$task = new PushTask('', ['key' => 'some value']);
}
public function testConstructorUrlWithoutLeadingSlash() {
$this->setExpectedException('\InvalidArgumentException',
"url_path must begin with '/'.");
$task = new PushTask('wrong', ['key' => 'some value']);
}
public function testConstructorUrlMustNotContainQueryString() {
$this->setExpectedException('\InvalidArgumentException',
'query strings not allowed in url_path.');
$task = new PushTask('/someurl?');
}
public function testConstructorQueryDataWrongType() {
$this->setExpectedException('\InvalidArgumentException',
'query_data must be an array. Actual type: string');
$task = new PushTask('/myUrl', 'abc');
}
public function testConstructorOptionsWrongType() {
$this->setExpectedException('\InvalidArgumentException',
'options must be an array. Actual type: integer');
$task = new PushTask('/someUrl', ['key' => 'some value'], 123);
}
public function testConstructorUnknownOptions() {
$this->setExpectedException('\InvalidArgumentException',
'Invalid options supplied: nonsense');
$task = new PushTask('/someUrl', ['key' => 'some value'],
['nonsense' => 123]);
}
public function testConstructorInvalidMethod() {
$this->setExpectedException('\InvalidArgumentException',
'Invalid method: POSTIT');
$task = new PushTask('/someUrl', ['key' => 'some value'],
['method' => 'POSTIT']);
}
public function testConstructorInvalidName() {
$this->setExpectedException('\InvalidArgumentException',
'name must be a string. Actual type: integer');
$task = new PushTask('/someUrl', ['key' => 'some value'],
['name' => 55]);
}
public function testConstructorNameTooLong() {
$name = str_repeat('a', 501);
$this->setExpectedException('\InvalidArgumentException',
'name exceeds maximum length of 500. First 1000 ' .
'characters of name: ' . $name);
$task = new PushTask('/someUrl', ['key' => 'some value'],
['name' => $name]);
}
public function testConstructorNameMaxLength() {
// Just tests that this doesn't throw an exception.
$task = new PushTask('/someUrl', ['key' => 'some value'],
['name' => str_repeat('a', 500)]);
}
public function testConstructorNameBadCharacters() {
$this->setExpectedException('\InvalidArgumentException',
'name must match pattern: ' . PushTask::NAME_PATTERN . '. name: @');
$task = new PushTask('/someUrl', ['key' => 'some value'],
['name' => '@']);
}
public function testConstructorInvalidDelaySeconds() {
$this->setExpectedException('\InvalidArgumentException',
'delay_seconds must be a numeric type.');
$task = new PushTask('/someUrl', ['key' => 'some value'],
['delay_seconds' => 'a few']);
}
public function testConstructorNegativeDelaySeconds() {
$this->setExpectedException('\InvalidArgumentException',
'delay_seconds must be between 0 and ' . PushTask::MAX_DELAY_SECONDS .
' (30 days). delay_seconds: -1');
$task = new PushTask('/someUrl', ['key' => 'some value'],
['delay_seconds' => -1]);
}
public function testConstructorDelaySecondsTooBig() {
$delay = 1 + 30 * 86400;
$this->setExpectedException('\InvalidArgumentException',
'delay_seconds must be between 0 and ' . PushTask::MAX_DELAY_SECONDS .
' (30 days). delay_seconds: ' . $delay);
$task = new PushTask('/someUrl', ['key' => 'some value'],
['delay_seconds' => $delay]);
}
public function testConstructorMaxDelaySecond() {
// Just tests that a 30 day delay_seconds doesn't throw an exception.
$task = new PushTask('/someUrl', ['key' => 'some value'],
['delay_seconds' => 30 * 86400]);
}
public function testAddInvalidQueue() {
$this->setExpectedException('\InvalidArgumentException');
(new PushTask('/someUrl'))->add(999);
}
public function testAddTaskTooBig() {
$this->setExpectedException(
'\google\appengine\api\taskqueue\TaskQueueException',
'Task greater than maximum size of ' . PushTask::MAX_TASK_SIZE_BYTES);
// Althought 102400 is the max size, it's for the serialized proto which
// includes the URL etc.
(new PushTask('/someUrl', ['field' => str_repeat('a', 102395)]))->add();
}
public function testAddUrlTooBig() {
$url = '/' . str_repeat('b', 2083);
$this->setExpectedException(
'\google\appengine\api\taskqueue\TaskQueueException',
'URL length greater than maximum of ' . PushTask::MAX_URL_LENGTH) .
'. URL: ' . $url;
(new PushTask($url))->add();
}
public function testPushTaskSimplestAdd() {
$req = self::buildBulkAddRequest();
$resp = new TaskQueueBulkAddResponse();
$task_result = $resp->addTaskResult();
$task_result->setResult(ErrorCode::OK);
$task_result->setChosenTaskName('fred');
$this->apiProxyMock->expectCall('taskqueue', 'BulkAdd', $req, $resp);
$task_name = (new PushTask('/someUrl'))->add();
$this->assertEquals('fred', $task_name);
$this->apiProxyMock->verify();
}
public function testPushTaskSimpleAddWithQueryData() {
$query_data = ['key' => 'some value'];
$req = self::buildBulkAddRequest();
$req->getAddRequest(0)->setBody(http_build_query($query_data));
$header = $req->getAddRequest(0)->addHeader();
$header->setKey('content-type');
$header->setValue('application/x-www-form-urlencoded');
$resp = new TaskQueueBulkAddResponse();
$this->apiProxyMock->expectCall('taskqueue', 'BulkAdd', $req, $resp);
(new PushTask('/someUrl', $query_data))->add();
$this->apiProxyMock->verify();
}
public function testPushTaskAddToNonDefaultQueue() {
$req = self::buildBulkAddRequest();
$req->getAddRequest(0)->setQueueName('myqueue');
$resp = new TaskQueueBulkAddResponse();
$this->apiProxyMock->expectCall('taskqueue', 'BulkAdd', $req, $resp);
(new PushTask('/someUrl'))->add('myqueue');
$this->apiProxyMock->verify();
}
public function testPushTaskAddBasicGetRequest() {
$req = self::buildBulkAddRequest();
$req->getAddRequest(0)->setMethod(RequestMethod::GET);
$resp = new TaskQueueBulkAddResponse();
$this->apiProxyMock->expectCall('taskqueue', 'BulkAdd', $req, $resp);
(new PushTask('/someUrl', [], ['method' => 'GET']))->add();
$this->apiProxyMock->verify();
}
public function testPushTaskAddGetRequestWithQueryData() {
$req = self::buildBulkAddRequest();
$req->getAddRequest(0)->setMethod(RequestMethod::GET);
$req->getAddRequest(0)->setUrl('/someUrl?aKey=aValue');
$resp = new TaskQueueBulkAddResponse();
$this->apiProxyMock->expectCall('taskqueue', 'BulkAdd', $req, $resp);
$task = new PushTask('/someUrl', ['aKey' => 'aValue'], ['method' => 'GET']);
$task->add();
$this->apiProxyMock->verify();
}
public function testPushTaskWithNameAndETA() {
$req = self::buildBulkAddRequest();
$add_req = $req->getAddRequest(0);
$add_req->setTaskName('customTaskName');
$add_req->setEtaUsec($add_req->getEtaUsec() + 5 * 1e6);
$resp = new TaskQueueBulkAddResponse();
$this->apiProxyMock->expectCall('taskqueue', 'BulkAdd', $req, $resp);
$task = new PushTask('/someUrl', [],
['delay_seconds' => 5, 'name' => 'customTaskName']);
$task_name = $task->add();
$this->assertEquals('customTaskName', $task_name);
$this->apiProxyMock->verify();
}
public function testUnknownQueueError() {
$req = self::buildBulkAddRequest();
$exception = new \google\appengine\runtime\ApplicationError(
ErrorCode::UNKNOWN_QUEUE, 'message');
$this->setExpectedException(
'\google\appengine\api\taskqueue\TaskQueueException',
'Unknown queue');
$this->apiProxyMock->expectCall('taskqueue', 'BulkAdd', $req, $exception);
(new PushTask('/someUrl'))->add();
}
public function testTransientError() {
$req = self::buildBulkAddRequest();
$exception = new \google\appengine\runtime\ApplicationError(
ErrorCode::TRANSIENT_ERROR, 'message');
$this->setExpectedException(
'\google\appengine\api\taskqueue\TransientTaskQueueException');
$this->apiProxyMock->expectCall('taskqueue', 'BulkAdd', $req, $exception);
(new PushTask('/someUrl'))->add();
}
public function testPermissionDeniedError() {
$req = self::buildBulkAddRequest();
$exception = new \google\appengine\runtime\ApplicationError(
ErrorCode::PERMISSION_DENIED, 'message');
$this->setExpectedException(
'\google\appengine\api\taskqueue\TaskQueueException',
'Permission Denied');
$this->apiProxyMock->expectCall('taskqueue', 'BulkAdd', $req, $exception);
(new PushTask('/someUrl'))->add();
}
public function testTombstonedTaskError() {
$req = self::buildBulkAddRequest();
$exception = new \google\appengine\runtime\ApplicationError(
ErrorCode::TOMBSTONED_TASK, 'message');
$this->setExpectedException(
'\google\appengine\api\taskqueue\TaskAlreadyExistsException');
$this->apiProxyMock->expectCall('taskqueue', 'BulkAdd', $req, $exception);
(new PushTask('/someUrl'))->add();
}
public function testTaskAlreadyExistsError() {
$req = self::buildBulkAddRequest();
$exception = new \google\appengine\runtime\ApplicationError(
ErrorCode::TASK_ALREADY_EXISTS, 'message');
$this->setExpectedException(
'\google\appengine\api\taskqueue\TaskAlreadyExistsException');
$this->apiProxyMock->expectCall('taskqueue', 'BulkAdd', $req, $exception);
(new PushTask('/someUrl'))->add();
}
public function testTaskInvalidQueueModeError() {
$req = self::buildBulkAddRequest();
$exception = new \google\appengine\runtime\ApplicationError(
ErrorCode::INVALID_QUEUE_MODE, 'message');
$this->setExpectedException(
'\google\appengine\api\taskqueue\TaskQueueException',
'Cannot add a PushTask to a pull queue.');
$this->apiProxyMock->expectCall('taskqueue', 'BulkAdd', $req, $exception);
(new PushTask('/someUrl'))->add();
}
}