Esempio n. 1
0
 public function testConnectionDisconnectAfterFailedQuery()
 {
     $conn = new Connection(["user" => $this->getDbUser(), "database" => $this::getDbName(), "auto_disconnect" => true], $this->getLoop());
     $hello = null;
     $conn->query("Some bad query")->subscribe(new CallbackObserver(function ($x) {
         echo "next\n";
         $this->fail('Should not get any items');
     }, function (\Exception $e) use(&$hello) {
         $hello = "Hello";
         $this->getLoop()->addTimer(0.1, function () {
             $this->stopLoop();
         });
     }, function () {
         echo "complete\n";
         $this->fail('Should not complete');
     }));
     $this->runLoopWithTimeout(2);
     $this->assertEquals('Hello', $hello);
     $this->assertEquals(Connection::CONNECTION_CLOSED, $conn->getConnectionStatus());
 }
Esempio n. 2
0
 public function getIdleConnection()
 {
     // we want to get the first available one
     // this will keep the connections at the front the busiest
     // and then we can add an idle timer to the connections
     foreach ($this->connections as $connection) {
         // need to figure out different states (in trans etc.)
         if ($connection->getState() === Connection::STATE_READY) {
             return $connection;
         }
     }
     // no idle connections were found - spin up new one
     $connection = new Connection($this->parameters, $this->loop);
     if (!$this->autoDisconnect) {
         $this->connections[] = $connection;
         $connection->on('close', function () use($connection) {
             $this->connections = array_filter($this->connections, function ($c) use($connection) {
                 return $connection !== $c;
             });
         });
     }
     return $connection;
 }