/** * Test basic connection pool. */ public function testConnectionPool() { $address = 'tcp://localhost:7000'; $serverSocket = stream_socket_server($address); $connectionPool = new StreamSocketConnectionPool($serverSocket); $clientSocket = stream_socket_client($address); $clientConnection = new StreamSocketConnection($clientSocket); // Should accept connection $initialReadableConnections = $connectionPool->getReadableConnections(0); $this->assertCount(0, $initialReadableConnections); // Ping test $clientConnection->write('ping'); $readableConnections = $connectionPool->getReadableConnections(0); $this->assertCount(1, $readableConnections); $serverConnection = array_pop($readableConnections); $this->assertEquals('ping', $serverConnection->read(4)); // Close test $serverConnection->close(); $finalReadableConnections = $connectionPool->getReadableConnections(0); $this->assertCount(0, $finalReadableConnections); $this->assertEquals(0, $connectionPool->count()); $clientConnection->close(); // Test that it closes an open connection $secondClientSocket = stream_socket_client($address); // Accept the connection $connectionPool->getReadableConnections(0); $connectionPool->close(); fread($secondClientSocket, 1); $this->assertTrue(feof($secondClientSocket)); fclose($secondClientSocket); }
/** * Tests that the StreamSocketConnection class can handle errors. */ public function testFailedRead() { // Create sockets and connection classes $sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); stream_set_blocking($sockets[0], 0); stream_set_blocking($sockets[1], 0); $streamSocket1 = new StreamSocketConnection($sockets[0]); $streamSocket2 = new StreamSocketConnection($sockets[1]); $streamSocket2->close(); try { $streamSocket1->read(5); $this->fail('Should have thrown exception'); } catch (ConnectionException $exception) { $this->assertEquals('fread failed', $exception->getMessage()); } $streamSocket1->close(); }