-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHttpRequestProducerAndWorkerTest.php
More file actions
168 lines (147 loc) · 5.92 KB
/
Copy pathHttpRequestProducerAndWorkerTest.php
File metadata and controls
168 lines (147 loc) · 5.92 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
<?php
namespace Webgriffe\Esb\Integration;
use Amp\Artax\DefaultClient;
use Amp\Artax\Request;
use Amp\Artax\Response;
use Amp\Http\Server\Options;
use Amp\Loop;
use Amp\Promise;
use Amp\Socket\ClientSocket;
use Amp\Socket\ConnectException;
use Monolog\Logger;
use org\bovigo\vfs\vfsStream;
use ReflectionObject;
use Webgriffe\Esb\DummyFilesystemWorker;
use Webgriffe\Esb\DummyHttpRequestProducer;
use Webgriffe\Esb\KernelTestCase;
use Webgriffe\Esb\Service\HttpProducersServer;
use Webgriffe\Esb\TestUtils;
use function Amp\call;
use function Amp\File\exists;
use function Amp\Socket\connect;
class HttpRequestProducerAndWorkerTest extends KernelTestCase
{
use TestUtils;
private $workerFile;
private $httpPort;
private const FLOW_CODE = 'http_producer_flow';
private function setUpKernel(array $additionalParameters = [])
{
$this->workerFile = vfsStream::url('root/worker.data');
self::createKernel(
[
'services' => [
DummyHttpRequestProducer::class => ['arguments' => []],
DummyFilesystemWorker::class => ['arguments' => [$this->workerFile]],
],
'flows' => [
self::FLOW_CODE => [
'description' => 'Http Request Producer And Worker Test Flow',
'producer' => ['service' => DummyHttpRequestProducer::class],
'worker' => ['service' => DummyFilesystemWorker::class],
]
],
'parameters' => $additionalParameters,
]
);
$this->httpPort = self::$kernel->getContainer()->getParameter('http_server_port');
}
public function testHttpRequestProducerAndWorker()
{
$this->setUpKernel();
Loop::delay(100, function () {
yield $this->waitForConnectionAvailable("tcp://127.0.0.1:{$this->httpPort}");
$payload = json_encode(['jobs' => ['job1', 'job2', 'job3']]);
$client = new DefaultClient();
$request = (new Request("http://127.0.0.1:{$this->httpPort}/dummy", 'POST'))->withBody($payload);
/** @var Response $response */
$response = yield $client->request($request);
$this->assertContains('"Successfully scheduled 3 job(s) to be queued."', yield $response->getBody());
});
$this->stopWhen(function () {
return (yield exists($this->workerFile)) && count($this->getFileLines($this->workerFile)) === 3;
});
self::$kernel->boot();
$workerFileLines = $this->getFileLines($this->workerFile);
$this->assertCount(3, $workerFileLines);
$this->assertContains('job1', $workerFileLines[0]);
$this->assertContains('job2', $workerFileLines[1]);
$this->assertContains('job3', $workerFileLines[2]);
$this->logHandler()->hasRecordThatMatches(
'/Successfully produced a new Job .*? "payload_data":["job1"]/',
Logger::INFO
);
$this->logHandler()->hasRecordThatMatches(
'/Successfully produced a new Job .*? "payload_data":["job2"]/',
Logger::INFO
);
$this->logHandler()->hasRecordThatMatches(
'/Successfully produced a new Job .*? "payload_data":["job3"]/',
Logger::INFO
);
$this->assertReadyJobsCountInTube(0, self::FLOW_CODE);
}
public function testHttpRequestProducerWithWrongUriShouldReturn404()
{
$this->setUpKernel();
Loop::delay(100, function () {
yield $this->waitForConnectionAvailable("tcp://127.0.0.1:{$this->httpPort}");
$payload = json_encode(['jobs' => ['job1', 'job2', 'job3']]);
$client = new DefaultClient();
$request = (new Request("http://127.0.0.1:{$this->httpPort}/wrong-uri", 'POST'))->withBody($payload);
/** @var Response $response */
$response = yield $client->request($request);
$this->assertEquals(404, $response->getStatus());
Loop::delay(200, function () {
Loop::stop();
});
});
self::$kernel->boot();
$this->assertFileNotExists($this->workerFile);
$this->assertReadyJobsCountInTube(0, self::FLOW_CODE);
}
public function testHttpKernelSettings()
{
$this->setUpKernel(['http_server_options' => ['bodySizeLimit' => 42]]);
Loop::delay(100, function () {
yield $this->waitForConnectionAvailable("tcp://127.0.0.1:{$this->httpPort}");
$httpProducersServer = self::$kernel->getContainer()->get(HttpProducersServer::class);
$httpServer = $this->getObjectProperty($httpProducersServer, 'httpServer');
/** @var Options $serverOptions */
$serverOptions = $this->getObjectProperty($httpServer, 'options');
$this->assertSame(42, $serverOptions->getBodySizeLimit());
Loop::stop();
});
self::$kernel->boot();
}
private function waitForConnectionAvailable(string $uri): Promise
{
return call(function () use ($uri) {
do {
try {
/** @var ClientSocket $connection */
$connection = yield connect($uri);
} catch (ConnectException $e) {
$connection = null;
}
} while ($connection === null);
$connection->close();
});
}
/**
* @param object $object
* @param string $propertyName
* @return mixed
* @throws \ReflectionException
*/
private function getObjectProperty(object $object, string $propertyName)
{
$reflectionProperty = (new ReflectionObject($object))->getProperty($propertyName);
$reflectionProperty->setAccessible(true);
try {
return $reflectionProperty->getValue($object);
} finally {
$reflectionProperty->setAccessible(false);
}
}
}