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:50101', ['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$channel->close();
60
61// Test GetTarget
62$channel = new Grpc\Channel('localhost:50102', ['credentials' => Grpc\ChannelCredentials::createInsecure()]);
63$target = $channel->getTarget();
64assert(is_string($target) == true);
65$channel->close();
66
67// Test WatchConnectivityState
68$channel = new Grpc\Channel('localhost:50103', ['credentials' => Grpc\ChannelCredentials::createInsecure()]);
69$now = Grpc\Timeval::now();
70$deadline = $now->add(new Grpc\Timeval(100*1000));
71
72$state = $channel->watchConnectivityState(1, $deadline);
73assert($state == true);
74
75unset($now);
76unset($deadline);
77
78$channel->close();
79