Queues.php
3.26 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
<?php
/**
* PHP OpenCloud library.
*
* @copyright 2014 Rackspace Hosting, Inc. See LICENSE for information.
* @license https://www.apache.org/licenses/LICENSE-2.0
* @author Jamie Hannaford <jamie.hannaford@rackspace.com>
*/
namespace OpenCloud\Smoke\Unit;
class Queues extends AbstractUnit implements UnitInterface
{
const QUEUE_NAME = 'test_queue';
private $queue;
public function setupService()
{
$service = $this->getConnection()->queuesService('cloudQueues');
$service->setClientId($service::generateUuid());
return $service;
}
public function main()
{
$this->doQueueBlock();
$this->doMessageBlock();
}
public function doQueueBlock()
{
$this->step('Queues');
// create
$this->stepInfo('Create queue');
$this->queue = $this->getService()->createQueue(self::QUEUE_NAME . rand(1, 9999));
// check existence
$this->stepInfo('Check queue existence');
$this->getService()->hasQueue($this->prepend(self::QUEUE_NAME));
// metadata
$this->stepInfo('Update metadata');
$this->queue->saveMetadata(array(
'foo' => 'bar'
));
// stats
$stats = $this->queue->getStats();
$this->stepInfo('Get stats: %s', print_r($stats, true));
// list
$step = $this->stepInfo('List queues');
$queues = $this->getService()->listQueues();
foreach ($queues as $queue) {
$step->stepInfo($queue->getName());
}
}
public function doMessageBlock()
{
$this->step('Messages');
// post
$this->stepInfo('Create messages for queue %s', $this->queue->getName());
$this->queue->createMessage(array(
'body' => (object) array(
'instructions' => 'Do it now!'
),
'ttl' => 300
));
$this->queue->createMessages(array(
array(
'body' => (object) array('foo' => 'bar'),
'ttl' => 700
),
array(
'body' => (object) array('baz' => 'lol'),
'ttl' => 600
)
));
// list
$step = $this->stepInfo('List messages for queue %s', $this->queue->getName());
$messages = $this->queue->listMessages();
$ids = array();
foreach ($messages as $message) {
$step->stepInfo($message->getId());
$ids[] = $message->getId();
}
array_pop($ids);
}
public function doClaimBlock()
{
$this->step('Claims');
// claim
$this->stepInfo('Create claims');
$this->queue->claimMessages(array(
'ttl' => 300,
'grace' => 300,
'limit' => 15
));
}
public function teardown()
{
$this->step('Delete queues');
$queues = $this->getService()->listQueues();
foreach ($queues as $queue) {
if ($this->shouldDelete($queue->getName())) {
try {
$this->stepInfo('Deleting %s', $queue->getName());
} catch (\Exception $e) {
$this->stepInfo('Failed to delete %s', $queue->getName());
}
}
}
}
}