Merge pull request #15594 from ZhouyihaiDing/flaky

PHP: fix flaky tests
diff --git a/src/php/tests/unit_tests/CallCredentials2Test.php b/src/php/tests/unit_tests/CallCredentials2Test.php
index 1c7e0c0..c63029f 100644
--- a/src/php/tests/unit_tests/CallCredentials2Test.php
+++ b/src/php/tests/unit_tests/CallCredentials2Test.php
@@ -35,6 +35,7 @@
         $this->channel = new Grpc\Channel(
             'localhost:'.$this->port,
             [
+            'force_new' => true,
             'grpc.ssl_target_name_override' => $this->host_override,
             'grpc.default_authority' => $this->host_override,
             'credentials' => $credentials,
diff --git a/src/php/tests/unit_tests/CallCredentialsTest.php b/src/php/tests/unit_tests/CallCredentialsTest.php
index 4b5721d..818b823 100644
--- a/src/php/tests/unit_tests/CallCredentialsTest.php
+++ b/src/php/tests/unit_tests/CallCredentialsTest.php
@@ -41,6 +41,7 @@
         $this->channel = new Grpc\Channel(
             'localhost:'.$this->port,
             [
+            'force_new' => true,
             'grpc.ssl_target_name_override' => $this->host_override,
             'grpc.default_authority' => $this->host_override,
             'credentials' => $this->credentials,
diff --git a/src/php/tests/unit_tests/CallTest.php b/src/php/tests/unit_tests/CallTest.php
index c5e1890..be1d77f 100644
--- a/src/php/tests/unit_tests/CallTest.php
+++ b/src/php/tests/unit_tests/CallTest.php
@@ -24,12 +24,14 @@
     public static function setUpBeforeClass()
     {
         self::$server = new Grpc\Server([]);
-        self::$port = self::$server->addHttp2Port('0.0.0.0:0');
+        self::$port = self::$server->addHttp2Port('0.0.0.0:53000');
     }
 
     public function setUp()
     {
-        $this->channel = new Grpc\Channel('localhost:'.self::$port, []);
+        $this->channel = new Grpc\Channel('localhost:'.self::$port, [
+            'force_new' => true,
+        ]);
         $this->call = new Grpc\Call($this->channel,
                                     '/foo',
                                     Grpc\Timeval::infFuture());
diff --git a/src/php/tests/unit_tests/EndToEndTest.php b/src/php/tests/unit_tests/EndToEndTest.php
index b54f1d8..d096565 100644
--- a/src/php/tests/unit_tests/EndToEndTest.php
+++ b/src/php/tests/unit_tests/EndToEndTest.php
@@ -22,13 +22,16 @@
     {
         $this->server = new Grpc\Server([]);
         $this->port = $this->server->addHttp2Port('0.0.0.0:0');
-        $this->channel = new Grpc\Channel('localhost:'.$this->port, []);
+        $this->channel = new Grpc\Channel('localhost:'.$this->port, [
+            "force_new" => true,
+        ]);
         $this->server->start();
     }
 
     public function tearDown()
     {
         $this->channel->close();
+        unset($this->server);
     }
 
     public function testSimpleRequestBody()
diff --git a/src/php/tests/unit_tests/InterceptorTest.php b/src/php/tests/unit_tests/InterceptorTest.php
index 11c5b43..d18c27c 100644
--- a/src/php/tests/unit_tests/InterceptorTest.php
+++ b/src/php/tests/unit_tests/InterceptorTest.php
@@ -206,13 +206,16 @@
     {
         $this->server = new Grpc\Server([]);
         $this->port = $this->server->addHttp2Port('0.0.0.0:0');
-        $this->channel = new Grpc\Channel('localhost:'.$this->port, ['credentials' => Grpc\ChannelCredentials::createInsecure()]);
+        $this->channel = new Grpc\Channel('localhost:'.$this->port, [
+            'force_new' => true,
+            'credentials' => Grpc\ChannelCredentials::createInsecure()]);
         $this->server->start();
     }
 
     public function tearDown()
     {
         $this->channel->close();
+        unset($this->server);
     }
 
 
@@ -222,6 +225,7 @@
         $channel_matadata_interceptor = new ChangeMetadataInterceptor();
         $intercept_channel = Grpc\Interceptor::intercept($this->channel, $channel_matadata_interceptor);
         $client = new InterceptorClient('localhost:'.$this->port, [
+            'force_new' => true,
             'credentials' => Grpc\ChannelCredentials::createInsecure(),
         ], $intercept_channel);
         $req = new SimpleRequest($req_text);
@@ -250,6 +254,7 @@
         $intercept_channel1 = Grpc\Interceptor::intercept($this->channel, $channel_matadata_interceptor);
         $intercept_channel2 = Grpc\Interceptor::intercept($intercept_channel1, $channel_matadata_intercepto2);
         $client = new InterceptorClient('localhost:'.$this->port, [
+            'force_new' => true,
             'credentials' => Grpc\ChannelCredentials::createInsecure(),
         ], $intercept_channel2);
 
@@ -275,6 +280,7 @@
         $intercept_channel3 = Grpc\Interceptor::intercept($this->channel,
             [$channel_matadata_intercepto2, $channel_matadata_interceptor]);
         $client = new InterceptorClient('localhost:'.$this->port, [
+            'force_new' => true,
             'credentials' => Grpc\ChannelCredentials::createInsecure(),
         ], $intercept_channel3);
 
@@ -304,6 +310,7 @@
         $intercept_channel = Grpc\Interceptor::intercept($this->channel,
             $change_request_interceptor);
         $client = new InterceptorClient('localhost:'.$this->port, [
+            'force_new' => true,
             'credentials' => Grpc\ChannelCredentials::createInsecure(),
         ], $intercept_channel);
 
@@ -354,6 +361,7 @@
         $intercept_channel = Grpc\Interceptor::intercept($this->channel,
             $channel_request_interceptor);
         $client = new InterceptorClient('localhost:'.$this->port, [
+            'force_new' => true,
             'credentials' => Grpc\ChannelCredentials::createInsecure(),
         ], $intercept_channel);
 
@@ -374,7 +382,10 @@
     {
         $channel = new Grpc\Channel(
             'localhost:0',
-            ['credentials' => Grpc\ChannelCredentials::createInsecure()]
+            [
+                'force_new' => true,
+                'credentials' => Grpc\ChannelCredentials::createInsecure()
+            ]
         );
         $interceptor_channel = Grpc\Interceptor::intercept($channel, new Grpc\Interceptor());
         $state = $interceptor_channel->getConnectivityState();
@@ -386,7 +397,10 @@
     {
         $channel = new Grpc\Channel(
             'localhost:0',
-            ['credentials' => Grpc\ChannelCredentials::createInsecure()]
+            [
+                'force_new' => true,
+                'credentials' => Grpc\ChannelCredentials::createInsecure()
+            ]
         );
         $interceptor_channel = Grpc\Interceptor::intercept($channel, new Grpc\Interceptor());
         $now = Grpc\Timeval::now();
@@ -402,7 +416,10 @@
     {
         $channel = new Grpc\Channel(
             'localhost:0',
-            ['credentials' => Grpc\ChannelCredentials::createInsecure()]
+            [
+                'force_new' => true,
+                'credentials' => Grpc\ChannelCredentials::createInsecure()
+            ]
         );
         $interceptor_channel = Grpc\Interceptor::intercept($channel, new Grpc\Interceptor());
         $this->assertNotNull($interceptor_channel);
@@ -413,7 +430,10 @@
     {
         $channel = new Grpc\Channel(
             'localhost:8888',
-            ['credentials' => Grpc\ChannelCredentials::createInsecure()]
+            [
+                'force_new' => true,
+                'credentials' => Grpc\ChannelCredentials::createInsecure()
+            ]
         );
         $interceptor_channel = Grpc\Interceptor::intercept($channel, new Grpc\Interceptor());
         $target = $interceptor_channel->getTarget();
diff --git a/src/php/tests/unit_tests/SecureEndToEndTest.php b/src/php/tests/unit_tests/SecureEndToEndTest.php
index dff4e87..071598c 100644
--- a/src/php/tests/unit_tests/SecureEndToEndTest.php
+++ b/src/php/tests/unit_tests/SecureEndToEndTest.php
@@ -34,6 +34,7 @@
         $this->channel = new Grpc\Channel(
             'localhost:'.$this->port,
             [
+            'force_new' => true,
             'grpc.ssl_target_name_override' => $this->host_override,
             'grpc.default_authority' => $this->host_override,
             'credentials' => $credentials,
@@ -44,6 +45,7 @@
     public function tearDown()
     {
         $this->channel->close();
+        unset($this->server);
     }
 
     public function testSimpleRequestBody()
diff --git a/src/php/tests/unit_tests/ServerTest.php b/src/php/tests/unit_tests/ServerTest.php
index ac6f2f0..cab92e5 100644
--- a/src/php/tests/unit_tests/ServerTest.php
+++ b/src/php/tests/unit_tests/ServerTest.php
@@ -55,7 +55,10 @@
         $port = $this->server->addHttp2Port('0.0.0.0:0');
         $this->server->start();
         $channel = new Grpc\Channel('localhost:'.$port,
-             ['credentials' => Grpc\ChannelCredentials::createInsecure()]);
+             [
+                 'force_new' => true,
+                 'credentials' => Grpc\ChannelCredentials::createInsecure()
+             ]);
 
         $deadline = Grpc\Timeval::infFuture();
         $call = new Grpc\Call($channel, 'dummy_method', $deadline);