public function testChannelBasics()
 {
     list($x, $y) = PhutilSocketChannel::newChannelPair();
     $str_len_8 = 'abcdefgh';
     $str_len_4 = 'abcd';
     // Do a write with no buffer limit.
     $x->write($str_len_8);
     while (true) {
         $x->update();
         $y->update();
         $read = $y->read();
         if (strlen($read)) {
             break;
         }
     }
     // We expect to read the entire message.
     $this->assertEqual($str_len_8, $read);
     // Again, with a read buffer limit.
     $y->setReadBufferSize(4);
     $x->write($str_len_8);
     while (true) {
         $x->update();
         $y->update();
         $read = $y->read();
         if (strlen($read)) {
             break;
         }
     }
     // We expect to see only the first 4 bytes of the message.
     $this->assertEqual($str_len_4, $read);
 }
 private function assertParserResult(array $expect, $input, $file)
 {
     list($x, $y) = PhutilSocketChannel::newChannelPair();
     $xp = new DiffusionMercurialWireClientSSHProtocolChannel($x);
     $y->write($input);
     $y->flush();
     $y->closeWriteChannel();
     $messages = array();
     for ($ii = 0; $ii < count($expect); $ii++) {
         try {
             $messages[] = $xp->waitForMessage();
         } catch (Exception $ex) {
             // This is probably the parser not producing as many messages as
             // we expect. Log the exception, but continue to the assertion below
             // since that will often be easier to diagnose.
             phlog($ex);
             break;
         }
     }
     $this->assertEqual($expect, $messages, $file);
     // Now, make sure the channel doesn't have *more* messages than we expect.
     // Specifically, it should throw when we try to read another message.
     $caught = null;
     try {
         $xp->waitForMessage();
     } catch (Exception $ex) {
         $caught = $ex;
     }
     $this->assertTrue($caught instanceof Exception, pht("No extra messages for '%s'.", $file));
 }
 public function testJSONChannelBasics()
 {
     list($x, $y) = PhutilSocketChannel::newChannelPair();
     $xp = new PhutilJSONProtocolChannel($x);
     $yp = new PhutilJSONProtocolChannel($y);
     $dict = array('rand' => mt_rand(), 'list' => array(1, 2, 3), 'null' => null);
     $xp->write($dict);
     $xp->flush();
     $result = $yp->waitForMessage();
     $this->assertEqual($dict, $result, pht('Values are identical.'));
 }
 public function testPHPObjectChannelBasics()
 {
     list($x, $y) = PhutilSocketChannel::newChannelPair();
     $xp = new PhutilPHPObjectProtocolChannel($x);
     $yp = new PhutilPHPObjectProtocolChannel($y);
     $object = (object) array('key' => mt_rand());
     $xp->write($object);
     $xp->flush();
     $result = $yp->waitForMessage();
     $this->assertEqual(true, (array) $object === (array) $result, "Values are identical.");
     $this->assertEqual(false, $object === $result, "Objects are not the same.");
 }
 public function testCloseSocketWriteChannel()
 {
     list($x, $y) = PhutilSocketChannel::newChannelPair();
     $xp = new PhutilPHPObjectProtocolChannel($x);
     $yp = new PhutilPHPObjectProtocolChannel($y);
     $yp->closeWriteChannel();
     $yp->update();
     // NOTE: This test is more broad than the implementation needs to be. A
     // better test would be to verify that this throws an exception:
     //
     //   $xp->waitForMessage();
     //
     // However, if the test breaks, that method will hang forever instead of
     // returning, which would be hard to diagnose. Since the current
     // implementation shuts down the entire channel, just test for that.
     $this->assertFalse($xp->update(), pht('Expected channel to close.'));
 }