|
| 1 | +<?php |
| 2 | + |
| 3 | +use Amp\ByteStream\StreamException; |
| 4 | +use Amp\Loop; |
| 5 | +use Amp\Websocket; |
| 6 | + |
| 7 | +require __DIR__ . '/../vendor/autoload.php'; |
| 8 | + |
| 9 | +const AGENT = 'amphp/websocket'; |
| 10 | + |
| 11 | +Loop::run(function () { |
| 12 | + $errors = 0; |
| 13 | + |
| 14 | + $options = (new Websocket\Options) |
| 15 | + ->withMaximumMessageSize(32 * 1024 * 1024) |
| 16 | + ->withMaximumFrameSize(32 * 1024 * 1024) |
| 17 | + ->withValidateUtf8(true); |
| 18 | + |
| 19 | + /** @var Websocket\Connection $connection */ |
| 20 | + $connection = yield Websocket\connect('ws://127.0.0.1:9001/getCaseCount'); |
| 21 | + /** @var Websocket\Message $message */ |
| 22 | + $message = yield $connection->receive(); |
| 23 | + $cases = (int) yield $message->buffer(); |
| 24 | + |
| 25 | + echo "Going to run {$cases} test cases." . PHP_EOL; |
| 26 | + |
| 27 | + for ($i = 1; $i < $cases; $i++) { |
| 28 | + $connection = yield Websocket\connect('ws://127.0.0.1:9001/getCaseInfo?case=' . $i . '&agent=' . AGENT); |
| 29 | + $message = yield $connection->receive(); |
| 30 | + $info = \json_decode(yield $message->buffer(), true); |
| 31 | + |
| 32 | + print $info['id'] . ' ' . str_repeat('-', 80 - strlen($info['id']) - 1) . PHP_EOL; |
| 33 | + print wordwrap($info['description'], 80, PHP_EOL) . ' '; |
| 34 | + |
| 35 | + $connection = yield Websocket\connect('ws://127.0.0.1:9001/runCase?case=' . $i . '&agent=' . AGENT, null, null, $options); |
| 36 | + |
| 37 | + try { |
| 38 | + while ($message = yield $connection->receive()) { |
| 39 | + $content = yield $message->buffer(); |
| 40 | + |
| 41 | + if ($message->isBinary()) { |
| 42 | + yield $connection->sendBinary($content); |
| 43 | + } else { |
| 44 | + yield $connection->send($content); |
| 45 | + } |
| 46 | + } |
| 47 | + } catch (Websocket\ClosedException $e) { |
| 48 | + // ignore |
| 49 | + } catch (AssertionError $e) { |
| 50 | + print 'Assertion error: ' . $e->getMessage() . PHP_EOL; |
| 51 | + $connection->close(); |
| 52 | + } catch (Error $e) { |
| 53 | + print 'Error: ' . $e->getMessage() . PHP_EOL; |
| 54 | + $connection->close(); |
| 55 | + } catch (StreamException $e) { |
| 56 | + print 'Stream exception: ' . $e->getMessage() . PHP_EOL; |
| 57 | + $connection->close(); |
| 58 | + } |
| 59 | + |
| 60 | + $connection = yield Websocket\connect('ws://127.0.0.1:9001/getCaseStatus?case=' . $i . '&agent=' . AGENT); |
| 61 | + $message = yield $connection->receive(); |
| 62 | + print ($result = \json_decode(yield $message->buffer(), true)['behavior']); |
| 63 | + |
| 64 | + if ($result === 'FAILED') { |
| 65 | + $errors++; |
| 66 | + } |
| 67 | + |
| 68 | + print PHP_EOL . PHP_EOL; |
| 69 | + } |
| 70 | + |
| 71 | + $connection = yield Websocket\connect('ws://127.0.0.1:9001/updateReports?agent=' . AGENT); |
| 72 | + $connection->close(); |
| 73 | + |
| 74 | + Loop::stop(); |
| 75 | + |
| 76 | + if ($errors) { |
| 77 | + exit(1); |
| 78 | + } |
| 79 | +}); |
0 commit comments