OLD | NEW |
(Empty) | |
| 1 <?php |
| 2 /* |
| 3 * |
| 4 * Copyright 2015-2016, Google Inc. |
| 5 * All rights reserved. |
| 6 * |
| 7 * Redistribution and use in source and binary forms, with or without |
| 8 * modification, are permitted provided that the following conditions are |
| 9 * met: |
| 10 * |
| 11 * * Redistributions of source code must retain the above copyright |
| 12 * notice, this list of conditions and the following disclaimer. |
| 13 * * Redistributions in binary form must reproduce the above |
| 14 * copyright notice, this list of conditions and the following disclaimer |
| 15 * in the documentation and/or other materials provided with the |
| 16 * distribution. |
| 17 * * Neither the name of Google Inc. nor the names of its |
| 18 * contributors may be used to endorse or promote products derived from |
| 19 * this software without specific prior written permission. |
| 20 * |
| 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 * |
| 33 */ |
| 34 require_once realpath(dirname(__FILE__).'/../../vendor/autoload.php'); |
| 35 require_once dirname(__FILE__).'/math.php'; |
| 36 |
| 37 abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase |
| 38 { |
| 39 /** |
| 40 * These tests require that a server exporting the math service must be |
| 41 * running on $GRPC_TEST_HOST. |
| 42 */ |
| 43 protected static $client; |
| 44 |
| 45 public function testWaitForNotReady() |
| 46 { |
| 47 $this->assertFalse(self::$client->waitForReady(1)); |
| 48 } |
| 49 |
| 50 public function testWaitForReady() |
| 51 { |
| 52 $this->assertTrue(self::$client->waitForReady(250000)); |
| 53 } |
| 54 |
| 55 public function testAlreadyReady() |
| 56 { |
| 57 $this->assertTrue(self::$client->waitForReady(250000)); |
| 58 $this->assertTrue(self::$client->waitForReady(100)); |
| 59 } |
| 60 |
| 61 public function testGetTarget() |
| 62 { |
| 63 $this->assertTrue(is_string(self::$client->getTarget())); |
| 64 } |
| 65 |
| 66 /** |
| 67 * @expectedException InvalidArgumentException |
| 68 */ |
| 69 public function testClose() |
| 70 { |
| 71 self::$client->close(); |
| 72 $div_arg = new math\DivArgs(); |
| 73 $call = self::$client->Div($div_arg); |
| 74 } |
| 75 |
| 76 /** |
| 77 * @expectedException InvalidArgumentException |
| 78 */ |
| 79 public function testInvalidMetadata() |
| 80 { |
| 81 $div_arg = new math\DivArgs(); |
| 82 $call = self::$client->Div($div_arg, [' ' => 'abc123']); |
| 83 } |
| 84 |
| 85 public function testGetCallMetadata() |
| 86 { |
| 87 $div_arg = new math\DivArgs(); |
| 88 $call = self::$client->Div($div_arg); |
| 89 $this->assertTrue(is_array($call->getMetadata())); |
| 90 } |
| 91 |
| 92 public function testTimeout() |
| 93 { |
| 94 $div_arg = new math\DivArgs(); |
| 95 $call = self::$client->Div($div_arg, [], ['timeout' => 100]); |
| 96 list($response, $status) = $call->wait(); |
| 97 $this->assertSame(\Grpc\STATUS_DEADLINE_EXCEEDED, $status->code); |
| 98 } |
| 99 |
| 100 public function testCancel() |
| 101 { |
| 102 $div_arg = new math\DivArgs(); |
| 103 $call = self::$client->Div($div_arg); |
| 104 $call->cancel(); |
| 105 list($response, $status) = $call->wait(); |
| 106 $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code); |
| 107 } |
| 108 |
| 109 public function testCallCredentialsCallback() |
| 110 { |
| 111 $div_arg = new math\DivArgs(); |
| 112 $call = self::$client->Div($div_arg, array(), array( |
| 113 'call_credentials_callback' => function ($context) { |
| 114 return array(); |
| 115 }, |
| 116 )); |
| 117 $call->cancel(); |
| 118 list($response, $status) = $call->wait(); |
| 119 $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code); |
| 120 } |
| 121 |
| 122 public function testCallCredentialsCallback2() |
| 123 { |
| 124 $div_arg = new math\DivArgs(); |
| 125 $call = self::$client->Div($div_arg); |
| 126 $call_credentials = Grpc\CallCredentials::createFromPlugin( |
| 127 function ($context) { |
| 128 return array(); |
| 129 } |
| 130 ); |
| 131 $call->setCallCredentials($call_credentials); |
| 132 $call->cancel(); |
| 133 list($response, $status) = $call->wait(); |
| 134 $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code); |
| 135 } |
| 136 |
| 137 /** |
| 138 * @expectedException InvalidArgumentException |
| 139 */ |
| 140 public function testInvalidMethodName() |
| 141 { |
| 142 $invalid_client = new DummyInvalidClient('host', [ |
| 143 'credentials' => Grpc\ChannelCredentials::createInsecure(), |
| 144 ]); |
| 145 $div_arg = new math\DivArgs(); |
| 146 $invalid_client->InvalidUnaryCall($div_arg); |
| 147 } |
| 148 |
| 149 /** |
| 150 * @expectedException Exception |
| 151 */ |
| 152 public function testMissingCredentials() |
| 153 { |
| 154 $invalid_client = new DummyInvalidClient('host', [ |
| 155 ]); |
| 156 } |
| 157 |
| 158 public function testPrimaryUserAgentString() |
| 159 { |
| 160 $invalid_client = new DummyInvalidClient('host', [ |
| 161 'credentials' => Grpc\ChannelCredentials::createInsecure(), |
| 162 'grpc.primary_user_agent' => 'testUserAgent', |
| 163 ]); |
| 164 } |
| 165 |
| 166 public function testWriteFlags() |
| 167 { |
| 168 $div_arg = new math\DivArgs(); |
| 169 $div_arg->setDividend(7); |
| 170 $div_arg->setDivisor(4); |
| 171 $call = self::$client->Div($div_arg, [], |
| 172 ['flags' => Grpc\WRITE_NO_COMPRESS]); |
| 173 $this->assertTrue(is_string($call->getPeer())); |
| 174 list($response, $status) = $call->wait(); |
| 175 $this->assertSame(1, $response->getQuotient()); |
| 176 $this->assertSame(3, $response->getRemainder()); |
| 177 $this->assertSame(\Grpc\STATUS_OK, $status->code); |
| 178 } |
| 179 |
| 180 public function testWriteFlagsServerStreaming() |
| 181 { |
| 182 $fib_arg = new math\FibArgs(); |
| 183 $fib_arg->setLimit(7); |
| 184 $call = self::$client->Fib($fib_arg, [], |
| 185 ['flags' => Grpc\WRITE_NO_COMPRESS]); |
| 186 $result_array = iterator_to_array($call->responses()); |
| 187 $status = $call->getStatus(); |
| 188 $this->assertSame(\Grpc\STATUS_OK, $status->code); |
| 189 } |
| 190 |
| 191 public function testWriteFlagsClientStreaming() |
| 192 { |
| 193 $call = self::$client->Sum(); |
| 194 $num = new math\Num(); |
| 195 $num->setNum(1); |
| 196 $call->write($num, ['flags' => Grpc\WRITE_NO_COMPRESS]); |
| 197 list($response, $status) = $call->wait(); |
| 198 $this->assertSame(\Grpc\STATUS_OK, $status->code); |
| 199 } |
| 200 |
| 201 public function testWriteFlagsBidiStreaming() |
| 202 { |
| 203 $call = self::$client->DivMany(); |
| 204 $div_arg = new math\DivArgs(); |
| 205 $div_arg->setDividend(7); |
| 206 $div_arg->setDivisor(4); |
| 207 $call->write($div_arg, ['flags' => Grpc\WRITE_NO_COMPRESS]); |
| 208 $response = $call->read(); |
| 209 $call->writesDone(); |
| 210 $status = $call->getStatus(); |
| 211 $this->assertSame(\Grpc\STATUS_OK, $status->code); |
| 212 } |
| 213 |
| 214 public function testSimpleRequest() |
| 215 { |
| 216 $div_arg = new math\DivArgs(); |
| 217 $div_arg->setDividend(7); |
| 218 $div_arg->setDivisor(4); |
| 219 $call = self::$client->Div($div_arg); |
| 220 $this->assertTrue(is_string($call->getPeer())); |
| 221 list($response, $status) = $call->wait(); |
| 222 $this->assertSame(1, $response->getQuotient()); |
| 223 $this->assertSame(3, $response->getRemainder()); |
| 224 $this->assertSame(\Grpc\STATUS_OK, $status->code); |
| 225 } |
| 226 |
| 227 public function testServerStreaming() |
| 228 { |
| 229 $fib_arg = new math\FibArgs(); |
| 230 $fib_arg->setLimit(7); |
| 231 $call = self::$client->Fib($fib_arg); |
| 232 $this->assertTrue(is_string($call->getPeer())); |
| 233 $result_array = iterator_to_array($call->responses()); |
| 234 $extract_num = function ($num) { |
| 235 return $num->getNum(); |
| 236 }; |
| 237 $values = array_map($extract_num, $result_array); |
| 238 $this->assertSame([1, 1, 2, 3, 5, 8, 13], $values); |
| 239 $status = $call->getStatus(); |
| 240 $this->assertSame(\Grpc\STATUS_OK, $status->code); |
| 241 } |
| 242 |
| 243 public function testClientStreaming() |
| 244 { |
| 245 $call = self::$client->Sum(); |
| 246 $this->assertTrue(is_string($call->getPeer())); |
| 247 for ($i = 0; $i < 7; ++$i) { |
| 248 $num = new math\Num(); |
| 249 $num->setNum($i); |
| 250 $call->write($num); |
| 251 } |
| 252 list($response, $status) = $call->wait(); |
| 253 $this->assertSame(21, $response->getNum()); |
| 254 $this->assertSame(\Grpc\STATUS_OK, $status->code); |
| 255 } |
| 256 |
| 257 public function testBidiStreaming() |
| 258 { |
| 259 $call = self::$client->DivMany(); |
| 260 $this->assertTrue(is_string($call->getPeer())); |
| 261 for ($i = 0; $i < 7; ++$i) { |
| 262 $div_arg = new math\DivArgs(); |
| 263 $div_arg->setDividend(2 * $i + 1); |
| 264 $div_arg->setDivisor(2); |
| 265 $call->write($div_arg); |
| 266 $response = $call->read(); |
| 267 $this->assertSame($i, $response->getQuotient()); |
| 268 $this->assertSame(1, $response->getRemainder()); |
| 269 } |
| 270 $call->writesDone(); |
| 271 $status = $call->getStatus(); |
| 272 $this->assertSame(\Grpc\STATUS_OK, $status->code); |
| 273 } |
| 274 } |
| 275 |
| 276 class DummyInvalidClient extends \Grpc\BaseStub |
| 277 { |
| 278 public function InvalidUnaryCall(\math\DivArgs $argument, |
| 279 $metadata = [], |
| 280 $options = []) |
| 281 { |
| 282 return $this->_simpleRequest('invalidMethodName', |
| 283 $argument, |
| 284 function () {}, |
| 285 $metadata, |
| 286 $options); |
| 287 } |
| 288 } |
OLD | NEW |