/**
  * @inheritdoc
  *
  * @param Task $task
  */
 public function handle(Task $task)
 {
     $config = $task->getData();
     if ($config["driver"] === "mysql") {
         $config += ["host" => "127.0.0.1", "port" => 3306, "charset" => "utf8", "socket" => null];
     }
     if ($config["remit"]["driver"] === "zeromq") {
         $config["remit"]["server"] += ["host" => "127.0.0.1"];
         $config["remit"]["client"] += ["host" => "127.0.0.1"];
         $server = new ZeroMqServer(new InMemoryLocation($config["remit"]["client"]["host"], $config["remit"]["client"]["port"]));
         $client = new ZeroMqClient(new InMemoryLocation($config["remit"]["server"]["host"], $config["remit"]["server"]["port"]));
     }
     $connection = new ExtendedPdo(new PDO($this->newConnectionString($config), $config["username"], $config["password"]));
     $server->addListener("q", function ($query, $values, $id) use($client, $connection) {
         $client->emit("r", [$connection->fetchAll($query, $values), $id]);
     });
     $server->addListener("d", function () use($connection, $server, $client) {
         $client->emit("dd");
         try {
             $connection->disconnect();
         } catch (Exception $exception) {
             // TODO: find an elegant way to deal with this
         }
         $server->disconnect();
         $client->disconnect();
         Loop\stop();
     });
     Loop\periodic(0, function () use($server) {
         $server->tick();
     });
     Loop\run();
 }
 /**
  * @dataProvider getConnectors
  *
  * @param array $config
  */
 public function testInsert(array $config)
 {
     Coroutine\create(function () use($config) {
         $factory = new ManagerFactory();
         $database = $factory->create($config);
         $time = time();
         (yield $database->table("test")->insert(["text" => $time]));
         $row = (yield $database->table("test")->select()->where("text = ?", $time)->first());
         $this->assertEqualsAfterDelay(0.5, $row["text"], $time);
     })->done();
     Loop\run();
 }
示例#3
0
<?php

require "vendor/autoload.php";
use Icicle\Http\Message\RequestInterface;
use Icicle\Http\Server\Server;
use Icicle\Loop;
use Icicle\Socket\SocketInterface;
$dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $collector) {
    require __DIR__ . "/routes.php";
});
$server = new Server(function (RequestInterface $request, SocketInterface $socket) use($dispatcher) {
    $dispatched = $dispatcher->dispatch($request->getMethod(), $request->getRequestTarget());
    switch ($dispatched[0]) {
        case FastRoute\Dispatcher::NOT_FOUND:
            // there is no matching route
            break;
        case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
            // the method called on this route is invalid
            break;
        case FastRoute\Dispatcher::FOUND:
            (yield call_user_func($dispatched[1], $request, $socket, $dispatched[2]));
            break;
    }
});
$server->listen(8000);
Loop\run();
 public function testStampedeProtection()
 {
     Coroutine\create(function () {
         $cache = new MemoryDriver();
         file_put_contents(__DIR__ . "/stampede", 1);
         $factory = function () use(&$counter) {
             $count = (int) file_get_contents(__DIR__ . "/stampede");
             file_put_contents(__DIR__ . "/stampede", ++$count);
             (yield $count);
         };
         $cache->set("counter", $factory);
         $cache->set("counter", $factory);
         $cache->set("counter", $factory);
         $cache->get("counter");
         $cache->get("counter");
         // resolve the first "counter" value
         (yield $cache->get("counter"));
         // fetch the second "counter" value from the cache memory store
         $actual = (yield $cache->get("counter"));
         // first check to see that the count stored in the filesystem
         // is correct...
         Loop\timer(0.5, function () {
             $count = (int) file_get_contents(__DIR__ . "/stampede");
             $this->assertEquals(2, $count);
             unlink(__DIR__ . "/stampede");
         });
         // then check to see that the count stored in the cache
         // is correct...
         $this->assertEqualsAfterDelay(2, $actual);
     })->done();
     Loop\run();
 }
示例#5
0
 /**
  * Runs the application.
  */
 public function run(int $port, string $address = '*')
 {
     $this->server->listen($port, $address);
     Loop\run();
 }
 public function testQueueUnbindDelete()
 {
     $coroutine = new Coroutine\Coroutine($this->goTestQueueUnbindDelete());
     $coroutine->done();
     Loop\run();
 }