aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/php/tests/unit_tests/ChannelTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/php/tests/unit_tests/ChannelTest.php')
-rw-r--r--src/php/tests/unit_tests/ChannelTest.php243
1 files changed, 169 insertions, 74 deletions
diff --git a/src/php/tests/unit_tests/ChannelTest.php b/src/php/tests/unit_tests/ChannelTest.php
index 63d4193a8b..49b0e350f7 100644
--- a/src/php/tests/unit_tests/ChannelTest.php
+++ b/src/php/tests/unit_tests/ChannelTest.php
@@ -1,7 +1,7 @@
<?php
/*
*
- * Copyright 2015 gRPC authors.
+ * Copyright 2018 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,21 +28,18 @@ class ChannelTest extends PHPUnit_Framework_TestCase
if (!empty($this->channel)) {
$this->channel->close();
}
- $channel_clean_persistent =
- new Grpc\Channel('localhost:50010', []);
- $channel_clean_persistent->cleanPersistentList();
}
public function testInsecureCredentials()
{
- $this->channel = new Grpc\Channel('localhost:0',
+ $this->channel = new Grpc\Channel('localhost:50000',
['credentials' => Grpc\ChannelCredentials::createInsecure()]);
$this->assertSame('Grpc\Channel', get_class($this->channel));
}
public function testGetConnectivityState()
{
- $this->channel = new Grpc\Channel('localhost:0',
+ $this->channel = new Grpc\Channel('localhost:50001',
['credentials' => Grpc\ChannelCredentials::createInsecure()]);
$state = $this->channel->getConnectivityState();
$this->assertEquals(0, $state);
@@ -50,7 +47,7 @@ class ChannelTest extends PHPUnit_Framework_TestCase
public function testGetConnectivityStateWithInt()
{
- $this->channel = new Grpc\Channel('localhost:0',
+ $this->channel = new Grpc\Channel('localhost:50002',
['credentials' => Grpc\ChannelCredentials::createInsecure()]);
$state = $this->channel->getConnectivityState(123);
$this->assertEquals(0, $state);
@@ -58,7 +55,7 @@ class ChannelTest extends PHPUnit_Framework_TestCase
public function testGetConnectivityStateWithString()
{
- $this->channel = new Grpc\Channel('localhost:0',
+ $this->channel = new Grpc\Channel('localhost:50003',
['credentials' => Grpc\ChannelCredentials::createInsecure()]);
$state = $this->channel->getConnectivityState('hello');
$this->assertEquals(0, $state);
@@ -66,7 +63,7 @@ class ChannelTest extends PHPUnit_Framework_TestCase
public function testGetConnectivityStateWithBool()
{
- $this->channel = new Grpc\Channel('localhost:0',
+ $this->channel = new Grpc\Channel('localhost:50004',
['credentials' => Grpc\ChannelCredentials::createInsecure()]);
$state = $this->channel->getConnectivityState(true);
$this->assertEquals(0, $state);
@@ -74,7 +71,7 @@ class ChannelTest extends PHPUnit_Framework_TestCase
public function testGetTarget()
{
- $this->channel = new Grpc\Channel('localhost:8888',
+ $this->channel = new Grpc\Channel('localhost:50005',
['credentials' => Grpc\ChannelCredentials::createInsecure()]);
$target = $this->channel->getTarget();
$this->assertTrue(is_string($target));
@@ -82,7 +79,7 @@ class ChannelTest extends PHPUnit_Framework_TestCase
public function testWatchConnectivityState()
{
- $this->channel = new Grpc\Channel('localhost:0',
+ $this->channel = new Grpc\Channel('localhost:50006',
['credentials' => Grpc\ChannelCredentials::createInsecure()]);
$now = Grpc\Timeval::now();
$deadline = $now->add(new Grpc\Timeval(100*1000)); // 100ms
@@ -96,7 +93,7 @@ class ChannelTest extends PHPUnit_Framework_TestCase
public function testClose()
{
- $this->channel = new Grpc\Channel('localhost:0',
+ $this->channel = new Grpc\Channel('localhost:50007',
['credentials' => Grpc\ChannelCredentials::createInsecure()]);
$this->assertNotNull($this->channel);
$this->channel->close();
@@ -116,7 +113,7 @@ class ChannelTest extends PHPUnit_Framework_TestCase
*/
public function testInvalidConstructorWith()
{
- $this->channel = new Grpc\Channel('localhost:0', 'invalid');
+ $this->channel = new Grpc\Channel('localhost:50008', 'invalid');
$this->assertNull($this->channel);
}
@@ -125,7 +122,7 @@ class ChannelTest extends PHPUnit_Framework_TestCase
*/
public function testInvalidCredentials()
{
- $this->channel = new Grpc\Channel('localhost:0',
+ $this->channel = new Grpc\Channel('localhost:50009',
['credentials' => new Grpc\Timeval(100)]);
}
@@ -134,7 +131,7 @@ class ChannelTest extends PHPUnit_Framework_TestCase
*/
public function testInvalidOptionsArray()
{
- $this->channel = new Grpc\Channel('localhost:0',
+ $this->channel = new Grpc\Channel('localhost:50010',
['abc' => []]);
}
@@ -143,7 +140,7 @@ class ChannelTest extends PHPUnit_Framework_TestCase
*/
public function testInvalidGetConnectivityStateWithArray()
{
- $this->channel = new Grpc\Channel('localhost:0',
+ $this->channel = new Grpc\Channel('localhost:50011',
['credentials' => Grpc\ChannelCredentials::createInsecure()]);
$this->channel->getConnectivityState([]);
}
@@ -153,7 +150,7 @@ class ChannelTest extends PHPUnit_Framework_TestCase
*/
public function testInvalidWatchConnectivityState()
{
- $this->channel = new Grpc\Channel('localhost:0',
+ $this->channel = new Grpc\Channel('localhost:50012',
['credentials' => Grpc\ChannelCredentials::createInsecure()]);
$this->channel->watchConnectivityState([]);
}
@@ -163,7 +160,7 @@ class ChannelTest extends PHPUnit_Framework_TestCase
*/
public function testInvalidWatchConnectivityState2()
{
- $this->channel = new Grpc\Channel('localhost:0',
+ $this->channel = new Grpc\Channel('localhost:50013',
['credentials' => Grpc\ChannelCredentials::createInsecure()]);
$this->channel->watchConnectivityState(1, 'hi');
}
@@ -188,10 +185,12 @@ class ChannelTest extends PHPUnit_Framework_TestCase
public function testPersistentChannelSameHost()
{
- $this->channel1 = new Grpc\Channel('localhost:1', []);
+ $this->channel1 = new Grpc\Channel('localhost:50014', [
+ "grpc_target_persist_bound" => 3,
+ ]);
// the underlying grpc channel is the same by default
// when connecting to the same host
- $this->channel2 = new Grpc\Channel('localhost:1', []);
+ $this->channel2 = new Grpc\Channel('localhost:50014', []);
// both channels should be IDLE
$state = $this->channel1->getConnectivityState();
@@ -216,8 +215,10 @@ class ChannelTest extends PHPUnit_Framework_TestCase
public function testPersistentChannelDifferentHost()
{
// two different underlying channels because different hostname
- $this->channel1 = new Grpc\Channel('localhost:1', []);
- $this->channel2 = new Grpc\Channel('localhost:2', []);
+ $this->channel1 = new Grpc\Channel('localhost:50015', [
+ "grpc_target_persist_bound" => 3,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50016', []);
// both channels should be IDLE
$state = $this->channel1->getConnectivityState();
@@ -242,8 +243,11 @@ class ChannelTest extends PHPUnit_Framework_TestCase
public function testPersistentChannelSameArgs()
{
- $this->channel1 = new Grpc\Channel('localhost:1', ["abc" => "def"]);
- $this->channel2 = new Grpc\Channel('localhost:1', ["abc" => "def"]);
+ $this->channel1 = new Grpc\Channel('localhost:50017', [
+ "grpc_target_persist_bound" => 3,
+ "abc" => "def",
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50017', ["abc" => "def"]);
// try to connect on channel1
$state = $this->channel1->getConnectivityState(true);
@@ -260,8 +264,10 @@ class ChannelTest extends PHPUnit_Framework_TestCase
public function testPersistentChannelDifferentArgs()
{
- $this->channel1 = new Grpc\Channel('localhost:1', []);
- $this->channel2 = new Grpc\Channel('localhost:1', ["abc" => "def"]);
+ $this->channel1 = new Grpc\Channel('localhost:50018', [
+ "grpc_target_persist_bound" => 3,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50018', ["abc" => "def"]);
// try to connect on channel1
$state = $this->channel1->getConnectivityState(true);
@@ -281,9 +287,11 @@ class ChannelTest extends PHPUnit_Framework_TestCase
$creds1 = Grpc\ChannelCredentials::createSsl();
$creds2 = Grpc\ChannelCredentials::createSsl();
- $this->channel1 = new Grpc\Channel('localhost:1',
- ["credentials" => $creds1]);
- $this->channel2 = new Grpc\Channel('localhost:1',
+ $this->channel1 = new Grpc\Channel('localhost:50019',
+ ["credentials" => $creds1,
+ "grpc_target_persist_bound" => 3,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50019',
["credentials" => $creds2]);
// try to connect on channel1
@@ -305,9 +313,11 @@ class ChannelTest extends PHPUnit_Framework_TestCase
$creds2 = Grpc\ChannelCredentials::createSsl(
file_get_contents(dirname(__FILE__).'/../data/ca.pem'));
- $this->channel1 = new Grpc\Channel('localhost:1',
- ["credentials" => $creds1]);
- $this->channel2 = new Grpc\Channel('localhost:1',
+ $this->channel1 = new Grpc\Channel('localhost:50020',
+ ["credentials" => $creds1,
+ "grpc_target_persist_bound" => 3,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50020',
["credentials" => $creds2]);
// try to connect on channel1
@@ -330,9 +340,11 @@ class ChannelTest extends PHPUnit_Framework_TestCase
$creds2 = Grpc\ChannelCredentials::createSsl(
file_get_contents(dirname(__FILE__).'/../data/ca.pem'));
- $this->channel1 = new Grpc\Channel('localhost:1',
- ["credentials" => $creds1]);
- $this->channel2 = new Grpc\Channel('localhost:1',
+ $this->channel1 = new Grpc\Channel('localhost:50021',
+ ["credentials" => $creds1,
+ "grpc_target_persist_bound" => 3,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50021',
["credentials" => $creds2]);
// try to connect on channel1
@@ -353,9 +365,11 @@ class ChannelTest extends PHPUnit_Framework_TestCase
$creds1 = Grpc\ChannelCredentials::createSsl();
$creds2 = Grpc\ChannelCredentials::createInsecure();
- $this->channel1 = new Grpc\Channel('localhost:1',
- ["credentials" => $creds1]);
- $this->channel2 = new Grpc\Channel('localhost:1',
+ $this->channel1 = new Grpc\Channel('localhost:50022',
+ ["credentials" => $creds1,
+ "grpc_target_persist_bound" => 3,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50022',
["credentials" => $creds2]);
// try to connect on channel1
@@ -371,34 +385,55 @@ class ChannelTest extends PHPUnit_Framework_TestCase
$this->channel2->close();
}
+ public function testPersistentChannelSharedChannelClose1()
+ {
+ // same underlying channel
+ $this->channel1 = new Grpc\Channel('localhost:50123', [
+ "grpc_target_persist_bound" => 3,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50123', []);
+
+ // close channel1
+ $this->channel1->close();
+
+ // channel2 can still be use. We need to exclude the possible that
+ // in testPersistentChannelSharedChannelClose2, the exception is thrown
+ // by channel1.
+ $state = $this->channel2->getConnectivityState();
+ $this->assertEquals(GRPC\CHANNEL_IDLE, $state);
+ }
+
/**
* @expectedException RuntimeException
*/
- public function testPersistentChannelSharedChannelClose()
+ public function testPersistentChannelSharedChannelClose2()
{
// same underlying channel
- $this->channel1 = new Grpc\Channel('localhost:1', []);
- $this->channel2 = new Grpc\Channel('localhost:1', []);
+ $this->channel1 = new Grpc\Channel('localhost:50223', [
+ "grpc_target_persist_bound" => 3,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50223', []);
// close channel1
$this->channel1->close();
- // channel2 is now in SHUTDOWN state
+ // channel2 can still be use
$state = $this->channel2->getConnectivityState();
- $this->assertEquals(GRPC\CHANNEL_FATAL_FAILURE, $state);
+ $this->assertEquals(GRPC\CHANNEL_IDLE, $state);
- // calling it again will result in an exception because the
- // channel is already closed
- $state = $this->channel2->getConnectivityState();
+ // channel 1 is closed
+ $state = $this->channel1->getConnectivityState();
}
public function testPersistentChannelCreateAfterClose()
{
- $this->channel1 = new Grpc\Channel('localhost:1', []);
+ $this->channel1 = new Grpc\Channel('localhost:50024', [
+ "grpc_target_persist_bound" => 3,
+ ]);
$this->channel1->close();
- $this->channel2 = new Grpc\Channel('localhost:1', []);
+ $this->channel2 = new Grpc\Channel('localhost:50024', []);
$state = $this->channel2->getConnectivityState();
$this->assertEquals(GRPC\CHANNEL_IDLE, $state);
@@ -407,9 +442,11 @@ class ChannelTest extends PHPUnit_Framework_TestCase
public function testPersistentChannelSharedMoreThanTwo()
{
- $this->channel1 = new Grpc\Channel('localhost:1', []);
- $this->channel2 = new Grpc\Channel('localhost:1', []);
- $this->channel3 = new Grpc\Channel('localhost:1', []);
+ $this->channel1 = new Grpc\Channel('localhost:50025', [
+ "grpc_target_persist_bound" => 3,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50025', []);
+ $this->channel3 = new Grpc\Channel('localhost:50025', []);
// try to connect on channel1
$state = $this->channel1->getConnectivityState(true);
@@ -447,10 +484,12 @@ class ChannelTest extends PHPUnit_Framework_TestCase
// If a ChannelCredentials object is composed with a
// CallCredentials object, the underlying grpc channel will
// always be created new and NOT persisted.
- $this->channel1 = new Grpc\Channel('localhost:1',
+ $this->channel1 = new Grpc\Channel('localhost:50026',
["credentials" =>
- $credsWithCallCreds]);
- $this->channel2 = new Grpc\Channel('localhost:1',
+ $credsWithCallCreds,
+ "grpc_target_persist_bound" => 3,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50026',
["credentials" =>
$credsWithCallCreds]);
@@ -484,11 +523,13 @@ class ChannelTest extends PHPUnit_Framework_TestCase
// object is composed with a CallCredentials object, the
// underlying grpc channel will always be separate and not
// persisted
- $this->channel1 = new Grpc\Channel('localhost:1',
- ["credentials" => $creds1]);
- $this->channel2 = new Grpc\Channel('localhost:1',
+ $this->channel1 = new Grpc\Channel('localhost:50027',
+ ["credentials" => $creds1,
+ "grpc_target_persist_bound" => 3,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50027',
["credentials" => $creds2]);
- $this->channel3 = new Grpc\Channel('localhost:1',
+ $this->channel3 = new Grpc\Channel('localhost:50027',
["credentials" => $creds3]);
// try to connect on channel1
@@ -509,10 +550,12 @@ class ChannelTest extends PHPUnit_Framework_TestCase
public function testPersistentChannelForceNew()
{
- $this->channel1 = new Grpc\Channel('localhost:1', []);
+ $this->channel1 = new Grpc\Channel('localhost:50028', [
+ "grpc_target_persist_bound" => 2,
+ ]);
// even though all the channel params are the same, channel2
// has a new and different underlying channel
- $this->channel2 = new Grpc\Channel('localhost:1',
+ $this->channel2 = new Grpc\Channel('localhost:50028',
["force_new" => true]);
// try to connect on channel1
@@ -528,19 +571,20 @@ class ChannelTest extends PHPUnit_Framework_TestCase
$this->channel2->close();
}
- public function testPersistentChannelForceNewOldChannelIdle()
+ public function testPersistentChannelForceNewOldChannelIdle1()
{
- $this->channel1 = new Grpc\Channel('localhost:1', []);
- $this->channel2 = new Grpc\Channel('localhost:1',
+ $this->channel1 = new Grpc\Channel('localhost:50029', [
+ "grpc_target_persist_bound" => 2,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50029',
["force_new" => true]);
// channel3 shares with channel1
- $this->channel3 = new Grpc\Channel('localhost:1', []);
+ $this->channel3 = new Grpc\Channel('localhost:50029', []);
// try to connect on channel2
$state = $this->channel2->getConnectivityState(true);
$this->waitUntilNotIdle($this->channel2);
-
$state = $this->channel1->getConnectivityState();
$this->assertEquals(GRPC\CHANNEL_IDLE, $state);
$state = $this->channel2->getConnectivityState();
@@ -552,34 +596,85 @@ class ChannelTest extends PHPUnit_Framework_TestCase
$this->channel2->close();
}
+ public function testPersistentChannelForceNewOldChannelIdle2()
+ {
+
+ $this->channel1 = new Grpc\Channel('localhost:50029', [
+ "grpc_target_persist_bound" => 2,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50029', []);
+
+ // try to connect on channel2
+ $state = $this->channel1->getConnectivityState(true);
+ $this->waitUntilNotIdle($this->channel2);
+ $state = $this->channel1->getConnectivityState();
+ $this->assertConnecting($state);
+ $state = $this->channel2->getConnectivityState();
+ $this->assertConnecting($state);
+
+ $this->channel1->close();
+ $this->channel2->close();
+ }
+
+ public function testPersistentChannelForceNewOldChannelClose1()
+ {
+
+ $this->channel1 = new Grpc\Channel('localhost:50130', [
+ "grpc_target_persist_bound" => 2,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50130',
+ ["force_new" => true]);
+ // channel3 shares with channel1
+ $this->channel3 = new Grpc\Channel('localhost:50130', []);
+
+ $this->channel1->close();
+
+ $state = $this->channel2->getConnectivityState();
+ $this->assertEquals(GRPC\CHANNEL_IDLE, $state);
+
+ // channel3 is still usable. We need to exclude the possibility that in
+ // testPersistentChannelForceNewOldChannelClose2, the exception is thrown
+ // by channel1 and channel2.
+ $state = $this->channel3->getConnectivityState();
+ $this->assertEquals(GRPC\CHANNEL_IDLE, $state);
+ }
+
/**
* @expectedException RuntimeException
*/
- public function testPersistentChannelForceNewOldChannelClose()
+ public function testPersistentChannelForceNewOldChannelClose2()
{
- $this->channel1 = new Grpc\Channel('localhost:1', []);
- $this->channel2 = new Grpc\Channel('localhost:1',
- ["force_new" => true]);
+ $this->channel1 = new Grpc\Channel('localhost:50230', [
+ "grpc_target_persist_bound" => 2,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50230',
+ ["force_new" => true]);
// channel3 shares with channel1
- $this->channel3 = new Grpc\Channel('localhost:1', []);
+ $this->channel3 = new Grpc\Channel('localhost:50230', []);
$this->channel1->close();
$state = $this->channel2->getConnectivityState();
$this->assertEquals(GRPC\CHANNEL_IDLE, $state);
- // channel3 already closed
+ // channel3 is still usable
$state = $this->channel3->getConnectivityState();
+ $this->assertEquals(GRPC\CHANNEL_IDLE, $state);
+
+ // channel 1 is closed
+ $this->channel1->getConnectivityState();
}
public function testPersistentChannelForceNewNewChannelClose()
{
- $this->channel1 = new Grpc\Channel('localhost:1', []);
- $this->channel2 = new Grpc\Channel('localhost:1',
+ $this->channel1 = new Grpc\Channel('localhost:50031', [
+ "grpc_target_persist_bound" => 2,
+ ]);
+ $this->channel2 = new Grpc\Channel('localhost:50031',
["force_new" => true]);
- $this->channel3 = new Grpc\Channel('localhost:1', []);
+ $this->channel3 = new Grpc\Channel('localhost:50031', []);
$this->channel2->close();