File: IntermittentlyFailingCommandTransport.php

package info (click to toggle)
icingadb-web 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,260 kB
  • sloc: php: 33,601; javascript: 640; sh: 17; xml: 16; makefile: 4
file content (40 lines) | stat: -rw-r--r-- 1,470 bytes parent folder | download
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
<?php

namespace Tests\Icinga\Module\Icingadb\Lib;

use Icinga\Application\Config;
use Icinga\Data\ConfigObject;
use Icinga\Module\Icingadb\Command\IcingaApiCommand;
use Icinga\Module\Icingadb\Command\Transport\ApiCommandTransport;
use Icinga\Module\Icingadb\Command\Transport\CommandTransport;
use Icinga\Module\Icingadb\Command\Transport\CommandTransportException;

class IntermittentlyFailingCommandTransport extends CommandTransport
{
    public static $failAtAttemptNo = 2;

    public static $attemptNo = 0;

    public static function getConfig(): Config
    {
        return Config::fromArray(['endpoint1' => ['host' => 'endpointA'], 'endpoint2' => ['host' => 'endpointB']]);
    }

    public static function createTransport(ConfigObject $config): ApiCommandTransport
    {
        return (new class extends ApiCommandTransport {
            protected function sendCommand(IcingaApiCommand $command)
            {
                $attemptNo = ++IntermittentlyFailingCommandTransport::$attemptNo;
                $failAtAttemptNo = IntermittentlyFailingCommandTransport::$failAtAttemptNo;

                if ($attemptNo === $failAtAttemptNo) {
                    throw (new CommandTransportException(sprintf('%s intermittently fails!', $this->getHost())))
                        ->setCommand($command);
                }

                return $command->getData() + ['endpoint' => $this->getHost()];
            }
        })->setHost($config->host);
    }
}