Пример #1
0
 /**
  * ServerSocket constructor
  *
  * @param string $host        Host to listen on
  * @param int $port           Port to listen on
  * @param resource   $context      Stream context
  * @return void
  */
 public function __construct($host = 'localhost', $port = 9090, $context = null)
 {
     $ssl_host = $this->getSSLHost($host);
     parent::__construct($ssl_host, $port);
     $this->context_ = $context;
 }
Пример #2
0
 public function testSocketClient()
 {
     $thriftClient = $this->createSocketServer();
     $this->assertInstanceOf('ThriftModel\\Test\\Test', $thriftClient->getFactory('ThriftModel\\Test\\Test'));
     //$pid = pcntl_fork();
     $handler = new TestHandler($this->factory);
     $processor = $this->factory->getProcessorInstance('test', $handler);
     $this->assertInstanceOf('ThriftModel\\Test\\TestServiceProcessor', $processor);
     $transport = new TServerSocket('localhost', 9090);
     $outputTransportFactory = $inputTransportFactory = new TTransportFactory($transport);
     $outputProtocolFactory = $inputProtocolFactory = new TBinaryProtocolFactory();
     $server = new TSimpleServer($processor, $transport, $inputTransportFactory, $outputTransportFactory, $inputProtocolFactory, $outputProtocolFactory);
     $pid = pcntl_fork();
     if ($pid > 0) {
         //Wait for the server to launch
         sleep(2);
         $client = $thriftClient->getClient();
         $this->assertInstanceOf('ThriftModel\\Test\\TestServiceClient', $client);
         // Void Method
         $this->assertNull($client->ping());
         // Return test object
         $test = $client->get(1);
         $this->assertInstanceOf('ThriftModel\\Test\\Test', $test);
         $this->assertEquals(1, $test->id);
         $this->assertEquals('TEST', $test->content);
         // Wrong ID return Exception
         try {
             $response = $client->get(-1);
         } catch (\Exception $e) {
             $this->assertInstanceOf('ThriftModel\\Test\\InvalidValueException', $e);
         }
         // Return array of test object
         $test = $client->getList(1);
         $this->assertInternalType('array', $test);
         $this->assertCount(2, $test);
         $this->assertContainsOnly('ThriftModel\\Test\\Test', $test);
         $this->assertEquals(1, $test[0]->id);
         $this->assertEquals('TEST', $test[0]->content);
         $this->assertEquals(1, $test[1]->id);
         $this->assertEquals('TEST2', $test[1]->content);
         // Wrong ID return Exception
         try {
             $response = $client->getList(-1);
         } catch (\Exception $e) {
             $this->assertInstanceOf('ThriftModel\\Test\\InvalidValueException', $e);
         }
         //Create test
         $testObject = $this->factory->getInstance('ThriftModel\\Test\\Test', array('content' => 'OK'));
         $test = $client->create($testObject);
         $this->assertTrue((bool) $test);
         $testObject->content = '';
         // Wrong ID return Exception
         try {
             $response = $client->create($testObject);
         } catch (\Exception $e) {
             $this->assertInstanceOf('ThriftModel\\Test\\InvalidValueException', $e);
         }
         posix_kill($pid, SIGTERM);
         pcntl_wait($status, WNOHANG);
     } elseif ($pid === 0) {
         $server->serve();
         $transport->close();
         posix_kill($pid, SIGTERM);
         exit(0);
     }
 }