1<?php 2/* 3 * 4 * Copyright 2015 gRPC authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 */ 19 20//==============Channel Test==================== 21 22function callbackFunc($context) 23{ 24 return []; 25} 26 27function callbackFunc2($context) 28{ 29 return ["k1" => "v1"]; 30} 31 32function assertConnecting($state) 33{ 34 assert(($state == GRPC\CHANNEL_CONNECTING || $state == GRPC\CHANNEL_TRANSIENT_FAILURE) == true); 35} 36 37function waitUntilNotIdle($channel) { 38 for ($i = 0; $i < 10; $i++) { 39 $now = Grpc\Timeval::now(); 40 $deadline = $now->add(new Grpc\Timeval(10000)); 41 if ($channel->watchConnectivityState(GRPC\CHANNEL_IDLE, 42 $deadline)) { 43 return true; 44 } 45 } 46 assert(true == false); 47} 48 49// Set up 50$channel = new Grpc\Channel('localhost:0', ['credentials' => Grpc\ChannelCredentials::createInsecure()]); 51 52// Test InsecureCredentials 53assert('Grpc\Channel' == get_class($channel)); 54 55// Test ConnectivityState 56$state = $channel->getConnectivityState(); 57assert(0 == $state); 58 59// Test GetConnectivityStateWithInt 60$state = $channel->getConnectivityState(123); 61assert(0 == $state); 62 63// Test GetConnectivityStateWithString 64$state = $channel->getConnectivityState('hello'); 65assert(0 == $state); 66 67// Test GetConnectivityStateWithBool 68$state = $channel->getConnectivityState(true); 69assert(0 == $state); 70 71$channel->close(); 72 73// Test GetTarget 74$channel = new Grpc\Channel('localhost:8888', ['credentials' => Grpc\ChannelCredentials::createInsecure()]); 75$target = $channel->getTarget(); 76assert(is_string($target) == true); 77$channel->close(); 78 79// Test WatchConnectivityState 80$channel = new Grpc\Channel('localhost:0', ['credentials' => Grpc\ChannelCredentials::createInsecure()]); 81$now = Grpc\Timeval::now(); 82$deadline = $now->add(new Grpc\Timeval(100*1000)); 83 84$state = $channel->watchConnectivityState(1, $deadline); 85assert($state == true); 86 87unset($now); 88unset($deadline); 89 90$channel->close(); 91